Slices and rounding
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.
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.
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.
From levels to 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.
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.
What rounding costs
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.
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.
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.
In code
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.
The working
# step 1/9 · levels 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
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.