---
title: Sampling and reconstruction in depth
heading: The sampled spectrum, aliasing and folding, ideal and zero-order-hold reconstruction, discrete-time processing of continuous-time signals, quantization and the case for oversampling
eyebrow: S7 · Lesson 4 · Sampling and reconstruction in depth
xp: 50
---

::: hook
Every digital system that touches the physical world sits between an analog-to-digital converter and a digital-to-analog one, and the theory that connects them is short and exact. Sampling at rate $f_s$ copies the signal's spectrum at every multiple of $f_s$; if the copies overlap, information is destroyed for good. Reconstruction removes the copies again, but real converters hold each sample as a staircase, which tilts the spectrum and leaves images behind. This lesson works through all of it with real numbers, ending with the reason modern audio converters sample far faster than the Nyquist rate requires.
:::

::: play
- The apparent frequency of a tone after sampling at $f_s$, against the tone's true frequency: $f_a = |f - f_s\,\text{round}(f/f_s)|$.
- Below $f_s/2$ the tone comes through unchanged. Above it the frequency folds back, a zigzag between 0 and $f_s/2$.
- At 44.1 kHz a 30 kHz tone appears at 14.1 kHz and a 25 kHz tone at 19.1 kHz, both inside the audio band, which is why the anti-aliasing filter must come before the converter.
:::
:::

```sim plot
view: graph
x: f = 0..60 kHz
y: a = 0..30 kHz
slider: s = 8..60 = 44.1 kHz
plot: abs(f - s*round(f/s)) | samples: 1200 | label: apparent frequency
plot: s/2 | dashed | color: ch3 | label: f_s/2
```

::: predict
## A CD player's converter holds each sample for one period (a zero-order hold). How much does that alone attenuate a 20 kHz tone at 44.1 kHz?

- [ ] Nothing: the hold is ideal at audio frequencies
- [ ] About 0.1 dB
- [x] About 3.2 dB
- [ ] About 20 dB

::: reveal
3.2 dB. A zero-order hold's frequency response is $\frac{\sin(\pi f/f_s)}{\pi f/f_s}$, which at 20 kHz (0.454 of $f_s$) is 0.694, or −3.17 dB, and at the Nyquist frequency −3.92 dB. Converters either compensate with a digital boost filter rising toward $f_s/2$, or oversample so the band of interest sits on the flat part of the curve.
:::
:::

::: nameit
## The sampled spectrum

Sampling $x_c(t)$ every T seconds gives $x[n] = x_c(nT)$ with

$$X(e^{j\omega}) = \frac{1}{T}\sum_{k=-\infty}^{\infty}X_c\left(j\left(\frac{\omega}{T} - \frac{2\pi k}{T}\right)\right)$$

copies of the analog spectrum every $\Omega_s = 2\pi/T$, with ω = ΩT. If $x_c$ is bandlimited to $\Omega_N$ and $\Omega_s > 2\Omega_N$ (the **Nyquist rate**), the copies don't overlap and $x_c$ can be recovered exactly; otherwise they **alias**: a tone at f appears at $|f - f_s\,\text{round}(f/f_s)|$.

## Reconstruction

- **Ideal**: lowpass the impulse train at $\pi/T$, which interpolates with sincs: $x_c(t) = \sum_nx[n]\frac{\sin(\pi(t - nT)/T)}{\pi(t - nT)/T}$.
- **Zero-order hold**: holding each value for T multiplies the spectrum by $\frac{\sin(\Omega T/2)}{\Omega T/2}e^{-j\Omega T/2}$: a droop of 3.92 dB at $f_s/2$, half a sample of delay, and images at multiples of $f_s$ that a smoothing filter must remove.

## Discrete-time processing of continuous-time signals

C/D conversion, a discrete-time system $H(e^{j\omega})$, then ideal D/C behaves like an analog filter

$$H_\text{eff}(j\Omega) = H(e^{j\Omega T}), \quad |\Omega| < \pi/T$$

so a digital cutoff $\omega_c$ means an analog cutoff $\omega_cf_s/2\pi$ Hz.

## Quantization

A B-bit quantizer driven by a full-scale sine has SNR ≈ 6.02B + 1.76 dB, with its noise spread evenly from 0 to $f_s/2$. **Oversampling** by a factor M spreads the same noise over M times the bandwidth, gaining 10 log M dB in band, and relaxes the anti-aliasing filter.
:::

::: example
## A 48 kHz audio converter must pass 20 kHz and reject anything that would alias into it by 60 dB. How steep must a Butterworth anti-aliasing filter be?

::: solution
1. **What aliases into the band.** Signals above 28 kHz alias into 0 to 20 kHz (48 − 28 = 20), so the filter needs 60 dB at 28 kHz with its corner at 20 kHz.
2. **Butterworth order.**
   $$\begin{aligned} N &\ge \frac{\log(10^6 - 1)}{2\log(1.4)} \\ &= 20.5 \end{aligned}$$
   so it takes a 21st-order analog filter.
3. **Oversample instead.** Oversampling by 64 moves the first aliasing band to
   $$\begin{aligned} f &= (64 \times 48 - 20)\ \text{kHz} \\ &= 3.05\ \text{MHz} \end{aligned}$$
   where a simple second-order filter is plenty; the digital decimation filter then does the sharp cutting.

**Answer:** a 21st-order Butterworth filter (N ≥ 20.5); oversampling by 64 lets a second-order filter do the job.
:::
:::

::: example
## A 30 kHz tone and a 25 kHz tone are sampled at 44.1 kHz without filtering. Where do they appear, and what does a zero-order hold do to a 20 kHz tone?

::: solution
1. **The 30 kHz tone.**
   $$\begin{aligned} f &= |30 - 44.1|\ \text{kHz} \\ &= 14.1\ \text{kHz} \end{aligned}$$
2. **The 25 kHz tone.**
   $$\begin{aligned} f &= |25 - 44.1|\ \text{kHz} \\ &= 19.1\ \text{kHz} \end{aligned}$$
3. **Hold droop at 20 kHz.** On playback through a zero-order hold, with 20/44.1 = 0.4535:
   $$\begin{aligned} G &= 20\log\frac{\sin(0.4535\pi)}{0.4535\pi} \\ &= -3.17\ \text{dB} \end{aligned}$$
4. **Image.** An image appears at 44.1 − 20 = 24.1 kHz for the smoothing filter to remove.

**Answer:** the tones appear at 14.1 kHz and 19.1 kHz; the hold leaves 20 kHz down 3.17 dB, with an image at 24.1 kHz.
:::
:::

::: example
## A zero-order-hold DAC is used up to 0.4$f_s$. How much does it droop there, and what must a compensating digital filter do?

::: solution
1. **Hold response at 0.4$f_s$.**
   $$\begin{aligned} H &= \frac{\sin(\pi f/f_s)}{\pi f/f_s} \\ &= \frac{\sin(0.4\pi)}{0.4\pi} \\ &= 0.757 \end{aligned}$$
   That's a droop of 2.42 dB.
2. **Compensation.** A short FIR "inverse-sinc" filter before the DAC boosts the top of the band by the same amount, so the analog output is flat.
3. **The cost.** It costs a little headroom, since the boosted samples can exceed full scale.

**Answer:** the hold droops 2.42 dB at 0.4$f_s$, and an inverse-sinc FIR filter before the DAC must boost the top of the band by the same amount.
:::
:::

::: check
given: f = 13 | 17 | 31 | 52; s = 10 | 16 | 22.05
answer: abs(f - s*round(f/s))
unit: kHz
tolerance: 0.01

## A {f} kHz tone is sampled at {s} kHz with no anti-aliasing filter. At what frequency does it appear?

::: right
Right. $|f - f_s\,\text{round}(f/f_s)|$ = {answer} kHz.
:::

::: misses
- [f] It's above the Nyquist frequency, so it folds.
- [any] Not yet. Subtract the nearest multiple of $f_s$ and take the magnitude.
:::
:::

::: check
given: f = 15 | 18 | 20; s = 44.1 | 48 | 96
answer: 20*log(sin(pi*f/s)/(pi*f/s))
unit: dB
tolerance: 0.02

## A zero-order-hold DAC runs at {s} kHz. What's its response at {f} kHz?

::: right
Right. $20\log\frac{\sin(\pi f/f_s)}{\pi f/f_s}$ = {answer} dB.
:::

::: misses
- [10*log(sin(pi*f/s)/(pi*f/s))] It's an amplitude response: 20 log.
- [any] Not yet. The hold's response is a sinc in $f/f_s$.
:::
:::

::: check
given: w = 0.1 | 0.25 | 0.4; s = 8 | 44.1 | 200
answer: w*s/2
unit: kHz
tolerance: 0.5%

## A digital lowpass filter with cutoff ω = {w}π runs inside a system sampling at {s} kHz. What's the equivalent analog cutoff?

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

::: misses
- [w*s] ω = π corresponds to $f_s/2$, not $f_s$.
- [any] Not yet. f = ω f_s/(2π).
:::
:::

::: check
given: B = 8 | 12 | 16 | 24
answer: 6.02*B + 1.76
unit: dB
tolerance: 0.05

## What SNR does an ideal {B}-bit quantizer give for a full-scale sine?

::: right
Right. 6.02B + 1.76 = {answer} dB.
:::

::: misses
- [6.02*B] Add 1.76 dB for a full-scale sine against uniform noise.
- [any] Not yet. 6.02B + 1.76 dB.
:::
:::

::: check
given: B = 12 | 16; M = 4 | 16 | 64
answer: 6.02*B + 1.76 + 10*log(M)
unit: dB
tolerance: 0.05

## A {B}-bit converter is oversampled by {M} and its output filtered to the original band. What in-band SNR results (no noise shaping)?

::: right
Right. 6.02B + 1.76 + 10 log M = {answer} dB.
:::

::: misses
- [6.02*B + 1.76 + 20*log(M)] Plain oversampling gains 10 log M: the noise power spreads over M times the bandwidth.
- [any] Not yet. Add 10 log M to the quantizer's SNR.
:::
:::

::: check
given: k = 0.25 | 0.5 | 1.5
answer: abs(sin(pi*k)/(pi*k))
tolerance: 0.5%

## Only one sample is nonzero, x[0] = 1. What value does ideal sinc reconstruction give at t = {k}T?

::: right
Right. $\frac{\sin(\pi t/T)}{\pi t/T}$ = {answer} in magnitude.
:::

::: misses
- [1] Away from the sample instants the sinc is less than 1.
- [any] Not yet. Evaluate the interpolating sinc at t/T = {k}.
:::
:::

::: check
kind: parts
given: s = 8 | 44.1 | 96; x = 0.2 | 0.3 | 0.45

## A zero-order-hold DAC runs at {s} kHz and is used up to {x} of its sample rate.

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

### How much does it droop at the top of that band?

::: misses
- [any] $-20\log_{10}\frac{\sin(\pi f/f_s)}{\pi f/f_s}$.
:::
:::

::: part
answer: 1/(2*s*1000)*1e6
unit: µs
tolerance: 0.5%

### What delay does the hold add?

::: misses
- [1/(s*1000)*1e6] Half a sample, not a whole one.
- [any] T/2.
:::
:::
:::

::: check
kind: derivation
vars: s = 1000..100000, f = 0.51..0.99

## A tone at a fraction f of the sample rate s (between half and all of it) is sampled with no anti-aliasing filter.

::: step
answer: s - f*s

### At what frequency does it appear?
:::

::: step
answer: 2*pi*(1 - f)

### At what digital frequency (radians per sample)?
:::

::: step
answer: sin(pi*(1 - f))/(pi*(1 - f))

### What gain would a zero-order-hold DAC give that alias on the way back out?
:::

::: right
Right. Once aliased, the tone is indistinguishable from a genuine one at the folded frequency.
:::
:::

::: explain
## Why is aliasing impossible to undo after sampling?

::: model
Sampling records only the values at the sample instants, and a tone at f and a tone at $f + kf_s$ pass through exactly the same values at every one of them, since their phases differ by a whole number of cycles each sample. In the spectrum this shows up as the copies of the analog spectrum at multiples of $f_s$: if the signal had content above $f_s/2$, the copies overlap and add, and the sum no longer says which part came from which frequency. No processing after the converter can separate them, because the information that would distinguish them was never captured. The only cure is to remove the offending content before sampling, with an analog anti-aliasing filter, or to sample fast enough that there is none.
:::
:::

::: practice

::: check
given: f = 9 | 27; s = 12 | 20
answer: abs(f - s*round(f/s))
unit: kHz
tolerance: 0.01

## Apparent frequency of {f} kHz sampled at {s} kHz?

::: misses
- [any] $|f - f_s\,\text{round}(f/f_s)|$.
:::
:::

::: check
given: f = 10 | 18; s = 44.1 | 48
answer: 20*log(sin(pi*f/s)/(pi*f/s))
unit: dB
tolerance: 0.02

## ZOH response at {f} kHz, {s} kHz?

::: misses
- [any] $20\log(\sin x/x)$, $x = \pi f/f_s$.
:::
:::

::: check
given: B = 10 | 14; M = 8 | 32
answer: 6.02*B + 1.76 + 10*log(M)
unit: dB
tolerance: 0.05

## In-band SNR: {B} bits, oversampled ×{M}?

::: misses
- [any] $6.02B + 1.76 + 10\log M$.
:::
:::
:::
