---
title: Quantization and dither
heading: Rounding a signal to a ladder of levels, the noise that leaves, and why adding noise can make it better
eyebrow: C9 · Lesson 22 · Quantization and dither
xp: 40
---

::: hook
Lesson 21 sampled a signal in time and showed when nothing is lost. A converter also rounds each sample to one of $2^N$ levels, and that loses something every time. For a busy signal, the rounding error behaves like a small, flat noise, and the familiar rule of 6 dB per bit follows from it. For a quiet or slowly changing signal it doesn't: the error locks to the signal and becomes distortion, and a signal smaller than one step vanishes entirely. The cure sounds wrong: add a little random noise before rounding. That's **dither**, and it's why audio, instruments and image processing deliberately add noise.
:::

::: play
- A sine wave (dashed) and its quantized version on a ladder of $2^N$ levels across ±1.
- At 3 bits the staircase is coarse and its error is clearly tied to the waveform: it repeats every cycle, which means harmonics, not noise.
- Raise the bits and the steps shrink by half each time. Lower the amplitude below one step and the output stays flat at zero.
:::

```sim plot
view: graph
x: t = 0..2
y: v = -1.1..1.1
slider: N = 2..8 step 1 = 3
slider: A = 0.05..1 step 0.01 = 0.9
plot: A*sin(2*pi*t) | dashed | label: input
plot: round(A*sin(2*pi*t)*2^(N - 1))/2^(N - 1) | samples: 2000 | label: quantized
readout: step size = 2/2^N | digits: 3
readout: ideal SNR for a full-scale sine, dB = 6.02*N + 1.76
```

::: predict
## A 12-bit converter's full-scale sine gives the ideal signal-to-quantization-noise ratio. If the sine is reduced to 1/100 of full scale, about what SNR remains, ideally?

- [ ] 74 dB
- [x] 34 dB
- [ ] 54 dB
- [ ] It's unchanged, since the bits are the same

::: reveal
The noise stays at $\Delta^2/12$ while the signal power falls by $100^2$, 40 dB: 74 − 40 = 34 dB. The SNR formula assumes a full-scale input, and every factor of two below full scale costs a bit's worth, 6 dB. That's why converters are preceded by gain that brings signals near full scale.
:::
:::

::: nameit
## The quantizer

An $N$-bit converter spanning a range $R$ has step $\Delta = R/2^N$ and rounds each sample to the nearest level. The error lies between $-\Delta/2$ and $\Delta/2$.

## Quantization as noise

If the signal crosses many levels in an irregular way, the error is close to uniformly distributed and uncorrelated with the signal, with variance

$$\sigma_q^2 = \frac{\Delta^2}{12}$$

spread evenly from 0 to $f_s/2$. A full-scale sine has power $R^2/8$, so its signal-to-quantization-noise ratio is

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

A real converter adds its own noise and distortion; its measured SINAD defines an **effective number of bits**, $\text{ENOB} = (\text{SINAD} - 1.76)/6.02$.

## When the noise model fails

For a small, slow or periodic signal the error repeats with the signal: it's distortion, concentrated in harmonics. A signal smaller than $\Delta/2$ around a level is rounded to that level every time and disappears.

## Dither

Adding random noise before quantizing, about one step peak to peak or more, makes the error independent of the signal. The rounding then varies from sample to sample, the harmonics turn into a smooth noise floor, and the average of many dithered samples recovers values finer than one step. Triangular dither (the sum of two independent uniform noises of ±½ step) removes the dependence of the error's mean and variance on the signal entirely, at the cost of a little more total noise.

## Oversampling

Quantization noise spreads over 0 to $f_s/2$. Sampling $M$ times faster than the signal needs and filtering away everything above the signal band keeps only $1/M$ of the noise: $10\log_{10}M$ dB better, half a bit per doubling. With dither (or the signal's own noise) to decorrelate the error, averaging buys real resolution.
:::

::: example
## A 12-bit ADC spans 0 to 3.3 V. Find its step, the RMS quantization noise, and the ideal SQNR for a full-scale sine.

::: solution
1. **Step.**
   $$\begin{aligned} \Delta &= 3.3/4096\ \text{V} \\ &= 0.806\ \text{mV} \end{aligned}$$
2. **RMS noise.**
   $$\Delta/\sqrt{12} = 0.233\ \text{mV}$$
3. **Ideal SQNR.**
   $$\begin{aligned} \text{SQNR} &= 6.02 \times 12 + 1.76 \\ &= 74.0\ \text{dB} \end{aligned}$$
4. **Compare a real part.** If the datasheet quotes a SINAD of 62 dB, the ENOB is
   $$\begin{aligned} \text{ENOB} &= (62 - 1.76)/6.02 \\ &= 10.0\ \text{bits} \end{aligned}$$
   Two bits are lost to the converter's own noise and distortion.

**Answer:** a step of 0.806 mV, 0.233 mV RMS of quantization noise, and an ideal SQNR of 74.0 dB.
:::
:::

::: example
## A sensor's output is a slow sine of amplitude 0.3 Δ around a level. Without dither, what does the converter report? With ±1 Δ of uniform dither and averaging 1000 samples per reading, how fine is the result?

::: solution
1. **Without dither.** The signal never reaches a rounding threshold half a step away, so every sample reads the same level: the signal is lost completely.
2. **With dither.** Each sample's rounding depends on the signal plus a random offset, and the average of many readings follows the signal.
3. **Noise per sample.** The dither's $2\Delta/\sqrt{12} = \Delta/\sqrt3$ rms and the rounding's $\Delta/\sqrt{12}$ together make
   $$\begin{aligned} \sigma &= \Delta\sqrt{1/3 + 1/12} \\ &= \Delta\sqrt{5/12} \\ &= 0.65\Delta \end{aligned}$$
4. **Average 1000 samples.** Averaging reduces that by $\sqrt{1000}$:
   $$0.65\Delta/\sqrt{1000} \approx 0.02\Delta$$
   So the 0.3 Δ sine is measured with a resolution about 50 times finer than one step.

**Answer:** without dither the converter reports one constant level and the signal is lost; with dither and averaging, the readings resolve about 0.02 Δ, some 50 times finer than one step.
:::
:::

::: example
## A 10-bit ADC samples an audio-band signal (0 to 20 kHz) at 640 kHz instead of 40 kHz, then filters digitally to 20 kHz. How much does the in-band SQNR improve, and what's the effective resolution?

::: solution
1. **Oversampling ratio.**
   $$640/40 = 16$$
2. **Noise reduction.** The in-band noise falls by two bits' worth:
   $$10\log_{10}16 = 12.0\ \text{dB}$$
3. **New SQNR.**
   $$62.0 + 12.0 = 74.0\ \text{dB}$$
   as good as an ideal 12-bit converter at the plain rate.
4. **Going further.** Getting much more needs noise shaping, as in the delta-sigma converters of the logic and DSP courses.

**Answer:** the in-band SQNR improves by 12.0 dB, to 74.0 dB, the resolution of an ideal 12-bit converter.
:::
:::

::: check
given: R = 2 | 3.3 | 5 | 10; N = 8 | 10 | 12 | 16
answer: R/2^N*1e3
unit: mV
tolerance: 0.5%

## An {N}-bit converter spans {R} V. What's its step size?

::: right
Right. $R/2^N$ = {answer}.
:::

::: misses
- [R/N*1e3] There are $2^N$ levels, not $N$.
- [any] Not yet. $\Delta = R/2^N$.
:::
:::

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

## What's the RMS quantization noise of an {N}-bit converter spanning {R} V?

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

::: misses
- [R/2^N/2*1e6] That's the largest error; the RMS of a uniform error is $\Delta/\sqrt{12}$.
- [R/2^N*1e6] That's one step. Divide by $\sqrt{12}$.
- [any] Not yet. The error is uniform over one step: RMS $\Delta/\sqrt{12}$.
:::
:::

::: check
given: N = 8 | 12 | 16 | 24; a = 0.5 | 0.1 | 0.01
answer: 6.02*N + 1.76 + 20*log(a)
tolerance: 0.5%

## An ideal {N}-bit converter digitizes a sine whose amplitude is {a} of full scale. What's its SQNR in dB?

::: right
Right. $6.02N + 1.76 + 20\log_{10}a$ = {answer} dB.
:::

::: misses
- [6.02*N + 1.76] That assumes a full-scale sine; the smaller signal loses $20\log_{10}a$.
- [6.02*N + 1.76 + 10*log(a)] Amplitude ratios use 20 log.
- [any] Not yet. Full-scale SQNR minus the signal's drop in dB.
:::
:::

::: check
given: S = 50 | 62 | 74 | 90
answer: (S - 1.76)/6.02
tolerance: 0.5%

## A converter measures a SINAD of {S} dB. What's its effective number of bits?

::: right
Right. $(\text{SINAD} - 1.76)/6.02$ = {answer}.
:::

::: misses
- [S/6.02] Subtract the 1.76 dB first.
- [any] Not yet. Invert $6.02N + 1.76$.
:::
:::

::: check
given: M = 4 | 16 | 64 | 256
answer: 10*log(M)/6.02
tolerance: 0.5%

## A converter oversamples by {M} and filters back to the signal band. Without noise shaping, how many bits of resolution does it gain?

::: right
Right. $10\log_{10}M$ dB, at 6.02 dB per bit: {answer}.
:::

::: misses
- [20*log(M)/6.02] Noise power falls as $1/M$: use 10 log.
- [log(M)/log(2)] That would be a bit per doubling; plain oversampling gains half a bit.
- [any] Not yet. $10\log_{10}M/6.02$.
:::
:::

::: check
kind: choice

## Why does adding dither before a quantizer reduce distortion, even though it increases the total noise?

- [x] It makes the rounding error random and independent of the signal, so harmonics that repeat with the signal become a smooth noise floor
- [ ] It cancels the quantization error exactly
- [ ] It raises the signal above the quantizer's dead zone permanently
- [ ] It reduces the step size

::: right
Right. Without dither the error is a deterministic function of the signal, which for a periodic signal means harmonics. Dither randomizes it, trading spectral lines for noise, which averaging can then reduce.
:::

::: misses
- [2] It adds noise; it doesn't cancel the error.
- [3] Dither is random, so its effect averages out; it doesn't shift the signal.
- [4] The step size is fixed by the converter.
:::
:::

::: check
kind: python
given: A = 0.3 | 0.7 | 2.5; seed = 1 | 2

## Quantize a sine with and without dither. With step 1 (so values are in steps), take `x = A*sin(2*pi*7*n/1024)` for `n = 0..1023` with `A` = {A}. Without dither, `q0 = round(x)`. With triangular dither, `q1 = round(x + d)` with `d = u1 + u2 - 1` and `u1, u2` from `numpy.random.default_rng(seed).random((2, 1024))`. Using an FFT, set `h3_0` and `h3_1`: the magnitudes of bin 21 (the third harmonic) divided by bin 7 for each, and `fund_1`, bin 7's magnitude for the dithered version divided by bin 7 of `x` itself.

```python starter
import numpy as np
h3_0 = h3_1 = 0.0
fund_1 = 1.0
```

```python solution
import numpy as np
n = np.arange(1024)
x = A * np.sin(2 * np.pi * 7 * n / 1024)
q0 = np.round(x)
u = np.random.default_rng(seed).random((2, 1024))
q1 = np.round(x + u[0] + u[1] - 1)
X, Q0, Q1 = np.fft.rfft(x), np.fft.rfft(q0), np.fft.rfft(q1)
h3_0 = abs(Q0[21]) / max(abs(Q0[7]), 1e-30)
h3_1 = abs(Q1[21]) / abs(Q1[7])
fund_1 = abs(Q1[7]) / abs(X[7])
```

```python tests
import numpy as np
n = np.arange(1024)
x = A * np.sin(2 * np.pi * 7 * n / 1024)
q0 = np.round(x)
u = np.random.default_rng(seed).random((2, 1024))
q1 = np.round(x + u[0] + u[1] - 1)
Q0, Q1, X = np.fft.rfft(q0), np.fft.rfft(q1), np.fft.rfft(x)
r0 = abs(Q0[21]) / max(abs(Q0[7]), 1e-30)
assert abs(h3_0 - r0) < 1e-9, "h3_0: third-harmonic bin over the fundamental bin, undithered."
assert abs(h3_1 - abs(Q1[21]) / abs(Q1[7])) < 1e-9, "h3_1: the same with dither."
assert abs(fund_1 - abs(Q1[7]) / abs(X[7])) < 1e-9, "fund_1: dithered fundamental over the true one."
assert 0.7 < fund_1 < 1.3, "With dither the fundamental survives, even below one step."
```

::: right
Right. At 0.3 steps the undithered output is all zeros (no fundamental at all), and at 0.7 or 2.5 steps it has a strong third harmonic. With dither, the fundamental comes through at about its true size and the harmonic drops into the noise.
:::
:::

::: explain
## A digital audio recording fades out slowly at the end of a song. Without dither, the last seconds sound harsh and buzzy before cutting to silence; with dither, they fade smoothly into a faint hiss. Explain both.

::: model
As the music fades, its amplitude falls toward a few quantization steps. Without dither, the rounding error is a fixed function of the waveform, so for a quiet, nearly periodic signal it repeats with the signal and becomes harmonic distortion, which sounds harsh and buzzy, and once the signal falls below half a step around zero it rounds to zero and stops abruptly. With dither, a small random noise is added before rounding, so the error becomes independent of the music: the distortion turns into a steady, low noise floor, and even signals smaller than one step still affect the rounding probabilities, so they remain audible inside the hiss and fade smoothly. Our ears tolerate a little steady noise much better than distortion that follows the music.
:::
:::

::: practice

::: check
given: N = 6 | 14 | 20
answer: 6.02*N + 1.76
tolerance: 0.5%

## What's the ideal SQNR of an {N}-bit converter for a full-scale sine?

::: misses
- [6.02*N] Add the 1.76 dB of a sine's power over a uniform error's.
- [any] $6.02N + 1.76$ dB.
:::
:::

::: check
given: R = 1 | 4; N = 10 | 16
answer: R/2^N/sqrt(12)*1e6
unit: µV
tolerance: 0.5%

## What's the RMS quantization noise of an {N}-bit converter spanning {R} V?

::: misses
- [R/2^N*1e6] Divide the step by $\sqrt{12}$.
- [any] $\Delta/\sqrt{12}$.
:::
:::
:::
