---
title: Building signals from sinusoids
heading: A square wave is a stack of sine waves
eyebrow: A10 · Lesson 1 · Building signals from sinusoids
xp: 40
---

::: hook
A square wave looks like the opposite of a sine wave: flat tops, instant jumps. Yet you can build one entirely out of sine waves, if you add the right ones in the right amounts. This is Fourier's idea, and it's why engineers can analyze any repeating signal one frequency at a time: a filter, an amplifier or a cable treats each sine wave separately, and the output is the sum of what it does to each.

::: play
- The top panel shows the sum of the first $N$ odd harmonics of a 200 Hz sine wave, each with amplitude $1/n$. The bottom panel shows how much of each frequency is in the sum.
- Start at $N = 1$ and add harmonics one at a time. What happens to the corners and the flat tops? Press Listen to hear the sound change.
- With all nine harmonics, look just next to each jump. Does the overshoot shrink as you add more?
:::
:::

```sim plot
view: spectrum
x: t = 0..10m s
y: v = -1.5..1.5 V
samples: 1024
freq: f = 0..4k Hz
slider: N = 1..9 step 1 = 3
slider: f0 = 100..300 step 100 = 200 Hz
plot: 4/pi*(step(N - 1)*sin(2*pi*1*f0*t)/1 + step(N - 2)*sin(2*pi*3*f0*t)/3 + step(N - 3)*sin(2*pi*5*f0*t)/5 + step(N - 4)*sin(2*pi*7*f0*t)/7 + step(N - 5)*sin(2*pi*9*f0*t)/9 + step(N - 6)*sin(2*pi*11*f0*t)/11 + step(N - 7)*sin(2*pi*13*f0*t)/13 + step(N - 8)*sin(2*pi*15*f0*t)/15 + step(N - 9)*sin(2*pi*17*f0*t)/17) | label: sum
sound: 4/pi*(step(N - 1)*sin(2*pi*1*f0*t)/1 + step(N - 2)*sin(2*pi*3*f0*t)/3 + step(N - 3)*sin(2*pi*5*f0*t)/5 + step(N - 4)*sin(2*pi*7*f0*t)/7 + step(N - 5)*sin(2*pi*9*f0*t)/9 + step(N - 6)*sin(2*pi*11*f0*t)/11 + step(N - 7)*sin(2*pi*13*f0*t)/13 + step(N - 8)*sin(2*pi*15*f0*t)/15 + step(N - 9)*sin(2*pi*17*f0*t)/17) | scale: 1.5
readout: highest harmonic = (2*N - 1)*f0 Hz
```

::: predict
## A 1 kHz square wave goes through a filter that removes everything above 2.5 kHz. What comes out?

- [ ] The same square wave
- [x] A plain 1 kHz sine wave
- [ ] A 3 kHz sine wave
- [ ] Nothing

::: reveal
A plain 1 kHz sine wave. A square wave's harmonics are at 1, 3, 5, 7 kHz and so on. The filter keeps only the 1 kHz one, with amplitude $4/\pi$ times the square wave's, and throws away the rest. Square-wave oscillators followed by a filter are a cheap way to make a sine wave, and it's also why a square wave sent down a slow cable arrives with rounded corners.
:::
:::

::: nameit
## Fourier series

**The claim.** Any periodic signal with period $T$ (frequency $f_0 = 1/T$) can be written as a sum of sinusoids at whole-number multiples of $f_0$:

$$x(t) = a_0 + \sum_{n=1}^{\infty} A_n\cos(2\pi nf_0t + \varphi_n)$$

$a_0$ is the average (DC) value, and the term with frequency $nf_0$ is the **nth harmonic**. The first harmonic, $f_0$ itself, is the **fundamental**.

**The square wave.** A square wave swinging between $+1$ and $-1$ is

$$x(t) = \frac{4}{\pi}\left(\sin\omega_0t + \frac{\sin3\omega_0t}{3} + \frac{\sin5\omega_0t}{5} + \cdots\right)$$

Only odd harmonics appear, and their amplitudes fall as $1/n$. The sharp corners need the high harmonics; with only a few, the corners are rounded.

**Other shapes:**

| Signal | Harmonics | Amplitudes fall as |
|---|---|---|
| sine | the fundamental only | |
| square | odd only | $1/n$ |
| triangle | odd only | $1/n^2$ |
| sawtooth | all | $1/n$ |

The smoother the signal, the faster its harmonics shrink. A jump needs harmonics falling only as $1/n$; a kink (like a triangle's point) needs only $1/n^2$.

**Gibbs's overshoot.** Right next to a jump, the partial sum overshoots by about 9% of the jump, however many harmonics you add. The overshoot gets narrower but no shorter. Real filters that cut off harmonics sharply show the same ringing.

**Why it matters.** A linear circuit responds to each harmonic separately (superposition, A8.14), multiplying it by its frequency response at that frequency. So to find what a filter does to a square wave, you work out what it does to each harmonic and add them up. The rest of this course builds on that idea.
:::

::: example
## A ±5 V square wave runs at 1 kHz. What are its first three nonzero harmonics?

::: solution
1. **Which harmonics.** A square wave has only odd harmonics, with amplitudes
   $$\frac{4A}{\pi n}$$
2. **The fundamental, at 1 kHz.** With $A = 5$ V:
   $$\frac{4 \times 5\ \text{V}}{\pi} = 6.37\ \text{V}$$
3. **The 3rd harmonic, at 3 kHz.**
   $$\begin{aligned} V_3 &= \frac{6.37\ \text{V}}{3} \\ &= 2.12\ \text{V} \end{aligned}$$
4. **The 5th harmonic, at 5 kHz.**
   $$\begin{aligned} V_5 &= \frac{6.37\ \text{V}}{5} \\ &= 1.27\ \text{V} \end{aligned}$$

**Answer:** 6.37 V at 1 kHz, 2.12 V at 3 kHz and 1.27 V at 5 kHz.
:::
:::

::: example
## That square wave drives an RC low-pass with its corner at 1 kHz. How big are the fundamental and the 3rd harmonic at the output?

::: solution
1. **The filter's gain.** The filter multiplies each harmonic by
   $$\frac{1}{\sqrt{1 + (f/f_c)^2}}$$
2. **The fundamental.** At $f = f_c$ the gain is $1/\sqrt2$:
   $$\begin{aligned} V_1 &= \frac{6.37\ \text{V}}{\sqrt2} \\ &= 4.50\ \text{V} \end{aligned}$$
3. **The 3rd harmonic.** At $f = 3f_c$ the gain is $1/\sqrt{10}$:
   $$\begin{aligned} V_3 &= \frac{2.12\ \text{V}}{\sqrt{10}} \\ &= 0.671\ \text{V} \end{aligned}$$
4. **Compare.** At the input the 3rd harmonic was a third of the fundamental. At the output it's
   $$\frac{0.671}{4.50} = 0.149$$
   of it, which is why the output looks rounder.

**Answer:** the fundamental comes out at 4.50 V and the 3rd harmonic at 0.671 V, so the 3rd harmonic drops from a third of the fundamental to 0.149 of it.
:::
:::

::: example
## What are the first two harmonics of a ±1 V triangle wave?

::: solution
1. **Which harmonics.** A triangle wave has only odd harmonics, with amplitudes
   $$\frac{8}{\pi^2n^2}$$
2. **The fundamental.**
   $$\begin{aligned} V_1 &= \frac{8}{\pi^2} \\ &= 0.811\ \text{V} \end{aligned}$$
3. **The 3rd harmonic.** It's nine times smaller:
   $$\begin{aligned} V_3 &= \frac{8}{9\pi^2} \\ &= 0.090\ \text{V} \end{aligned}$$
4. **Why it falls faster.** Its harmonics fall as $\frac{1}{n^2}$, against $\frac1n$ for a square wave, because a triangle has no jumps, only corners. The smoother a waveform, the faster its spectrum falls off.

**Answer:** the fundamental is 0.811 V and the 3rd harmonic is 0.090 V.
:::
:::

::: check
given: f0 = 50 | 60 | 100 | 400 | 1000; n = 3 | 5 | 7
answer: n*f0
unit: Hz
tolerance: 0.5%

## What is the frequency of the {n}th harmonic of a {f0} Hz signal?

::: right
Right. Harmonics are whole-number multiples of the fundamental: {n} × {f0} Hz = {answer}.
:::

::: misses
- [f0/n] Harmonics are higher than the fundamental, not lower.
- [f0 + n] Multiply by n.
- [any] Not yet. The nth harmonic is at $nf_0$.
:::
:::

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

## A square wave swings between +{A} V and −{A} V. What is the amplitude of its {n}th harmonic?

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

::: misses
- [A/n] Include the factor $4/\pi$: even the fundamental is bigger than the square wave itself.
- [4*A/(n^2*pi)] That's how a triangle wave's harmonics fall. A square wave's fall as $1/n$.
- [4*A/pi] Divide by n.
- [any] Not yet. The square wave's harmonics have amplitude $4A/(n\pi)$.
:::
:::

::: check
kind: choice

## A square wave with equal time high and low contains which harmonics?

- [ ] All of them
- [x] Only the odd ones
- [ ] Only the even ones
- [ ] Only the fundamental

::: right
Right. The square wave's symmetry (each half is the other flipped) cancels every even harmonic.
:::

::: misses
- [1] The even harmonics are all zero for a symmetric square wave.
- [3] It's the odd ones that survive, including the fundamental.
- [4] The sharp corners need higher harmonics.
:::
:::

::: check
given: k = 2 | 3 | 4; m = 3 | 5 | 7; f = 50 | 100
answer: f
unit: Hz
tolerance: 0.5%

## A signal is the sum of a {k}×{f} Hz sine wave and a {m}×{f} Hz sine wave. What is the fundamental frequency of the sum?

::: right
Right. Both are whole-number multiples of {f} Hz (and of nothing bigger), so the sum repeats every $1/\given{f}$ seconds.
:::

::: misses
- [k*f] The sum only repeats when both parts do: find the largest frequency both are multiples of.
- [(k + m)*f] Adding sinusoids doesn't add their frequencies.
- [any] Not yet. Find the biggest frequency that both are whole-number multiples of.
:::
:::

::: check
given: f0 = 1..5 int; fc = 2 | 4 | 6
answer: (4/(3*pi))/sqrt(1 + (3*f0/fc)^2)
unit: V
tolerance: 1%

## A ±1 V square wave at {f0} kHz goes through an RC low-pass filter with corner {fc} kHz. What is the amplitude of the 3rd harmonic at the output?

::: right
Right. The 3rd harmonic has amplitude $4/(3\pi)$ going in, and the filter multiplies it by $1/\sqrt{1 + (3f_0/f_c)^2}$: {answer}.
:::

::: misses
- [4/(3*pi)] The filter shrinks it too, by $1/\sqrt{1 + (f/f_c)^2}$ at $f = 3f_0$.
- [(4/(3*pi))/sqrt(1 + (f0/fc)^2)] Use the 3rd harmonic's own frequency, $3f_0$.
- [(4/pi)/sqrt(1 + (3*f0/fc)^2)] The 3rd harmonic starts with amplitude $4/(3\pi)$.
- [any] Not yet. Find the harmonic's amplitude, then the filter's gain at its frequency.
:::
:::

::: check
kind: parts
given: A = 1 | 5; n = 3 | 5

## A ±{A} V triangle wave.

::: part
answer: 8*A/pi^2
unit: V
tolerance: 0.5%

### What is the amplitude of its fundamental?

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

::: part
answer: 8*A/(pi^2*n^2)
unit: V
tolerance: 0.5%

### What is the amplitude of its harmonic number {n}?

::: misses
- [8*A/(pi^2*n)] A triangle's harmonics fall as $\frac{1}{n^2}$.
- [any] $\frac{8A}{\pi^2n^2}$.
:::
:::
:::

::: check
kind: derivation
vars: A = 1..10, n = 1..9

## A ±$A$ square wave has odd harmonics $\frac{4A}{\pi n}$.

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

### What is the amplitude of harmonic $n$?
:::

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

### What power (into 1 Ω) does that harmonic carry?
:::

::: right
Right. Each harmonic carries half its amplitude squared, and Parseval says they add up to $A^2$.
:::
:::

::: explain
## Why does a square wave need high harmonics, when a sine wave needs only one frequency?

::: model
Each sinusoid is smooth, so adding a few low-frequency ones can only make a smooth, rounded shape. A square wave jumps almost instantly from one level to the other, and changing that fast needs components that themselves change fast, which means high frequencies. The more harmonics included, the steeper the edges and the flatter the tops. A pure sine wave already is one sinusoid, so it needs nothing else.
:::
:::

::: practice

::: check
given: f0 = 100..900 step 100; n = 2..9 int
answer: n*f0
unit: Hz
tolerance: 0.5%

## A signal's fundamental is {f0} Hz. Where is its harmonic number {n}?

::: misses
- [f0/n] Harmonics are multiples of the fundamental.
- [any] $nf_0$.
:::
:::

::: check
given: f0 = 100 | 200 | 250; n = 3 | 5 | 7
answer: n
tolerance: 0.5%

## A square wave at {f0} Hz has a component at {n}×{f0} Hz. Which harmonic is it? (Give the number.)

::: misses
- [any] Divide its frequency by the fundamental.
:::
:::

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

## A triangle wave's harmonics have amplitudes $\frac{8A}{\pi^2n^2}$. For a {A} V triangle wave, how big is the {n}th harmonic?

::: misses
- [8*A/(pi^2*n)] Divide by $n^2$.
- [any] $8A/(\pi^2n^2)$.
:::
:::
:::
