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.
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).
The implementation:
- generates a deterministic Gaussian sample set;
- initializes internal boundaries from sample quantiles;
- assigns samples to regions;
- updates each reproduction level to the mean of its assigned samples;
- moves internal boundaries to the midpoint of adjacent reproduction levels; and
- 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.
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.
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.
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.pyRunning python quantization.py executes the original 5-bit experiment and opens an interactive visualization. Run the checks with:
python -m unittest discover -s tests.
├── 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
- 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.


