---
title: Digital processing of analog signals and changing the sample rate
heading: Filters that run on samples, and moving between sample rates
eyebrow: C9 · Lesson 23 · Digital processing of analog signals and changing the sample rate
xp: 40
---

::: hook
Most filters today are a converter, a processor and another converter: sample the analog signal, run a difference equation, turn the result back into a voltage. Done right, the whole chain behaves exactly like an analog filter, and its cutoff moves when the sample rate does. Systems also have to move signals between rates all the time: audio recorded at 44.1 kHz and played at 48 kHz, a radio that samples fast and then works at a much slower rate. Dropping samples or inserting zeros sounds trivial, but each needs a filter, or aliases and images creep in.
:::

::: play
- The dashed curve is a signal's spectrum, occupying low frequencies up to $\omega_b$, plotted from 0 to π. The solid curve is the spectrum after inserting L − 1 zeros between every pair of samples.
- At L = 1 nothing changes. Raise L: the spectrum is squeezed toward 0 by a factor L, and copies, the **images**, appear at multiples of 2π/L.
- An interpolation filter keeps everything below the marker at π/L and removes the images; what's left is the same signal at L times the sample rate.
:::
:::

```sim plot
view: graph
x: w = 0..3.1416 rad
y: X = 0..1.1
slider: L = 1..6 = 3 step 1
slider: wb = 0.3..3 = 1.5 step 0.05
plot: ((1 - w/wb) + abs(1 - w/wb))/2 | dashed | label: before
plot: ((1 - acos(cos(w*L))/wb) + abs(1 - acos(cos(w*L))/wb))/2 | samples: 2000 | label: zeros inserted
marker: x = pi/L | label: π/L
readout: images centered at multiples of, rad = 2*pi/L | digits: 3
```

::: predict
## A signal sampled at 48 kHz contains a 10 kHz tone. Every fourth sample is kept, with no filter first. Where does the tone appear at the new rate?

- [ ] 10 kHz
- [ ] 2.5 kHz
- [x] 2 kHz
- [ ] It disappears

::: reveal
2 kHz. The new rate is 12 kHz, whose Nyquist frequency is 6 kHz, so 10 kHz aliases to |10 − 12| = 2 kHz. A decimator must low-pass the signal to the new Nyquist frequency before throwing samples away.
:::
:::

::: nameit
## Digital processing of analog signals

Sample at $f_s$, filter with $H(e^{j\omega})$, convert back. For a band-limited input the chain acts like an analog filter with

$$H_\text{eff}(f) = H(e^{j2\pi f/f_s}) \quad\text{for } \lvert f\rvert < f_s/2$$

So a digital cutoff $\omega_c$ is an analog cutoff $f_c = \omega_c f_s/2\pi$, and the same coefficients at twice the sample rate cut off at twice the frequency.

## Decimation by M

Keep every Mth sample: the rate becomes $f_s/M$ and frequencies are stretched by M, $\omega \to M\omega$. Anything above $\pi/M$ beforehand aliases, so first low-pass to $\pi/M$ (the **anti-aliasing** step done digitally).

## Interpolation by L

Insert L − 1 zeros after each sample: the spectrum is squeezed by L, $\omega \to \omega/L$, leaving L − 1 images in the band. A low-pass at $\pi/L$ with gain L removes them and fills in the missing samples.

## Rational rate changes

Up by L, filter at the lower of the two cutoffs, down by M: the rate becomes $f_sL/M$. 44.1 kHz to 48 kHz is L/M = 160/147.

## Oversampling

Sampling faster than needed relaxes the analog anti-aliasing filter (a wide guard band) and spreads the converter's quantization noise over a wider band: each doubling of the rate, filtered back down to the signal band, lowers the in-band noise by 3 dB, $10\log_{10}(\text{OSR})$ in all.
:::

::: example
## A digital low-pass has $\omega_c = \pi/4$. What analog cutoff does it give at 48 kHz and at 96 kHz?

::: solution
1. **Map to analog frequency.**
   $$\begin{aligned} f_c &= \omega_cf_s/2\pi \\ &= f_s/8 \end{aligned}$$
2. **At each rate.** That's 6 kHz at 48 kHz and 12 kHz at 96 kHz.
3. **Keeping 6 kHz.** To keep 6 kHz at the higher rate, the coefficients must be redesigned for $\omega_c = \pi/8$.

**Answer:** 6 kHz at 48 kHz and 12 kHz at 96 kHz.
:::
:::

::: example
## Convert 44.1 kHz audio to 48 kHz.

::: solution
1. **Ratio in lowest terms.**
   $$48000/44100 = 160/147$$
2. **Upsample by 160.** Insert 159 zeros between samples, which takes the rate to 7.056 MHz.
3. **Filter.** Low-pass below 20 kHz or so, under both rates' Nyquist frequencies (22.05 kHz being the lower).
4. **Downsample by 147.** Keep every 147th sample.
5. **In practice.** A polyphase filter computes only the samples that are kept.

**Answer:** upsample by 160, low-pass below about 20 kHz, then keep every 147th sample.
:::
:::

::: example
## A 1 kHz tone sampled at 8 kHz is upsampled by 3 (two zeros between samples). Where do images appear before the interpolation filter?

::: solution
1. **New rate.** The new rate is 24 kHz, so the spectrum now runs to a Nyquist frequency of 12 kHz.
2. **Images.** Inserting zeros keeps the old spectrum and its copies around 8 kHz: below 12 kHz there are tones at 1, 7 and 9 kHz.
3. **The interpolation filter.** It must keep 1 kHz and remove everything from 7 kHz up, which is why it's a low-pass with cutoff at the old Nyquist frequency, 4 kHz.

**Answer:** images appear at 7 and 9 kHz, beside the 1 kHz tone.
:::
:::

::: check
given: wc = 0.25 | 0.5 | 1.2; fs = 8 | 48 | 96
answer: wc*fs/(2*pi)
unit: kHz
tolerance: 0.5%

## A digital filter's cutoff is ω = {wc} rad. What analog cutoff does it give at {fs} kHz?

::: right
Right. $\omega_cf_s/2\pi$ = {answer} kHz.
:::

::: misses
- [wc*fs] Divide by 2π: ω = 2π corresponds to $f_s$.
- [any] Not yet. $f = \omega f_s/2\pi$.
:::
:::

::: check
kind: parts
given: fs = 48 | 96; M = 2 | 4 | 6

## A signal at {fs} kHz is decimated by {M}.

::: part
answer: fs/M
unit: kHz
tolerance: 0.5%

### What is the new sample rate?

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

::: part
answer: pi/M
unit: rad
tolerance: 0.5%

### Below what ω (at the old rate) must the signal be filtered first?

::: misses
- [pi] The new Nyquist frequency is π at the new rate, which is π/M at the old one.
- [any] Not yet. π/M.
:::
:::
:::

::: check
given: f = 7 | 9 | 10; fs = 48; M = 4
answer: abs(f - fs/M*round(f*M/fs))
unit: kHz
tolerance: 0.5%

## A {f} kHz tone at {fs} kHz is decimated by {M} with no filter. At what frequency does it appear?

::: right
Right. The new rate is 12 kHz, so it folds to {answer} kHz.
:::

::: misses
- [f] Above the new Nyquist frequency, 6 kHz, it aliases.
- [any] Not yet. Fold {f} kHz into 0 to 6 kHz.
:::
:::

::: check
kind: parts
given: w0 = 0.3 | 0.6 | 1.2; L = 2 | 3 | 4

## A tone at ω = {w0} is interpolated by inserting {L} − 1 zeros between samples.

::: part
answer: w0/L
unit: rad
tolerance: 0.5%

### Where is the tone after zero insertion?

::: misses
- [w0*L] Inserting zeros squeezes the spectrum: frequencies are divided by L.
- [any] Not yet. $\omega_0/L$.
:::
:::

::: part
answer: 2*pi/L - w0/L
unit: rad
tolerance: 0.5%

### Where is the lowest image?

::: misses
- [any] Mirrored around 2π/L: $2\pi/L - \omega_0/L$.
:::
:::
:::

::: check
given: fs = 8 | 44.1; L = 2 | 3 | 5; M = 4 | 7
answer: fs*L/M
unit: kHz
tolerance: 0.5%

## A {fs} kHz signal is upsampled by {L} and then downsampled by {M}. What is the final rate?

::: right
Right. $f_sL/M$ = {answer} kHz.
:::

::: misses
- [fs*M/L] Up by L multiplies; down by M divides.
- [any] Not yet. $f_s \times L/M$.
:::
:::

::: check
given: osr = 4 | 16 | 64
answer: 10*log(osr)/log(10)
unit: dB
tolerance: 0.5%

## A converter oversamples by {osr} and filters back down to the signal band. By how many dB does the in-band quantization noise fall?

::: right
Right. $10\log_{10}(\text{OSR})$ = {answer} dB, 3 dB per doubling.
:::

::: misses
- [20*log(osr)/log(10)] Noise power spreads over OSR times the band: $10\log_{10}$, not 20.
- [any] Not yet. The noise is spread over {osr} times the bandwidth.
:::
:::

::: check
kind: choice

## Why does interpolation's low-pass filter need a gain of L?

- [ ] To make up for the filter's own loss
- [x] Inserting zeros divides the average signal level by L, and the filter's gain restores it
- [ ] To boost the images
- [ ] It doesn't; a gain of 1 is right

::: misses
- [1] An ideal low-pass has no loss in its passband.
- [3] The filter removes the images.
- [4] With L − 1 zeros for every sample, the smoothed result comes out L times too small without it.
:::
:::

::: check
kind: parts
given: fs = 48 | 96 | 192; M = 2 | 4 | 8

## A {fs} kHz signal is decimated by {M}.

::: part
answer: fs/M
unit: kHz
tolerance: 0.5%

### What is the new sample rate?

::: misses
- [any] Divide by {M}.
:::
:::

::: part
answer: fs/(2*M)
unit: kHz
tolerance: 0.5%

### Below what frequency must the anti-aliasing filter keep the signal?

::: misses
- [fs/M] Half the new rate.
- [any] The new Nyquist frequency.
:::
:::
:::

::: check
kind: parts
given: f = 1 | 2 | 3; fs = 8 | 10; L = 2 | 4

## A {f} kHz tone sampled at {fs} kHz is upsampled by {L}.

::: part
answer: fs - f
unit: kHz
tolerance: 0.5%

### Where is the lowest image the interpolation filter must remove?

::: misses
- [any] The first copy, mirrored around the old sample rate.
:::
:::

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

### What is the new sample rate?

::: misses
- [any] {L} times the old.
:::
:::
:::

::: explain
## Why must a decimator filter before it throws samples away, rather than after?

::: model
Keeping every Mth sample lowers the sample rate to $f_s/M$, so frequencies between $f_s/2M$ and $f_s/2$ no longer fit and fold down onto lower frequencies. Once they've folded, they sit on top of the wanted signal and can't be separated: the aliasing is permanent. Filtering first removes them while they're still at their own frequencies, distinct from the signal, so that what's left after dropping samples is only what the lower rate can represent.
:::
:::

::: practice

::: check
given: wc = 0.4 | 1; fs = 16 | 44.1
answer: wc*fs/(2*pi)
unit: kHz
tolerance: 0.5%

## Analog cutoff of ω = {wc} at {fs} kHz?

::: misses
- [any] $\omega f_s/2\pi$.
:::
:::

::: check
given: M = 2..10 int
answer: pi/M
unit: rad
tolerance: 0.5%

## Anti-aliasing cutoff before decimating by {M}?

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

::: check
given: osr = 2 | 4 | 8 | 16 | 32 | 64 | 128 | 256
answer: 10*log(osr)/log(10)
unit: dB
tolerance: 0.5%

## Noise reduction from {osr}× oversampling?

::: misses
- [any] $10\log_{10}$ OSR.
:::
:::
:::
