Fourier series of a square wave
A musical chord is several pure notes played together. A Fourier series is the same idea for functions: almost any repeating shape can be made by adding sine and cosine waves of different sizes. This page builds a square wave that way.
What you need first
- How to integrate sin and cos.
- Odd and even functions. An odd function has f(−x) = −f(x), like sin x. An even function has f(−x) = f(x), like cos x.
- What the graph of sin(nx) looks like: n complete waves between 0 and 2π.
What you will be able to do
- Explain what a Fourier series is in plain words.
- Derive the coefficients for a square wave.
- Describe the Gibbs phenomenon and why it does not contradict convergence.
The idea
Take a function that repeats every 2π. The claim is that it can be written as a constant plus a sum of waves:
f(x) = a₀/2 + Σ (an cos nx + bn sin nx)
The numbers an and bn are the recipe. They say how much of each wave to mix in.
Key words
- Harmonic
- One of the waves sin nx or cos nx. The value n says how many times it repeats in 2π.
- Coefficient
- The amount of a harmonic in the mix: one of the numbers an or bn.
- Partial sum
- What you get by adding only the first few harmonics.
How the recipe is found
The coefficients come from these integrals:
an = (1/π) ∫−ππ f(x) cos nx dx, bn = (1/π) ∫−ππ f(x) sin nx dx
Why do they work? Because of a useful fact: if you multiply two different harmonics together and integrate over a full period, the answer is zero. The positive and negative parts cancel exactly. So multiplying f by sin nx and integrating wipes out every wave in the mix except the sin nx one, leaving just its coefficient. The integral acts as a filter that picks out one harmonic at a time.
Deriving the coefficients for a square wave
Take the square wave with f(x) = 1 for x between 0 and π, and f(x) = −1 for x between −π and 0.
- Use symmetry to save work. This square wave is an odd function. Cosines are even, and an odd function contains no even waves, so every an is zero. Only sines are needed.
- Simplify the integral. f(x) sin nx is odd times odd, which is even. So the integral from −π to π is twice the integral from 0 to π, where f = 1:
bn = (2/π) ∫0π sin nx dx = (2/nπ)(1 − cos nπ)
- Evaluate cos nπ. It is +1 when n is even and −1 when n is odd. So the bracket is 0 for even n and 2 for odd n.
- Collect the result. bn = 4/(nπ) for odd n, and 0 for even n.
f(x) = (4/π)(sin x + sin 3x / 3 + sin 5x / 5 + …)
Only odd harmonics appear, and each is weaker than the last.
Try it yourself
For each step, make a prediction before you move anything. Then check it.
Hide green and dark red, leaving the square wave and the single sine wave. How good a match is one term?
What you should see
Rough. It has the right rhythm, positive where the square wave is positive, but it is rounded and peaks too high, at 4/π, about 1.27.
Show green, which adds sin 3x / 3. Predict what the extra term does to the top of the wave.
What you should see
It flattens it. The new term pulls the middle of each hump down and pushes the shoulders up, so the top is closer to level.
Show dark red, the 5-term sum.
What you should see
Flatter still, with steeper sides. More terms give a better square, but notice the small ripples along the top.
Look at x = 0, where the square wave jumps from −1 to 1. What value does every partial sum take there?
What you should see
Zero. Every sine term is zero at x = 0. Zero is halfway between −1 and 1, and that is the rule: at a jump, a Fourier series converges to the midpoint.
The Gibbs phenomenon
Look closely at the peak just to the right of x = 0 on the 5-term curve. Zoom in if you like. It rises to about 1.18, clearly above 1.
You might expect this overshoot to shrink as you add terms. It does not. With more terms the peak moves closer to the jump and becomes narrower, but its height settles at
(2/π) ∫0π (sin t / t) dt ≈ 1.179
The jump has size 2, and the overshoot of 0.179 is about 9% of that. This stubborn overshoot is called the Gibbs phenomenon, and it appears at any jump in any function.
This can seem to contradict the claim that the series converges. It does not, and the reason is worth understanding. Pick any fixed x that is not at a jump. As you add terms, the overshoot slides past that point towards the jump, and the sums at your chosen x do settle down to f(x). What fails is a stronger property called uniform convergence: the worst error anywhere on the interval does not go to zero, because there is always a spike somewhere near the jump.
A bonus result
Put x = π/2 into the series. The square wave equals 1 there, and sin(nπ/2) alternates between 1 and −1 for odd n. So
π/4 = 1 − 1/3 + 1/5 − 1/7 + …
A formula for π has dropped out of a calculation about square waves.
Check yourself
Try each one on paper before you open the answer. Getting one wrong is useful: it shows you which part to reread.
Why are there no cosine terms in this series?
Show the answer
The square wave is odd and cosines are even. An odd function is built only from odd waves, which are the sines.
What is b₂, and why?
Show the answer
Zero. Between 0 and π, sin 2x has one positive hump and one equal negative hump, so its integral is zero.
What is the next term after sin 5x / 5?
Show the answer
sin 7x / 7, also multiplied by 4/π. Only odd harmonics appear, each divided by its own number.
What to remember
- A Fourier series writes a repeating function as a sum of sines and cosines.
- Each coefficient is found by multiplying by that wave and integrating, which filters out all the others.
- At a jump the series converges to the midpoint.
- Near a jump the partial sums overshoot by about 9% of the jump, however many terms you take.