|
Quantization in Signal Processing - Interactive Engineering Tutorial
This note provides an interactive laboratory for exploring Quantization in signal processing. Quantization is the process of mapping a continuous range of analog amplitude values to a finite set of discrete levels, a fundamental step in Analog-to-Digital Conversion (ADC).
The simulation is organized into two tabs:
- Basic Tab: Explore quantization with synthetic signals (sine, sawtooth, triangle, square, composite, speech-like). Adjust bit depth, amplitude, frequency, and companding to see how quantization affects ideal waveforms. This tab is best for understanding the theory.
- Application Tab: Apply quantization to real audio recordings from the audio dataset. Load speech files (left, right, stop), quantize them at different bit depths and companding modes, and play back both original and quantized audio to hear the difference. This tab demonstrates the practical impact of quantization on real-world signals.
Both tabs provide four synchronized charts (original signal, quantized output, error waveform, error histogram) and real-time statistics (SQNR, MSE, theoretical SNR), enabling hands-on exploration of the trade-offs in digital signal representation.
Math behind the Simulation
1. Uniform Quantization (Mid-Rise)
A uniform quantizer divides the input amplitude range [−1, +1] into L = 2n equal intervals, where n is the number of bits. The step size (quantization interval) is:
-
Step Size: Δ = 2 / L = 2 / 2n. For example, with 3 bits: L = 8, Δ = 0.25.
-
Decision Boundaries: Located at −1, −1 + Δ, −1 + 2Δ, …, +1. The input is mapped to the nearest reconstruction level.
-
Reconstruction Levels: The output for each bin is the mid-point of that bin: x̂k = −1 + (k + 0.5) × Δ, for k = 0, 1, …, L − 1.
-
Quantization Error: e(t) = x(t) − x̂(t). For a uniform quantizer, the error is bounded: |e| ≤ Δ/2.
2. Signal-to-Quantization-Noise Ratio (SQNR)
SQNR measures the quality of the quantized representation:
-
Definition: SQNR = 10 · log10(Psignal / Pnoise), where Psignal is the mean signal power and Pnoise is the mean squared quantization error (MSE).
-
Theoretical SQNR (Sine Wave): For a full-scale sinusoidal input with uniform quantization, the well-known formula is: SQNR ≈ 6.02n + 1.76 dB. This means each additional bit adds approximately 6 dB of dynamic range.
-
6 dB per Bit Rule: Doubling the number of quantization levels (adding 1 bit) reduces the quantization noise power by a factor of 4 (= 6 dB). This is the fundamental trade-off between bit rate and signal quality in ADC design.
3. Companding (Non-Uniform Quantization)
Companding (COMpressing + exPANDING) applies a non-linear transformation before uniform quantization, then inverts it after. This allocates more quantization levels to small amplitudes, improving SNR for low-level signals (e.g., speech, audio):
-
μ-Law Companding (ITU-T G.711, North America/Japan):
F(x) = sgn(x) · ln(1 + μ|x|) / ln(1 + μ), where μ = 255 (standard). This logarithmic compression gives more resolution to quiet sounds. Used in T1 telephone systems (8-bit, μ-law PCM at 8 kHz = 64 kbps per channel).
-
A-Law Companding (ITU-T G.711, Europe):
F(x) = sgn(x) · { A|x| / (1 + ln A) if |x| < 1/A ; (1 + ln(A|x|)) / (1 + ln A) if |x| ≥ 1/A }, where A = 87.6 (standard). Similar logarithmic behavior to μ-law but with a linear segment near zero for reduced idle-channel noise.
-
Improvement: Companding can provide 20–30 dB improvement in SNR for small signals compared to uniform quantization at the same bit depth, at the cost of slightly worse performance for large-amplitude signals.
4. Mean Squared Error (MSE) and Peak Error
Two complementary measures of quantization distortion:
-
MSE: MSE = (1/N) Σ [x(i) − x̂(i)]2. Average distortion power. For uniform quantization of a uniformly distributed signal, MSE = Δ2/12.
-
Peak Error: max |e(i)|. Worst-case instantaneous distortion. For uniform quantization: peak error ≤ Δ/2.
Usage Example — Basic Tab
Follow these steps to explore quantization with synthetic signals:
-
Choose a Signal: Select a signal type from the "Signal Type" dropdown. Start with "Sine Wave" for the simplest case, then try more complex waveforms to see how quantization affects different signal characteristics.
-
Adjust Bit Depth: Drag the "Bit Depth" slider from 1 to 16 bits. Observe how increasing bits improves the staircase approximation, reduces the error, and increases SQNR by ~6 dB per bit. At 1 bit, you only get 2 levels (essentially a comparator). At 8 bits, you get 256 levels (CD-quality for telephony). At 16 bits, you get 65,536 levels (CD audio quality).
-
Observe the Error: The third chart shows the quantization error e(t) = x(t) − x̂(t). Notice how the error is bounded by ±Δ/2 for uniform quantization. The error histogram (fourth chart) shows the distribution of error values — for a sine wave with many levels, it approaches a uniform distribution.
-
Compare Uniform vs. Companded: Switch the "Companding" dropdown from "Uniform" to "μ-Law" or "A-Law". Notice how the quantization levels become non-uniformly spaced: more levels are allocated to small amplitudes and fewer to large amplitudes. This dramatically improves SNR for quiet signals.
-
Experiment with Amplitude: Reduce the amplitude slider and observe how the SQNR degrades for uniform quantization (fewer levels are utilized). Then enable companding and notice that SQNR remains more consistent across amplitude levels — this is the key advantage of companding.
Usage Example — Application Tab
Follow these steps to hear and see quantization on real audio:
-
Select Audio: Click the "Application" tab. Choose a word category (Left, Right, Stop) and a specific recording. The audio file is automatically loaded, quantized, and displayed.
-
Adjust Bit Depth: Drag the slider to change the number of quantization bits. At 8 bits (256 levels), the audio sounds nearly identical to the original. At 4 bits (16 levels), you hear noticeable quantization noise. At 2 bits (4 levels), the signal is severely distorted.
-
Listen to the Difference: Click "▶ Original" to play the unmodified audio, then "▶ Quantized" to hear the quantized version. Compare the two to hear exactly what quantization noise sounds like.
-
Apply Companding: Switch to μ-Law or A-Law and reduce the bit depth to 3–4 bits. Compare the audio quality against uniform quantization at the same bit depth. Companding preserves speech intelligibility at lower bit rates.
-
Observe the Charts: The four charts show the same waveform/error/histogram analysis as the Basic tab, but applied to the real audio signal. The SQNR and MSE values reflect actual speech quality metrics.
Tips:
-
6 dB Rule: Watch the SQNR value as you increase bit depth by 1. For a sine wave, it should increase by approximately 6 dB each time (the exact formula is 6.02n + 1.76 dB). Compare the measured SQNR to the theoretical value shown in the info box.
-
Clipping: With the "Square" or "Sawtooth" signals, the peaks may exceed the quantizer range at high amplitude. This causes clipping distortion, visible as flat regions in the quantized signal and large spikes in the error plot.
-
Companding Effect: With μ-Law at low bit depths (2–4 bits), compare the quantized waveform against uniform. The companded version preserves the shape of quiet signal portions much better, while the loud portions have slightly more distortion.
-
Real Audio Comparison: In the Application tab, try reducing bit depth to 2–3 bits with and without μ-Law. The difference in audio clarity is dramatic — companding was invented precisely for this telephone-quality speech encoding scenario.
-
Complex Signals: Try the "Composite" or "Speech-Like" signals to see how multi-frequency content is affected by quantization. The error pattern becomes more complex, and the histogram may no longer be uniform.
Visualizations (both tabs)
Both tabs provide four synchronized charts that update instantly when any parameter changes:
-
1. Original Signal: The input signal (cyan line on dark grid). In the Basic tab, this is a synthetic waveform normalized to [−1, +1]. In the Application tab, this is the decoded audio waveform from a real WAV file.
-
2. Quantized Signal: Shows the original signal (faded cyan) overlaid with the quantized output (orange). In the Basic tab, a staircase effect and dashed quantization level lines are visible. In the Application tab, the waveform is drawn as a continuous line since individual samples are too dense. The chart title dynamically shows the number of quantization levels (2n).
-
3. Quantization Error: The difference between original and quantized signals, with a filled area visualization. For uniform quantization, this error is bounded by ±Δ/2. The vertical scale auto-adjusts to show detail. In the Application tab, the error reveals where speech content causes the largest distortion.
-
4. Error Distribution Histogram: A histogram of all error samples, showing the statistical distribution of quantization noise. For speech signals with companding, the distribution is noticeably different from the uniform distribution seen with synthetic sine waves.
Controls — Basic Tab
-
Signal Type Dropdown: Choose the input waveform (Sine, Composite, Sawtooth, Triangle, Square, Speech-Like).
-
Frequency Slider: Number of cycles visible in the display window (1–20).
-
Amplitude Slider: Signal amplitude from 0.05 to 1.0. Reducing amplitude demonstrates SQNR degradation.
-
Bit Depth Slider: Quantization bits from 1 to 16 (2 to 65,536 levels). Each additional bit adds ~6 dB of SQNR.
-
Companding Dropdown: Uniform (None), μ-Law (ITU-T G.711), or A-Law (ITU-T G.711).
Controls — Application Tab
-
Word Dropdown: Select a word category (Left, Right, Stop) from the audio dataset.
-
File Dropdown: Select a specific audio recording. The file is automatically loaded and quantized.
-
Bit Depth Slider: Same as Basic tab (1–16 bits). Changing bit depth immediately re-quantizes and redraws.
-
Companding Dropdown: Same options as Basic tab. Changing companding immediately re-processes the audio.
-
▶ Original Button: Play the original (unquantized) audio through speakers.
-
▶ Quantized Button: Play the quantized audio through speakers. Compare with the original to hear quantization noise.
-
■ Stop Button: Stop audio playback.
-
Status Display: Shows current state (Ready, Loading, Playing Original/Quantized, Stopped).
Statistics Info Box (both tabs)
- Levels: Total number of quantization levels = 2n.
- Step Δ: Quantization step size = 2 / Levels. Smaller is better.
- SQNR: Measured Signal-to-Quantization-Noise Ratio in dB. Computed from the actual signal and error data. Color-coded: green (> 40 dB), orange (< 15 dB).
- SNRtheory: Theoretical SQNR for a full-scale sine wave = 6.02n + 1.76 dB. Compare with measured SQNR to understand the approximation.
- MSE: Mean Squared Error of the quantization noise. Decreases by factor of 4 for each additional bit.
- Peak Err: Maximum absolute error sample. For uniform quantization, should be ≤ Δ/2.
Audio Data Format
The Application tab loads audio files from the Audio/audio_data/ directory, which is shared with the Audio Signal Processing Lab. The files are organized by word (left, right, stop) and listed in manifest.json. Audio files are WAV format (16-bit PCM, typically 16 kHz sample rate). The Web Audio API decodes the audio and provides floating-point sample data for quantization processing.
Note: The Application tab requires the page to be served from a web server (not opened as a local file) due to browser CORS restrictions when fetching audio files.
|
|