---
title: Downsampling, upsampling and polyphase filters
heading: Changing the sample rate inside a digital system, the spectra of decimation and interpolation, rational rate changes, the noble identities and polyphase implementation
eyebrow: S7 · Lesson 7 · Downsampling, upsampling and polyphase filters
xp: 50
---

::: hook
Digital systems change sample rates constantly: an oversampled converter's output is brought down to the audio rate, a 44.1 kHz recording is resampled for a 48 kHz device, a software radio digitizes megahertz of spectrum and then zooms in on a 10 kHz channel. Two primitive operations do it all: keep every Mth sample, or insert L − 1 zeros between samples. Each one does something precise to the spectrum, stretching it and aliasing it or compressing it and imaging it, and both need a filter to clean up. Polyphase structures then let that filter run at the lower of the two rates, which is where most of the efficiency in real converters comes from.
:::

::: play
- The spectrum of a signal with a triangular spectrum of half-width b (dashed), and after keeping every Mth sample (solid), from $X_d(e^{j\omega}) = \frac{1}{M}\sum_{k=0}^{M-1}X(e^{j(\omega - 2\pi k)/M})$.
- Downsampling stretches the spectrum by M and adds M shifted copies. If b < π/M the copies don't touch.
- Widen the spectrum or raise M past that point and the copies overlap: aliasing, which only a lowpass filter before downsampling prevents.
:::
:::

```sim plot
view: graph
x: w = -3.1416..3.1416
y: X = 0..1.1
slider: M = 1..4 = 2 step 1
slider: b = 0.2..3.1 = 1.2
plot: (1 - abs(w - 2*pi*round(w/(2*pi)))/b + abs(1 - abs(w - 2*pi*round(w/(2*pi)))/b))/2 | samples: 800 | dashed | color: ch3 | label: X(e^{jω}) before
plot: ((1 - abs(w/M - 2*pi*round(w/M/(2*pi)))/b + abs(1 - abs(w/M - 2*pi*round(w/M/(2*pi)))/b))/2 + step(M - 1.5)*(1 - abs((w - 2*pi)/M - 2*pi*round((w - 2*pi)/M/(2*pi)))/b + abs(1 - abs((w - 2*pi)/M - 2*pi*round((w - 2*pi)/M/(2*pi)))/b))/2 + step(M - 2.5)*(1 - abs((w - 4*pi)/M - 2*pi*round((w - 4*pi)/M/(2*pi)))/b + abs(1 - abs((w - 4*pi)/M - 2*pi*round((w - 4*pi)/M/(2*pi)))/b))/2 + step(M - 3.5)*(1 - abs((w - 6*pi)/M - 2*pi*round((w - 6*pi)/M/(2*pi)))/b + abs(1 - abs((w - 6*pi)/M - 2*pi*round((w - 6*pi)/M/(2*pi)))/b))/2)/M | samples: 800 | label: after downsampling by M
readout: largest b without aliasing = pi/M
```

::: predict
## A signal sampled at 48 kHz is to be decimated to 16 kHz by keeping every third sample. What must happen first?

- [ ] Nothing, as long as the original sampling was alias-free
- [x] A digital lowpass filter must remove everything above 8 kHz
- [ ] An analog filter at 24 kHz
- [ ] The signal must be upsampled first

::: reveal
A digital lowpass at π/3, which at 48 kHz is 8 kHz. The new Nyquist frequency is 8 kHz, and anything between 8 and 24 kHz in the original would fold into the new band. The original sampling being alias-free is irrelevant; decimation is a second sampling and needs its own anti-aliasing filter.
:::
:::

::: nameit
## Downsampling (compression) by M

$x_d[n] = x[nM]$, with

$$X_d(e^{j\omega}) = \frac{1}{M}\sum_{k=0}^{M-1}X\left(e^{j(\omega - 2\pi k)/M}\right)$$

Frequencies stretch by M; a component at $\omega_0$ moves to $M\omega_0$. To avoid aliasing, lowpass to $\pi/M$ first. The pair, filter then downsample, is a **decimator**.

## Upsampling (expansion) by L

Insert L − 1 zeros: $X_e(e^{j\omega}) = X(e^{j\omega L})$. The spectrum compresses by L and L − 1 **images** appear. A lowpass with cutoff π/L and gain L removes them: an **interpolator**.

## Rational rate change and efficiency

- Change by L/M: upsample by L, one lowpass with cutoff $\min(\pi/L, \pi/M)$ and gain L, then downsample by M. 44.1 → 48 kHz is 160/147.
- **Noble identities**: a filter $H(z^M)$ before a downsampler by M equals H(z) after it; $H(z^L)$ after an upsampler equals H(z) before it.
- **Polyphase**: $H(z) = \sum_{k=0}^{M-1}z^{-k}E_k(z^M)$. By the noble identities each $E_k$ runs at the low rate, so an N-tap decimating FIR costs N multiplications per output sample (N/M per input) instead of N per input.
- Large factors are cheaper in stages, since early stages can use short filters with wide transition bands.
:::

::: example
## A 120-tap FIR filter decimates by 4. Count multiplications per output sample, done naively and in polyphase form.

::: solution
1. **Naive.** The filter runs at the input rate, computing 120 products per input sample, and only every fourth output is kept:
   $$120 \times 4 = 480\ \text{per output}$$
2. **Polyphase.** Four 30-tap subfilters each run once per output:
   $$4 \times 30 = 120\ \text{per output}$$
   That's 30 per input sample.
3. **Compare.** Polyphase saves a factor of 4 and gives an identical result.

**Answer:** 480 multiplications per output sample naively, and 120 in polyphase form.
:::
:::

::: example
## A 1 kHz tone sampled at 8 kHz is upsampled by 3. Where are its images, and what filter removes them?

::: solution
1. **New rate.** The new rate is 24 kHz, with its Nyquist frequency at 12 kHz.
2. **Images.** Inserting zeros leaves the original spectrum's copies at 8 ± 1 and 16 ± 1 kHz visible, so images appear at 7 and 9 kHz, below the new 12 kHz Nyquist frequency.
3. **Interpolation filter.** A lowpass at π/3 (4 kHz at 24 kHz) with gain 3 removes them and restores the amplitude.

**Answer:** images at 7 and 9 kHz, removed by a lowpass filter at π/3 (4 kHz) with a gain of 3.
:::
:::

::: example
## Convert 44.1 kHz audio to 48 kHz with one polyphase filter of 3840 taps. What is the intermediate rate, the filter's cutoff, and the cost per output sample?

::: solution
1. **Rate ratio.**
   $$\frac{48}{44.1} = \frac{160}{147}$$
   so L = 160 and M = 147.
2. **Intermediate rate.** The conceptual intermediate rate is
   $$\begin{aligned} f &= 160 \times 44.1\ \text{kHz} \\ &= 7.056\ \text{MHz} \end{aligned}$$
3. **Cutoff.**
   $$\begin{aligned} \omega_c &= \min(\pi/160, \pi/147) \\ &= \pi/160 \end{aligned}$$
   which is 22.05 kHz, the input's Nyquist frequency.
4. **Cost per output.** In polyphase form each output uses only one of the 160 subfilters:
   $$\frac{3840}{160} = 24\ \text{taps}$$
   That's 24 multiplications per output sample, against millions per second for the naive version.

**Answer:** the intermediate rate is 7.056 MHz, the cutoff is π/160 (22.05 kHz), and each output sample costs 24 multiplications.
:::
:::

::: check
given: w = 0.05 | 0.1 | 0.2; M = 2 | 3 | 4
answer: M*w
tolerance: 0.5%

## A tone at ω = {w}π is downsampled by {M}. Where does it appear, in units of π?

::: right
Right. Downsampling stretches frequencies by M: {answer}π.
:::

::: misses
- [w/M] Downsampling stretches frequencies; upsampling compresses them.
- [any] Not yet. $\omega_0 \to M\omega_0$.
:::
:::

::: check
given: s = 48 | 96 | 192; M = 2 | 3 | 6
answer: s/(2*M)
unit: kHz
tolerance: 0.5%

## A signal at {s} kHz is decimated by {M}. Above what frequency must the anti-aliasing filter remove content?

::: right
Right. The new Nyquist frequency, $f_s/2M$ = {answer} kHz.
:::

::: misses
- [s/M] That's the new sample rate; the limit is half of it.
- [s/2] That's the old Nyquist frequency.
- [any] Not yet. The output rate is $f_s/M$; keep content below half of it.
:::
:::

::: check
given: f = 0.5 | 1 | 2.5; s = 8 | 16; L = 2 | 4
answer: s - f
unit: kHz
tolerance: 0.5%

## A {f} kHz tone at {s} kHz is upsampled by {L}. What's the lowest image frequency?

::: right
Right. $f_s - f_0$ = {answer} kHz.
:::

::: misses
- [s + f] That's an image too, but not the lowest.
- [L*s - f] Images appear around every multiple of the original rate, starting with $f_s$.
- [any] Not yet. The zero-stuffed signal keeps the copies at $kf_s \pm f_0$.
:::
:::

::: check
given: N = 64 | 120 | 255; M = 2 | 4 | 8
answer: N/M
tolerance: 0.5%

## An N = {N}-tap decimating FIR (factor {M}) is built in polyphase form. How many multiplications does it need per input sample?

::: right
Right. N per output, so N/M = {answer} per input.
:::

::: misses
- [N] That's per output sample; there are M inputs per output.
- [N*M] That's the naive count per output.
- [any] Not yet. Each output needs N products, and outputs come once every M inputs.
:::
:::

::: check
given: L = 3 | 160 | 5; M = 2 | 147 | 4
answer: 1/((L + M + abs(L - M))/2)
tolerance: 0.5%

## A rate converter upsamples by {L} and downsamples by {M}. What's its single filter's cutoff, in units of π (at the upsampled rate)?

::: right
Right. $\min(\pi/L, \pi/M)$ = {answer}π.
:::

::: misses
- [any] Not yet. The filter must both remove images (π/L) and prevent aliasing (π/M).
:::
:::

::: check
given: s = 44.1 | 48; L = 160 | 147
answer: s*L/1000
unit: MHz
tolerance: 0.5%

## A {s} kHz signal is upsampled by {L} inside a rational rate converter. At what rate does the conceptual interpolation filter run?

::: right
Right. {s} × {L} = {answer} MHz, which is why polyphase structures that never compute at that rate are essential.
:::

::: misses
- [s/L] Upsampling raises the rate.
- [any] Not yet. Multiply the rate by L.
:::
:::

::: check
kind: parts
given: L = 3 | 5 | 160; s = 8 | 44.1; let M = L - 1 - 12*step(L - 100)

## A rate converter upsamples a {s} kHz signal by {L} and downsamples by {M}.

::: part
answer: L*s
unit: kHz
tolerance: 0.5%

### What is the intermediate rate?

::: misses
- [any] L times the input rate.
:::
:::

::: part
answer: L/M*s
unit: kHz
tolerance: 0.5%

### What is the output rate?

::: misses
- [M/L*s] It's L/M times the input.
- [any] $(L/M)f_s$.
:::
:::
:::

::: check
kind: derivation
vars: N = 8..4000, M = 2..64

## An N-tap FIR decimates by M.

::: step
answer: N

### How many multiplications per input sample does the naive version need?
:::

::: step
answer: N/M

### And the polyphase version?
:::

::: step
answer: N

### How many multiplications does the polyphase version spend per output sample?
:::

::: right
Right. Polyphase computes only the outputs that survive downsampling, saving a factor of M.
:::
:::

::: explain
## Why does a decimator's filter only need to compute every Mth output?

::: model
A decimator filters and then throws away M − 1 of every M output samples, so computing those discarded outputs is wasted work. Because the filter is an FIR sum of past inputs, each kept output depends only on the inputs, not on any other output, and can be computed on its own. The polyphase form makes this systematic: splitting the filter's taps into M interleaved groups lets each group act on the inputs that belong to it at the low rate, and by the noble identity the result is exactly the same as filtering at the high rate and keeping every Mth sample. The arithmetic drops by a factor of M with no change in the output.
:::
:::

::: practice

::: check
given: w = 0.08 | 0.15; M = 3 | 5
answer: M*w
tolerance: 0.5%

## Tone at {w}π after downsampling by {M}?

::: misses
- [any] $M\omega_0$.
:::
:::

::: check
given: s = 96 | 384; M = 4 | 8
answer: s/(2*M)
unit: kHz
tolerance: 0.5%

## Anti-aliasing limit: {s} kHz decimated by {M}?

::: misses
- [any] $f_s/2M$.
:::
:::

::: check
given: N = 96 | 200; M = 3 | 5
answer: N/M
tolerance: 0.5%

## Polyphase multiplications per input: {N} taps, factor {M}?

::: misses
- [any] N/M.
:::
:::
:::
