Web Simulation

 

 

 

 

Gray Coding vs Binary — 16-QAM Constellation

This simulation shows how Gray coding in 16-QAM (Quadrature Amplitude Modulation) limits damage from noise compared to natural binary mapping. In a high-SNR environment, a single bit error with binary ordering can cause a large jump in the decoded value; with Gray coding, adjacent constellation points differ by only one bit, so errors usually change the pixel by a small amount.

16-QAM

Each pixel is 8 bits (0–255), sent as two 4-bit symbols (high and low nibbles)—so one pixel uses two constellation points. Each symbol is mapped to a point in the I–Q plane (In-phase and Quadrature). We use a 4×4 grid with levels −3, −1, 1, 3 on both axes. Gaussian noise is added to simulate the channel; the receiver picks the nearest constellation point and decodes back to 4 bits. Noise is seeded from the SNR so the same SNR gives reproducible results and a fair Gray vs Binary comparison.

Binary vs Gray mapping

Natural binary: Symbols 0–15 map in order (00, 01, 10, 11 on the two 2-bit axes). Adjacent points can differ by two or more bits (e.g. 01 next to 10). A small noise push across a decision boundary can flip the MSB and cause a big change in the decoded pixel.

Gray coding: The same 4×4 grid is labeled so that neighbors differ by exactly one bit (e.g. 00, 01, 11, 10). So n → Gray is n ^ (n >> 1). A boundary crossing then flips only one bit, so the pixel value changes by at most one step—the image stays much cleaner at the same SNR.

What to observe

Lower the SNR (more noise). The Natural Binary image will show strong “salt and pepper” errors; the Gray Coded image will look noticeably cleaner because errors are mostly single-bit and thus smaller in magnitude. Click or drag on the original image to select a pixel: the two corresponding constellation points are highlighted on both diagrams (yellow = correct symbol, red = symbol received in error). The selection box on the original is red when that pixel has a bit error.

20
Original image (grayscale)

Gray Coded (1-bit diff between neighbors)

Constellation (IQ) + noise cloud Demodulated image
Bit Error Rate:

Adjacent symbols differ by 1 bit → smaller errors

Natural Binary (sequential 00, 01, 10, 11)

Constellation (IQ) + noise cloud Demodulated image
Bit Error Rate:

Boundary crossing can flip multiple bits → salt & pepper

Usage

  1. Load image: At the top. Optional; upload an image (resized to 64×64 grayscale). If none is loaded, Lena.png from the GrayCoding folder or a built-in pattern is used.
  2. SNR slider: Signal-to-Noise Ratio in dB (0–40). Lower = more noise. Same SNR uses the same noise seed for reproducibility and a fair Gray vs Binary comparison.
  3. Original image (200×200): Click or drag to select a pixel. The two constellation points for that pixel (high and low nibbles) are highlighted on both diagrams; yellow ring = correct, red ring = symbol in error. The selection box is red when the selected pixel has a bit error.

Visualizations

  • Original image: 200×200 grayscale source. One pixel = 8 bits = two 4-bit symbols (two constellation points).
  • Constellation (IQ plot): 16 ideal points with 4-bit labels, decision-boundary grid, and noise cloud. Green dots = Gray path; red dots = Binary path. Selected pixel’s two points are ringed (yellow = correct, red = error).
  • Demodulated image: 128×128 display of the reconstructed 64×64 image. BER is shown per constellation.

Controls

  • Load image: File input at top; 64×64 grayscale. Default: Lena.png.
  • SNR: 0–40 dB. Seeded by SNR for deterministic noise.
  • Original image: Click or drag to select a pixel and highlight its two constellation points.