Quantization
Sampling discretizes time; quantization discretizes amplitude. A 16-bit audio sample can take only 65536 values, so every sample is rounded, and the rounding errors behave like a small added noise. This lesson computes that noise, derives the famous "6 dB per bit" rule, and shows how dithering trades distortion for a little extra noise.
Uniform quantization
With bits covering the range , the step is . Rounding to the nearest level makes an error in . For busy signals the error is close to uniformly distributed, with variance
A full-scale sine (amplitude 1, power ) then has signal-to-quantization-noise ratio
import numpy as np
t = np.arange(200000) / 48000
x = 0.999 * np.sin(2 * np.pi * 997 * t) # nearly full-scale tone
def quantize(x, bits):
step = 2 / 2 ** bits
return np.clip(np.round(x / step) * step, -1, 1 - step)
for bits in (4, 8, 12, 16):
e = quantize(x, bits) - x
sqnr = 10 * np.log10(np.mean(x ** 2) / np.mean(e ** 2))
print(f"{bits:2d} bits: SQNR {sqnr:6.2f} dB rule 6.02b + 1.76 = {6.02 * bits + 1.76:6.2f} dB"
f" error variance / (step^2/12) = {np.var(e) / ((2 / 2 ** bits) ** 2 / 12):.3f}") 4 bits: SQNR 23.28 dB rule 6.02b + 1.76 = 25.84 dB error variance / (step^2/12) = 1.651
8 bits: SQNR 49.34 dB rule 6.02b + 1.76 = 49.92 dB error variance / (step^2/12) = 1.135
12 bits: SQNR 74.02 dB rule 6.02b + 1.76 = 74.00 dB error variance / (step^2/12) = 0.995
16 bits: SQNR 98.12 dB rule 6.02b + 1.76 = 98.08 dB error variance / (step^2/12) = 0.992
At 4 bits the measurement falls short of the rule: the highest level is , so the peaks of this near-full-scale sine are clipped, and with only 16 levels the error is far from uniform noise. From 8 bits on the rule fits, and each extra bit adds about 6 dB. CD audio's 16 bits give about 98 dB, more than the dynamic range of most listening rooms.
When the noise model fails: distortion
For quiet or simple signals the error is not noise-like: it repeats with the signal and becomes harmonic distortion. Adding a small random dither (about one step of noise) before rounding breaks that correlation. The result has slightly more total noise but no distortion tones.
import numpy as np
fs = 8000
t = np.arange(8000) / fs
step = 2 / 2 ** 8 # 8-bit quantizer
x = 1.3 * step * np.sin(2 * np.pi * 250 * t) # a very quiet tone: barely over one step
rng = np.random.default_rng(1)
plain = np.round(x / step) * step
tpdf = (rng.uniform(-0.5, 0.5, x.size) + rng.uniform(-0.5, 0.5, x.size)) * step # triangular dither
dithered = np.round((x + tpdf) / step) * step
for name, y in (("plain", plain), ("dithered", dithered)):
A = 2 * np.abs(np.fft.rfft(y - x)) / x.size
harmonics = [round(float(A[k * 250]) / step, 3) for k in (3, 5, 7)]
print(f"{name:9s} error power {np.mean((y - x) ** 2) / step ** 2:.3f} step^2 error tones at 750/1250/1750 Hz (in steps): {harmonics}")plain error power 0.070 step^2 error tones at 750/1250/1750 Hz (in steps): [0.042, 0.205, 0.189]
dithered error power 0.252 step^2 error tones at 750/1250/1750 Hz (in steps): [0.009, 0.007, 0.006]
Without dither the error is concentrated in odd harmonics of the tone, audible as a buzz. With triangular dither those tones vanish into a flat noise floor about 3.6 times as strong in power.
In software
- Audio mastering applies dither (often noise-shaped) when reducing 24-bit masters to 16 bits. Image tools use error diffusion (Floyd-Steinberg) when reducing colour depth: the same idea in two dimensions.
- Neural-network quantization to int8 or 4-bit formats uses the same step-size analysis to predict accuracy loss; see Floating-Point Formats.
Exercises
Exercise 1: Bits for a target SQNR
How many bits are needed for at least 90 dB SQNR with a full-scale sine?
Answer
15 bits.
Solution
gives , so 15 bits (about 92.1 dB).
Exercise 2: Quiet signals lose bits
A recording peaks at dB below full scale. How many effective bits of a 16-bit converter does it use?
Answer
About 12.
Solution
Each 6 dB of unused headroom wastes about one bit: , leaving about 12 bits of resolution for the signal.
Exercise 3: Error variance (code)
Quantize 100000 seeded uniform values in to 6 bits and compare the error variance with .
Answer
The ratio is close to 1.
Solution
import numpy as np
rng = np.random.default_rng(5)
x = rng.uniform(-1, 1, 100000)
step = 2 / 2 ** 6
e = np.round(x / step) * step - x
print(round(float(np.var(e) / (step ** 2 / 12)), 3))1.002
Test yourself
Each additional bit of quantization improves SQNR by about…
- 1 dB
- 3 dB
- 6 dB
- 10 dB
The variance of uniform quantization error with step is…
Dither is added before quantization to…
- reduce total noise
- turn signal-dependent distortion into benign noise
- increase the sampling rate
- compress the signal