---
title: LDPC codes and iterative decoding
heading: Sparse parity-check matrices and their Tanner graphs, regular ensembles and design rate, the peeling decoder on the erasure channel, density evolution and the decoding threshold, belief propagation with log-likelihood ratios, and why these codes run within a fraction of a decibel of capacity
eyebrow: S8 · Lesson 37 · LDPC codes and iterative decoding
xp: 50
---

::: hook
Lesson 36 ended with codes that come close to Shannon's limit. Low-density parity-check codes, invented by Gallager in 1960 and forgotten for thirty years, now protect the data in Wi-Fi, 5G, satellite television and most solid-state drives. Their parity-check matrices are almost entirely zeros, and they're decoded by passing short messages back and forth along the edges of a graph. Richardson and Urbanke showed how to predict, from a one-line recursion, exactly how much noise such a code survives. Where does that recursion come from?
:::

::: play
- A (d_v, d_c)-regular LDPC code on the binary erasure channel, which erases each bit with probability ε. Density evolution tracks x, the probability that a message from a bit node is still an erasure, one decoding iteration at a time: $x \to f(x) = \epsilon\left(1 - (1 - x)^{d_c - 1}\right)^{d_v - 1}$.
- Decoding succeeds if iterating from x = ε reaches zero, which happens exactly when the curve f(x) stays below the line y = x. Where they touch, decoding stalls at a nonzero fixed point.
- For the (3, 6) code (rate 1/2) the threshold is ε* = 0.4294, against the capacity limit of 0.5. Move ε across it and watch the tunnel between the curve and the line close.
:::

```sim plot
view: graph
x: x = 0..0.6
y: y = 0..0.6
slider: e = 0.2..0.6 = 0.42
slider: dv = 2..6 step 1 = 3
slider: dc = 3..12 step 1 = 6
plot: e*(1 - (1 - x)^(dc - 1))^(dv - 1) | samples: 400 | label: f(x), one iteration
plot: x | samples: 50 | dashed | color: ch3 | label: y = x
readout: design rate = 1 - dv/dc | digits: 3
readout: capacity limit on ε = dv/dc | digits: 3
```

::: predict
## A rate-1/2 code is used on an erasure channel that erases 45% of the bits. Could any code at rate 1/2 work? Can the (3, 6) LDPC code with iterative decoding?

- [ ] Neither: 45% is too many erasures
- [x] Some code could (capacity is 0.55 > 0.5), but the (3, 6) code decoded iteratively can't (its threshold is 0.429)
- [ ] Both: the LDPC code achieves capacity
- [ ] Only the LDPC code

::: reveal
The erasure channel's capacity is 1 − ε = 0.55 bits per use, above the code's rate of 0.5, so reliable codes exist. But density evolution for the (3, 6) ensemble stalls above ε* = 0.4294: at 0.45, the erasure probability settles at about 0.36 instead of falling to zero. Irregular degree distributions, with some bits in many checks and some in few, push the threshold to within a hair of 0.5.
:::
:::

::: nameit
## Sparse parity checks and Tanner graphs

An LDPC code is defined by a parity-check matrix H that is mostly zeros. Draw it as a bipartite **Tanner graph**: n bit (variable) nodes, one per column, m check nodes, one per row, and an edge for every 1. In a $(d_v, d_c)$-**regular** code every bit sits in $d_v$ checks and every check involves $d_c$ bits, so there are $nd_v = md_c$ edges and the **design rate** is

$$R = 1 - \frac{m}{n} = 1 - \frac{d_v}{d_c}$$

Codes are drawn from an **ensemble** of random graphs with the given degrees; for long blocks almost every member performs like the average.

## Decoding erasures by peeling

On the binary erasure channel each bit arrives correctly or as "?". Any check with exactly one erased bit determines it: that bit is the XOR of the others. Fill it in, remove it, and repeat. Decoding stops when every bit is known or when every remaining check has two or more erasures (a **stopping set**). The work is proportional to the number of edges, linear in block length.

## Density evolution

For a long random code, the graph around any edge looks like a tree for many iterations, so messages are independent. A check sends an erasure unless all $d_c - 1$ other incoming messages are known; a bit sends an erasure only if its channel value and all $d_v - 1$ other check messages are erasures. So the erasure probability x on bit-to-check messages evolves as

$$x_{\ell + 1} = \epsilon\left(1 - (1 - x_\ell)^{d_c - 1}\right)^{d_v - 1}$$

The **threshold** ε* is the largest ε for which $x_\ell \to 0$. For (3, 6), ε* = 0.4294; for (4, 8), 0.3834. Higher degrees aren't better for iterative decoding, though they help the code's minimum distance.

## Belief propagation on noisy channels

On channels with noise, messages are **log-likelihood ratios** $L = \ln\frac{P(b = 0)}{P(b = 1)}$. A bit node adds its channel LLR and the incoming check messages (except on the edge it sends along). A check node combines its other inputs by the **tanh rule**

$$L_\text{out} = 2\,\text{atanh}\prod_i\tanh\frac{L_i}{2}$$

often approximated by the **min-sum** rule: the smallest magnitude, with the product of the signs. Density evolution then tracks distributions of LLRs, and optimized irregular codes reach within a few hundredths of a decibel of the Shannon limit on the AWGN channel at long block lengths.
:::

::: example
## A (3, 6)-regular code has n = 1000 bits. How many checks and edges does it have, and what's its rate? How does decoding go at ε = 0.4?

::: solution
1. **Edges.**
   $$3 \times 1000 = 3000$$
2. **Checks.**
   $$\begin{aligned} m &= 3000/6 \\ &= 500 \end{aligned}$$
3. **Rate.**
   $$\begin{aligned} R &= 1 - 500/1000 \\ &= 1/2 \end{aligned}$$
4. **Density evolution at ε = 0.4.** From $x_0$ = 0.4 it gives 0.340, 0.306, 0.282, 0.262, 0.244, 0.227, slowing as it squeezes through the narrow gap between the curve and the line near x ≈ 0.25, then racing to zero: below 10⁻⁶ after 18 iterations.
5. **Against the threshold.** Since 0.4 is below ε* = 0.4294, a long code decodes; at 0.45 the recursion settles at x = 0.355 and most erased bits stay unknown.

**Answer:** 500 checks and 3000 edges, rate 1/2; at ε = 0.4 decoding succeeds, with the erasure fraction below 10⁻⁶ after 18 iterations.
:::
:::

::: example
## A check node receives LLRs of 2 and 3 from its other two bits. What does it send to the third, by the tanh rule and by min-sum?

::: solution
1. **Tanh of each half-LLR.**
   $$\begin{aligned} \tanh 1 &= 0.762 \\ \tanh 1.5 &= 0.905 \end{aligned}$$
   with product 0.689.
2. **Tanh rule.**
   $$\begin{aligned} L_\text{out} &= 2\,\text{atanh}(0.689) \\ &= \ln\frac{1 + 0.689}{1 - 0.689} \\ &= 1.69 \end{aligned}$$
3. **Min-sum.** It sends min(2, 3) = 2, with a positive sign.
4. **Why the output is weaker.** The parity check is weaker evidence than either input: the third bit is inferred from two uncertain bits, so its confidence is below the less certain one. Min-sum's overestimate is why practical decoders scale or offset its outputs.

**Answer:** 1.69 by the tanh rule and 2 by min-sum.
:::
:::

::: example
## A check node receives LLRs 1.5, −2 and 3 from its other bits. What does it send by the tanh rule, and by min-sum?

::: solution
1. **Tanh of each half-LLR.**
   $$\begin{aligned} \tanh 0.75 &= 0.635 \\ \tanh(-1) &= -0.762 \\ \tanh 1.5 &= 0.905 \end{aligned}$$
2. **Tanh rule.**
   $$\begin{aligned} L_\text{out} &= 2\,\text{artanh}(0.635 \times -0.762 \\ &\quad \times 0.905) \\ &= 2\,\text{artanh}(-0.438) \\ &= -0.94 \end{aligned}$$
3. **Min-sum.** The product of the signs (−) times the smallest magnitude (1.5): −1.5.
4. **Overconfidence.** Min-sum overstates confidence, which practical decoders correct with a scaling factor or an offset.

**Answer:** −0.94 by the tanh rule and −1.5 by min-sum.
:::
:::

::: check
given: v = 2 | 3 | 4; c = 7 | 9 | 12
answer: 1 - v/c
tolerance: 0.5%

## What's the design rate of a ({v}, {c})-regular LDPC code?

::: right
Right. $1 - d_v/d_c$ = {answer}.
:::

::: misses
- [v/c] That's the fraction of rows to columns, m/n. The rate is one minus it.
- [any] Not yet. Count edges two ways: $nd_v = md_c$.
:::
:::

::: check
given: n = 600 | 1200 | 64800; v = 3 | 4; c = 6 | 12
answer: n*v/c
tolerance: 0

## A ({v}, {c})-regular code has {n} bits. How many check nodes does it have?

::: right
Right. $nd_v/d_c$ = {answer} checks.
:::

::: misses
- [n*v] That's the number of edges. Each check has $d_c$ of them.
- [any] Not yet. Edges counted from both sides must agree.
:::
:::

::: check
given: e = 0.3 | 0.4 | 0.45; v = 3 | 4; c = 6 | 8
answer: e*(1 - (1 - e)^(c - 1))^(v - 1)
tolerance: 0.5%

## A ({v}, {c})-regular code sees erasure probability {e}. What's the erasure probability on bit-to-check messages after one iteration of density evolution?

::: right
Right. $\epsilon(1 - (1 - \epsilon)^{d_c - 1})^{d_v - 1}$ = {answer}.
:::

::: misses
- [e*(1 - (1 - e)^c)^v] Each message ignores its own edge: use $d_c - 1$ and $d_v - 1$.
- [any] Not yet. A check sends an erasure unless all its other inputs are known; a bit only if all its other inputs are erased.
:::
:::

::: check
given: a = 1 | 2 | 4; b = 1.5 | 3 | 5
answer: ln((1 + (exp(a) - 1)/(exp(a) + 1)*(exp(b) - 1)/(exp(b) + 1))/(1 - (exp(a) - 1)/(exp(a) + 1)*(exp(b) - 1)/(exp(b) + 1)))
tolerance: 0.5%

## A check node's other inputs carry LLRs {a} and {b}. What LLR does it send by the tanh rule?

::: right
Right. $2\,\text{atanh}(\tanh(a/2)\tanh(b/2))$ = {answer}.
:::

::: misses
- [a + b] That's a bit node's rule. A check makes the output weaker than its inputs.
- [any] Not yet. Multiply the tanh of half of each LLR, then invert.
:::
:::

::: check
given: c = 0.5 | 1.2 | 2.5; a = -1 | 0.8 | 2; b = 1.5 | -0.4 | 3
answer: c + a + b
tolerance: 0.5%

## A bit node has channel LLR {c} and receives {a} and {b} from its other checks. What does it send to its third check?

::: right
Right. The sum, {answer}: independent evidence adds in the log domain.
:::

::: misses
- [a + b] Include the channel LLR.
- [any] Not yet. A bit node adds its channel LLR and the other incoming messages.
:::
:::

::: check
given: e = 0.3 | 0.35 | 0.42
answer: 1 - e
tolerance: 0.5%

## An erasure channel erases bits with probability {e}. What's the highest rate any code can reliably achieve on it?

::: right
Right. Capacity is 1 − ε = {answer} bits per use: the fraction that gets through.
:::

::: misses
- [e] That's the erasure probability itself.
- [any] Not yet. Only the unerased fraction carries information.
:::
:::

::: check
kind: parts
given: v = 3 | 4; c = 6 | 8 | 12; n = 1200 | 4800

## A ({v}, {c})-regular LDPC code has {n} bits.

::: part
answer: n*v/c
unit: checks
tolerance: 0.5%

### How many check nodes does it have?

::: misses
- [any] $nd_v/d_c$.
:::
:::

::: part
answer: n*v
unit: edges
tolerance: 0.5%

### How many edges does its Tanner graph have?

::: misses
- [any] $nd_v$.
:::
:::

::: part
answer: 1 - v/c
tolerance: 0.5%

### What is its design rate?

::: misses
- [any] $1 - d_v/d_c$.
:::
:::
:::

::: check
kind: derivation
vars: e = 0.1..0.6, x = 0..1, v = 2..6, c = 3..20

## Density evolution runs on a (v, c)-regular code over an erasure channel with probability e; the current bit-to-check erasure probability is x.

::: step
answer: 1 - (1 - x)^(c - 1)

### What erasure probability do check-to-bit messages carry?
:::

::: step
answer: e*(1 - (1 - x)^(c - 1))^(v - 1)

### What is the next bit-to-check erasure probability?
:::

::: right
Right. Below the threshold this map drives x to zero; above it, x sticks at a nonzero fixed point.
:::
:::

::: explain
## Why can a one-line recursion predict exactly how a long LDPC code decodes?

::: model
Iterative decoding is local: each message depends only on the part of the graph within a few edges of where it's computed. In a long code with randomly placed sparse connections, that neighborhood is almost surely a tree for many iterations, with no short cycles, so the messages entering any node come from disjoint parts of the graph and are statistically independent. Independence lets the probability of an erasure, or the distribution of an LLR, be computed from those of the incoming messages with no other information about the particular code. That is density evolution. And concentration results show that almost every code in the ensemble behaves like the average, so the threshold the recursion predicts is the threshold real long codes show. Finite codes have cycles and stopping sets, which create an error floor and a gradual waterfall rather than a sharp threshold.
:::
:::

::: practice

::: check
given: v = 2..6 int; c = 6..20 int
answer: 1 - v/c
tolerance: 0.5%

## Design rate of a ({v}, {c})-regular code?

::: misses
- [any] $1 - d_v/d_c$.
:::
:::

::: check
given: e = 0.05..0.6; v = 2..5 int; c = 4..10 int
answer: e*(1 - (1 - e)^(c - 1))^(v - 1)
tolerance: 0.5%

## One density-evolution step: ε = {e}, ({v}, {c})?

::: misses
- [any] $\epsilon(1 - (1 - \epsilon)^{d_c - 1})^{d_v - 1}$.
:::
:::
:::
