---
title: Error-correcting codes
heading: Adding redundant bits so the receiver can find and fix errors, code rate and minimum distance, how many errors a code corrects, the repetition code that does worse than nothing, the Hamming (7,4) code, Reed–Solomon codes for bursts, and the modern codes that approach Shannon's limit
eyebrow: D4 · Lesson 8 · Error-correcting codes
xp: 40
---

::: hook
In 1947 Richard Hamming, frustrated that a relay computer at Bell Labs kept stopping over the weekend at the first error it detected, asked why it couldn't find and fix the error itself. His answer, a few extra parity bits computed so that any single flipped bit leaves a unique signature, began coding theory. Codes are now everywhere: in every disk, memory chip, QR code and radio link. They're how real systems approach the Shannon limit of lesson 6, by spending a little of the bandwidth on redundancy that lets the receiver correct what noise breaks.
:::

::: play
- Bit error rates against $E_b/N_0$ for uncoded BPSK, a 3× repetition code with majority vote, and the Hamming (7,4) code, all with hard decisions.
- Coded bits get less energy each, since the same energy per information bit is shared among more transmitted bits, so the raw error rate p rises before decoding repairs it.
- The repetition code repairs too little to pay for its 3× overhead and stays worse than sending nothing extra. Hamming's code wins at high $E_b/N_0$.
:::

```sim plot
view: graph
x: E = 2..12
y: P = 1e-8..0.1 log
slider: g = 2..12 step 0.2 = 10
plot: Q(sqrt(2*10^(E/10))) | samples: 300 | label: uncoded BPSK (E = Eb/N0 in dB)
plot: 3*Q(sqrt(2/3*10^(E/10)))^2 - 2*Q(sqrt(2/3*10^(E/10)))^3 | samples: 300 | color: ch3 | label: repetition (3,1)
plot: 9*Q(sqrt(8/7*10^(E/10)))^2 | samples: 300 | color: ch4 | label: Hamming (7,4), approximately 9p²
marker: x = g | label: your Eb/N0
readout: uncoded = Q(sqrt(2*10^(g/10))) | digits: 3
readout: Hamming (7,4) = 9*Q(sqrt(8/7*10^(g/10)))^2 | digits: 3
readout: repetition = 3*Q(sqrt(2/3*10^(g/10)))^2 - 2*Q(sqrt(2/3*10^(g/10)))^3 | digits: 3
```

::: predict
## Each bit is sent three times and the receiver takes a majority vote. At the same energy per information bit, is this better or worse than sending each bit once?

- [ ] Better: it corrects any single error
- [x] Worse: each copy gets a third of the energy
- [ ] The same
- [ ] Better only at low $E_b/N_0$

::: reveal
Splitting the energy three ways makes each copy much more likely to flip: at 10 dB, p rises from 3.9 × 10⁻⁶ uncoded to 4.9 × 10⁻³ per copy. The vote fails when two or three copies flip, $3p^2 - 2p^3$ = 7.2 × 10⁻⁵, about 19 times worse than not coding at all. Good codes add far less redundancy per error they correct.
:::
:::

::: nameit
## Rate and distance

An (n, k) block code turns k information bits into n coded bits: its **rate** is $r = k/n$, and the link's data rate falls by that factor. The **minimum distance** $d_\min$ is the fewest bits in which two codewords differ. A code can

- detect up to $d_\min - 1$ errors, and
- correct up to $t = \lfloor(d_\min - 1)/2\rfloor$ errors, by choosing the nearest codeword.

## Energy per coded bit

At a fixed energy per information bit, each coded bit gets $rE_b$, so with BPSK the raw error rate before decoding is $p = Q(\sqrt{2rE_b/N_0})$. A code helps only if its correction more than makes up for that.

## Hamming codes

With m parity bits, $n = 2^m - 1$ and $k = n - m$: (7,4), (15,11), (31,26). Each corrects one error ($d_\min$ = 3): the parity checks that fail, read as a binary number, point to the flipped bit. For small p, the (7,4) code's decoded bit error rate is about $9p^2$.

## Stronger codes

- **Reed–Solomon** codes work on bytes and fix bursts: RS(255,223) corrects any 16 wrong bytes in 255. CDs, QR codes and deep-space links use them.
- **Convolutional** codes with Viterbi decoding, and **soft decisions** that pass the receiver's confidence along with each bit, gain about 2 dB more.
- **Turbo** and **LDPC** codes, decoded iteratively, operate within a decibel of Shannon's limit; LDPC codes are in Wi-Fi, 5G and DVB-S2.
:::

::: example
## At $E_b/N_0$ = 10 dB, compare uncoded BPSK with the Hamming (7,4) code.

::: solution
1. **Uncoded.** The bit error rate is $3.9 \times 10^{-6}$.
2. **Coded bits.** They get 4/7 of the energy:
   $$\begin{aligned} p &= Q(\sqrt{2 \times 4/7 \times 10}) \\ &= 3.6 \times 10^{-4} \end{aligned}$$
3. **After decoding.**
   $$9p^2 = 1.2 \times 10^{-6}$$
4. **Compare.** That's three times better, at the cost of sending 7 bits for every 4.

**Answer:** the Hamming code gives 1.2 × 10⁻⁶ against 3.9 × 10⁻⁶ uncoded.
:::
:::

::: example
## How many errors can a code with $d_\min$ = 7 correct? What's the rate of RS(255,223)?

::: solution
1. **Errors corrected.**
   $$\lfloor 6/2 \rfloor = 3$$
   errors, or 6 detected.
2. **Rate of RS(255,223).**
   $$223/255 = 0.875$$
3. **What RS(255,223) corrects.**
   $$(255 - 223)/2 = 16$$
   byte errors.

**Answer:** it corrects 3 errors (or detects 6), and RS(255,223) has rate 0.875 and corrects 16 byte errors.
:::
:::

::: example
## A CRC-16 checks every frame. If 1000 frames a day arrive with random errors, how often does a bad frame slip through undetected?

::: solution
1. **Chance a bad frame passes.** A good 16-bit CRC passes a random error pattern with probability about
   $$2^{-16} = 1.5 \times 10^{-5}$$
2. **Per day.** That's 0.015 bad frames a day, about one every 66 days.
3. **A 32-bit CRC.** With $2^{-32}$ in place of $2^{-16}$, a bad frame slips through about once in 12,000 years.

**Answer:** about one bad frame every 66 days.
:::
:::

::: check
given: d = 3 | 5 | 8 | 11
answer: floor((d - 1)/2)
tolerance: 0.5%

## A code has minimum distance {d}. How many errors per codeword can it always correct?

::: right
Right. $\lfloor(d_\min - 1)/2\rfloor$ = {answer}.
:::

::: misses
- [d - 1] That's how many it can detect.
- [any] Not yet. Each error must stay closer to its codeword than to any other.
:::
:::

::: check
given: m = 3 | 4 | 5 | 6
answer: (2^m - 1 - m)/(2^m - 1)
tolerance: 0.5%

## A Hamming code has {m} parity bits. What's its rate?

::: right
Right. $k/n = (2^m - 1 - m)/(2^m - 1)$ = {answer}.
:::

::: misses
- [m/(2^m - 1)] That's the parity bits' share.
- [any] Not yet. $n = 2^m - 1$, $k = n - m$.
:::
:::

::: check
given: p = 0.02 | 0.05 | 0.1
answer: 3*p^2 - 2*p^3
tolerance: 0.5%

## Each copy of a bit sent three times flips with probability {p}. How often does the majority vote come out wrong?

::: right
Right. $3p^2(1 - p) + p^3 = 3p^2 - 2p^3$ = {answer}.
:::

::: misses
- [3*p^2] Add the case where all three flip, and use $(1 - p)$ for the third.
- [any] Not yet. Two or three of the copies must flip.
:::
:::

::: check
given: r = 0.5 | 0.75 | 0.875; E = 4 | 7
answer: Q(sqrt(2*r*10^(E/10)))
tolerance: 0.5%

## A rate-{r} code sends BPSK at $E_b/N_0$ = {E} dB per information bit. What's the raw error rate of each coded bit before decoding?

::: right
Right. $Q(\sqrt{2rE_b/N_0})$ = {answer}.
:::

::: misses
- [Q(sqrt(2*10^(E/10)))] Each coded bit gets only r of the energy per information bit.
- [any] Not yet. Share the energy among the coded bits.
:::
:::

::: check
kind: parts
given: d = 3 | 5 | 7 | 9

## A block code has minimum distance {d}.

::: part
answer: floor((d - 1)/2)
tolerance: 0

### How many errors can it correct?

::: misses
- [any] $\lfloor(d - 1)/2\rfloor$.
:::
:::

::: part
answer: d - 1
tolerance: 0

### How many can it detect, if it corrects none?

::: misses
- [any] $d - 1$.
:::
:::
:::

::: check
kind: parts
given: n = 8 | 16 | 32; f = 100 | 1000 | 100000

## A CRC of {n} bits checks frames; {f} frames a day arrive with random errors.

::: part
answer: 2^(-n)
tolerance: 0.5%

### What fraction of bad frames slip through?

::: misses
- [any] About $2^{-n}$.
:::
:::

::: part
answer: f*2^(-n)
tolerance: 0.5%

### How many undetected bad frames is that per day?

::: misses
- [any] Bad frames × the miss fraction.
:::
:::
:::

::: explain
## Why can a repetition code make a link worse, while a well-designed code makes it better?

::: model
At a fixed energy per information bit, every redundant bit a code adds takes some of that energy, so each transmitted bit is weaker and more likely to be flipped by noise. A code helps only if its ability to correct errors outweighs the extra errors this creates. Repeating each bit three times spends two thirds of the energy on redundancy and corrects only a single error per three bits, which is not enough to make up for the much higher raw error rate. A good code spreads a small amount of redundancy across many bits, so that each parity bit protects many data bits and the code corrects many errors per unit of overhead, giving a net gain.
:::
:::

::: practice

::: check
given: d = 2 | 3 | 4 | 5 | 6 | 7 | 9 | 13 | 17
answer: d - 1
tolerance: 0.5%

## Errors detectable with $d_\min$ = {d}?

::: misses
- [any] $d_\min - 1$.
:::
:::

::: check
given: p = 0.0001 | 0.001 | 0.01 | 0.02 | 0.05 | 0.1 | 0.2
answer: 3*p^2 - 2*p^3
tolerance: 0.5%

## Majority-of-3 error rate at p = {p}?

::: misses
- [any] $3p^2 - 2p^3$.
:::
:::
:::
