Case Study 2 — Building a Square Wave from Pure Tones

Field: Signal processing / data science (Fourier synthesis and the Gibbs phenomenon)

The problem a synthesizer designer faced

In the early 1970s, the engineers building analog music synthesizers wanted to generate a square wave — the buzzy, hollow tone that defines so much electronic music. A square wave is a brutally simple shape: the voltage snaps to $+1$, holds, then snaps to $-1$, holds, and repeats. On an oscilloscope it looks like a row of battlements. Yet when you analyze what a square wave is, made of, you find something surprising and beautiful, and it leads straight to a question that haunts every digital audio system today: why can't you reproduce a sharp edge perfectly, no matter how much computing power you throw at it?

The answer is a Fourier series (Section 24.7), and the stubborn artifact is the Gibbs phenomenon. This case study builds a square wave from pure sine tones, watches the synthesis converge, and explains why the convergence is genuine but forever imperfect at the corners — a fact that shapes how MP3, JPEG, and every digital filter are engineered.

What a square wave is made of

Define the standard square wave with period $2\pi$: $f(x)=+1$ on $(0,\pi)$ and $f(x)=-1$ on $(-\pi,0)$, repeating. The Fourier machinery of Section 24.7 says any such periodic function is a sum of sines and cosines:

$$f(x)=\frac{a_0}{2}+\sum_{n=1}^{\infty}\big(a_n\cos(nx)+b_n\sin(nx)\big),\qquad b_n=\frac{1}{\pi}\int_{-\pi}^{\pi}f(x)\sin(nx)\,dx.$$

Two symmetries do most of the work before any integration. The square wave is an odd function ($f(-x)=-f(x)$), so every cosine coefficient $a_n$ vanishes — including the average $a_0$, which is zero because the wave spends equal time at $+1$ and $-1$. Only the sine terms can survive.

Computing the sine integral (this is where Chapter 14's integral calculus reappears) gives $b_n=\dfrac{2}{n\pi}\big(1-\cos(n\pi)\big)$. Since $\cos(n\pi)=(-1)^n$, this is $0$ for even $n$ and $\dfrac{4}{n\pi}$ for odd $n$. Only odd harmonics appear, with amplitudes falling off like $1/n$:

$$f(x)=\frac{4}{\pi}\left(\sin x+\frac{\sin 3x}{3}+\frac{\sin 5x}{5}+\frac{\sin 7x}{7}+\cdots\right)=\frac{4}{\pi}\sum_{k=0}^{\infty}\frac{\sin((2k+1)x)}{2k+1}.$$

Read that as a recipe. A square wave is the fundamental tone plus a third-harmonic at one-third the strength, plus a fifth at one-fifth, and so on, odd harmonics only. The absence of even harmonics is exactly why a square wave sounds hollow — it is the same physics that makes a clarinet (which suppresses even harmonics) sound hollow compared to a flute.

Watching it converge

Add the harmonics one at a time and the shape sharpens. The single term $\tfrac4\pi\sin x$ is a lone hump; adding $\tfrac{4}{3\pi}\sin 3x$ flattens the top and steepens the sides; each further odd harmonic squares the corners a little more.

We can check a value by hand. At $x=\pi/2$ (the middle of the $+1$ plateau), every $\sin((2k+1)\pi/2)$ equals $+1,-1,+1,-1,\ldots$, so the series becomes

$$f(\tfrac\pi2)=\frac{4}{\pi}\left(1-\frac13+\frac15-\frac17+\cdots\right).$$

The bracket is the Leibniz series for $\pi/4$ (Section 24.9), so the sum is $\tfrac{4}{\pi}\cdot\tfrac{\pi}{4}=1$ — exactly the height of the plateau. The Fourier series of a square wave contains the most famous slow series for $\pi$. A few harmonics already land close.

# Build the square wave from N odd harmonics; check the plateau at x = pi/2.
import numpy as np
x = np.linspace(-np.pi, np.pi, 2001)
def square_approx(x, N):
    s = np.zeros_like(x)
    for k in range(N):
        n = 2*k + 1
        s += np.sin(n*x) / n
    return (4/np.pi) * s
for N in (1, 4, 16, 64):
    val = square_approx(np.array([np.pi/2]), N)[0]
    print(f"N={N:2d} harmonics -> f(pi/2) = {val:.4f}")
# N= 1 -> 1.2732   N= 4 -> 1.0631   N=16 -> 1.0156   N=64 -> 1.0039  (-> 1)

The midpoint value marches obediently toward $1$. The trouble is not in the middle of the plateau — it is at the edges.

The corner that will not behave: Gibbs

Watch what happens near the jump at $x=0$. As you add more harmonics, the approximation does get narrower in its wobble, but it always shoots past the target by about the same amount: roughly 9% overshoot just before each cliff. Pile on a thousand harmonics and the spike does not shrink — it only gets thinner and crowds closer to the discontinuity. This is the Gibbs phenomenon (Section 24.7), and it is not a numerical bug. It is a theorem.

The reason the series still "converges" despite a permanent overshoot is that convergence here is measured in the mean-square ($L^2$) sense: the area of the error shrinks to zero even though its peak height does not. The thin Gibbs spike contributes ever-less energy as it narrows. Pointwise, at the jump itself ($x=0$), every sine term is zero, so the series equals $0$ — the average of the left value $-1$ and the right value $+1$. Fourier series, faced with a discontinuity, split the difference. This is the precise statement Section 24.7 warned about: convergence is genuine but subtle, and "the function equals its Fourier series" needs an asterisk at jumps.

# The Gibbs overshoot does not shrink: peak stays ~1.09 as N grows.
import numpy as np
x = np.linspace(0, np.pi, 100001)   # fine grid just right of the jump
def square_approx(x, N):
    return (4/np.pi)*sum(np.sin((2*k+1)*x)/(2*k+1) for k in range(N))
for N in (10, 50, 250):
    print(f"N={N:3d}: peak = {square_approx(x, N).max():.4f}")
# N= 10: peak = 1.0950   N= 50: peak = 1.0896   N=250: peak = 1.0894  (~1.0895, not 1)

The peak settles near $1.0895$ — about 9% above the true value of $1$ — and stays there forever.

Why it matters for data science and audio

This is not an academic curiosity. Every time a digital system represents a signal with a finite set of frequencies — which is always, since computers are finite — it inherits Gibbs. Sharpen a JPEG too aggressively and "ringing" halos appear next to high-contrast edges: that is the two-dimensional cosine transform of Section 24.7 overshooting at the boundary between black text and white page. Brick-wall audio filters that cut frequencies abruptly produce "pre-echo," a faint Gibbs ripple before percussive transients, which is why MP3 and AAC encoders deliberately smooth their filter edges rather than cut sharply. Engineers fight Gibbs with windowing — tapering the harmonics gently to zero instead of truncating them abruptly — trading a touch of blur for the elimination of ringing. The entire field of window-function design (Hamming, Hann, Blackman windows) exists to manage this single consequence of representing sharp edges with smooth waves.

The deeper lesson is the chapter's own: a complicated signal is a sum of simple waves, and that decomposition is exact in the limit but controlled in the finite case. Knowing exactly how a finite Fourier sum fails — where it overshoots, what it converges to at a jump — is what separates a working codec from a buzzing one.

Discussion questions

  1. Explain, using the $1/n$ decay of the square-wave coefficients, why the series converges but only slowly. How does this decay rate compare to the $1/n^2$ decay you would get from a continuous triangle wave, and why does smoothness speed convergence?
  2. At $x=0$ the series equals $0$, the average of $\pm1$. Restate this as a general rule about Fourier series at jump discontinuities (Section 24.7).
  3. The Gibbs overshoot stays ~9% but narrows. Reconcile this with the claim that the series "converges," using the mean-square ($L^2$) notion of convergence.
  4. The plateau value at $x=\pi/2$ reproduced the Leibniz series for $\pi$. Why is it satisfying — and a little ironic — that a slow series for $\pi$ shows up inside a fast-looking square-wave synthesis?

A short annotated reading

  • Stewart, Calculus: Early Transcendentals, Section 11.10–11.11 (Taylor and Maclaurin series) plus the chapter's applications. Grounds the series tools; pair with any Fourier supplement, since Stewart treats Fourier series in a separate applied volume.
  • OpenStax Calculus Volume 2, Section 6.3–6.4 (power and Taylor series). Free, careful coverage of the series convergence facts this case study leans on.
  • Bracewell, The Fourier Transform and Its Applications. The classic engineering reference; its chapters on the Gibbs phenomenon and windowing show exactly how the theory here becomes filter-design practice.
  • Smith, The Scientist and Engineer's Guide to Digital Signal Processing (free online). Reader-friendly chapters on the FFT, windowing, and why sharp filters ring — the applied payoff of Section 24.7.