The Fourier Coefficients
Watch the bars jump on a music player's equalizer: one column for the deep bass, one for the
mid-range, one for the shimmering treble, each rising and falling as the song plays. Somehow the
machine has looked at a single wobbling voltage — the raw sound wave — and worked out
exactly how much bass, how much treble, and how much of everything in between is hiding
inside it. How?
For a periodic function, the answer is a precise recipe, not a guess. We want to write a function
as a sum of sines and cosines. The
orthogonality
of those building blocks hands us each coefficient almost for free — by exactly the same
projection that recovers a vector's components.
For a vector, the component along a unit axis \mathbf{e}_k is the
dot product \mathbf{v}\cdot\mathbf{e}_k. For a function we project the
same way: to find the amount of \sin\frac{n\pi x}{L} inside
f, take the inner product of f with that
mode and divide by the mode's own "length squared". That single idea — project onto each wave in
turn — is the whole engine behind the equalizer's bouncing bars.
Deriving one coefficient
Suppose f(x) = \sum_k b_k \sin\frac{k\pi x}{L}. Take the inner product
of both sides with a single mode \sin\frac{n\pi x}{L}:
\int_{-L}^{L} f(x)\sin\frac{n\pi x}{L}\,dx = \sum_k b_k \int_{-L}^{L}\sin\frac{k\pi x}{L}\sin\frac{n\pi x}{L}\,dx.
Orthogonality kills every term in the sum except k = n, where
the integral equals L. The entire infinite sum collapses to one term:
\int_{-L}^{L} f(x)\sin\frac{n\pi x}{L}\,dx = b_n\,L \;\Longrightarrow\; b_n = \frac{1}{L}\int_{-L}^{L} f(x)\sin\frac{n\pi x}{L}\,dx.
That is the whole idea: orthogonality turns "solve for infinitely many unknowns at once" into
"read off each one with a single integral".
For a function f on [-L, L], the coefficients are
- a_0 = \dfrac{1}{L}\displaystyle\int_{-L}^{L} f(x)\,dx — twice the average value of f.
- a_n = \dfrac{1}{L}\displaystyle\int_{-L}^{L} f(x)\cos\dfrac{n\pi x}{L}\,dx.
- b_n = \dfrac{1}{L}\displaystyle\int_{-L}^{L} f(x)\sin\dfrac{n\pi x}{L}\,dx.
Each b_n is the projection of f onto the nth sine — how much of that ripple is "inside" f.
Worked example: a square wave, by hand
Let's actually turn the crank. Take the square wave of period 2\pi
(so L = \pi):
f(x) = \begin{cases} +1 & 0 < x < \pi \\ -1 & -\pi < x < 0. \end{cases}
First, a shortcut that saves real work: f is an odd
function (f(-x) = -f(x)), and every cosine term
\cos\frac{n\pi x}{L} is even. An odd function times an even one is odd,
and the integral of an odd function over a symmetric interval [-L, L] is
always zero. So without integrating a single cosine, we already know
a_0 = 0 and a_n = 0 for every
n.
Now the sine coefficients. Since f(x)\sin(nx) is even (odd
times odd), we can integrate over half the interval and double it:
b_n = \frac{1}{\pi}\int_{-\pi}^{\pi} f(x)\sin(nx)\,dx = \frac{2}{\pi}\int_0^{\pi} (1)\sin(nx)\,dx = \frac{2}{\pi}\left[-\frac{\cos(nx)}{n}\right]_0^{\pi} = \frac{2}{n\pi}\big(1 - \cos(n\pi)\big).
Since \cos(n\pi) = (-1)^n, this is
b_n = \dfrac{2}{n\pi}\big(1 - (-1)^n\big) — which is
0 whenever n is even (because
1 - (-1)^n = 0) and \dfrac{4}{n\pi} whenever
n is odd. Plugging in the first few:
b_1 = \frac{4}{\pi},\qquad b_2 = 0,\qquad b_3 = \frac{4}{3\pi},\qquad b_4 = 0,\qquad b_5 = \frac{4}{5\pi}.
A clean pattern falls out of the arithmetic: only the odd harmonics show up at all,
and their size shrinks like 1/n. That is not a coincidence of this one
example — it is exactly the kind of shortcut symmetry buys you, and it is the reason a square wave's
full series (built from these very numbers) turns out to be a pure sum of odd sines.
It's worth double-checking the symmetry argument with a direct integral, just once, so you trust
it. Take a_1 = \frac{1}{\pi}\int_{-\pi}^{\pi} f(x)\cos(x)\,dx. Split it
at zero: \frac{1}{\pi}\left(\int_{-\pi}^{0} (-1)\cos(x)\,dx + \int_0^{\pi} (1)\cos(x)\,dx\right) = \frac{1}{\pi}\big(-[\sin x]_{-\pi}^{0} + [\sin x]_0^{\pi}\big) = \frac{1}{\pi}(0 + 0) = 0,
exactly as the symmetry shortcut predicted — but it took a genuine integral (two of them, in fact)
to confirm what the odd/even check gave us for free. That's the whole point of checking symmetry
first: it lets you skip work you already know the answer to.
How an equalizer "hears" bass and treble
Back to the opening question. A spectrum analyzer or graphic equalizer chops a sound recording into
short, roughly-periodic snippets, then computes something extremely close to a Fourier coefficient
for a whole ladder of frequencies at once — one for deep bass, one a little higher, and so on up
through the treble. The size of each coefficient is literally "how much of that frequency
is present right now," which is exactly what each bar's height displays. Turn up the bass knob on a
stereo and, under the hood, you're multiplying one of these coefficients before the sound is
rebuilt.
Doing this fast enough to update thirty times a second, for thousands of frequencies, for every
speaker in every phone, would be hopeless by hand-integrating each one — so engineers use a
blisteringly efficient algorithm called the Fast Fourier Transform that computes an
entire batch of coefficients at once. The formulas are exactly the ones above; the speed comes from
clever bookkeeping, not new mathematics.
A concrete picture: a typical equalizer might report a "bin" for every few dozen hertz, from about
20 Hz (a deep rumble you feel more than hear) up past 15 kHz (a bright hiss). Each bin's height is
|a_n| or |b_n|-style magnitude for the
frequency that bin represents — feed in a pure bass drum thump and the low bins light up while the
high ones stay flat; feed in a cymbal crash and the pattern flips. The whole visual is nothing more
than a bar chart of Fourier coefficients, redrawn many times a second.
One mode at a time
Take f(x) = x on [-\pi, \pi]. Its sine
coefficients work out to b_n = \dfrac{2(-1)^{n+1}}{n} — so the
low modes carry most of the weight and the contributions taper off. The faint line is
f; the bold curve is the single projected mode
b_n\sin(nx). Step n to watch each ripple's
amplitude — exactly what the integral above measures, and exactly what an equalizer's bars are
doing thirty times a second.
Two classic traps when you actually sit down to compute coefficients:
-
Integrate over exactly one full period — no more, no less. The formulas above
were derived assuming the integral runs across the whole interval
[-L, L] of length 2L. Integrate over only
half of it, or over some arbitrary chunk that isn't a full period, and the orthogonality
cancellation that makes the derivation work breaks down — you'll get a number, but it won't be
the coefficient.
-
Check symmetry before you integrate anything. As the square wave example just
showed, spotting that f is even or odd lets you declare a whole family
of coefficients zero without doing a single integral. Skipping this check means grinding
through integrals that were always going to come out to zero — see the shortcut spelled out
properly in
sine and cosine series.
Compression formats like MP3 (for audio) and JPEG (for images)
lean directly on this idea. Break a sound clip or an image into Fourier-like coefficients, and you
typically find a few large ones carrying most of the content, surrounded by a long tail of tiny
coefficients that barely matter — a whisper-quiet high harmonic, or a texture detail too fine for
the eye to register.
Throw away the smallest coefficients — round them down to zero and don't bother storing them — and
you can shrink the file dramatically while the sound or picture looks and sounds almost identical.
That "almost" is doing a lot of work: throw away too many and you hear the tell-tale warble of a
heavily compressed MP3, or see the blocky smudges of an over-compressed JPEG. Every time you've
streamed a song or opened a photo on the web, some computer somewhere had already decided which
Fourier coefficients were worth keeping.
See it explained