---
title: Power in the harmonics
heading: Parseval's theorem, distortion and the Gibbs overshoot
eyebrow: A10 · Lesson 4 · Power and harmonics
xp: 50
---

::: hook
An audio amplifier's datasheet quotes its total harmonic distortion: how much power ends up in harmonics that weren't in the input. A power-quality meter on a factory floor measures the same thing for the mains current. Both rest on one fact: a signal's power is the sum of the powers of its harmonics, as if each one were heating the load on its own. That's **Parseval's theorem**, and with it you can find the RMS of a square wave, the distortion of a clipped sine, and how many harmonics a filter must pass to keep a signal's shape.
:::

::: play
- The curve adds up the square wave's harmonics through number $N$. The flat line is the square wave itself.
- The readout gives the fraction of the square wave's power captured so far.
- Even with many harmonics the sum overshoots near each jump by about 9%. Does adding harmonics shrink the overshoot, or just squeeze it closer to the jump?
:::
:::

```sim plot
view: graph
x: x = 0..6.2832
y: y = -1.4..1.4
slider: N = 1..9 step 2 = 3
plot: 4/pi*(sin(x) + step(N - 2)*sin(3*x)/3 + step(N - 4)*sin(5*x)/5 + step(N - 6)*sin(7*x)/7 + step(N - 8)*sin(9*x)/9) | label: harmonics 1 to N | samples: 2000
plot: 1 - 2*step(x - pi) | label: square wave | color: ch3
readout: fraction of power captured = 8/pi^2*(1 + step(N - 2)/9 + step(N - 4)/25 + step(N - 6)/49 + step(N - 8)/81) | digits: 4
```

::: predict
## The fundamental alone carries what fraction of a square wave's power?

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

::: reveal
81%. The square wave has power 1 (it's always $\pm 1$). Its fundamental has amplitude $\frac{4}{\pi}$ and power $\frac{1}{2}\left(\frac{4}{\pi}\right)^2 = \frac{8}{\pi^2} = 0.811$. The other 19% is spread over the odd harmonics, falling as $\frac{1}{n^2}$.
:::
:::

::: nameit
## Parseval's theorem

For a periodic signal with complex coefficients $c_n$,

$$\frac{1}{T}\int_0^T|x(t)|^2\,dt = \sum_{n=-\infty}^\infty|c_n|^2 = c_0^2 + \sum_{n=1}^\infty\frac{A_n^2}{2}$$

where $A_n = 2|c_n|$ is the amplitude of harmonic $n$. Mean-square power is the sum of the harmonics' powers, because the cross terms between different harmonics average to zero. The RMS value is its square root.

## Total harmonic distortion

$$\text{THD} = \frac{\sqrt{A_2^2 + A_3^2 + \cdots}}{A_1}$$

the RMS of everything except the fundamental, relative to the fundamental.

| Waveform | THD |
|---|---|
| sine | 0 |
| triangle | $\sqrt{\pi^4/96 - 1} = 12.1\%$ |
| square | $\sqrt{\pi^2/8 - 1} = 48.3\%$ |

## The Gibbs phenomenon

Wherever a signal jumps, a truncated Fourier series overshoots by about 9% of the jump, however many harmonics you keep. The overshoot narrows toward the jump but doesn't shrink. It's why filtering a square wave sharply makes ringing on its edges, and why S7 shapes filters to roll off gently.
:::

::: example
## Use Parseval's theorem to find $\sum_{\text{odd }n}\frac{1}{n^2}$.

::: solution
1. **The power in time.** The $\pm 1$ square wave has power 1.
2. **The power in its harmonics.** They have amplitudes $\frac{4}{\pi n}$ for odd $n$, so by Parseval's theorem
   $$\begin{aligned} 1 &= \sum_{\text{odd }n}\frac{1}{2}\left(\frac{4}{\pi n}\right)^2 \\ &= \frac{8}{\pi^2}\sum_{\text{odd }n}\frac{1}{n^2} \end{aligned}$$
3. **Solve for the sum.**
   $$\begin{aligned} \sum_{\text{odd }n}\frac{1}{n^2} &= \frac{\pi^2}{8} \\ &= 1.2337 \end{aligned}$$
   A fact about numbers, found by measuring a signal's power two ways.

**Answer:** the sum is $\frac{\pi^2}{8} = 1.2337$.
:::
:::

::: example
## What is the THD of a square wave?

::: solution
1. **Split the power.** The fundamental's power is $\frac{8}{\pi^2}$ of the total 1, leaving $1 - \frac{8}{\pi^2}$ in the harmonics.
2. **Take the ratio.**
   $$\begin{aligned} \text{THD} &= \sqrt{\frac{1 - 8/\pi^2}{8/\pi^2}} \\ &= \sqrt{\frac{\pi^2}{8} - 1} \\ &= 0.483 \end{aligned}$$

**Answer:** the THD of a square wave is 0.483, or 48.3%.
:::
:::

::: example
## A 5 V pulse train with 20% duty. What is its RMS value, and how much of its power is DC?

::: solution
1. **The mean square.**
   $$25\ \text{V}^2 \times 0.2 = 5\ \text{V}^2$$
2. **The RMS value.**
   $$\sqrt{5\ \text{V}^2} = 2.24\ \text{V}$$
3. **The DC part.** The DC level is 1 V, contributing $1^2 = 1$ V² of the 5.
4. **The harmonics.** The other 4 V², 80%, is spread over the harmonics. By Parseval's theorem the sum of their squared amplitudes over two must equal exactly 4.

**Answer:** the RMS value is 2.24 V, and DC carries 1 V² of the 5 V²; the other 80% is in the harmonics.
:::
:::

::: check
given: N = 3 | 5 | 7
answer: 100*8/pi^2*(1 + step(N - 2)/9 + step(N - 4)/25 + step(N - 6)/49)
tolerance: 0.5%

## What percent of a square wave's power is in its harmonics 1 through {N}?

::: right
Right. $\frac{8}{\pi^2}\sum_{\text{odd }n \le N}\frac{1}{n^2}$ = {answer}%.
:::

::: misses
- [100*8/pi^2] That's the fundamental alone.
- [any] Not yet. Add $\frac{1}{2}(\frac{4}{\pi n})^2$ for each odd $n \le N$.
:::
:::

::: check
given: a0 = 1 | 2; a1 = 3 | 4; a2 = 1 | 2
answer: sqrt(a0^2 + a1^2/2 + a2^2/2)
tolerance: 0.5%

## A signal is $\given{a0} + \given{a1}\cos(\omega t) + \given{a2}\cos(2\omega t + 30°)$. What is its RMS value?

::: right
Right. $\sqrt{c_0^2 + \frac{A_1^2}{2} + \frac{A_2^2}{2}}$ = {answer}.
:::

::: misses
- [sqrt(a0^2 + a1^2 + a2^2)] Each sinusoid contributes half its amplitude squared.
- [a0 + (a1 + a2)/sqrt(2)] Powers add, not RMS values.
- [any] Not yet. Parseval.
:::
:::

::: check
given: A1 = 10 | 5; A2 = 1 | 0.5; A3 = 2 | 0.3
answer: 100*sqrt(A2^2 + A3^2)/A1
tolerance: 0.5%

## An amplifier's output has a fundamental of {A1} V and harmonics of {A2} V and {A3} V. What is its THD, in percent?

::: right
Right. $\frac{\sqrt{A_2^2 + A_3^2}}{A_1}$ = {answer}%.
:::

::: misses
- [100*(A2 + A3)/A1] Harmonics add as a root-sum-square.
- [any] Not yet. RMS of the harmonics over the fundamental.
:::
:::

::: check
given: A = 1..12 int
answer: 8/pi^2*100
tolerance: 0.5%

## What percent of the power of a square wave switching between $\pm\given{A}$ V is in its fundamental?

::: right
Right. $\frac{8}{\pi^2}$ = {answer}%, whatever the amplitude.
:::

::: misses
- [50] The fundamental carries most of it.
- [any] Not yet. $\frac{1}{2}(\frac{4}{\pi})^2$ over 1.
:::
:::

::: check
given: J = 1..20 int
answer: 0.09*J
tolerance: 2%

## A truncated Fourier series of a signal that jumps by {J} V overshoots near the jump. By about how many volts?

::: right
Right. About 9% of the jump: {answer} V, however many harmonics are kept.
:::

::: misses
- [0] Adding harmonics narrows the overshoot but doesn't remove it.
- [any] Not yet. The Gibbs overshoot is about 9% of the jump.
:::
:::

::: check
kind: python

## Write `thd(x)` for an array `x` holding exactly one period of a signal: take `np.fft.rfft(x)`, use bin 1 as the fundamental and bins 2 and up as the harmonics, and return the THD as a fraction.

```python starter
import numpy as np

def thd(x):
    return 0.0

N = 4096
t = np.arange(N) / N
print(thd(np.where(t < 0.5, 1.0, -1.0)))
```

```python solution
import numpy as np

def thd(x):
    X = np.abs(np.fft.rfft(x))
    return np.sqrt(np.sum(X[2:] ** 2)) / X[1]

N = 4096
t = np.arange(N) / N
print(thd(np.where(t < 0.5, 1.0, -1.0)))
```

```python tests
import numpy as np
N = 4096
t = np.arange(N) / N
assert abs(thd(np.sin(2 * np.pi * t))) < 1e-9, "A pure sine has no distortion."
sq = thd(np.where(t < 0.5, 1.0, -1.0))
assert abs(sq - 0.4834) < 0.002, f"A square wave's THD is about 0.483; got {sq:.4f}."
tri = thd(1 - 4 * np.abs(t - 0.5))
assert abs(tri - 0.1212) < 0.002, f"A triangle wave's THD is about 0.121; got {tri:.4f}."
```

::: right
Right. Try it on `np.clip(np.sin(2*np.pi*t), -0.8, 0.8)`: clipping 20% off the peaks gives about 9% THD.
:::
:::

::: check
kind: parts
given: A = 3 | 5; d = 0.1 | 0.25

## A pulse train of {A} V with duty {d}.

::: part
answer: A*sqrt(d)
unit: V
tolerance: 0.5%

### What is its RMS value?

::: misses
- [A*d] That's the average; the RMS is $A\sqrt d$.
- [any] The mean square is $A^2d$.
:::
:::

::: part
answer: A^2*d*(1 - d)
unit: V²
tolerance: 0.5%

### How much of its mean square is in the harmonics (everything but DC)?

::: misses
- [any] $A^2d - (Ad)^2$.
:::
:::
:::

::: check
kind: derivation
vars: a = 0.1..5, b = 0.1..5

## A signal is $a\cos t + b\sin 3t$.

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

### What is its mean square?
:::

::: step
answer: b/a

### What is its THD, the RMS of the harmonic over the RMS of the fundamental?
:::

::: right
Right. Parseval: orthogonal components add in power, so the mean square is $\frac{a^2 + b^2}{2}$.
:::
:::

::: explain
## Why does the power of a signal equal the sum of the powers of its harmonics?

::: model
Square the Fourier series and average over a period. The square contains every product of two harmonics. Products of the same harmonic average to half its amplitude squared, its power. Products of two different harmonics average to zero, because sinusoids at different whole-number multiples of the fundamental are orthogonal over a period. So only the individual powers survive, and the total is their sum, as if each harmonic heated the load separately.
:::
:::

::: practice

::: check
given: A = 1..20 int
answer: A/sqrt(2)
tolerance: 0.5%

## What is the RMS value of a sine of amplitude {A}, from Parseval?

::: misses
- [any] One harmonic: $\sqrt{A^2/2}$.
:::
:::

::: check
given: d = 20 | 30 | 40 | 48 | 60 | 80
answer: 100*sqrt(1 - 1/(1 + (d/100)^2))
tolerance: 1%

## A signal has THD of {d}%. What percent of its total RMS is in the harmonics?

::: misses
- [d] THD is relative to the fundamental, not the total.
- [any] $\frac{\text{THD}}{\sqrt{1 + \text{THD}^2}}$.
:::
:::

::: check
given: A = 1..12 int
answer: A
tolerance: 0.5%

## What is the RMS value of a square wave switching between $+\given{A}$ and $-\given{A}$?

::: misses
- [A/sqrt(2)] That's a sine. A square wave sits at full value all the time.
- [any] Its square is always $A^2$.
:::
:::
:::
