Web Simulation 

 

 

 

 

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 (n = number of bits). The step size, reconstruction levels, and bounded error are:

Δ = 2 / L = 2 / 2n
k = −1 + (k + 0.5) × Δ,  k = 0 ... L−1
e(t) = x(t) − x̂(t),  |e| ≤ Δ/2

For example, with 3 bits: L = 8, Δ = 0.25. Decision boundaries sit at −1, −1+Δ, …, +1, and each input maps to the nearest reconstruction level (bin mid-point).

2. Signal-to-Quantization-Noise Ratio (SQNR)

SQNR is the ratio of signal power to quantization-noise power; for a full-scale sinusoid with uniform quantization it has the famous closed form:

SQNR = 10 log10(Psignal / Pnoise)  →   SQNR ≈ 6.02n + 1.76 dB

So each added bit gives ~6 dB more dynamic range: doubling the levels cuts the noise power by 4× (the 6 dB per bit rule) — the core bit-rate vs. quality trade-off in ADC design.

3. Companding (Non-Uniform Quantization)

Companding (COMpressing + exPANDING) applies a non-linear transform before uniform quantization, then inverts it after, allocating more levels to small amplitudes (better SNR for quiet speech/audio):

μ-law (G.711, NA/Japan, μ=255): F(x) = sgn(x) · ln(1 + μ|x|) / ln(1 + μ)
A-law (G.711, Europe, A=87.6): F(x) = sgn(x) · A|x|/(1+lnA) for |x|<1/A,  (1+ln(A|x|))/(1+lnA) for |x|≥1/A

μ-law is used in T1 telephony (8-bit PCM at 8 kHz = 64 kbps/channel); A-law adds a linear segment near zero for lower idle-channel noise. Companding can improve small-signal SNR by 20–30 dB at the same bit depth, at slightly worse large-signal performance.

4. Mean Squared Error (MSE) and Peak Error

Two complementary distortion measures — average power and worst case:

MSE = (1/N) Σ [x(i) − x̂(i)]2 (≈ Δ2/12),   peak error = max|e(i)| ≤ Δ/2

Simulation

The interactive simulator is below. Use the controls to explore the concepts described above.

 

Usage Example — Basic Tab

Follow these steps to explore quantization with synthetic signals:

  1. 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.
  2. 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).
  3. 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.
  4. 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.
  5. 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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.

Limitations

  • Quantization only, ideal sampling. The tool isolates amplitude quantization; it assumes perfect, alias-free sampling and does not model the anti-alias filter, sample-and-hold, or sample-rate effects of a real ADC.
  • Mid-rise uniform + G.711 companding. Other quantizer designs (mid-tread, non-uniform Lloyd-Max optimal, dithered, noise-shaping/sigma-delta) are not implemented.
  • Memoryless, scalar. Each sample is quantized independently; predictive coding (DPCM/ADPCM), vector quantization, and perceptual audio coding are out of scope.
  • 6.02n + 1.76 dB is sinusoid-specific. The theoretical SQNR assumes a full-scale sine with uniform error statistics; real signals, clipping, and low input levels deviate from it.
  • No real ADC non-idealities. Differential/integral non-linearity (DNL/INL), missing codes, offset/gain error, and thermal/jitter noise of physical converters are not represented.
  • Limited audio set. The Application tab uses a small bundled speech dataset at fixed sample rates; it is a demonstration, not a general audio-quality benchmark.