---
title: Linear block codes
heading: Adding structured redundancy, generator and parity-check matrices, Hamming distance and what it detects and corrects, Hamming and Golay codes, syndrome decoding and block error probability
eyebrow: S8 · Lesson 34 · Linear block codes
xp: 50
---

::: hook
The coding theorem says reliable communication is possible near capacity, but only through redundancy cleverly arranged. Block codes were the first practical answer: take k data bits, append n − k check bits computed from them, and send the n-bit codeword. If the valid codewords all differ from each other in at least $d_\text{min}$ places, a few flipped bits leave the received word closer to the right codeword than to any other. Richard Hamming's 1950 code, built because a relay computer kept stopping on parity errors over the weekend, corrects any single error in seven bits with three check bits.
:::

::: play
- Block error probability against Eb/N0 for 4 data bits sent uncoded, as a Hamming (7,4) codeword, and 12 data bits as a Golay (23,12) codeword (hard decisions, BPSK; coded bits get energy $(k/n)E_b$).
- The Hamming code corrects one error per block but spends 43% of its energy on check bits: it gains only about 0.7 dB at a block error rate of 10⁻⁵.
- The Golay code corrects three errors in 23 and gains about 2.8 dB there; stronger codes (lessons 35 and 36) gain far more.
:::
:::

```sim plot
view: graph
x: E = 2..14 dB
y: P = 1e-9..1 log
plot: 1 - (1 - Q(sqrt(2*10^(E/10))))^4 | samples: 400 | dashed | color: ch3 | label: 4 bits uncoded
plot: 1 - (1 - Q(sqrt(2*4/7*10^(E/10))))^7 - 7*Q(sqrt(2*4/7*10^(E/10)))*(1 - Q(sqrt(2*4/7*10^(E/10))))^6 | samples: 400 | label: Hamming (7,4)
plot: 1 - (1 - Q(sqrt(2*12/23*10^(E/10))))^23 - 23*Q(sqrt(2*12/23*10^(E/10)))*(1 - Q(sqrt(2*12/23*10^(E/10))))^22 - 253*Q(sqrt(2*12/23*10^(E/10)))^2*(1 - Q(sqrt(2*12/23*10^(E/10))))^21 - 1771*Q(sqrt(2*12/23*10^(E/10)))^3*(1 - Q(sqrt(2*12/23*10^(E/10))))^20 | samples: 400 | color: ch2 | label: Golay (23,12)
```

::: predict
## A code's codewords all differ in at least 5 positions. How many bit errors per block can it always correct?

- [ ] 5
- [ ] 4
- [x] 2
- [ ] 1

::: reveal
Two. With $d_\text{min}$ = 5, a received word with 2 errors is still at distance 2 from the true codeword and at least 3 from any other, so nearest-codeword decoding picks the right one. With 3 errors it might be closer to a wrong codeword. In general a code corrects $\lfloor(d_\text{min} - 1)/2\rfloor$ errors, or detects $d_\text{min} - 1$.
:::
:::

::: nameit
## Linear block codes

An (n, k) code maps k data bits to an n-bit codeword; its **rate** is R = k/n. A linear code's codewords are $\mathbf{c} = \mathbf{d}G$ for a k × n **generator matrix** G (often systematic, $G = [I_k\,|\,P]$), and every codeword satisfies $\mathbf{c}H^T = 0$ for the (n − k) × n **parity-check matrix** $H = [P^T\,|\,I_{n-k}]$.

## Distance

The **Hamming distance** between two words is the number of places they differ; for a linear code, $d_\text{min}$ equals the smallest weight of a nonzero codeword. A code can

- **detect** up to $d_\text{min} - 1$ errors;
- **correct** up to $t = \lfloor(d_\text{min} - 1)/2\rfloor$ errors.

## Hamming and Golay codes

- **Hamming codes**: $n = 2^m - 1$, $k = n - m$, $d_\text{min}$ = 3: the columns of H are all nonzero m-bit patterns, so the **syndrome** $\mathbf{s} = \mathbf{r}H^T$ of a single error is the column at the error's position, which it names directly.
- **Golay (23,12)**: $d_\text{min}$ = 7, t = 3.
- Both are **perfect**: the $2^{n-k}$ syndromes exactly cover all patterns of up to t errors, $\sum_{i=0}^{t}\binom{n}{i} = 2^{n-k}$ (the Hamming bound met with equality).

## Performance

With hard decisions and coded-bit error probability $p = Q\left(\sqrt{2RE_b/N_0}\right)$, a t-error-correcting code fails when more than t errors occur:

$$P_\text{block} = 1 - \sum_{i=0}^{t}\binom{n}{i}p^i(1 - p)^{n - i}$$

Spreading the bit energy over more transmitted bits raises p, so a code must correct enough errors to come out ahead.
:::

::: example
## With raw bit error probability p = 0.01, compare 4 uncoded bits with a Hamming (7,4) codeword and a Golay (23,12) codeword.

::: solution
1. **Uncoded 4-bit block.**
   $$1 - 0.99^4 = 0.039$$
2. **Hamming (7,4).**
   $$\begin{aligned} P &= 1 - 0.99^7 \\ &\quad - 7(0.01)(0.99^6) \\ &= 0.0020 \end{aligned}$$
3. **Golay (23,12).**
   $$\begin{aligned} P &= 1 - \sum_{i=0}^{3}\binom{23}{i}p^i(1-p)^{23-i} \\ &= 7.6 \times 10^{-5} \end{aligned}$$
4. **A caveat.** At equal Eb/N0 the coded bits have higher p because each carries less energy; the sim accounts for that.

**Answer:** block error probabilities of 0.039 uncoded, 0.0020 with the Hamming code and 7.6 × 10⁻⁵ with the Golay code.
:::
:::

::: example
## Check that the Golay (23,12) code is perfect.

::: solution
1. **Count the correctable patterns.**
   $$\begin{aligned} N &= 1 + 23 + \binom{23}{2} + \binom{23}{3} \\ &= 1 + 23 + 253 + 1771 \\ &= 2048 \end{aligned}$$
2. **Count the syndromes.**
   $$\begin{aligned} 2^{23-12} &= 2^{11} \\ &= 2048 \end{aligned}$$
3. **Perfect.** Every one of the 2048 syndromes corresponds to exactly one correctable error pattern, with none left over.

**Answer:** the 2048 patterns of up to three errors exactly fill the 2048 syndromes, so the code is perfect.
:::
:::

::: example
## A Hamming (15, 11) code sees raw bit errors with probability 0.001. What is its block error probability, against 11 uncoded bits?

::: solution
1. **Coded block.** It fails only with two or more errors:
   $$\begin{aligned} P &= 1 - 0.999^{15} \\ &\quad - 15(0.001)(0.999)^{14} \\ &= 1.04 \times 10^{-4} \end{aligned}$$
2. **Eleven uncoded bits.** They arrive with at least one error with probability
   $$1 - 0.999^{11} = 0.011$$
3. **Compare.** That's a hundredfold improvement for a 27% rate penalty.
4. **At p = 0.01.** The gain would be far smaller, as double errors become common.

**Answer:** 1.04 × 10⁻⁴ for the Hamming block, against 0.011 for 11 uncoded bits.
:::
:::

::: check
given: d = 3 | 5 | 7 | 9
answer: floor((d - 1)/2 + 0.0001)
tolerance: 0

## A code has $d_\text{min}$ = {d}. How many errors per block can it always correct?

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

::: misses
- [d - 1] That's how many it can detect.
- [any] Not yet. Errors must leave the received word nearer the sent codeword than any other.
:::
:::

::: check
given: m = 3 | 4 | 5 | 6
answer: 2^m - 1 - m
tolerance: 0

## A Hamming code has m = {m} check bits. How many data bits does each codeword carry?

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

::: misses
- [2^m - 1] That's the codeword length n.
- [any] Not yet. $k = 2^m - 1 - m$.
:::
:::

::: check
given: n = 7 | 15 | 31; p = 0.001 | 0.01 | 0.03
answer: 1 - (1 - p)^n - n*p*(1 - p)^(n - 1)
tolerance: 0.5%

## A single-error-correcting code of length {n} sees raw bit errors with probability {p}. What's its block error probability?

::: right
Right. More than one error: {answer}.
:::

::: misses
- [1 - (1 - p)^n] That's the probability of any error; one error is corrected.
- [any] Not yet. $1 - (1 - p)^n - np(1 - p)^{n-1}$.
:::
:::

::: check
given: m = 3 | 4 | 5 | 6; let n = 2^m - 1; let k = 2^m - 1 - m
answer: 100*k/n
unit: %
tolerance: 0.5%

## A Hamming code puts {k} data bits into each {n}-bit codeword. What's its rate?

::: right
Right. k/n = {answer}%.
:::

::: misses
- [100*(n - k)/n] That's the redundancy fraction.
- [any] Not yet. R = k/n.
:::
:::

::: check
given: R = 0.5 | 0.571 | 0.75; E = 6 | 9
answer: Q(sqrt(2*R*10^(E/10)))
tolerance: 1%

## A rate-{R} code is sent with BPSK at Eb/N0 = {E} dB and hard decisions. What's the raw coded-bit error probability?

::: right
Right. Each coded bit gets $RE_b$: $Q(\sqrt{2RE_b/N_0})$ = {answer}.
:::

::: misses
- [Q(sqrt(2*10^(E/10)))] Coded bits carry only R of the energy per data bit.
- [any] Not yet. $E_c = RE_b$.
:::
:::

::: check
given: n = 7 | 15 | 31 | 63
answer: 1 + n
tolerance: 0

## How many error patterns of weight 0 or 1 are there in an {n}-bit word, the number a single-error-correcting code's syndromes must distinguish?

::: right
Right. 1 + n = {answer}; for a Hamming code it equals $2^{n-k}$ exactly.
:::

::: misses
- [n] Include the no-error pattern.
- [any] Not yet. One pattern with no errors and n with one.
:::
:::

::: check
kind: parts
given: m = 3 | 4 | 5 | 6; p = 0.001 | 0.01

## A Hamming code with m = {m} check bits sees raw bit errors with probability {p}.

::: part
answer: (2^m - 1 - m)/(2^m - 1)
tolerance: 0.5%

### What is its rate?

::: misses
- [any] $k/n = (2^m - 1 - m)/(2^m - 1)$.
:::
:::

::: part
answer: 1 - (1 - p)^(2^m - 1) - (2^m - 1)*p*(1 - p)^(2^m - 2)
tolerance: 1%

### What is its block error probability?

::: misses
- [any] Two or more errors in n bits.
:::
:::
:::

::: check
kind: derivation
vars: m = 2..12, n = 3..1000

## A Hamming code has m check bits; separately, a single-error-correcting code has length n.

::: step
answer: 2^m - 1

### What is the Hamming code's length?
:::

::: step
answer: 2^m - 1 - m

### How many data bits does it carry?
:::

::: step
answer: 1 + n

### How many error patterns must a single-error-correcting code of length n distinguish?
:::

::: right
Right. A Hamming code's $2^m$ syndromes match its $1 + n$ patterns exactly: it's perfect.
:::
:::

::: explain
## Why can a code with minimum distance d correct ⌊(d − 1)/2⌋ errors but no more?

::: model
Picture every codeword surrounded by a sphere containing all the words within distance t of it. If 2t is less than the minimum distance, these spheres don't overlap, so any received word with t or fewer errors lies inside exactly one sphere, its own codeword's, and the decoder can identify it unambiguously by choosing the nearest codeword. With t + 1 errors and t = ⌊(d − 1)/2⌋, a received word can land halfway or more toward another codeword, where it's at least as close to the wrong one, and the decoder fails. Detection is easier: any error pattern of fewer than d bits can't turn one codeword into another, so it's always noticed even when it can't be fixed.
:::
:::

::: practice

::: check
given: d = 3 | 4 | 5 | 7 | 8 | 11
answer: floor((d - 1)/2 + 0.0001)
tolerance: 0

## Correctable errors for $d_\text{min}$ = {d}?

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

::: check
given: n = 15 | 63; p = 0.002 | 0.02
answer: 1 - (1 - p)^n - n*p*(1 - p)^(n - 1)
tolerance: 0.5%

## Block error: single-error-correcting, n {n}, p {p}?

::: misses
- [any] $1 - (1 - p)^n - np(1 - p)^{n-1}$.
:::
:::

::: check
given: m = 3 | 4 | 5 | 6 | 7 | 8
answer: 2^m - 1 - m
tolerance: 0

## Data bits of the Hamming code with m = {m}?

::: misses
- [any] $2^m - 1 - m$.
:::
:::
:::
