Lesson 2
Oscillators
Lesson 1 established that a sound is a set of partial amplitudes and a shape in time. An oscillator is the thing that supplies the first of those. There are four classic answers, they are usually taught as pictures, and teaching them as pictures is what makes the next part come as a shock.
Stop thinking about the shape
Every introduction to synthesis draws you a sawtooth as a ramp that snaps back, and a square as a shape with corners. Those pictures are true and they are the wrong way round, because the shape is the consequence and the spectrum is the definition.
A sawtooth is every harmonic present at amplitude 1/n. A square is the odd harmonics at 1/n. A triangle is the odd harmonics at 1/n2, which is the entire reason it sounds so much darker than a square despite having partials in exactly the same places: its fifth harmonic is at 1/25 rather than 1/5.
Read that direction and a lot stops being arbitrary. Why is a square hollow and a saw bright? Because one is missing every even partial. Why does a triangle sound close to a sine? Because past the third harmonic there is almost nothing left.
A waveform is a table of amplitudes
The bars are the definition. The trace below is the measurement.
Press play to see the trace
Every harmonic at 1/n. The richest of the classic four, and the usual starting point for subtractive patches. Of the first 16 partials, 16 are actually present.
Pulse width, and the comb
The pulse is the interesting one, because it has a control. A pulse with duty cycle w has harmonic amplitudes proportional to |sin(n·π·w)| / n, and that sine term is a comb: it drives to zero every harmonic that is a multiple of 1/w.
At w = 0.5 the zeros land on every even harmonic, and a pulse is a square. At w = 0.25 they land on every fourth. Sweep the width and those notches slide through the spectrum, which you can watch in the bar strip and hear at the same time. That sliding comb is the sound everybody calls PWM, and it is worth noticing that nothing is being filtered: harmonics are being switched off and on by an interference pattern.
Now the part that bites
All four of those definitions have infinitely many harmonics. A sawtooth at 1/n does not stop at the twentieth partial, or the thousandth. And digital audio has a hard ceiling at half the sample rate: on a 44,100 Hz device nothing above 22,050 Hz can be represented at all, and on a 48,000 Hz one the ceiling is 24,000. That limit is called Nyquist.
Here is the trap. If you generate a waveform the obvious way, by evaluating the ideal shape at each sample, the harmonics above Nyquist do not politely vanish. They fold. A component at frequency f above the ceiling comes back down and is heard at sampleRate − f, and this is not a subtle effect you have to strain for.
Work it through for a sawtooth at 3,520 Hz on a 44,100 Hz device. Six harmonics fit under Nyquist. The seventh should be at 24,640 Hz, so it arrives at 19,460 Hz. The eighth should be at 28,160 and arrives at 15,940. The ninth arrives at 12,420.
Read those three numbers again. As the harmonic number goes up, the frequency you actually hear comes down, and that holds at any sample rate. Aliasing runs backwards, which is why it never sounds like brightness: play a naively generated line upwards and a shower of debris descends through it, moving against everything else.
And crucially, none of 19,460, 15,940 or 12,420 is a whole-number multiple of 3,520, and the same is true of whatever three numbers your own hardware produces. They are not harmonics of the note any more. They do not reinforce it, they sit beside it as unrelated tones, which is precisely the recipe for a bell rather than a sawtooth.
One caveat before you play with it: the figure below computes all of this against your hardware, not against the 44,100 Hz in the worked example, so on a 48,000 Hz device the numbers will differ from the paragraphs above. That is worth knowing rather than worrying about. Where the debris lands depends on the sample rate, which means the same badly built oscillator can sound clean on one machine and dirty on another.
The same sawtooth, computed two ways
Identical at A2. Start at A7 and switch sides.
Press play to see the trace
| n | Should be | Heard at | Status |
|---|---|---|---|
| 1 | 3,520 Hz | 3,520 Hz | |
| 2 | 7,040 Hz | 7,040 Hz | |
| 3 | 10,560 Hz | 10,560 Hz | |
| 4 | 14,080 Hz | 14,080 Hz | |
| 5 | 17,600 Hz | 17,600 Hz | |
| 6 | 21,120 Hz | 21,120 Hz | |
| 7 | 24,640 Hz | 19,460 Hz | folded |
| 8 | 28,160 Hz | 15,940 Hz | folded |
| 9 | 31,680 Hz | 12,420 Hz | folded |
| 10 | 35,200 Hz | 8,900 Hz | folded |
Harmonic 7 should sit at 24,640 Hz, which does not exist at this sample rate, so it arrives at 19,460 Hz instead. Read the folded rows downwards: 19,460, 15,940, 12,420, 8,900. They descend as n climbs. None of them is a whole multiple of 3520, so they do not reinforce the note. They sit beside it as unrelated tones, which is why this sounds like a bell being hit rather than like a bright sawtooth.
The fix is to refuse to generate them
Everything in this course builds its waveforms from an explicit list of partial amplitudes, and a list with a finite number of entries cannot contain a partial above its last one. So the entire anti-aliasing strategy is one line: count how many harmonics fit under Nyquist at this pitch, and generate exactly that many.
That is called band-limiting, and it has an audible consequence you should expect rather than be surprised by. A band-limited sawtooth gets duller as it climbs, because at 3,520 Hz there is genuinely only room for six harmonics. That dullness is not a defect. It is what the ceiling actually costs, honestly accounted for, instead of being paid in rubbish.
Real synthesizers do this in cleverer ways, because recomputing a harmonic table per note is expensive: they precompute band-limited tables per octave and switch between them, or use closed-form tricks with names like BLIT and PolyBLEP. Those are optimisations of the same idea, and lesson 8 runs into the per-octave version again from the other direction.
Why this is here and not in an appendix
Aliasing is usually filed under implementation detail, and it is not. It is the first place where the mathematics of a synthesizer makes an audible demand rather than an abstract one, and it is the reason two plugins claiming the same oscillator can sound unmistakably different in the top octave.
It is also the first appearance of a pattern the rest of this course keeps running into: an ideal that is easy to describe, a machine that cannot represent it, and a method that is really a strategy for spending a fixed budget well.