Lesson 3 of 15
Quantization
Quantization
After sampling, the continuous amplitude values must be represented by a finite set of discrete levels — this is quantization. The number of bits determines how many levels are available:
Uniform Quantization
For a signal normalized to , the step size is:
The quantized value maps input to the midpoint of its quantization bin:
Values outside are clamped.
Quantization SNR
The theoretical signal-to-noise ratio for -bit quantization is:
Each additional bit adds roughly 6 dB of dynamic range. A 16-bit audio CD achieves dB SNR.
Example
With bits ( levels, ):
- Input bin index , clamped to ... Actually bin , midpoint
Your Task
Implement:
quantize(x, n_bits)— clamp to , quantize to levels, return midpoint of binquantization_snr_db(n_bits)— returns
import math
def quantize(x, n_bits):
x = max(-1.0, min(1.0, x))
levels = 2 ** n_bits
step = 2.0 / levels
q = math.floor((x + 1.0) / step)
q = min(q, levels - 1)
return (q + 0.5) * step - 1.0
def quantization_snr_db(n_bits):
return 6.02 * n_bits + 1.76
print(quantize(0.0, 2)) # 0.25
print(round(quantization_snr_db(8), 4)) # 49.92Python runtime loading...
Loading...
Click "Run" to execute your code.