---
title: Periodic inputs and Fourier series
heading: A square wave is a sum of sines, and a linear circuit answers each one separately
eyebrow: A8 · Lesson 31 · Periodic inputs and Fourier series
xp: 40
---

::: hook
Lesson 30's phasors answer a single sine. Real drives are rarely single sines: a microcontroller's PWM output is a square wave, a rectifier delivers half-sine humps, a switching converter's node is a trapezoid. Each of these repeats, and every repeating waveform is a sum of sines at multiples of its frequency, its **Fourier series**. Because the circuit's equation is linear, the response to the sum is the sum of the responses, each harmonic multiplied by the circuit's gain at its own frequency. That's how a low-pass smooths a PWM signal, and how a tuned circuit can pull a pure sine out of a square wave.
:::

::: play
- A ±1 V, 1 kHz square wave (time in milliseconds) drives an RC low-pass with its corner at $f_c$ kHz. The curve is the steady-state output built from the odd harmonics up to $N$ (at most 15), each through the filter's gain at its frequency.
- With a few harmonics the output already looks right; the corner frequency decides how much of the square's sharp edges survive.
:::

```sim plot
view: graph
x: t = 0..2
y: v = -1.5..1.5
slider: fc = 0.2..10 step 0.1 = 1
slider: N = 1..41 step 2 = 9
plot: 1*sign(sin(2*pi*t)) | dashed | label: input, ±1 V at 1 kHz
plot: (4/pi)*(sin(2*pi*t - atan(1/fc))/sqrt(1 + 1/fc^2) + step(N - 3)*sin(6*pi*t - atan(3/fc))/(3*sqrt(1 + 9/fc^2)) + step(N - 5)*sin(10*pi*t - atan(5/fc))/(5*sqrt(1 + 25/fc^2)) + step(N - 7)*sin(14*pi*t - atan(7/fc))/(7*sqrt(1 + 49/fc^2)) + step(N - 9)*sin(18*pi*t - atan(9/fc))/(9*sqrt(1 + 81/fc^2)) + step(N - 11)*sin(22*pi*t - atan(11/fc))/(11*sqrt(1 + 121/fc^2)) + step(N - 13)*sin(26*pi*t - atan(13/fc))/(13*sqrt(1 + 169/fc^2)) + step(N - 15)*sin(30*pi*t - atan(15/fc))/(15*sqrt(1 + 225/fc^2))) | samples: 600 | label: output, first harmonics
readout: fundamental out = (4/pi)/sqrt(1 + 1/fc^2) | digits: 3
```

::: predict
## A ±1 V, 1 kHz square wave drives a series RLC tuned to 3 kHz with a high Q, and you look at the voltage across the resistor. What comes out?

- [ ] A smaller square wave at 1 kHz
- [x] Mostly a 3 kHz sine, about a third of the square's height
- [ ] Nothing, since 1 kHz is off resonance
- [ ] A 1 kHz sine

::: reveal
Mostly a 3 kHz sine. A square wave contains every odd harmonic: 1, 3, 5 kHz and up, with amplitudes $4/(\pi n)$ volts. The tuned circuit passes the one at its resonance, 3 kHz with amplitude $4/(3\pi)$ = 0.42 V, and strongly rejects the others. That's how frequency multipliers and harmonic-tuned oscillators pick a harmonic out of a distorted wave.
:::
:::

::: nameit
## Fourier series

A waveform that repeats every $T$ seconds, with $\omega_0 = 2\pi/T$, can be written

$$u(t) = a_0 + \sum_{n=1}^{\infty} c_n\cos(n\omega_0t + \varphi_n)$$

Three to know:

| Waveform | Harmonics |
|---|---|
| Square wave, $\pm A$ | odd $n$ only, $\dfrac{4A}{\pi n}\sin(n\omega_0t)$ |
| Triangle wave, $\pm A$ | odd $n$ only, amplitude $\dfrac{8A}{\pi^2n^2}$ |
| Sawtooth, $\pm A$ | every $n$, amplitude $\dfrac{2A}{\pi n}$ |

A jump in the waveform makes the harmonics fall as $1/n$; a kink with no jump makes them fall as $1/n^2$. Smoother waveforms have weaker high harmonics.

## The response, harmonic by harmonic

For a linear circuit with frequency response $H(j\omega)$, each harmonic comes out multiplied by $|H(jn\omega_0)|$ and shifted by $\angle H(jn\omega_0)$:

$$y(t) = H(0)a_0 + \sum_n |H(jn\omega_0)|\,c_n\cos\!\left(n\omega_0t + \varphi_n + \angle H(jn\omega_0)\right)$$

That's the steady state, after the starting transient (lesson 14) has died away.

## Power and RMS

Harmonics at different frequencies don't interfere on average, so their powers add (Parseval):

$$V_\text{rms}^2 = a_0^2 + \sum_n \frac{c_n^2}{2}$$

The **total harmonic distortion** compares everything but the fundamental with the fundamental: $\text{THD} = \sqrt{\sum_{n \ge 2} c_n^2}\,/\,c_1$.
:::

::: example
## A ±1 V, 1 kHz square wave drives an RC low-pass whose corner is also 1 kHz. Find the output's first three harmonics.

::: solution
1. **Gain at each harmonic.** The filter's gain at harmonic $n$ is $1/\sqrt{1 + n^2}$, since $nf_0/f_c = n$.
2. **Scale each input harmonic.** The square wave's harmonic $n$ has amplitude $4/(\pi n)$:

| $n$ | In: $4/(\pi n)$ | Gain | Out |
|---|---|---|---|
| 1 | 1.273 V | 0.707 | 0.900 V |
| 3 | 0.424 V | 0.316 | 0.134 V |
| 5 | 0.255 V | 0.196 | 0.050 V |

3. **How the harmonics fall.** The output harmonics now fall as $1/n^2$, like a triangle's: the filter has rounded the square's corners into exponential ramps.
4. **The full waveform.** Adding up enough harmonics reproduces the exact steady state, a pair of exponential segments with peak-to-peak $2\tanh(T/4\tau)$ = 1.834 V.

**Answer:** the first three output harmonics are 0.900 V at 1 kHz, 0.134 V at 3 kHz and 0.050 V at 5 kHz.
:::
:::

::: example
## The same square wave drives a series RLC bandpass at 3 kHz with $Q$ = 10 (output across the resistor). Find the output's harmonics and its THD.

::: solution
1. **The bandpass gain.** A series RLC's gain at frequency $f$ is
   $$\frac{1}{\sqrt{1 + Q^2(f/f_r - f_r/f)^2}}$$
2. **Apply it to each harmonic.** With $f_r$ = 3 kHz and $Q$ = 10:

| $n$ | Gain | Out |
|---|---|---|
| 1 | 0.0375 | 0.048 V |
| 3 | 1 | 0.424 V |
| 5 | 0.093 | 0.024 V |
| 7 | 0.052 | 0.010 V |

3. **The result.** The output is a 0.424 V, 3 kHz sine with a THD of 12.9%, almost all of it from the leftover fundamental.
4. **Why the fundamental leaks through.** A higher Q would clean it further; with $Q$ = 10 the fundamental is only rejected by a factor of 27, because 1 kHz is just a factor of 3 below resonance.

**Answer:** the output is mostly a 0.424 V, 3 kHz sine, with 0.048 V at 1 kHz, 0.024 V at 5 kHz and 0.010 V at 7 kHz, for a THD of 12.9%.
:::
:::

::: example
## Check the low-pass result without harmonics: what is the exact steady-state peak-to-peak output of the RC low-pass ($\tau$ = 0.159 ms) driven by the ±1 V, 1 kHz square wave?

::: solution
1. **The steady-state cycle.** In the steady state the capacitor charges toward +1 V for half a period, then toward −1 V for the other half, ending each half where it started the previous one with the sign flipped.
2. **Write the first half.** With swing $\pm v_p$,
   $$v_p = 1 - (1 + v_p)e^{-T/2\tau}$$
3. **Solve for $v_p$.**
   $$\begin{aligned} v_p &= \frac{1 - e^{-T/2\tau}}{1 + e^{-T/2\tau}} \\ &= \tanh\frac{T}{4\tau} \end{aligned}$$
4. **Put in the numbers.** With $T/4\tau = 1.571$,
   $$\tanh 1.571 = 0.917$$
   and the peak-to-peak is 1.834 V.
5. **Compare with the Fourier sum.** Summing the Fourier series with a thousand harmonics gives the same 1.834 V, so the two methods agree. The Fourier view explains *which* frequencies remain; the time-domain view gives the waveform directly.

**Answer:** the output swings 1.834 V peak to peak, between −0.917 V and +0.917 V.
:::
:::

::: check
given: A = 1 | 2.5 | 5; n = 3 | 5 | 7 | 9
answer: 4*A/(pi*n)
unit: V
tolerance: 0.5%

## What is the amplitude of harmonic {n} of a square wave that switches between +{A} and −{A} V?

::: right
Right. A square wave of height $\pm A$ has odd harmonics of amplitude $4A/(\pi n)$: {answer}.
:::

::: misses
- [4*A/pi] That's the fundamental. Harmonic {n} is {n} times weaker.
- [2*A/(pi*n)] That's a sawtooth's coefficient. A square wave's is $4A/(\pi n)$.
- [A/n] Close in shape, but the factor is $4/\pi$, not 1: the fundamental of a square wave is taller than the square itself.
- [any] Not yet. Use $4A/(\pi n)$.
:::
:::

::: check
given: A = 1 | 5; n = 3 | 5; r = 0.5 | 1 | 2 | 5
answer: 4*A/(pi*n)/sqrt(1 + (n*r)^2)
unit: V
tolerance: 0.5%

## A ±{A} V square wave at frequency $f_0$ drives an RC low-pass whose corner is $f_0/\given{r}$. What is the amplitude of harmonic {n} at the output?

::: right
Right. Harmonic {n} is at $\given{n}f_0$, which is $\given{n} \times \given{r}$ times the corner, so it's multiplied by $1/\sqrt{1 + (\given{n}\given{r})^2}$: {answer}.
:::

::: misses
- [4*A/(pi*n)/sqrt(1 + r^2)] That uses the fundamental's gain for every harmonic. Harmonic {n} is {n} times higher in frequency.
- [4*A/(pi*n)/(1 + n*r)] The gain's size is $1/\sqrt{1 + (f/f_c)^2}$, not $1/(1 + f/f_c)$.
- [any] Not yet. Multiply $4A/(\pi n)$ by the filter's gain at $nf_0$.
:::
:::

::: check
kind: parts
given: Q = 5 | 10 | 30; m = 3 | 5

## A ±1 V square wave at $f_0$ drives a series RLC bandpass tuned to harmonic {m} ($f_r = \given{m}f_0$) with $Q$ = {Q}.

::: part
answer: 1/sqrt(1 + Q^2*(1/m - m)^2)
tolerance: 0.5%

### What is the bandpass's gain at the fundamental?

::: misses
- [any] Not yet. Use $1/\sqrt{1 + Q^2(f/f_r - f_r/f)^2}$ with $f/f_r = 1/\given{m}$.
:::
:::

::: part
answer: 4/(pi*m)
unit: V
tolerance: 0.5%

### What is the output's amplitude at harmonic {m}?

::: misses
- [4/pi] That's the input's fundamental. At resonance the gain is 1, so harmonic {m} passes at its input size.
- [any] Not yet. At resonance the gain is 1, so the output is the input harmonic's amplitude, $4/(\pi\given{m})$.
:::
:::

::: right
Right. A higher $Q$ rejects the fundamental more, but it's only ever a few octaves away, so the rejection grows in proportion to $Q$, not faster.
:::
:::

::: check
kind: python
given: fc = 0.5 | 1 | 3; A = 1 | 2

## A ±`A` V, 1 kHz square wave drives an RC low-pass with corner `fc` = {fc} kHz. Build its steady-state output from the odd harmonics $n = 1, 3, \ldots, 1999$, each through the filter's complex gain $1/(1 + jn f_0/f_c)$, at 4000 times across one period. Set `pp_fourier` to the peak-to-peak of the sum and `pp_exact` to $2A\tanh(T/4\tau)$ with $\tau = 1/(2\pi f_c)$.

```python starter
import numpy as np
pp_fourier = 2 * A
pp_exact = 2 * A
print(pp_fourier, pp_exact)
```

```python solution
import numpy as np
f0, T = 1e3, 1e-3
t = np.arange(4000) / 4000 * T
y = np.zeros_like(t)
for n in range(1, 2000, 2):
    H = 1 / (1 + 1j * n * f0 / (fc * 1e3))
    y += np.imag(4 * A / (np.pi * n) * H * np.exp(1j * 2 * np.pi * n * f0 * t))
pp_fourier = y.max() - y.min()
tau = 1 / (2 * np.pi * fc * 1e3)
pp_exact = 2 * A * np.tanh(T / (4 * tau))
print(pp_fourier, pp_exact)
```

```python tests
import numpy as np
tau = 1 / (2 * np.pi * fc * 1e3)
ex = 2 * A * np.tanh(1e-3 / (4 * tau))
assert abs(pp_exact - ex) < 1e-12, "pp_exact is 2A·tanh(T/(4τ)) with τ = 1/(2π fc)."
assert abs(pp_fourier - ex) < 2e-3 * A, f"The Fourier sum should give a peak-to-peak close to {ex:.4f} V. Each harmonic is (4A/πn)·H·e^(jnω0t), keeping the imaginary part for a sine."
```

::: right
Right. A thousand harmonics land within a thousandth of the exact answer; the small difference is the sum's truncation and the sampling of the peak. A faster corner keeps more harmonics and more of the square's shape.
:::
:::

::: check
kind: python
given: Q = 5 | 10 | 20; m = 3 | 5

## A ±1 V square wave drives a series RLC bandpass tuned to harmonic `m` = {m} with `Q` = {Q}. Set `amps` to a list of the output amplitudes of harmonics 1, 3, 5, …, 99, and `thd` to the output's total harmonic distortion relative to harmonic `m`.

```python starter
import math
amps = [4 / (math.pi * n) for n in range(1, 100, 2)]
thd = 0
print(thd)
```

```python solution
import math
amps = []
for n in range(1, 100, 2):
    g = 1 / math.sqrt(1 + Q**2 * (n / m - m / n)**2)
    amps.append(4 / (math.pi * n) * g)
k = (m - 1) // 2
thd = math.sqrt(sum(a * a for i, a in enumerate(amps) if i != k)) / amps[k]
print(thd)
```

```python tests
import math
w = [4 / (math.pi * n) / math.sqrt(1 + Q**2 * (n / m - m / n)**2) for n in range(1, 100, 2)]
k = (m - 1) // 2
t = math.sqrt(sum(a * a for i, a in enumerate(w) if i != k)) / w[k]
assert len(amps) == 50 and all(abs(a - b) < 1e-12 for a, b in zip(amps, w)), "Each harmonic's output is 4/(πn) times 1/√(1 + Q²(n/m − m/n)²)."
assert abs(thd - t) < 1e-12, f"The THD is {t:.4f}: every harmonic but m, root-sum-squared, over harmonic m."
```

::: right
Right. The distortion is dominated by the nearest harmonics on either side of the one you're selecting, and it falls roughly as $1/Q$. Selecting the 5th is harder than the 3rd, since it starts five times weaker than the fundamental.
:::
:::

::: check
kind: choice

## Which waveform's harmonics fall off fastest with $n$?

- [x] A triangle wave
- [ ] A square wave
- [ ] A sawtooth
- [ ] A train of narrow pulses

::: right
Right. A triangle has no jumps, only corners, so its harmonics fall as $1/n^2$. The square and sawtooth jump, so theirs fall as $1/n$, and narrow pulses have nearly equal harmonics far up the spectrum.
:::

::: misses
- [2] A square wave's jumps make its harmonics fall only as $1/n$.
- [3] A sawtooth jumps once a period, so its harmonics fall as $1/n$ too, and it has even ones as well.
- [4] A narrow pulse is nearly an impulse, whose harmonics hardly fall at all until $n$ approaches the inverse of the duty cycle.
:::
:::

::: check
kind: derivation
vars: A = 0.5..3, n = 1..9, w = 1..5

## Find the coefficient $b_n$ of $\sin(n\omega_0t)$ for a square wave that is $+A$ for the first half period and $-A$ for the second, with $\omega_0 = w$.

::: step
answer: (2*A/(n*pi))*(1 - cos(n*pi))

### $b_n = \frac{2}{T}\int_0^T u(t)\sin(n\omega_0t)\,dt$. By the waveform's symmetry this is twice the integral over the first half. What is it?
:::

::: step
answer: 4*A/(n*pi)

### What does it equal for odd $n$, where $\cos n\pi = -1$?
:::

::: right
Right. For even $n$, $\cos n\pi = 1$ and $b_n = 0$: a square wave with equal halves has only odd harmonics.
:::
:::

::: explain
## Why can a circuit's response to a square wave be found one harmonic at a time?

::: model
The circuit's differential equation is linear: if $u_1$ gives $y_1$ and $u_2$ gives $y_2$, then $u_1 + u_2$ gives $y_1 + y_2$. A square wave is a sum of sines, its Fourier series, so the response is the sum of the responses to each sine. Each of those is a steady-state sine at the same frequency, found with phasors by multiplying by $H(jn\omega_0)$. The method fails only when the circuit isn't linear, for instance a diode or a saturating amplifier, which creates new frequencies instead of just reweighting the old ones.
:::
:::

::: practice

::: check
given: A = 1 | 3; n = 1 | 3 | 5
answer: 8*A/(pi^2*n^2)
unit: V
tolerance: 0.5%

## What is the amplitude of harmonic {n} of a ±{A} V triangle wave?

::: misses
- [4*A/(pi*n)] That's a square wave's. A triangle's fall as $1/n^2$: $8A/(\pi^2n^2)$.
- [any] Use $8A/(\pi^2n^2)$.
:::
:::

::: check
given: c1 = 1 | 2; c3 = 0.1 | 0.3; c5 = 0.05 | 0.1
answer: sqrt(c3^2 + c5^2)/c1
tolerance: 0.5%

## A waveform's harmonics have amplitudes {c1}, {c3} and {c5} V (and nothing else). What is its THD?

::: misses
- [(c3 + c5)/c1] Add the harmonics' powers, not their amplitudes: root-sum-square.
- [any] $\text{THD} = \sqrt{c_3^2 + c_5^2}/c_1$.
:::
:::
:::
