---
title: PWM and filtering
heading: Pulse-width modulation as a way to set an average with on-off switching, the spectrum of a PWM wave (a DC term plus harmonics of the switching frequency), and the RC and LC filters that remove the harmonics and leave the average
eyebrow: D2 · Lesson 5 · PWM and filtering
xp: 40
---

::: hook
Every converter in this course works the same trick: switch fast, then average. Pulse-width modulation makes the trick explicit. A digital pin that is high 40% of the time has an average of 40% of its supply, and a microcontroller can dim an LED, set a motor's speed or make an analog voltage that way, with nothing but a timer. The price is everything other than the average: a train of harmonics at the switching frequency and its multiples, which a filter, or the inertia of a motor or the persistence of an eye, must remove.
:::

::: play
- A 12 V PWM wave at 500 Hz with duty cycle D, and its spectrum.
- The DC term is D × 12 V; the rest is harmonics at multiples of 500 Hz, with amplitudes $(2V/n\pi)|\sin(n\pi D)|$.
- At 50% duty the even harmonics vanish; at small duty the harmonics fall off slowly, because narrow pulses are rich in high frequencies.
:::

```sim plot
view: spectrum
x: t = 0..10m s
y: v = -1..13 V
samples: 1024
freq: f = 0..5k Hz
slider: D = 0.05..0.95 = 0.25 step 0.05
plot: 12*step(D - (500*t - floor(500*t))) | label: PWM wave
marker: f = 500 | label: switching frequency
```

::: predict
## A 3.3 V microcontroller pin runs PWM at 40% duty into an RC filter whose time constant is much longer than the PWM period. What DC voltage comes out?

- [ ] 3.3 V
- [ ] 2 V
- [x] 1.32 V
- [ ] 0.4 V

::: reveal
The filter keeps the average and removes the rest: 0.4 × 3.3 = 1.32 V. With R = 10 kΩ, C = 1 µF and a 1 kHz PWM, the leftover ripple is about $VD(1 - D)/(RCf_s)$ = 3.3 × 0.24/10 = 79 mV peak to peak. A longer time constant cuts the ripple but slows the output's response to a change in duty.
:::
:::

::: nameit
## PWM

A pulse train of height V, frequency $f_s$ and duty cycle D has

- a **DC term** $DV$;
- harmonics at $nf_s$ with amplitudes $\dfrac{2V}{n\pi}|\sin(n\pi D)|$.

Changing D changes the average linearly, which is why PWM is used for dimming, motor speed, heater power and cheap digital-to-analog conversion.

## Filtering

A filter with its corner well below $f_s$ passes the DC term (and slow changes of D) and attenuates the harmonics.

- **RC** (one pole): attenuation grows at 20 dB/decade. For RC ≫ $1/f_s$, the ripple is about $\dfrac{VD(1 - D)}{RCf_s}$ peak to peak, worst at D = 0.5.
- **LC** (two poles, as in a buck converter or an inverter's output filter): attenuation grows at 40 dB/decade above $f_0 = 1/(2\pi\sqrt{LC})$, so the switching harmonic is reduced by about $(f_0/f_s)^2$, or $40\log_{10}(f_s/f_0)$ dB.

The faster the switching, the easier the filtering, which is the main reason converters switch at hundreds of kilohertz.

## Natural filters

A motor's winding inductance filters PWM into a smooth current, and the eye averages an LED flickering faster than about 100 Hz. Audio class-D amplifiers rely on the speaker and an LC filter.
:::

::: example
## An inverter's LC output filter has L = 1 mH and C = 10 µF, and it switches at 20 kHz. How much does the filter reduce the switching harmonic?

::: solution
1. **Filter corner.**
   $$\begin{aligned} f_0 &= \frac{1}{2\pi\sqrt{1\ \text{mH} \times 10\ \text{µF}}} \\ &= 1592\ \text{Hz} \end{aligned}$$
2. **Attenuation at 20 kHz.** About
   $$(1592/20\,000)^2 = 0.0063$$
   or 44 dB.
3. **The fundamental.** A 60 Hz fundamental passes almost untouched.

**Answer:** the filter cuts the 20 kHz harmonic by about 44 dB (a factor of 0.0063).
:::
:::

::: example
## A 12 V PWM wave runs at 25% duty. What are the DC term and the first and third harmonics' amplitudes?

::: solution
1. **DC term.** The duty cycle times the amplitude:
   $$0.25 \times 12 = 3\ \text{V}$$
2. **First harmonic.**
   $$\begin{aligned} a_1 &= \frac{2 \times 12}{\pi}\sin(\pi/4) \\ &= 5.40\ \text{V} \end{aligned}$$
3. **Third harmonic.**
   $$\begin{aligned} a_3 &= \frac{2 \times 12}{3\pi}|\sin(3\pi/4)| \\ &= 1.80\ \text{V} \end{aligned}$$

**Answer:** the DC term is 3 V, the first harmonic 5.40 V and the third 1.80 V.
:::
:::

::: example
## A 12 V PWM signal at 50% duty and 10 kHz is smoothed by a single RC stage to make an analog voltage. What corner keeps the fundamental's ripple to 1% of 12 V?

::: solution
1. **Fundamental.**
   $$2 \times 12/\pi = 7.64\ \text{V}$$
2. **Attenuation needed.** Reducing it to 0.12 V needs
   $$7.64/0.12 = 63.7$$
   about 64 times attenuation.
3. **Corner.** A first-order filter well above its corner attenuates by $f/f_c$, so
   $$\begin{aligned} f_c &= 10\ \text{kHz}/63.7 \\ &= 157\ \text{Hz} \end{aligned}$$
4. **Second order.** A second-order filter could keep the corner far higher and respond faster.

**Answer:** a corner of 157 Hz.
:::
:::

::: check
given: V = 3.3 | 5 | 12; D = 0.15 | 0.4 | 0.85
answer: D*V
unit: V
tolerance: 0.5%

## A {V} V PWM signal at duty cycle {D} goes through a slow low-pass filter. What DC voltage results?

::: right
Right. DV = {answer} V.
:::

::: misses
- [(1 - D)*V] D is the fraction of time high.
- [any] Not yet. The average of the pulse train.
:::
:::

::: check
given: V = 5 | 12; D = 0.1 | 0.2 | 0.3; n = 1 | 2 | 3
answer: 2*V/(n*pi)*abs(sin(n*pi*D))
unit: V
tolerance: 0.5%

## A {V} V PWM wave has duty cycle {D}. What's the amplitude of its harmonic number {n}?

::: right
Right. $(2V/n\pi)|\sin(n\pi D)|$ = {answer} V.
:::

::: misses
- [4*V/(n*pi)] That's a ±V square wave's odd harmonic.
- [any] Not yet. The pulse train's Fourier coefficient.
:::
:::

::: check
given: V = 3.3 | 5; D = 0.2 | 0.5 | 0.7; R = 4.7 | 10; C = 1 | 2.2; f = 1 | 10
answer: V*D*(1 - D)/(R*1e3*C*1e-6*f*1e3)*1000
unit: mV
tolerance: 0.5%

## A {V} V PWM at {f} kHz and duty {D} feeds a {R} kΩ, {C} µF RC filter. About how much ripple is left, peak to peak?

::: right
Right. $VD(1 - D)/(RCf_s)$ = {answer} mV.
:::

::: misses
- [any] Not yet. The capacitor's current during the on-time, times the on-time, over C.
:::
:::

::: check
given: L = 0.1 | 1 | 4.7; C = 1 | 10 | 47; f = 20 | 100
answer: 40*log(f*1e3*2*pi*sqrt(L*1e-3*C*1e-6))
unit: dB
tolerance: 1%

## An LC filter with L = {L} mH and C = {C} µF follows a {f} kHz switching stage. By about how many decibels does it attenuate the switching frequency?

::: right
Right. $40\log_{10}(f_s/f_0)$ = {answer} dB.
:::

::: misses
- [20*log(f*1e3*2*pi*sqrt(L*1e-3*C*1e-6))] An LC filter has two poles: 40 dB per decade.
- [any] Not yet. Find $f_0$, then 40 dB per decade above it.
:::
:::

::: check
kind: parts
given: L = 0.5 | 1 | 2; C = 4.7 | 10 | 22; f = 10 | 20 | 40

## An inverter's LC filter has L = {L} mH and C = {C} µF, switching at {f} kHz.

::: part
answer: 1/(2*pi*sqrt(L*1e-3*C*1e-6))
unit: Hz
tolerance: 0.5%

### What is its corner frequency?

::: misses
- [any] $1/(2\pi\sqrt{LC})$.
:::
:::

::: part
answer: 40*log(f*1000*2*pi*sqrt(L*1e-3*C*1e-6))
unit: dB
tolerance: 0.5%

### Roughly how much does it attenuate the switching frequency?

::: misses
- [20*log(f*1000*2*pi*sqrt(L*1e-3*C*1e-6))] A second-order filter falls 40 dB per decade.
- [any] $40\log_{10}(f_s/f_0)$.
:::
:::
:::

::: check
kind: parts
given: V = 5 | 12 | 24; D = 10 | 25 | 40; n = 1 | 3

## A {V} V PWM wave runs at {D}% duty.

::: part
answer: V*D/100
unit: V
tolerance: 0.5%

### What is its DC term?

::: misses
- [any] $VD$.
:::
:::

::: part
answer: 2*V/(n*pi)*abs(sin(n*pi*D/100))
unit: V
tolerance: 0.5%

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

::: misses
- [any] $(2V/n\pi)|\sin(n\pi D)|$.
:::
:::
:::

::: explain
## Why does switching faster make a PWM signal easier to filter?

::: model
A PWM signal is its average plus harmonics at the switching frequency and its multiples. A low-pass filter must pass the average, and any slow changes of it, while attenuating those harmonics, and a filter's attenuation grows with how far above its corner a frequency lies. Raising the switching frequency moves every harmonic further above the corner, so the same filter removes more of them, or a smaller, cheaper filter achieves the same ripple. The limit is that each switching transition costs some energy, so faster switching raises losses.
:::
:::

::: practice

::: check
given: V = 1 | 48; D = 0.01 | 0.99
answer: D*V
unit: V
tolerance: 0.5%

## PWM average: {V} V at D = {D}?

::: misses
- [any] DV.
:::
:::

::: check
given: V = 1 | 400; D = 0.05 | 0.1 | 0.2 | 0.3 | 0.4; n = 1 | 2 | 3
answer: 2*V/(n*pi)*abs(sin(n*pi*D))
unit: V
tolerance: 0.5%

## Harmonic {n} of a {V} V PWM at D = {D}?

::: misses
- [any] $(2V/n\pi)|\sin(n\pi D)|$.
:::
:::

::: check
given: L = 0.1 | 10; C = 1 | 100; f = 20 | 500
answer: 40*log(f*1e3*2*pi*sqrt(L*1e-3*C*1e-6))
unit: dB
tolerance: 1%

## LC attenuation: {L} mH, {C} µF, {f} kHz?

::: misses
- [any] $40\log(f_s/f_0)$.
:::
:::
:::
