Quantization and bit depth
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
Sampling picks the moments; quantization picks the values. Follow one tone through a 3-bit converter: each sample falls into one of 8 slices 4 mV tall, is rounded to the slice's middle and stored as a 3-bit number. You will see the smooth wave turn into a staircase, measure the rounding error (never more than half a step), and learn why each extra bit halves that error, about 6 dB less noise. It builds on the lessons "Sampling: taking snapshots" and "Binary and Boolean logic".
A converter has a fixed set of levels
Sampling gave exact values such as 3.39 mV. A converter can only store a fixed set of numbers. A 3-bit converter splits −16 to +16 mV into 2³ = 8 equal slices. Each slice is Δ = 32 / 8 = 4 mV tall.
full-scale range −16 .. +16 mV (32 mV) bits N = 3 levels 2^N = 2 × 2 × 2 = 8 step (1 LSB) Δ = 32 mV / 8 = 4 mV slice covers stands for 7 +12 .. +16 mV +14 mV 6 +8 .. +12 mV +10 mV 5 +4 .. +8 mV +6 mV 4 0 .. +4 mV +2 mV 3 −4 .. 0 mV −2 mV ... 0 −16 .. −12 mV −14 mV
اقرأ الدرس خطوة بخطوة
A converter has a fixed set of levels
Sampling gave exact values such as 3.39 mV. A converter can only store a fixed set of numbers. A 3-bit converter splits −16 to +16 mV into 2³ = 8 equal slices. Each slice is Δ = 32 / 8 = 4 mV tall.
full-scale range −16 .. +16 mV (32 mV) bits N = 3 levels 2^N = 2 × 2 × 2 = 8 step (1 LSB) Δ = 32 mV / 8 = 4 mV slice covers stands for 7 +12 .. +16 mV +14 mV 6 +8 .. +12 mV +10 mV 5 +4 .. +8 mV +6 mV 4 0 .. +4 mV +2 mV 3 −4 .. 0 mV −2 mV ... 0 −16 .. −12 mV −14 mVA sample finds its slice
Sample x[1] = 3.39 mV arrives. The converter compares it with the ruler: it is above 0 mV and below 4 mV, so it falls in slice 4.
x[n] = 10·sin(2π·440·n / 8000) mV x[1] = 3.39 mV which slice? 0 mV ≤ 3.39 mV < 4 mV slice = floor(3.39 / 4) + 4 = floor(0.847) + 4 = 0 + 4 = 4Round to the middle of the slice
Every value in a slice is replaced by the slice's middle, so 3.39 mV becomes 2 mV. The difference, 2 − 3.39 = −1.39 mV, is the quantization error. It is never more than half a step: 2 mV.
slice 4 covers 0 .. 4 mV its middle: 2 mV q[1] = 2 mV e[1] = q[1] − x[1] = 2 − 3.39 = −1.39 mV worst case: half a step |e| ≤ Δ/2 = 2 mV (as long as x stays inside −16 .. +16 mV; beyond that the converter clips)Each level is a pattern of bits
The converter outputs the slice number, not the voltage. Slice 4 is written in binary as 100. Three bits give 2 × 2 × 2 = 8 patterns, 000 to 111: one per slice. Bits per sample is the bit depth.
slice bits slice bits 0 000 4 100 1 001 5 101 2 010 6 110 3 011 7 111 4 = 1·4 + 0·2 + 0·1 -> 100 bit depth N = bits per sample N bits -> 2^N levels (binary numbers: see the lesson on binary and Boolean logic)One code per sample
Each sample goes through the same two moves: find the slice, keep its number. The first five samples are stored as 100, 100, 101, 110, 110. The memory now holds 3 bits per sample instead of an exact value.
n x[n] mV slice bits q[n] mV 0 0.00 4 100 +2 1 3.39 4 100 +2 2 6.37 5 101 +6 3 8.61 6 110 +10 4 9.82 6 110 +10 x[0] = 0 sits exactly on a threshold: the floor puts it in slice 4 (+2 mV)The rounded wave is a staircase
Draw every rounded sample and hold it until the next one: the smooth wave becomes a staircase. Only 8 heights are allowed, so it follows the shape, but coarsely.
40 samples in 5 ms each q[n] is one of 8 heights: −14 −10 −6 −2 +2 +6 +10 +14 mV this 10 mV wave never reaches ±12 mV, so it uses only slices 1 .. 6 held between samples (drawn as steps) to make the comparison easy to seeThe error looks like noise
Plot the error e[n] = q[n] − x[n]. It jumps between −2 and +2 mV and looks random over these 40 samples. With many bits and a busy signal such as music, it sounds like a faint hiss, so it is called quantization noise.
e[n] = q[n] − x[n] largest |e| here: 2.00 mV (= Δ/2) every error lies in −2 .. +2 mV the signal = what we want the error = noise added on top q[n] = x[n] + e[n] this pure tone repeats every 200 samples (440/8000 = 11/200), so its error repeats too: heard as a buzz, not a hissOne more bit halves the error
With 4 bits there are 16 slices of 2 mV, and the error stays within ±1 mV. Each extra bit doubles the levels and halves the error: about 6 dB less noise. CDs use 16 bits: 65,536 levels.
bits levels step Δ max |e| 3 8 4 mV 2 mV 4 16 2 mV 1 mV 8 256 0.125 mV 0.0625 mV 16 65,536 0.49 µV 0.24 µV half the error -> noise power ÷ 4 10·log10(4) = 20·log10(2) = 6.02 dB per bit full-scale sine: SQNR ≈ 6.02·N + 1.76 dB 16 bits (CD): about 98 dBQuantizing in Python
With numpy, x is the array of 40 samples. np.floor finds each slice, + 4 makes the numbers start at 0, np.clip keeps them within 0 to 7, and the last line turns slices back into millivolts.
import numpy as np n = np.arange(40) x = 10*np.sin(2*np.pi*440*n/8000) bits = 3; d = 32 / 2**bits i = np.floor(x / d) + 4 i = np.clip(i, 0, 2**bits - 1) q = (i - 3.5) * d >>> i[:5] array([4., 4., 5., 6., 6.]) >>> q[:5] array([ 2., 2., 6., 10., 10.]) >>> abs(q - x).max() np.float64(2.0) (Python 3 with numpy)
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01A 4-bit converter covers −8 V to +8 V. How tall is one step Δ?
4 bits give 2⁴ = 16 levels. The range is 16 V wide, so Δ = 16 V / 16 = 1 V.
02With the lesson's 3-bit converter (slices of 4 mV from −16 mV), which level stands for a sample of 7.2 mV?
7.2 mV lies between +4 and +8 mV, which is slice floor(7.2 / 4) + 4 = 5 (binary 101). Its middle is +6 mV, so the error is 6 − 7.2 = −1.2 mV.
03Inside the range, what is the largest possible quantization error of a rounding quantizer with step Δ?
Every value is replaced by the middle of its slice, and nothing in a slice is more than half a slice from its middle.
04You go from 12 bits to 13 bits per sample over the same range. What happens?
Each bit doubles the number of levels (2^N), so the step and the worst error halve. Half the error is a quarter of the noise power: 10·log10(4) ≈ 6 dB.
05How many different values can one 16-bit CD sample take?
2¹⁶ = 65,536. CD audio stores 16 bits per sample, 44,100 samples a second per channel.
المسرد
- Quantization
- Replacing each sample's exact value with the nearest of a fixed set of levels, so it can be stored as a whole number.
- Bit depth (N)
- The number of bits stored per sample. N bits give 2^N different levels.
- Full-scale range
- The span of input voltages a converter covers, here −16 to +16 mV. Values outside it are clipped to the end levels.
- Step size Δ (1 LSB)
- The height of one slice: full-scale range / 2^N. LSB means least significant bit, the value of the smallest change in the code.
- Quantization error
- e[n] = q[n] − x[n], the difference between the stored level and the true sample; within ±Δ/2 inside the range.
- Quantization noise
- The quantization error seen as a small, noise-like signal added to the true one; with many bits and a busy signal, heard as a faint hiss.
- Mid-rise and mid-tread
- Two quantizer layouts: mid-rise has a threshold at 0 (levels ±Δ/2, ±3Δ/2 ...), mid-tread has a level at exactly 0.
- SQNR
- Signal-to-quantization-noise ratio in decibels; it rises by about 6 dB for each extra bit.
الملاحظات والمصادر
- The tone (10 mV, 440 Hz, sampled 8000 times a second) is the example from "What a signal is". The 3-bit converter over −16 to +16 mV is chosen so the levels are easy to see; real audio converters use 16 to 24 bits. Every number in the figure is computed from this model.
- This is a mid-rise quantizer: a threshold sits at 0 mV and every level is the middle of its slice. Many converters use a mid-tread quantizer instead, which has a level at exactly 0. Both keep the error within half a step.
- "About 6 dB per bit" is 20·log10(2) = 6.02 dB. The formula 6.02·N + 1.76 dB holds for a full-scale sine and treats the error as noise spread evenly over ±Δ/2.
- A. V. Oppenheim, R. W. Schafer, J. R. Buck, Discrete-Time Signal Processing, 2nd ed., §4.8 (A/D conversion and quantization error), listed on MIT OCW 6.341 readings
- MIT OpenCourseWare 6.341 Discrete-Time Signal Processing (A. V. Oppenheim)
- A. V. Oppenheim, A. S. Willsky, S. H. Nawab, Signals and Systems, 2nd ed., Pearson
- B. P. Lathi, R. A. Green, Linear Systems and Signals, Oxford University Press, 2018 (Internet Archive record)
- MIT OpenCourseWare 6.003 Signals and Systems (D. Freeman, Fall 2011)
- MIT OpenCourseWare 6.02 Digital Communication Systems (H. Balakrishnan, G. Verghese)
- Quantization (signal processing) (Wikipedia): mid-riser and mid-tread, error within ±½ LSB, about 6 dB per bit, SQNR ≈ 1.761 + 6.02·Q dB for a full-amplitude sine; periodic signals give periodic quantization noise
- Signal-to-quantization-noise ratio (Wikipedia): each added bit raises SQNR by about 20·log10(2) ≈ 6 dB
- Audio bit depth (Wikipedia): bits per sample; 16 bits = 65,536 values; 98 dB theoretical SNR on CD
- Compact Disc Digital Audio (Wikipedia): 44,100 Hz and 16 bits per sample
- NumPy documentation: numpy.floor
- NumPy documentation: numpy.clip
اختبار يتاح بعد الخطوة الأخيرة.