Fourier Series, Explained
After reading this you can write down the Fourier series for a square, sawtooth or triangle wave, predict how fast the fit improves as you add harmonics, and explain why the overshoot at a jump never shrinks below about 9%.
What a Fourier series is
Take a periodic signal: a pattern that repeats every 2\pi in the variable x. A Fourier series claims you can rebuild that signal exactly by adding together sines and cosines whose frequencies are whole-number multiples of the base frequency. Each sine is called a harmonic. The first harmonic completes one cycle over the period, the second completes two, the third completes three, and so on.
The startling part, and the reason Fourier's contemporaries doubted him around 1807, is that the target can have sharp corners or even instant jumps, yet the ingredients are all smooth curves. A square wave leaps from -1 to +1 in zero distance. Sines never jump. Somehow an infinite stack of them reproduces the jump anyway.
The tool shows the same series two ways. On one side, each harmonic is drawn as a spinning circle (an epicycle) mounted on the tip of the previous one; the final tip traces out the wave. On the other side, the partial sum is drawn straight over the target so you can watch the error shrink. Both pictures encode the same coefficients.
When the series helps, and when it fights you
A Fourier series is the right tool when your signal is periodic and you care about its frequency content: the pitch of a note, the ripple in a power supply, the seasonal cycle in a data set. It is also the natural language for linear systems, because a sine fed into a linear filter comes out as a sine of the same frequency, only rescaled and shifted.
It fights you in three situations. If the signal is not periodic, you want the Fourier transform, not a series. If the signal has jumps and you need the value right at a jump, no finite sum will behave, because of the overshoot covered below. And if you need a good approximation with very few terms near a corner, expect disappointment: corners and jumps are exactly where sines struggle most.
The series converges to the average of the left and right values at a jump. For a square wave stepping from -1 to +1, every partial sum passes through 0 at the jump point, no matter how many harmonics you add.
The formula and what each piece means
Write a period-2\pi signal f(x) as a constant plus a sum of harmonics:
Here a_0/2 is the average value of the signal (its DC offset). Each a_n weights the cosine at frequency n, and each b_n weights the sine. You find each weight by integrating the signal against the matching wave over one period:
The cosine coefficients use the same integral with \cos(nx). This works because sines and cosines of different frequencies are orthogonal: integrate \sin(nx)\sin(mx) over a full period and you get 0 unless n = m. So each integral pulls out exactly one coefficient and ignores the rest.
Symmetry shortcuts save most of the work. An odd signal (where f(-x) = -f(x), like the square and sawtooth centred on zero) has all a_n = 0: it is built from sines alone. An even signal is built from cosines alone.
A worked example: the square wave
Building the odd square wave from sines
Take the square wave that equals +1 for 0 \lt x \lt \pi and -1 for -\pi \lt x \lt 0. It is odd, so only sine terms survive. Carrying out the integral gives coefficients that are non-zero only for odd n:
- The n=1 term has amplitude 4/\pi \approx 1.273. A single sine already overshoots the flat top of the square, since
1.273 > 1. - The n=3 term has amplitude 4/(3\pi) \approx 0.4244. Adding it flattens the top and steepens the edges.
- The n=5 term adds 4/(5\pi) \approx 0.2546. Even terms (n=2,4,6) contribute nothing.
- Amplitudes fall as 1/n. That slow decay is the fingerprint of a jump discontinuity.
Check the value at x = \pi/2, where the true signal is +1. There \sin((2k+1)\pi/2) alternates +1, -1, +1, ..., so the sum is the Leibniz series:
The three-term partial sum gives \frac{4}{\pi}(1 - 0.3333 + 0.2) = \frac{4}{\pi}(0.8667) \approx 1.103. Add the n=7 term (-1/7) and it drops to about 0.921. The sum closes in on 1 from both sides.
The Gibbs overshoot
Watch the partial sum near the jump and you will see a spike that pokes above +1 just before the edge, then rings below just after. Add more harmonics and the spike gets narrower and moves closer to the jump, but its height barely changes. It settles at about 1.0895, an overshoot of roughly 9% of the jump height. This is the Gibbs phenomenon, and it never disappears.
The limiting overshoot is set by an integral of the sinc function:
For a wave that jumps a total height of 2 (from -1 to +1), the peak overshoots by 2 \times (0.5895 - 0.5) = 0.179, so the sum reaches about 1.179 above the lower value or 0.089 above the top. That 0.089 divided by the flat level of 1 is the famous 9%. The key fact: this number does not shrink with more terms. What shrinks is the width of the spike, so the overshoot occupies less and less area and the mean-square error still goes to zero.
Do not try to smooth away Gibbs ringing by adding harmonics. It will not go. If ringing hurts your application (audio clicks, image ringing at edges), damp the high-frequency coefficients instead, for example by multiplying term n by a window that tapers to zero. That trades sharpness for a calmer edge.
Reading the coefficient decay
The rate at which coefficients shrink tells you how smooth the signal is, and vice versa. A jump costs you 1/n decay. A corner (continuous value, jumping slope) costs you 1/n^2. Each extra degree of smoothness earns you one more power of n in the denominator.
| Signal | Worst feature | Harmonics used | Coefficient decay |
|---|---|---|---|
| Square | jump | odd only | 1/n |
| Sawtooth | jump | all | 1/n |
| Triangle | corner | odd only | 1/n² |
Compare the square and triangle at n=9. The square's ninth harmonic has amplitude 4/(9\pi) \approx 0.1415. The triangle's ninth (which decays as 1/n^2) is proportional to 1/81 \approx 0.01235, roughly one tenth as tall. That is why the triangle looks almost perfect after only a handful of terms while the square still rings: the triangle has a corner, not a jump, so its high harmonics vanish far faster.
Common mistakes
Three errors trip up most people building their first series.
- Expecting the overshoot to vanish
- More terms narrow the ring but not its height. If you report the peak of a 100-term square-wave sum, you will still measure about
1.089, not1.000. - Forgetting the missing harmonics
- A square wave uses odd harmonics only. If your reconstruction looks lopsided, you may have accidentally included even terms with the wrong sign or amplitude.
- Mishandling the endpoints
- At a jump the series gives the midpoint of the two sides, not either side. For the
-1-to-+1square that midpoint is exactly0. Treating it as+1or-1will bias any error you compute at that point.
Related tools
If you want to feed the series an arbitrary drawing instead of a fixed wave, the Fourier Epicycles tool traces any closed curve with a chain of rotating circles built from its Fourier coefficients. To see sines solve a physical smoothing problem, the Heat Equation Simulator uses the same harmonics as the modes that decay over time. For rolling-circle curves that look like epicycles but come from gears rather than frequencies, try the Spirograph. And to see the unit-circle definitions of sine and cosine that every harmonic depends on, drag the angle in the Interactive Unit Circle.
Frequently asked questions
Why does a square wave only use odd harmonics?
The square wave has half-wave symmetry: shift it by half a period and it flips sign. Even harmonics do not flip under that shift, so they cannot help build the shape, and their coefficients integrate to exactly 0. Only odd harmonics survive.
What is the Gibbs overshoot in one number?
About 9% of the jump height. For a jump from -1 to +1 (height 2), the partial sum peaks near 1.089 above the lower level, and this holds however many terms you add.
How many harmonics do I need for a good fit?
It depends on the worst feature. For a triangle wave (corner), coefficients fall as 1/n^2, so 10 to 15 terms already look clean. For a square or sawtooth (jump), the 1/n decay means the edges keep ringing even at 50 or more terms.
What is the difference between a Fourier series and a Fourier transform?
A series applies to periodic signals and gives a discrete set of harmonics at frequencies 1, 2, 3, \dots times the base. A transform applies to non-periodic signals and gives a continuous spectrum. The series is the special case where the frequencies are forced onto a grid by the period.
Do the spinning circles and the partial sum show different things?
No. Each circle's radius is a harmonic's amplitude and its spin rate is that harmonic's frequency. The tip of the last circle traces exactly the partial sum you see plotted over the target. They are two views of the same coefficients.