The Grime Square
A pulse wave has one knob, its width, and the width decides which harmonics do not exist. At half it is a square and every even harmonic is gone. At a third, every third. At a quarter, every fourth. Measured, exactly, and then the naive digital square measured against the exact one.
What it is
A pulse wave at 220 Hz, the eskibeat lead, the sound of every games console before 1990, and the simplest waveform with a knob on it. The knob is the width: how much of each cycle the wave spends high.
At half width it’s a square and it sounds hollow, and the bars along the bottom say why. Every even harmonic is missing. Drag the width to a third and the evens come back and every third harmonic goes instead. A quarter, every fourth. The width is one over k and every kth harmonic is gone, and the ice marks under the bars are where the measurement found them missing rather than where the formula says they should be.
Sweep the width and the holes walk down through the spectrum. That’s pulse width modulation, which is the movement in every PWM lead and most of the character of a Commodore 64.
How it works
The nth harmonic of a pulse of width d has an amplitude of four over n pi times the size of sin(n pi d). That sine is zero whenever n times d is a whole number, so a width of one over k takes out every kth harmonic. The rest of the harmonics keep the pulse’s one over n slope, so a narrow pulse sounds like a thin saw with holes in it, which is what it is.
The bars are measured. The pulse at the current width is built from its harmonics for twenty four cycles, every harmonic is read back under a Hann window, and a harmonic is called missing when it measures more than 40 dB below the most a pulse could put there. The cyan caps are the formula. Against a one second render the measured nulls are 2, 4, 6, 8, 10, 12, 14, 16 at half, 3, 6, 9, 12, 15 at a third, 4, 8, 12, 16 at a quarter and 5, 10, 15 at a fifth, each exactly the list the formula gives, and the surviving harmonics sit on the formula to a tenth of a dB.
The sound is a periodic wave with those exact harmonics, so its width is whatever number you ask for and not the nearest sample. Rendered offline, the real oscillator’s missing harmonics are the same lists, its evens against odds is the same to a tenth of a dB at every width, and the worst disagreement on any surviving harmonic is 0.00 dB.
What surprised me
How gently the evens come back. At exactly half the even harmonics are gone, 157 dB down, which is the noise floor of the arithmetic. At 45%, five per cent off square, they’re already at -10.4 dB relative to the odds. At a third they’re -4.9, at a quarter -3.1, at a tenth -1.0. The hollow sound of a square is a very narrow target, and a pulse width knob that’s not exactly centred isn’t a square, it’s a pulse with quiet evens.
The thing I set out to find was worse than it is. Every synthesis text warns that a naive digital pulse, one that flips high or low per sample, is unusable because of aliasing: it has harmonics all the way up, the ones above half the sample rate fold back down, and they land where no harmonic belongs. I built one to measure how bad, and at a lead’s pitch it’s a fraction of a per cent.
The measurement is exact and needs no window. The naive pulse is ±1 so its power is exactly one, and its harmonics up to half the sample rate account for a known share of that. Whatever is left over is the harmonics that didn’t fit and had to fold. At 220 Hz that’s -24.3 dB, 0.4% of the power. At 440 Hz, -21.2. At 880, -18.4. At 1760, -15.4, which is 3%. Measured off the samples, adding up the power at every harmonic that fits and subtracting from the total, the same numbers to a tenth of a dB.
And the folded harmonics land high. The first ones to fold, just above half the sample rate, come back just below it, so nearly all of the aliasing sits above ten kilohertz. Between the low harmonics, where a lead lives, the naive square at 220 Hz has -82 dB of anything that should not be there, and its even harmonics are 88 dB down. The naive square’s low end is right. Its problem is a hiss up top that grows with pitch, and a moving one under modulation, which is what people are actually hearing when they say a naive oscillator sounds bad.
I nearly reported something worse. My first attempt at the aliasing subtracted the exact pulse from the naive one in time and measured what was left, which came out at -19 dB at 220 Hz. Then I noticed I had lined the two up to the nearest whole sample, and half a sample of misalignment on two edges of a 218 sample cycle is itself about -20 dB. The residual was my alignment. The power argument doesn’t need alignment, which is why the day uses it.
What I would do next
Part ten is sidechain pumping, the last one, and it goes back to day 63: how much quieter a bass gets under a kick in loudness units, against how much quieter it sounds.
The naive pulse deserves the modulation measurement. Its aliasing at a fixed pitch is a small, steady hiss. Under PWM or a pitch bend the folded harmonics move against the real ones, and whether that’s heard as the birdies everyone describes is a threshold worth finding.
The other thing is the two square lead, which is what most grime leads actually are: two pulses a few cents apart, which is part four again with holes in the spectrum. Which harmonics survive both the width and the detune is a picture I haven’t drawn.