Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lloyd-Max Quantizer for Gaussian Sources

This project implements and evaluates a scalar Lloyd-Max quantizer for samples drawn from a zero-mean Gaussian source. It iteratively learns decision boundaries and reproduction levels that reduce empirical mean squared quantization error.

Problem definition

For a bit depth b, the quantizer uses L = 2^b reproduction levels. Each input sample is assigned to one decision region and replaced by that region's centroid. The implementation measures distortion as

D = mean((x - Q(x))²)

and reports the signal-to-quantization-noise ratio as

SQNR = 10 log10(σ² / D).

Lloyd-Max algorithm

The implementation:

  1. generates a deterministic Gaussian sample set;
  2. initializes internal boundaries from sample quantiles;
  3. assigns samples to regions;
  4. updates each reproduction level to the mean of its assigned samples;
  5. moves internal boundaries to the midpoint of adjacent reproduction levels; and
  6. repeats until the distortion tolerance is reached or the iteration limit is exhausted.

Empty regions are handled explicitly by selecting a midpoint or a finite offset from the available boundary.

Experimental setup

The submitted experiment uses σ = 1, 300,000 samples, random seed 12345, quantile initialization, a visualization/update range of ±4σ, distortion tolerance 1e-9, and at most 200 iterations. The reproducibility script evaluates bit depths 2 through 5 with those parameters.

Results

The checked-in metrics were reproduced from the source code on the same deterministic sample set.

Bits Levels Iterations MSE SQNR (dB)
2 4 32 0.1170605606 9.315894
3 8 65 0.0342777468 14.649877
4 16 163 0.0093695110 20.282831
5 32 200 0.0025255458 25.976448

For this fixed sample set and configuration, increasing the bit depth reduced empirical distortion and increased SQNR. The 5-bit run reached the configured 200-iteration limit, so its values should be interpreted as the best state reached within that budget rather than proof of convergence.

MSE and SQNR across bit depths

Quantizer visualizations

2-bit quantizer

2-bit Lloyd-Max quantizer

5-bit quantizer

5-bit Lloyd-Max quantizer

How to run

Python 3.10 or newer is recommended.

python -m venv .venv
# Windows: .venv\Scripts\activate
# macOS/Linux: source .venv/bin/activate
python -m pip install -r requirements.txt
python generate_results.py

Running python quantization.py executes the original 5-bit experiment and opens an interactive visualization. Run the checks with:

python -m unittest discover -s tests

Project structure

.
├── quantization.py          # Canonical submitted implementation
├── generate_results.py      # Reproducible metrics and figure generation
├── requirements.txt
├── results/metrics.csv      # Deterministic results for 2–5 bits
├── docs/
│   ├── images/              # Selected generated visualizations
│   └── report-excerpt.pdf   # Privacy-sanitized report excerpt
└── tests/test_quantization.py

Limitations

  • The optimization is based on a finite Monte Carlo sample rather than analytical integration of the Gaussian density.
  • Results are specific to the documented seed, sample count, initialization, clipping parameter, and iteration budget.
  • The source contains a level-spacing tolerance check; the distortion tolerance is the effective convergence condition in the reproduced runs.
  • The project evaluates one zero-mean Gaussian source and does not implement a separate fixed uniform-quantizer benchmark.
  • The sanitized report excerpt excludes the original cover and terminal screenshots because they contained personal or local-machine information.

About

Lloyd-Max scalar quantization implementation with distortion, MSE, SQNR analysis, tests, and visualized experiments.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages