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). The binary–to–Gray conversion is a single XOR with the right-shifted value:

Gray(n) = 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.

Try this: set the SNR where both images are visibly noisy, then compare the two demodulated panels and their Bit Error Rates. The BER is nearly identical — Gray coding does not reduce the number of symbol errors, it reduces the number of bit errors per symbol error, which is why the Gray image looks cleaner at the same BER-driving SNR.

Simulation

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

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.

Limitations

  • 16-QAM only. The demo fixes the constellation at 4×4 (16-QAM, 4 bits/symbol). It does not let you explore QPSK, 64-QAM, 256-QAM, or non-square constellations where Gray labeling is more involved.
  • AWGN, hard-decision nearest-point. The channel is additive white Gaussian noise and the receiver picks the nearest ideal point. There is no fading, no phase/frequency offset, and no soft-decision or iterative decoding.
  • No channel coding. Raw symbols are sent with no FEC (convolutional, LDPC, turbo); real links pair Gray mapping with coding, which changes the error picture substantially.
  • Seeded, reproducible noise. Noise is deterministically seeded from the SNR for a fair side-by-side comparison, so it is not a true Monte-Carlo BER estimate — the displayed BER is a single realization, not an average.
  • Idealized I/Q. Perfect synchronization, unit pulse shaping, and no inter-symbol interference are assumed; SNR maps directly to noise variance on the constellation.
  • Pixel-mapped payload. Bits come from an image purely for visualization; the "value jump" intuition is specific to treating symbols as pixel intensities.