A showcase for rakupp --target=js
Everything is sine waves
Give me a shape that repeats — square, jagged, however unlike a wave it looks — and I will build it out of nothing but smooth sines. Not approximately, in the limit: exactly. This is the single most useful fact in applied mathematics, and you can drag it.
Every number on this page is computed in your browser by Raku code. The engine is lib/Fourier.rakumod; the same file runs a command-line program in a terminal and this book in a browser, because rakupp --target=js turns it into JavaScript. Move a slider and Raku runs.
Start with a square wave: flat at +1 for half the period, flat at −1 for the other half, with two vertical cliffs. Nothing about it resembles a sine. Add the first harmonic and you get a lonely sine. Add the third, the fifth, the seventh — always odd, always smaller — and the corners start to appear out of nowhere.
The partial sum, chasing its targetengine: wave-coefficients, series-value
Drag the harmonics slider. The flat parts settle down quickly; the corners never quite do. That stubborn ringing at the cliffs has a name and an exact size, and it is chapter 4.
Chapter 2
How much of each
The recipe is an integral. To find how much of the nth sine is in a signal, multiply the signal by that sine and take the average — every other harmonic cancels itself out over a full period, and only the one you asked for survives.
Do those integrals on paper once and the classic waveforms have closed forms. The interesting thing is not the formulas but how fast they shrink, because that is the whole story of how well the series works:
| Waveform | Coefficients | Fall-off | Because |
|---|---|---|---|
| square | bₙ = 4/πn, odd n | 1/n | it jumps |
| sawtooth | bₙ = 2(−1)n+1/πn | 1/n | it jumps |
| triangle | bₙ = 8(−1)(n−1)/2/(πn)², odd n | 1/n² | continuous, but kinked |
| rectified sine | aₙ = −4/π(4n²−1) | 1/n² | continuous, but kinked |
| pulse | aₙ = 2 sin(πnd)/πn | 1/n, in a sinc envelope | it jumps, twice |
A discontinuity costs you 1/n. A mere corner costs 1/n². A genuinely smooth signal costs less than any power of 1/n — its coefficients vanish faster than you can write down. Smoothness is compressibility, and that sentence is why JPEG exists.
The spectrum of a shapeengine: wave-coefficients, rms-error
Amplitude of each harmonic. On the pulse wave, drag the duty slider and watch the sinc envelope — sin(πnd)/πn — breathe in and out: a narrow pulse in time needs a wide spread of frequencies, and a wide one needs few. That trade is the uncertainty principle, and it is the same theorem.
And the error against the number of harmonics, both axes logarithmic, so a power law is a straight line. The 1/n waveforms and the 1/n² ones sit at visibly different slopes.
Chapter 3
Build your own
Sixteen sliders, one per harmonic. Everything above said which amplitudes to use; here you choose them, and the shape is whatever you make it.
The presets load the closed forms from chapter 2 into the sliders, so you can start from a real waveform and break it. Try loading the square wave and then killing just the third harmonic, or setting every odd harmonic to the same height instead of letting them fall off as 1/n.
A synthesiserengine: series-value, parseval
The energy readout is Parseval's theorem: the mean square of the wave equals the sum of the squares of its coefficients. Move any slider and the two agree, always, because they are the same number written in two coordinate systems.
Chapter 4
The overshoot that stays
Add harmonics to a square wave and the partial sum gets closer everywhere — except right at the jump, where it always shoots about 9% too far. More terms make the overshoot narrower. They never make it shorter.
This is the Gibbs phenomenon, and it is the reason "the series converges" needs saying carefully. It converges at every individual point. It does not converge uniformly: the maximum error over the whole period does not go to zero. The peak settles on an exact number:
Zoomed in on the cliffengine: gibbs-peak, si-pi
With the zoom locked, the view rescales with the number of harmonics — and the bump looks exactly the same however far you slide, only narrower in absolute terms. Unlock it to watch the ringing shrink into the cliff while its height refuses to move.
Chapter 5
Taking a signal apart
So far we have been building. Going the other way — samples in, frequencies out — is the discrete Fourier transform, and it is the same integral with the smooth curve replaced by a finite list of numbers.
Build a signal below by adding tones, bury it in noise, and the transform still finds them. This is not clever peak-hunting: every bar is one term of an exact identity, and the signal can be rebuilt from the bars without losing anything.
Three tones, plus whatever noise you likeengine: tone-signal, spectrum
the signal, 128 samples
Push the noise past the tones and the waveform above becomes unreadable while the bars below stay exactly where they were. That is what a transform is for: noise is spread thinly over every frequency, and a tone is concentrated in one. Slide a tone above 64 — half the sample count — and watch its bar walk back down the axis instead of off the end. That is aliasing: sampled at 128 points, a 70-cycle wave is indistinguishable from a 58-cycle one.
Chapter 6
Making it fast
The transform as written costs n² multiplications. In 1965 Cooley and Tukey noticed that a transform of length n is two transforms of length n/2 glued together, and the cost collapsed to n log n. It is arguably the most consequential algorithm of the century.
The trick is that the even-indexed and odd-indexed samples each form a shorter transform, and every output of the long one is a sum of one output from each, with a rotation — a twiddle factor — applied to the odd half:
Two half-length transforms plus n cheap combinations. Recurse, and n² becomes n log₂n. Both are implemented below — the naive one straight from the definition, the fast one from those two lines — and they are timed against each other in your browser, on the same samples.
n² against n log n, liveengine: dft, fft
Measured here, not quoted: the bars are wall-clock milliseconds from your machine, and the curves are the two operation counts. Uncheck the naive transform before pushing the length past 211 unless you are patient — that is rather the point.
What this cost, and what it did not
The engine is 258 lines of Raku with no dependencies. It carries its own correctness checks — fourier.raku --check runs fifteen of them, and every one has an exact answer to compare against: the FFT must equal the DFT, the inverse must return the signal, Parseval must balance, the closed-form coefficients must equal their integrals, and the Gibbs peak must land on 2 Si(π)/π. Two of those checks failed the first time they ran, and both were real errors in coefficients I had written out by hand.
showcase/fourier/lib/Fourier.rakumod in the rakupp repository. fourier.raku is the terminal version of this book, with an oscilloscope drawn in characters; tools/build.raku bundles the engine with web/api.raku, transpiles it with rakupp --target=js --standalone, and inlines the result here. The page you are reading is one file with no dependencies.