---
title: Sampling and reconstruction
heading: When samples capture a signal exactly, and when they lie
eyebrow: C9 · Lesson 21 · Sampling and reconstruction
xp: 50
---

::: hook
Every digital audio file, oscilloscope trace and phone call depends on a claim that sounds too good to be true: a continuous signal can be recovered exactly from its samples, as long as it's sampled more than twice as fast as its highest frequency. Break that rule and the samples don't just lose detail, they lie: a high frequency masquerades as a low one, and nothing done afterward can tell the difference. That's **aliasing**, the wagon wheels that turn backward in films. This lesson covers the sampling theorem, what aliasing does, and how a converter turns samples back into a smooth signal.
:::

::: play
- The dashed curve is a cosine at frequency f, and the dots are its samples at rate $f_s$. The solid curve is the lowest-frequency cosine through the same dots: what the samples look like they came from.
- Below $f/f_s = 0.5$ the two curves agree. Above it they part: at 0.9 the samples trace a slow cosine at 0.1 of the sample rate.
- Press Listen while moving the slider (at $f_s$ = 1 kHz): the pitch you hear is the alias, rising to 500 Hz and then falling again as the true frequency keeps rising.
:::
:::

```sim plot
view: graph
x: n = 0..20
y: v = -1.3..1.3
slider: r = 0..1.5 = 0.9 step 0.005
plot: cos(2*pi*r*n) | dashed | samples: 3000 | label: true signal
plot: cos(2*pi*abs(r - round(r))*n) | samples: 1500 | label: alias
plot: cos(2*pi*r*n) | stems | label: samples
readout: apparent frequency, as a fraction of fs = abs(r - round(r)) | digits: 3
sound: cos(2*pi*round(1000*abs(r - round(r)))*t)
```

::: predict
## A 900 Hz tone is sampled at 1 kHz without an anti-aliasing filter. What frequency do the samples show?

- [ ] 900 Hz
- [ ] 1900 Hz
- [x] 100 Hz
- [ ] 500 Hz

::: reveal
100 Hz. Sampling can't distinguish frequencies that differ by a multiple of $f_s$, and 900 = 1000 − 100, so the samples of a 900 Hz cosine are exactly those of a 100 Hz one. Once sampled, the error can't be undone.
:::
:::

::: nameit
## The sampling theorem

Sampling at $f_s$ makes copies of the signal's spectrum at every multiple of $f_s$. A signal with no energy above B hertz is captured exactly if

$$f_s > 2B$$

(2B is the **Nyquist rate**, and $f_s/2$ the **Nyquist frequency**), because then the copies don't overlap.

## Aliasing

A sinusoid at f is indistinguishable after sampling from one at $f - kf_s$ for any whole k. Folded into the range 0 to $f_s/2$, it appears at

$$f_\text{alias} = \lvert f - f_s\,\mathrm{round}(f/f_s)\rvert$$

Above $f_s/2$, frequencies fold back down. An **anti-aliasing filter** before the converter removes them while they can still be told apart (C4 designed one); it needs a **guard band** between B and $f_s - B$ to roll off in.

## Reconstruction

Ideally, each sample is replaced by a sinc, $\mathrm{sinc}((t - nT)/T)$, and the sincs added: that rebuilds the band-limited signal exactly. A real DAC holds each sample for one period, a **zero-order hold**, which multiplies the spectrum by

$$\lvert H_\text{ZOH}(f)\rvert = \lvert\mathrm{sinc}(f/f_s)\rvert$$

drooping to 0.637 (−3.9 dB) at $f_s/2$, and leaves **images** of each tone at $f_s \pm f$, $2f_s \pm f$, …, which a reconstruction filter removes.
:::

::: example
## Sampled at 1 kHz, where do 900 Hz, 1.1 kHz, 600 Hz and 1.6 kHz tones appear?

::: solution
1. **Fold each tone.**
   $$\begin{aligned} \lvert 900 - 1000\rvert &= 100\ \text{Hz} \\ \lvert 1100 - 1000\rvert &= 100\ \text{Hz} \\ \lvert 600 - 1000\rvert &= 400\ \text{Hz} \\ \lvert 1600 - 2000\rvert &= 400\ \text{Hz} \end{aligned}$$
2. **Compare.** Four different tones, two apparent frequencies: once sampled, each pair is identical.

**Answer:** 900 Hz and 1.1 kHz both appear at 100 Hz, and 600 Hz and 1.6 kHz both appear at 400 Hz.
:::
:::

::: example
## CD audio is sampled at 44.1 kHz to carry 20 kHz. What guard band does that leave, and how much does a zero-order hold droop at 20 kHz?

::: solution
1. **Where the first copy starts.**
   $$44.1 - 20 = 24.1\ \text{kHz}$$
2. **Guard band.** The anti-aliasing filter has from 20 to 24.1 kHz, a 4.1 kHz guard band, to fall.
3. **Hold droop.**
   $$\operatorname{sinc}(20/44.1) = 0.694$$
   or −3.2 dB, which players correct with a gentle boost.

**Answer:** a 4.1 kHz guard band, from 20 to 24.1 kHz, and a droop to 0.694 (−3.2 dB) at 20 kHz.
:::
:::

::: example
## A telephone channel carries 0 to 4 kHz. What sample rate gives the anti-aliasing filter a 1 kHz guard band to fall in?

::: solution
1. **Where the first copy starts.** The first copy of the spectrum after sampling starts at $f_s - 4$ kHz.
2. **Leave room to fall.** For the filter to have from 4 kHz to that point to fall:
   $$\begin{aligned} f_s - 4 &\ge 4 + 1 \\ f_s &\ge 9\ \text{kHz} \end{aligned}$$
3. **What telephony chose.** Telephony settled on 8 kHz with a filter that starts rolling off at 3.4 kHz, which gives a guard band of about the same size, 1.2 kHz.

**Answer:** a sample rate of at least 9 kHz.
:::
:::

::: check
given: f = 700 | 1300 | 2900 | 4400; fs = 1000 | 2000
answer: abs(f - fs*round(f/fs))
unit: Hz
tolerance: 0.5%

## A {f} Hz tone is sampled at {fs} Hz with no anti-aliasing filter. At what frequency do the samples show it (between 0 and {fs}/2)?

::: right
Right. $\lvert f - f_s\,\mathrm{round}(f/f_s)\rvert$ = {answer} Hz.
:::

::: misses
- [any] Not yet. Subtract the nearest multiple of $f_s$.
:::
:::

::: check
given: B = 3.4 | 20 | 100
answer: 2*B
unit: kHz
tolerance: 0.5%

## A signal has no content above {B} kHz. What is its Nyquist rate?

::: right
Right. 2B = {answer} kHz; sample faster than that.
:::

::: misses
- [B] The sample rate must exceed twice the highest frequency.
- [any] Not yet. 2B.
:::
:::

::: check
given: f = 1 | 5 | 10; fs = 22.05 | 48
answer: 20*log(abs(sin(pi*f/fs)/(pi*f/fs)))/log(10)
unit: dB
tolerance: 0.5%

## A DAC with a zero-order hold runs at {fs} kHz. How much does it droop at {f} kHz, in dB?

::: right
Right. $20\log_{10}\lvert\mathrm{sinc}(f/f_s)\rvert$ = {answer} dB.
:::

::: misses
- [any] Not yet. $\mathrm{sinc}(x) = \sin(\pi x)/(\pi x)$ with $x = f/f_s$.
:::
:::

::: check
kind: parts
given: f = 1 | 3; fs = 8 | 44.1

## A DAC at {fs} kHz plays a {f} kHz tone through a zero-order hold.

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

### Where is the lowest image?

::: misses
- [fs] The images sit at $f_s \pm f$.
- [any] Not yet. $f_s - f$.
:::
:::

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

### Where is the next one up?

::: misses
- [any] $f_s + f$.
:::
:::
:::

::: check
given: B = 15 | 20 | 22; fs = 44.1 | 48 | 96
answer: fs - 2*B
unit: kHz
tolerance: 0.5%

## A {B} kHz signal is sampled at {fs} kHz. How wide is the guard band between the signal's edge and the first spectral copy's edge?

::: right
Right. The copy starts at $f_s - B$, so the gap is $f_s - 2B$ = {answer} kHz.
:::

::: misses
- [fs - B] That's where the copy starts; the gap is from B to there.
- [any] Not yet. $(f_s - B) - B$.
:::
:::

::: check
kind: choice

## A signal has components up to 20 kHz. Which sample rate captures it without aliasing (with a suitable anti-aliasing filter)?

- [ ] 20 kHz
- [ ] 32 kHz
- [ ] 40 kHz exactly
- [x] 48 kHz

::: misses
- [1] That's the highest frequency; the rate must exceed twice it.
- [2] Below 40 kHz: the top of the band folds back.
- [3] Exactly 2B leaves no guard band for a real filter, and a sine at exactly $f_s/2$ can be sampled at its zeros.
:::
:::

::: check
given: f = 0.3 | 0.45 | 0.7
answer: abs(sin(pi*f)/(pi*f))
tolerance: 0.5%

## What is the zero-order hold's gain at {f} times the sample rate?

::: right
Right. $\lvert\mathrm{sinc}(f/f_s)\rvert$ = {answer}.
:::

::: misses
- [any] Not yet. $\sin(\pi x)/(\pi x)$.
:::
:::

::: check
kind: parts
given: f = 700 | 1300 | 2900 | 4600; fs = 1000 | 2000

## A {f} Hz tone is sampled at {fs} Hz.

::: part
answer: abs(f - fs*round(f/fs))
unit: Hz
tolerance: 0.5%

### At what frequency does it appear?

::: misses
- [any] Distance to the nearest multiple of {fs}.
:::
:::

::: part
answer: fs/2
unit: Hz
tolerance: 0.5%

### What is the highest frequency that can appear?

::: misses
- [any] Half the sample rate.
:::
:::
:::

::: check
kind: parts
given: r = 0.1 | 0.25 | 0.4

## A zero-order hold reconstructs a signal at {r} of the sample rate.

::: part
answer: sin(pi*r)/(pi*r)
tolerance: 0.5%

### What is its gain there?

::: misses
- [any] $\operatorname{sinc}$: $\sin(\pi f/f_s)/(\pi f/f_s)$.
:::
:::

::: part
answer: 20*log(sin(pi*r)/(pi*r))
unit: dB
tolerance: 0.5%

### And in decibels?

::: misses
- [any] $20\log_{10}$ of the gain.
:::
:::
:::

::: explain
## Why can't aliasing be fixed after the signal has been sampled?

::: model
After sampling, all that remains is the list of numbers. A tone at f and one at $f_s - f$ produce exactly the same list, sample for sample, so no processing of the samples can tell which one was there, or separate them if both were. The information that distinguished them was in the values between the samples, which were never recorded. The only fix is to remove the frequencies above $f_s/2$ before sampling, with an analog anti-aliasing filter, while they're still different signals.
:::
:::

::: practice

::: check
given: f = 600 | 800 | 1200 | 1300 | 1700 | 2100 | 2900 | 3600; fs = 1000
answer: abs(f - fs*round(f/fs))
unit: Hz
tolerance: 0.5%

## Where does {f} Hz appear when sampled at {fs} Hz?

::: misses
- [any] Fold into 0 to 500 Hz.
:::
:::

::: check
given: B = 3.4 | 4 | 10 | 20 | 22 | 50 | 100
answer: 2*B
unit: kHz
tolerance: 0.5%

## Nyquist rate for a {B} kHz band?

::: misses
- [any] 2B.
:::
:::

::: check
given: f = 2 | 7; fs = 16 | 32
answer: fs - f
unit: kHz
tolerance: 0.5%

## Lowest image of a {f} kHz tone from a {fs} kHz DAC?

::: misses
- [any] $f_s - f$.
:::
:::
:::
