Square Wave Oscillator

Overview

Square Wave

The square wave oscillator programmatically generates a pulse waveform with a 50% pulse width. The basic_square_osc is not bandwidth limited, while square_osc is, using a technique called "PolyBLEP" to reduce aliasing artifacts that can occur when generating the waveform with a sharp discontinuity as the signal transitions from -1.0 to 1.0.

PolyBLEP works by precalculating a correction function that subtracts the waveform’s high-frequency components at discontinuities. This correction function is then applied to the generated waveform, effectively removing frequencies above the waveform’s Nyquist frequency to avoid aliasing artifacts.

Include

#include <q/synth/square_osc.hpp>

Declaration

struct basic_square_osc
{
    constexpr float operator()(phase p) const;
    constexpr float operator()(phase_iterator i) const;
};

constexpr auto basic_square = basic_square_osc{};

struct square_osc
{
    constexpr float operator()(phase p, phase dt) const;
    constexpr float operator()(phase_iterator i) const;
};

constexpr auto square = square_osc{};

Expressions

basic_square_osc is a model of BasicOscillator and implements all valid expressions of BasicOscillator. square_osc is a model of BandwidthLimitedOscillator and implements all valid expressions of BandwidthLimitedOscillator.

basic_square_osc and square_osc are stateless. The global instances basic_square and square can be used anywhere without needing to be placed in a class or struct. They are semantically equivalent to global functions.