Fourier · Differential Equations · Numerical Analysis · Psychoacoustics

The Mathematics of a Synthesizer

Inside a music synthesizer built from scratch, there is no sound — only functions, evaluated 48,000 times per second. This is a tour of the mathematics that lives in one, written for people who like their trigonometric identities load-bearing.

SECTION 1

Your ear is a logarithm with opinions

Every design decision in a synthesizer answers to a single, slightly unreasonable client: the human auditory system. Before any engineering, two of its habits set the rules of the game.

First habit: the ear hears ratios, not differences. The musical interval between 110 Hz and 220 Hz sounds identical to the interval between 1,760 Hz and 3,520 Hz — both are “one octave,” a doubling. Pitch perception is logarithmic, so music divides the octave not into equal frequency steps but into equal ratio steps. Western music uses twelve of them, which makes the frequency of MIDI note n:

f(n) = 440 · 2(n − 69)/12 Hz Note 69 is A above middle C. Each semitone multiplies frequency by 21/12 ≈ 1.0595 — an irrational number at the heart of all tuning.

This exponential appears everywhere in the synth. When a player bends a note by s semitones, the engine multiplies frequency by 2s/12. When two oscillators are detuned by c cents (hundredths of a semitone), their ratio is 2c/1200. The filter’s cutoff knob sweeps exponentially, because a linear sweep would feel like it spends ten seconds in the treble and a millisecond in the bass.

Second habit: the ear performs harmonic analysis in hardware. The cochlea is a coiled, fluid-filled frequency analyzer: different positions along its basilar membrane resonate at different frequencies, so what the brain receives is not the pressure waveform itself but something close to its spectrum. Two waveforms that look wildly different on an oscilloscope can sound identical if their spectra match. This means the natural domain for thinking about timbre is not time — it is frequency. Which brings us directly to the patron saint of this whole project.

SECTION 2

Fourier, employed at 48,000 Hz

The engine at the heart of this synth is a wavetable oscillator: one period of a wave, stored as 2,048 numbers, read in a loop at whatever speed produces the desired pitch. Everything interesting about it is a corollary of Fourier’s theorem.

Any reasonable periodic function decomposes into a sum of sines at integer multiples of the fundamental:

x(t) = Σh=1 Ah sin(2π h f t + φh) The Ah are the harmonic amplitudes — the recipe of the timbre. A sawtooth wave has Ah = 1/h; a square wave has Ah = 1/h for odd h only.

Play with the recipe below. Adding harmonics one at a time, you can watch a pure sine sharpen into a sawtooth — the buzzy, brassy backbone of most electronic music. This is the Fourier series converging in front of you, Gibbs ripples and all.

sawtooth recipe: Ah = 1/h
Partial sums of the sawtooth’s Fourier series. Note the overshoot that refuses to die near the discontinuity as more terms arrive — the Gibbs phenomenon, about 9% forever. The audio button plays the exact partial sum you see (via your browser’s audio engine), so you can hear the timbre assemble itself.

A wavetable is simply this series, pre-summed and sampled: 2,048 points of one period. But the synth doesn’t store one wave — it stores a whole family of them, and lets the player morph continuously along the family while playing. Morphing is implemented as pointwise linear interpolation between neighboring family members — a homotopy between waveforms, heard as timbre in motion. The professional libraries this synth imports (from instruments like Serum) are exactly such families: 256 waveforms per file, each 2,048 samples, each one frame of a timbral animation.

A synthesizer patch is a path through a space of periodic functions. The knobs are coordinates.
SECTION 3

The theorem that makes digital audio legal

Digital audio replaces a continuous pressure wave with a list of numbers — samples of the waveform taken 48,000 times per second. That this loses nothing is one of the great surprises of 20th-century mathematics.

The Nyquist–Shannon sampling theorem: a signal containing no frequencies above B is completely determined by samples taken at any rate above 2B. Not approximated — determined, exactly, via an interpolation formula built from sinc functions:

x(t) = Σn x[n] · sinc(fst − n),   sinc(u) = sin(πu)/(πu) A basis expansion: the shifted sincs are orthogonal, and band-limited functions are exactly their span. Digital audio is a statement about a function space.

At the standard studio rate of 48,000 samples per second, everything below 24 kHz — comfortably past the edge of human hearing — survives the trip into numbers and back untouched. The catch is the hypothesis: no frequencies above B. Violate it and the theorem doesn’t merely degrade politely. It retaliates.

SECTION 4

Aliasing, or: modular arithmetic you can hear

Sample a frequency above the Nyquist limit and it doesn’t disappear — it comes back wearing a disguise. The disguise is arithmetic modulo the sample rate.

A sampled sinusoid at frequency f is indistinguishable from one at f + k·fs for any integer k, because the samples land on identical values: sin(2π(f + k fs)(n/fs)) = sin(2π f n/fs + 2πkn). Every frequency therefore gets projected — folded — into the band below Nyquist, reflecting off the boundary like a billiard ball:

Top: the folding map. True frequency in, perceived frequency out — a triangle wave, the reflection of arithmetic mod fs off the Nyquist mirror at 24 kHz. Bottom: the waveform. Grey is the true oscillation; cyan dots are the samples; the cyan curve is what those samples actually describe. Past Nyquist, the samples trace a slower, entirely fictitious wave. The audio button sweeps the true frequency upward — listen for the moment the pitch starts falling instead.

Why does a wavetable synth care? Because a sawtooth’s recipe is Ah = 1/h — harmonics forever. Play a stored sawtooth at a high pitch and its upper harmonics sail past Nyquist and fold back as inharmonic garbage: frequencies unrelated to the note, heard as a metallic sourness that no equalizer can remove. Naïve digital oscillators sound cheap for precisely this reason.

The fix: eleven pre-filtered copies

This synth’s solution is mip-mapping, borrowed in spirit from computer graphics. For each waveform, it precomputes a ladder of eleven band-limited versions: the full wave with 1,024 harmonics, then 512, then 256… down to a bare sine. The construction is pure Fourier surgery, done once at load time:

FFTzero every bin h > Hmaxinverse FFT Truncating the Fourier series is the ideal low-pass filter — a projection onto the subspace of waves that cannot alias at the target pitch.

At play time, the engine computes how many harmonics fit under Nyquist for the current pitch (H = ⌊fs / 2f⌋) and reads from the rung of the ladder that fits — actually from a smooth blend of two adjacent rungs, so that a gliding pitch never audibly “switches.” The blend must lean on the safer rung: mixing in any table whose harmonics exceed Nyquist, even at 10% amplitude, reintroduces the crime.

war story · caught by a theorem with a unit test The project keeps an automated spectral regression harness: render the oscillator, take a 32,768-point FFT, and assert that everything which is not a harmonic of the fundamental sits below −100 dB — one part in 100,000. When the crossfade was first written, it blended from the mathematically wrong rung of the ladder. No human ear flagged it in testing; the FFT did, instantly, reporting alias energy at −54 dB. The fix changed one index. The harness also contains a test that verifies the test: it renders a deliberately aliasing naïve sawtooth and asserts the harness fails it — a falsifiability check, because a detector that never fires is indistinguishable from a broken one.
SECTION 5

Reading between the samples

A table holds 2,048 points, but a playing note needs the wave’s value at position 1371.640625. Interpolation — and the choice of interpolant is audible.

Nearest-neighbor lookup produces staircase error rich in high-frequency images. Linear interpolation — connecting the dots — is better, but its error spectrum still leaks audible ghost frequencies; in graphics you’d see it as jaggies, in audio you hear it as a faint fizz. This synth reads through a Catmull-Rom spline: the cubic through four neighboring points whose slope at each sample is the centered difference of its neighbors,

x(t) ≈ ((a·t + b)·t + c)·t + p0 with a = ½(3(p₀−p₁) − p₋₁ + p₂), b = p₁ + p₁ + p₋₁ − ½(5p₀ + p₂), c = ½(p₁ − p₋₁) — a C¹ piecewise cubic, evaluated in Horner form: four multiplies per sample.

The payoff is in the error orders: linear interpolation’s error is O(h²), the cubic’s is O(h⁴) with smooth first derivatives — and its spectral images fall below the −100 dB alias floor the project enforces elsewhere. Numerical analysis, in service of not annoying the ear.

Three interpolants through the same eleven samples (dots) of a smooth wave (grey). Nearest is a step function; linear is C⁰ with visible corners; the cubic hugs the truth with continuous slope. The corners are not just ugly — Section 7 shows that corners, as a class, are audible.
SECTION 6

The filter is a differential equation with a resonance knob

The most beloved control on any synthesizer — the filter cutoff — is a knob on a second-order ODE. Sweep it and you are continuously re-parameterizing a damped harmonic oscillator while it runs.

Analog synth filters were literal circuits: capacitors integrating currents. The canonical state-variable filter is, in continuous time, exactly the mass-spring-damper equation with the input as forcing:

ÿ + 2ζωc ẏ + ωc2 y = ωc2 x(t) ωc = cutoff frequency, ζ = damping. Low damping ⇒ the system rings near ωc — that ringing is resonance, the “wet, whistling” quality of a squelchy synth bass. The resonance knob is literally 1/ζ.

One equation, three outputs for free: y is the low-pass, ÿ-side gives the high-pass, and the band-pass — which is why the synth offers all three modes from a single structure.

Digitizing without lying

To run this on samples, the integrators become sums — this synth uses the trapezoidal rule, which in the signal world goes by bilinear transform. It has a beautiful geometric character: it is the Möbius map s = 2fs(z−1)/(z+1), which carries the entire imaginary axis of the analog frequency plane onto the unit circle of the digital one. Stability is preserved exactly (left half-plane → inside the disc), but frequency gets nonlinearly compressed near Nyquist. The distortion is known in closed form, so you cancel it in advance — pre-warping:

g = tan(π fc / fs) Feed the digital filter this warped coefficient and its cutoff lands at exactly fc. The tangent — the Möbius map’s fingerprint — sits in the hot loop of the synth, recomputed 1,500 times per second per voice while you turn the knob.

One more subtlety separates a textbook digital filter from a musical one. The trapezoidal integrator makes the filter’s feedback instantaneous — output depends on this very sample’s input. Naïve implementations insert a one-sample delay in the loop and drift out of tune at high cutoffs; the correct approach (called “zero-delay feedback” in the trade) solves the implicit equation algebraically, the same move as an implicit ODE solver. This synth’s filter is exact in that sense, which is why its resonance stays stable and in tune even while being violently modulated — there is a unit test that whips the cutoff across four octaves at audio rate and asserts the state stays bounded.

The exact magnitude response |H(e)| of the synth’s filter, computed from the same equations the engine runs, on a log-frequency axis (the ear’s axis). Raise the resonance and watch the pole pair slide toward the unit circle — the peak at cutoff is the ODE ringing. The engine caps resonance at 0.98: the remaining 0.02 is the margin between “sounds alive” and “is an oscillator now.”
SECTION 7

The calculus of clicks

Here is the most surprising thing this project taught its author: the human ear can hear differentiability class. Not metaphorically — operationally, reproducibly, at levels far below what a waveform plot reveals.

Every note is shaped by an envelope: a function that fades the sound in at key-press and out at release. This synth’s envelopes are exponential approaches — the solution of ẏ = (target − y)/τ, computed as the recurrence y[n+1] = y[n] + k(target − y[n]) — one multiply-add per sample, the discrete shadow of an RC circuit.

Exponentials are lovely, but an ADSR envelope is piecewise exponential, and at each junction the derivative jumps. During development, listeners reported a faint tick at chord changes — yet the recorded waveform, zoomed to the sample level, showed no jump at all. The signal was continuous. Its slope was continuous. The bug was hiding one derivative deeper, and the instrument that found it was a bank of finite differences:

Δ¹ — a value step

C⁻¹ failure. A hard click, plainly visible in the waveform. Example: cutting a voice without a fade. Energy spectrum falls as 1/f — loud, broadband.

Δ² — a slope kink

C⁰ but not C¹. A softer click, hard to see. Example caught here: switching a channel between two filters whose internal states remembered different histories.

Δ³ — a curvature jump

C¹ but not C². A feeble tick, invisible in any waveform plot. Example: 24 envelope attack-corners igniting on the same sample. Spectrum falls as 1/f³ — quiet, but the ear flagged it.

The measurement

Steady-state third difference of the signal: 0.00008. In the ten milliseconds after a chord change: 0.002 — a 25× spike, precisely event-locked. The listener was right; the oscilloscope was blind.

The repair is a classic from analysis: convolve the envelope with a smoothing kernel — a mollifier, in practice a 0.5-millisecond one-pole — which rounds every corner and lifts the output from C⁰ to effectively C at the junctions. Applied to the control signal only, never the audio: at steady state the smoother is the identity, so timbre is untouched by construction.

bottom trace: second difference (click detector), amplified
An ADSR envelope and its second difference. The raw envelope looks perfectly smooth to the eye, but its difference trace spikes at every stage junction — those spikes are what a quiet room and a good ear receive. The mollified version’s difference trace is flat. The ear as a functional: it evaluates something close to ∫|x‴|² and objects when it is large.
war story · the ear beat the instruments, twice The click-hunting metric itself had to be debugged. The first regression test passed with the bug present — its test choreography kept the two filter states identical, so there was no divergent state to expose. The second version also passed wrongly: it toggled faster than the detuned oscillators could decorrelate. The final protocol: let the system genuinely diverge (half a second of wide detune), make one transition, measure the third difference — and then re-introduce the bug deliberately to confirm the test fails. A test you haven’t watched fail is a conjecture, not a theorem.
SECTION 8

Distortion, rescued by the Mean Value Theorem

To make a sound “growl,” synths deliberately push it through a nonlinearity. The one used here is tanh — a smooth soft-clipper. Nonlinearity manufactures new harmonics; new harmonics can exceed Nyquist; Section 4’s villain returns. The escape is one of the prettiest tricks in modern audio DSP, and it is essentially the Mean Value Theorem run in reverse.

The problem: applying tanh sample-by-sample is applying it to the sampled signal, and the distortion products fold. The insight (Parker et al., 2016 — “antiderivative anti-aliasing”): instead of evaluating the nonlinearity at each sample, output its average over the inter-sample interval. For that, you don’t need the signal between samples — the fundamental theorem of calculus hands it to you from the antiderivative alone:

y[n] = 1/(x[n] − x[n−1]) · ∫x[n−1]x[n] tanh(u) du = ( F(x[n]) − F(x[n−1]) ) / ( x[n] − x[n−1] ) with F(u) = ln cosh u. Averaging is a convolution with a box kernel — a low-pass applied in the continuous domain, before sampling can fold anything. Cost: one extra state variable.

When consecutive samples nearly coincide, the divided difference degenerates to 0/0 — and its limit, by the very definition of the derivative, is F′ = tanh at the midpoint. The code contains that limit as a branch. L’Hôpital, shipped to production.

A numerical-analysis ambush

The first implementation used 32-bit floats and developed a strange rasp at heavy drive — noise injected by the anti-noise stage. The diagnosis is textbook catastrophic cancellation: at a drive of +36 dB the inputs reach |x| ≈ 150, where F(x) ≈ 149.3 and a 32-bit float’s spacing (ULP) is 1.5×10⁻⁵. Subtracting two such values for a small Δx leaves almost pure rounding error, then the division amplifies it — up to 30% per sample. The same computation in 64-bit floats errs at one part in 10⁸. Two lines changed; a whole class of error extinguished.

Left: the transfer curve y = tanh(g·x) steepening with drive — a sine entering, a rounded square emerging. Right: the output’s harmonic spectrum, computed live by a small DFT. tanh is odd, so only odd harmonics appear — symmetry classes of the nonlinearity are audible as a timbre family (even harmonics would require breaking the odd symmetry).
SECTION 9

A choir of one, and the trigonometry of width

The “supersaw” — the huge sound under most modern dance leads — is one note played by up to eight copies of the oscillator, each mistuned by a few cents. Everything you hear in it is interference.

Two sines detuned by δ obey the sum-to-product identity:

sin(2πf t) + sin(2π(f+δ)t) = 2 cos(πδt) · sin(2π(f + δ/2)t) A carrier at the average frequency, amplitude-modulated at the difference frequency — beats. With eight copies at eight detunes, the beat pattern never exactly repeats: motion without an LFO in sight.
Seven detuned copies, summed. The envelope’s slow undulation is the pairwise beat structure — the sound’s “shimmer.” Detune wider and the beating accelerates from slow breathing to thick chorus.

Energy bookkeeping on the unit circle

Spreading the copies across the stereo field uses constant-power panning: each copy’s left/right gains are (cos θ, sin θ) for a pan angle θ — so gL² + gR² = 1 identically, and loudness (which tracks power, not amplitude) is invariant as a copy sweeps across the image. The Pythagorean identity, employed as a conservation law.

Summing N copies raises a normalization question with a probabilistic answer. The copies start at random phases, so they add like independent random variables: amplitudes don’t sum — variances do, and the ensemble’s RMS grows as √N. The engine therefore divides by √(Σ aₖ²), keeping perceived loudness constant whether the choir has one voice or eight. (A subtle bug lived here once: with an even number of copies, the “center” weight was handed to one off-center copy, tilting the stereo image 2.5 dB leftward and biasing the ensemble’s mean pitch flat. Symmetry restored it: even choirs now split the center weight between the two middle voices.)

SECTION 10

The architecture of echoes

Reverberation — the sound of a room — is a linear operator with an enormous, decaying impulse response. Simulating it honestly would convolve every sample with a few seconds of response. The classic alternative, running in this synth, builds a room from feedback loops and a small miracle of complex analysis.

The atom is the feedback comb filter: a delay line of D samples feeding back with gain g. One impulse in, a geometric series out — echoes at spacing D, amplitudes g, g², g³… Exponential decay for free; the “room size” knob simply sets g. Its frequency response, though, is a picket fence — resonances at every multiple of fs/D — so one comb sounds like singing into a pipe. The design (Schroeder, 1962; refined as “Freeverb”) runs eight combs in parallel with carefully chosen, near-coprime delay lengths (1116, 1188, 1277, 1356, 1422, 1491, 1557, 1617 samples), so their resonance fences almost never align — the sum’s response is dense and roomlike. Number theory, deployed against flutter echo.

Then the miracle: the parallel combs feed four allpass filters in series,

H(z) = ( z−D + g ) / ( 1 + g·z−D ),   |H(e)| = 1  for all ω Poles and zeros in reciprocal-conjugate pairs: the magnitude response is exactly flat — it changes no tone color at all — while the phase response scrambles arrival times, multiplying echo density.

An operator that is audibly “transparent” in the frequency domain yet dramatically active in the time domain — students meet allpass filters and assume they do nothing. Rooms are built out of them.

Impulse responses. One comb: a bare geometric series — metallic, periodic. The full Schroeder network: echo density growing until individual reflections fuse into the statistical wash the ear reads as “a room.” Decay time follows RT₆₀ ≈ 3·ln(10)·D / (fs·ln(1/g)) — solve the geometric series for −60 dB.
SECTION 11

Watching the spectrum, and paying the uncertainty tax

The synth’s display shows a live spectrum — a Fourier transform of the last thousand samples, recomputed dozens of times per second. Doing this honestly means negotiating with an uncertainty principle.

The discrete Fourier transform of a finite window is really the transform of the signal times the window — and by the convolution theorem, the spectrum you see is the true spectrum smeared by the window’s own transform. A rectangular window has vicious sidelobes (−13 dB): energy from one loud partial “leaks” across the whole display. The synth uses a Hann window, w[n] = ½(1 − cos 2πn/N), trading a slightly wider main lobe for sidelobes at −31 dB and falling fast.

Underneath sits the real constraint, formally identical to Heisenberg’s:

Δt · Δf ≥ 1/4π A window Δt seconds long cannot resolve frequencies finer than ~1/Δt apart. Sharper pitch reading demands a longer look; a longer look blurs time. The analyzer, the ear, and quantum mechanics all pay the same tax — it is a theorem about Fourier pairs, not about physics.

The same discipline shapes the project’s test suite: test tones are chosen to land exactly on FFT bins (frequencies of the form k·fs/N), so harmonics occupy single bins and any energy elsewhere is provably artifact. Measurement theory first; assertions second. The whole quality story of this synth — the −100 dB alias floors, the click taxonomy, the cancellation hunt — rests on knowing what the measuring instrument itself distorts.

CODA

48,000 theorems per second

Zoom out, and one note on this synthesizer is a stack of mathematics executing in real time.

Press a key: an exponential map turns note number into frequency. Eleven Fourier projections stand ready so the harmonic series never crosses Nyquist. A C¹ cubic reads between samples. A trapezoidally-integrated second-order ODE colors the tone, its cutoff pre-warped through a Möbius map’s tangent. The Mean Value Theorem averages a hyperbolic tangent so distortion can’t fold. Mollified exponentials shape the loudness so no derivative jumps loudly enough to hear. Sum-to-product identities shimmer the choir; the Pythagorean identity conserves its power; √N normalizes its loudness. Near-coprime geometric series build the room, and flat-magnitude Möbius atoms densify it. All of it, per sample, forty-eight thousand times a second, with a hard real-time deadline and no permission to allocate memory or throw an error — because a single missed deadline is itself a Δ¹ discontinuity, and Section 7 explains exactly how that sounds.

The part that might please a mathematician most: almost every bug worth telling a story about was found not by reading code but by measurement against theory — an FFT that knew where harmonics must lie, a finite difference that knew what smoothness requires, a floating-point model that knew where subtraction goes to die. The theory did not decorate the engineering. It was the debugger.

Music is the pleasure the human mind experiences from counting without being aware that it is counting.
— Leibniz, who would have adored a spectral regression harness