---
title: Sampling and quantization
heading: Turning a continuous signal into numbers by sampling it in time and rounding it in value, the Nyquist rate and the aliases that appear below it, the anti-aliasing filter, quantization step and noise, and the 6 dB per bit rule for an ADC's signal-to-noise ratio
eyebrow: D5 · Lesson 1 · Sampling and quantization
xp: 40
---

::: hook
Every digital audio recording, phone call and oscilloscope trace starts with two acts of approximation. **Sampling** keeps only the signal's values at regular instants, and **quantization** rounds each value to one of a fixed set of levels. Done right, sampling loses nothing at all: a signal with no frequencies above half the sampling rate can be rebuilt exactly. Done wrong, high frequencies masquerade as low ones, and no later processing can tell them apart. Quantization, meanwhile, always adds a little noise, and each extra bit cuts it in half.
:::

::: play
- A tone of frequency f sampled at $f_s$: the stems are the samples, the solid curve the original tone, the dashed curve the lowest-frequency tone through the same samples, which is what a reconstruction would play.
- Below $f_s/2$ they agree. Above it the samples fit a lower **alias** just as well.
- Listen to the alias: as you sweep f up past $f_s/2$, the reconstructed tone comes back down.
:::

```sim plot
view: graph
x: n = 0..32
y: v = -1.3..1.3
slider: f = 100..16000 = 1000 Hz log
slider: s = 4000..16000 = 8000 Hz
plot: cos(2*pi*f/s*n) | samples: 800 | color: ch3 | label: original tone (n = sample number)
plot: cos(2*pi*abs(f - s*round(f/s))/s*n) | samples: 800 | dashed | color: ch4 | label: lowest tone through the samples
plot: cos(2*pi*f/s*n) | stems | label: samples
readout: alias frequency, Hz = abs(f - s*round(f/s)) | digits: 4
readout: Nyquist frequency, Hz = s/2 | digits: 4
sound: cos(2*pi*abs(f - s*round(f/s))*t) | scale: 1
```

::: predict
## A 5 kHz tone is sampled 8000 times per second with no filter in front. What frequency does the digital signal contain?

- [ ] 5 kHz
- [x] 3 kHz
- [ ] 13 kHz
- [ ] Nothing: the tone is lost

::: reveal
The Nyquist frequency is 4 kHz, and a tone above it folds back by the distance it overshoots: 8 − 5 = 3 kHz. The samples of a 5 kHz cosine are exactly the samples of a 3 kHz cosine, so once sampled the two are indistinguishable. That's why an analog **anti-aliasing filter** must remove everything above 4 kHz before the ADC.
:::
:::

::: nameit
## Sampling

Sampling at rate $f_s$ keeps $x(nT)$ with $T = 1/f_s$. The **sampling theorem**: a signal with no content at or above $f_s/2$, the **Nyquist frequency**, is determined exactly by its samples. A tone at f produces the same samples as tones at $|f - kf_s|$ for every integer k, so content above $f_s/2$ appears at

$$f_a = |f - f_s\,\mathrm{round}(f/f_s)|$$

CD audio samples at 44.1 kHz to cover hearing up to 20 kHz with room for the anti-aliasing filter to roll off.

## Quantization

A B-bit converter with full-scale range FS has $2^B$ levels with step

$$\Delta = \frac{FS}{2^B}$$

and rounding adds an error spread evenly over ±Δ/2, a noise of RMS value $\Delta/\sqrt{12}$. For a full-scale sine wave,

$$\text{SQNR} = 6.02B + 1.76 \text{ dB}$$

so each extra bit buys 6 dB: 16 bits gives 98 dB.
:::

::: example
## A 12-bit ADC spans 0 to 3.3 V. What's its step, its quantization noise and its best SNR?

::: solution
1. **Step size.** Twelve bits give $2^{12} = 4096$ steps across the range:
   $$\begin{aligned} \Delta &= \frac{3.3\ \text{V}}{4096} \\ &= 0.806\ \text{mV} \end{aligned}$$
2. **Quantization noise.**
   $$\begin{aligned} \sigma_q &= \frac{0.806\ \text{mV}}{\sqrt{12}} \\ &= 0.233\ \text{mV RMS} \end{aligned}$$
3. **Best SNR.**
   $$\begin{aligned} \text{SQNR} &= 6.02 \times 12 + 1.76 \\ &= 74\ \text{dB} \end{aligned}$$

**Answer:** a step of 0.806 mV, 0.233 mV RMS of quantization noise, and a best SQNR of 74 dB.
:::
:::

::: example
## A system samples at 8 kHz. Where do tones at 7.5 kHz and 9 kHz land? How many bits give an 80 dB SQNR?

::: solution
1. **The 7.5 kHz tone.** It folds back around the 8 kHz sampling rate:
   $$|7.5 - 8| = 0.5\ \text{kHz}$$
2. **The 9 kHz tone.**
   $$|9 - 8| = 1\ \text{kHz}$$
3. **Bits for 80 dB.** Solve $6.02B + 1.76 = 80$ for B:
   $$\begin{aligned} B &= \frac{80 - 1.76}{6.02} \\ &= 13.0\ \text{bits} \end{aligned}$$
   so a 13-bit converter, in practice a 16-bit one.

**Answer:** 7.5 kHz aliases to 0.5 kHz and 9 kHz to 1 kHz, and 80 dB takes a 13-bit converter, in practice a 16-bit one.
:::
:::

::: example
## What are the best SQNRs of 16-bit and 24-bit audio converters, and which limit is likely to matter?

::: solution
1. **16-bit converter.**
   $$\begin{aligned} \text{SQNR} &= 6.02 \times 16 + 1.76 \\ &= 98.1\ \text{dB} \end{aligned}$$
2. **24-bit converter.**
   $$\begin{aligned} \text{SQNR} &= 6.02 \times 24 + 1.76 \\ &= 146.2\ \text{dB} \end{aligned}$$
3. **The limit that matters.** No analog circuit is quiet enough to use 146 dB: real 24-bit converters reach about 120 dB, limited by their analog noise rather than by quantization.

**Answer:** 98.1 dB for 16 bits and 146.2 dB for 24 bits, but a real 24-bit converter stops near 120 dB, set by its analog noise.
:::
:::

::: check
given: f = 7 | 9 | 13 | 17; s = 8 | 10
answer: abs(f - s*round(f/s))
unit: kHz
tolerance: 0.5%

## A {f} kHz tone is sampled at {s} kHz without filtering. At what frequency does it appear?

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

::: misses
- [f] It's above the Nyquist frequency, so it folds back.
- [any] Not yet. Fold the tone around multiples of the sampling rate.
:::
:::

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

## What's the best signal-to-quantization-noise ratio a {B}-bit converter can reach with 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.
- [any] Not yet. Six decibels per bit.
:::
:::

::: check
given: F = 1 | 3.3 | 5; B = 8 | 10 | 12 | 16
answer: F/2^B*1000
unit: mV
tolerance: 0.5%

## A {B}-bit ADC spans {F} V. How big is one step?

::: right
Right. $FS/2^B$ = {answer} mV.
:::

::: misses
- [any] Not yet. Full scale over the number of levels.
:::
:::

::: check
given: F = 1 | 3.3 | 5; B = 8 | 10 | 12 | 16
answer: F/2^B/sqrt(12)*1e6
unit: µV
tolerance: 0.5%

## What's the RMS quantization noise of that {B}-bit, {F} V converter?

::: right
Right. $\Delta/\sqrt{12}$ = {answer} µV.
:::

::: misses
- [F/2^B/2*1e6] The error is spread evenly over ±Δ/2: its RMS value is $\Delta/\sqrt{12}$.
- [any] Not yet. The RMS of a uniform error.
:::
:::

::: check
kind: parts
given: N = 8 | 12 | 16; V = 1 | 3.3 | 5

## An {N}-bit ADC spans 0 to {V} V.

::: part
answer: V/2^N*1000
unit: mV
tolerance: 0.5%

### What is its step?

::: misses
- [any] Range ÷ $2^N$.
:::
:::

::: part
answer: 6.02*N + 1.76
unit: dB
tolerance: 0.5%

### What is its best SQNR for a full-scale sine?

::: misses
- [any] $6.02N + 1.76$.
:::
:::
:::

::: check
kind: parts
given: fs = 8 | 44.1 | 48; f = 5 | 30 | 41

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

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

### At what frequency does it appear?

::: misses
- [any] Fold it into 0 to $f_s/2$.
:::
:::

::: part
answer: step(fs/2 - f)
tolerance: 0

### Is it below the Nyquist frequency (1 for yes)?

::: misses
- [any] Compare with $f_s/2$.
:::
:::
:::

::: explain
## Why must the anti-aliasing filter come before the ADC and not after it?

::: model
Sampling makes a tone at any frequency produce exactly the same sample values as tones at that frequency shifted by any multiple of the sampling rate, so content above half the sampling rate ends up looking identical to some frequency below it. Once the samples are taken, the information about which of those frequencies was really there is gone, and no digital filter can separate an alias from a genuine signal at the same frequency. The only way to prevent aliasing is to remove the high frequencies while the signal is still continuous, with an analog low-pass filter in front of the converter.
:::
:::

::: practice

::: check
given: f = 1.5 | 2.5 | 3.7 | 5.2 | 6.9 | 9.3 | 11.1 | 14.6 | 21.4 | 27.3; s = 4 | 6 | 8 | 10 | 16
answer: abs(f - s*round(f/s))
unit: kHz
tolerance: 0.5%

## Alias of a {f} kHz tone sampled at {s} kHz?

::: misses
- [any] Fold around multiples of $f_s$.
:::
:::

::: check
given: B = 4 | 8 | 10 | 12 | 16 | 24 | 32
answer: 6.02*B + 1.76
unit: dB
tolerance: 0.5%

## SQNR of {B} bits?

::: misses
- [any] $6.02B + 1.76$.
:::
:::
:::
