---
title: "L² and the convergence of Fourier series"
heading: In what sense a square wave equals the sum of its harmonics
eyebrow: "A14 · Lesson 40 · L² and the convergence of Fourier series"
xp: 40
---

::: hook
A square wave is a sum of odd harmonics, and every engineer has seen the partial sums ring at the edges. In what sense does the series "equal" the square wave? Not uniformly: the ringing peak never shrinks below about 9% of the jump, however many terms you add (the **Gibbs phenomenon**). Not exactly at the jump either, where the series gives the midpoint. But in **energy** it converges perfectly: the energy of the error goes to zero. That's convergence in $L^2$, the inner-product space of finite-energy signals, and it's the sense in which Fourier analysis is exact. Parseval's identity, which splits a signal's power among its harmonics, is its most useful consequence.
:::

::: play
- The dashed curve sums the first m odd harmonics of a ±1 square wave, $\frac4\pi\sum\frac{\sin kx}{k}$.
- The readouts show the share of the square wave's energy that the sum captures, by Parseval, and the $L^2$ error, which shrinks steadily toward 0.
- The overshoot next to each jump doesn't shrink: it narrows and moves toward the jump but stays near 1.18 in height. Small in energy, but never small in the sup norm.
:::
:::

```sim plot
view: graph
x: x = -3.1416..3.1416
y: y = -1.4..1.4
slider: m = 1..13 step 1 = 5
plot: sign(x) | label: square wave
plot: 4/pi*(step(m - 0.5)*sin(1*x)/1 + step(m - 1.5)*sin(3*x)/3 + step(m - 2.5)*sin(5*x)/5 + step(m - 3.5)*sin(7*x)/7 + step(m - 4.5)*sin(9*x)/9 + step(m - 5.5)*sin(11*x)/11 + step(m - 6.5)*sin(13*x)/13 + step(m - 7.5)*sin(15*x)/15 + step(m - 8.5)*sin(17*x)/17 + step(m - 9.5)*sin(19*x)/19 + step(m - 10.5)*sin(21*x)/21 + step(m - 11.5)*sin(23*x)/23 + step(m - 12.5)*sin(25*x)/25) | samples: 1600 | dashed | label: Fourier sum, m odd harmonics
readout: share of the energy captured, % = 100*8/pi^2*(step(m - 0.5)/1 + step(m - 1.5)/9 + step(m - 2.5)/25 + step(m - 3.5)/49 + step(m - 4.5)/81 + step(m - 5.5)/121 + step(m - 6.5)/169 + step(m - 7.5)/225 + step(m - 8.5)/289 + step(m - 9.5)/361 + step(m - 10.5)/441 + step(m - 11.5)/529 + step(m - 12.5)/625)
readout: L² error = sqrt(2*pi - 16/pi*(step(m - 0.5)/1 + step(m - 1.5)/9 + step(m - 2.5)/25 + step(m - 3.5)/49 + step(m - 4.5)/81 + step(m - 5.5)/121 + step(m - 6.5)/169 + step(m - 7.5)/225 + step(m - 8.5)/289 + step(m - 9.5)/361 + step(m - 10.5)/441 + step(m - 11.5)/529 + step(m - 12.5)/625))
readout: overshoot as m grows, about = 1.179
```

::: predict
## What fraction of a square wave's power is in its fundamental?

- [ ] 50%
- [ ] 64%
- [x] 81%
- [ ] 100%

::: reveal
$\frac{8}{\pi^2} = 81.1\%$. The fundamental's amplitude is $\frac4\pi$ times the square wave's, so its power is $\frac12\left(\frac4\pi\right)^2 = \frac{8}{\pi^2}$ of the square wave's power of 1. The other 19% is spread over the odd harmonics, falling as $\frac{1}{k^2}$: the square wave's distortion.
:::
:::

::: nameit
## The space $L^2$

$L^2[-\pi, \pi]$ is the space of functions with finite energy $\lVert f\rVert^2 = \int_{-\pi}^{\pi}\lvert f\rvert^2$, with the inner product of lesson 37 (functions that agree except on a set of zero length count as equal). It's complete: a Hilbert space.

## Fourier series as an orthogonal expansion

The functions 1, cos kx, sin kx are orthogonal in $L^2[-\pi, \pi]$, so the Fourier coefficients are projections:

$$a_k = \frac{\langle f, \cos kx\rangle}{\pi}, \quad b_k = \frac{\langle f, \sin kx\rangle}{\pi}, \quad a_0 = \frac{\langle f, 1\rangle}{2\pi}$$

and each partial sum $S_N$ is the best approximation to f, in energy, by trigonometric polynomials of degree N.

## Convergence

- **In $L^2$:** for every $f \in L^2$, $\lVert f - S_N\rVert \to 0$. And **Parseval's identity** holds:

$$\frac1\pi\int_{-\pi}^\pi f^2 = 2a_0^2 + \sum_{k\ge 1}(a_k^2 + b_k^2)$$

- **Pointwise:** if f is piecewise smooth, $S_N(x) \to f(x)$ where f is continuous, and to the midpoint $\frac{f(x^-) + f(x^+)}{2}$ at a jump.
- **Uniformly:** if f is continuous, periodic and piecewise smooth (so $\sum(\lvert a_k\rvert + \lvert b_k\rvert) < \infty$).
- **Gibbs:** at a jump of size J, the partial sums overshoot by about $0.0895J$ on each side, for every large N.
- **Fejér:** averaging the partial sums removes the overshoot and converges uniformly for every continuous periodic f; it's the trigonometric Weierstrass theorem.

## In measurement

Parseval makes distortion computable: a signal's total harmonic distortion is the RMS of its harmonics over the fundamental's RMS, which for a square wave is $\sqrt{\pi^2/8 - 1} = 48.3\%$.
:::

::: example
## How much of a square wave's energy do the harmonics through the 25th capture?

::: solution
1. **All the energy.** The odd harmonics have powers proportional to $\frac{1}{k^2}$, and together they account for all of it, since
   $$\sum_{\text{odd }k}\frac{1}{k^2} = \frac{\pi^2}{8}$$
2. **The share through k = 25.**
   $$\frac{8}{\pi^2}\sum_{k\text{ odd} \le 25}\frac{1}{k^2} = 98.4\%$$
3. **What's left.** That leaves an $L^2$ error of
   $$\sqrt{0.098} = 0.31$$
   on [−π, π], where the square wave's own norm is $\sqrt{2\pi} = 2.51$.

**Answer:** the harmonics through the 25th capture 98.4% of the energy.
:::
:::

::: example
## What does the Fourier series of the ±1 square wave give at x = 0?

::: solution
1. **Each term at 0.** Every term $\sin(k \cdot 0)$ is 0, so the series gives 0.
2. **Where that sits.** 0 is the midpoint of the jump from −1 to 1. Pointwise convergence can't pick a side.

**Answer:** the series gives 0, the midpoint of the jump.
:::
:::

::: example
## A triangle wave between −1 and +1 has odd harmonics of amplitude $\frac{8}{\pi^2k^2}$. Check Parseval and find its THD.

::: solution
1. **Mean square.** Its mean square is $\frac13$.
2. **Sum the harmonics' powers.**
   $$\begin{aligned} \text{power} &= \sum_{k\text{ odd}}\frac12\left(\frac{8}{\pi^2k^2}\right)^2 \\ &= \frac{32}{\pi^4}\sum_{k\text{ odd}}\frac{1}{k^4} \\ &= \frac{32}{\pi^4}\cdot\frac{\pi^4}{96} \\ &= \frac13 \end{aligned}$$
   as Parseval requires.
3. **The THD.** It's the RMS of the harmonics above the fundamental over the fundamental's:
   $$\begin{aligned} \text{THD} &= \sqrt{\sum_{k\ge3\text{ odd}}k^{-4}} \\ &= \sqrt{\frac{\pi^4}{96} - 1} \\ &= 0.121 \end{aligned}$$
   or 12.1%, against a square wave's 48%.
4. **Why it's low.** The coefficients fall as $\frac{1}{k^2}$ because the triangle is continuous.

**Answer:** the harmonics' powers add to $\frac13$, matching the mean square, and the THD is 12.1%.
:::
:::

::: check
given: k = 1 | 3 | 5 | 9
answer: 4/(pi*k)
tolerance: 0.1%

## What's the amplitude of harmonic {k} of a square wave that switches between −1 and +1?

::: right
Right. $\frac{4}{\pi k}$ = {answer}.
:::

::: misses
- [2/(pi*k)] The square wave's coefficients are $\frac4{\pi k}$ for odd k.
- [any] Not yet. $b_k = \frac1\pi\int_{-\pi}^\pi\text{sign}(x)\sin kx\,dx$.
:::
:::

::: check
given: m = 1 | 2 | 3
answer: 8/pi^2*(1 + step(m - 1.5)/9 + step(m - 2.5)/25)
tolerance: 0.1%

## By Parseval, what fraction of a square wave's power do its first {m} odd harmonics carry?

::: right
Right. $\frac{8}{\pi^2}\sum\frac{1}{k^2}$ over those harmonics = {answer}.
:::

::: misses
- [any] Not yet. Each odd harmonic's share is $\frac{8}{\pi^2k^2}$.
:::
:::

::: check
given: J = 2 | 5 | 10
answer: 0.0895*J
tolerance: 1%

## At a jump of size {J}, about how far past the higher level do the Fourier partial sums overshoot?

::: right
Right. About 8.95% of the jump: {answer}, for every large N.
:::

::: misses
- [0.179*J] That's the overshoot relative to half the jump.
- [any] Not yet. Gibbs: about 0.0895 times the jump.
:::
:::

::: check
given: a = 1 | -2; b = 3 | 4
answer: (a + b)/2
tolerance: 0.1%

## A piecewise smooth f jumps from {a} to {b} at x = 0. What does its Fourier series converge to there?

::: right
Right. The midpoint: {answer}.
:::

::: misses
- [b] The series can't choose a side; it takes the average.
- [any] Not yet. $\frac{f(0^-) + f(0^+)}{2}$.
:::
:::

::: check
given: K = 3 | 5 | 7
answer: sqrt(1/9 + step(K - 4)/25 + step(K - 6)/49)
tolerance: 0.1%

## Measured up to harmonic {K}, what's a square wave's THD (RMS of the odd harmonics from 3 to {K} over the fundamental's RMS)?

::: right
Right. {answer}; with every harmonic included it's $\sqrt{\pi^2/8 - 1} = 0.483$.
:::

::: misses
- [any] Not yet. Harmonic k has $\frac1k$ of the fundamental's amplitude.
:::
:::

::: check
kind: choice

## The Fourier partial sums of a square wave converge in which sense?

- [ ] Uniformly
- [x] In $L^2$ (energy), and pointwise everywhere, to the midpoint at the jumps
- [ ] Only in $L^2$, not pointwise anywhere
- [ ] Not at all

::: misses
- [1] The Gibbs overshoot never shrinks, so the sup error doesn't go to 0.
- [3] They converge pointwise too; only not uniformly.
- [4] The energy of the error goes to 0.
:::
:::

::: check
kind: parts
given: k = 3 | 5 | 9

## A triangle wave between −1 and +1 has harmonics of amplitude $\frac{8}{\pi^2k^2}$ for odd k.

::: part
answer: 8/(pi^2*k^2)
tolerance: 0.5%

### What is harmonic {k}'s amplitude?

::: misses
- [4/(pi*k)] That's a square wave.
- [any] $\frac{8}{\pi^2k^2}$.
:::
:::

::: part
answer: 1/k^2
tolerance: 0.5%

### What is it relative to the fundamental?

::: misses
- [1/k] Falls as $\frac{1}{k^2}$ for a continuous wave.
- [any] $\frac{1}{k^2}$.
:::
:::
:::

::: check
kind: derivation
vars: a = -3..3, b = -3..3

## $f(x) = a\cos x + b\sin 2x$ on [−π, π].

::: step
answer: a^2 + b^2

### What is $\frac1\pi\int_{-\pi}^{\pi}f^2\,dx$?
:::

::: step
answer: sqrt((a^2 + b^2)/2)

### What is its RMS value?
:::

::: right
Right. Orthogonality kills the cross term, and each sinusoid contributes half its amplitude squared to the mean square.
:::
:::

::: explain
## How can the Fourier series of a square wave converge in energy while its overshoot never shrinks?

::: model
Energy convergence measures the integral of the squared error, while the overshoot is about the largest error. As more harmonics are added, the Gibbs ripple near each jump keeps its height, about 9% of the jump, but gets narrower in proportion to $\frac1N$. A spike of fixed height and shrinking width has shrinking area and shrinking energy, so the $L^2$ error goes to zero even though the sup norm of the error doesn't. That's why "the Fourier series equals the signal" is exact as a statement about energy, power and filtering, but not as a statement about the worst-case value at every instant.
:::
:::

::: practice

::: check
given: k = 3 | 5 | 7 | 9 | 11 | 15 | 21
answer: 4/(pi*k)
tolerance: 0.1%

## Square-wave harmonic {k} amplitude?

::: misses
- [any] $\frac{4}{\pi k}$.
:::
:::

::: check
given: J = 0.4 | 1 | 2 | 3 | 5 | 10
answer: 0.0895*J
tolerance: 1%

## Gibbs overshoot for a jump of {J}?

::: misses
- [any] $0.0895J$.
:::
:::

::: check
given: a = 0 | 5; b = 2 | -1
answer: (a + b)/2
tolerance: 0.1%

## Series value at a jump from {a} to {b}?

::: misses
- [any] The midpoint.
:::
:::
:::
