The idea: a periodic function is a sum of harmonics
A function that repeats every 2π can be written as a constant plus waves that fit the period a whole number of times: cos x and sin x once, cos 2x and sin 2x twice, and so on. Wave n is the n-th harmonic. The sum is the Fourier series:
f(x) = a₀/2 + a₁ cos x + b₁ sin x + a₂ cos 2x + b₂ sin 2x + …
Cutting the sum off after harmonic N gives the partial sum SN(x), the blue curve on the page. Each Add Harmonic works out one more coefficient and adds one more wave.
Where a coefficient comes from
Two different harmonics are orthogonal: the product sin(mx)·sin(nx) has zero area over one period when m ≠ n, and the same holds for cosines and for a sine against a cosine. So if you multiply f by sin nx and take the area, every harmonic except the n-th one drops out, and what is left is π·bn:
bₙ = (1/π) ∫ f(x)·sin(nx) dx aₙ = (1/π) ∫ f(x)·cos(nx) dx (over −π … π)
That is step 1 of every Add Harmonic: the purple curve is f(x)·sin(nx), green strokes are area above the axis, red strokes area below. For the square wave and n = 1 the product is |sin x|, all green, with area 4, so b1 = 4/π = 1.2732. For n = 2 every green patch has a red twin and the area is exactly 0 (Demo 3: why no even harmonics).
The three series on the page
| function | symmetry | coefficients | series |
|---|---|---|---|
| square wave, sign x | odd: sine terms only | bn = 4/(nπ), odd n | (4/π)(sin x + sin 3x/3 + sin 5x/5 + …) |
| sawtooth, x/π | odd: sine terms only | bn = ±2/(nπ), every n | (2/π)(sin x − sin 2x/2 + sin 3x/3 − …) |
| triangle, |x|/π | even: cosine terms only | a0/2 = 1/2, an = −4/(n²π²), odd n | 1/2 − (4/π²)(cos x + cos 3x/9 + …) |
An odd function (f(−x) = −f(x)) times a cosine is odd, so its area is 0: no cosine terms. An even function times a sine is odd too: no sine terms. The square wave also has a half-wave symmetry, f(x + π) = −f(x), which kills every even harmonic. a0/2 is the average height of f: 0 for the two odd functions, 1/2 for the triangle (the orange bar 0).
How fast the coefficients shrink
The coefficient chart shows the rule: a function with a jump has coefficients that shrink like 1/n; a function that is continuous but has corners shrinks like 1/n²; each extra derivative that is smooth gains one more power of n. So the triangle converges much faster: at N = 9 its RMS error is 0.0037, against 0.2010 for the square wave (Demo 5: triangle).
The Gibbs overshoot
Next to a jump, SN overshoots. For the square wave the peak is 1.2732 with one harmonic, 1.2004 with N = 3, 1.1823 with N = 9 and 1.1790 with N = 99: it never goes below 1.1790. The overshoot of 0.179 is 8.95% of the jump from −1 to +1. More harmonics only squeeze the bump towards the jump: its peak sits at x = π/(N+1). This is the Gibbs phenomenon (Demo 2: Gibbs). The sawtooth has the same limit, 1.179, but reaches it more slowly because the function itself slopes up to the jump (Demo 6). The triangle has no jump and no overshoot: its peak creeps up to 1.
| N | square: peak | square: RMS error | sawtooth: peak | triangle: peak |
|---|---|---|---|---|
| 1 | 1.2732 | 0.4352 | 0.6366 | 0.9053 |
| 9 | 1.1823 | 0.2010 | 1.0773 | 0.9798 |
| 19 | 1.1798 | 0.1423 | 1.1286 | 0.9899 |
| 99 | 1.1790 | 0.0637 | 1.1690 | 0.9980 |
The error still goes to zero: Parseval
Both things are true at once: the peak stays 9% too high, and SN gets closer to f. The table's last column is the RMS error, the square root of the average of (f − SN)² over a period. Parseval's identity gives it without an integral: mean of f² = (a₀/2)² + ½Σ(an² + bn²), so the squared error is whatever the harmonics so far have not yet accounted for. It shrinks to 0 because the bump gets narrower, even though it does not get lower. At every fixed x away from the jump SN(x) converges to f(x); at the jump itself it gives the midpoint, SN(0) = 0 for the square wave.
The epicycles
an cos nx + bn sin nx is the height of a point on a circle of radius √(an² + bn²) that turns n times per period. Put each circle on the tip of the one before (starting at height a₀/2) and the height of the last tip is SN(x*), carried to the red dot on the blue curve. Spin moves x* once round the period (Demo 1, Demo 4). The page draws the five biggest circles and the rest of the harmonics as one thin line, because their circles are only a few pixels wide.
See also Fourier Series and the FFT for complex epicycles that draw a shape and the discrete transform, Taylor Series for the other famous series (powers of x instead of harmonics, good near one point instead of over a whole period), Sine Waves: What a, b, c, d Do for one harmonic's amplitude and frequency, The Unit Circle for why a turning point traces a sine, and Standing Waves and Wave Interference for harmonics and sums of waves in physics.
The ringing next to a sharp edge in a low-quality JPEG is this same Gibbs overshoot: see JPEG Compression (Demo 5).
A Fourier series whose coefficients shrink too slowly can be continuous but have no derivative anywhere: see The Weierstrass Function: Continuous Everywhere, Differentiable Nowhere.
The same circles can draw a closed shape in the plane, one circle per term of its discrete Fourier transform: see Fourier Epicycles: Drawing a Shape with Spinning Circles.
What the page leaves out
Periods other than 2π (replace nx by 2πnx/T), the complex form Σ cneinx, and functions that are not piecewise smooth, whose series can fail to converge at some points. Ways to remove the Gibbs overshoot: averaging the partial sums (Fejér) or σ-factors (Lanczos). The epicycles move between 24 positions per period in straight lines, so the fast circles jump. Sampled signals and the FFT are on the FFT page.