Stronger codes: convolutional codes, Viterbi and LDPC
About this lesson
Hamming codes protect short blocks. Stronger codes spread each message bit over many sent bits. Follow one example end to end: a rate-1/2 convolutional encoder turns 1011 into 11 10 00 01 01 11, Gaussian noise flips two bits, and the Viterbi algorithm walks a trellis of four states to recover 1011 exactly. Then meet LDPC codes, the sparse parity checks inside 5G and Wi-Fi 6, and fix a flipped bit by bit flipping. It builds on the lessons on parity and Hamming codes, noise and decisions (AWGN and SNR), and discrete-time convolution; the encoder is literally np.convolve(u, taps) % 2.
A code with memory
A Hamming code guards fixed blocks. A convolutional code works on a stream. The encoder keeps a sliding window of the last K = 3 message bits in a shift register: the new bit u[n] and the two before it.
message u = 1 0 1 1
window [u[n], u[n−1], u[n−2]]
K = 3 bits (the constraint length)
the register starts at 0 0
each tick: the new bit enters on the left,
the oldest bit falls off the right
block codes (Hamming): see "Parity and Hamming codes"Read the lesson step by step
A code with memory
A Hamming code guards fixed blocks. A convolutional code works on a stream. The encoder keeps a sliding window of the last K = 3 message bits in a shift register: the new bit u[n] and the two before it.
message u = 1 0 1 1 window [u[n], u[n−1], u[n−2]] K = 3 bits (the constraint length) the register starts at 0 0 each tick: the new bit enters on the left, the oldest bit falls off the right block codes (Hamming): see "Parity and Hamming codes"Two parity bits per message bit
Two mod-2 adders read the window (1 ⊕ 1 = 0). c1 = u[n] ⊕ u[n−1] ⊕ u[n−2] and c2 = u[n] ⊕ u[n−2]. The first bit, 1, after two zeros, gives c1 c2 = 1 1. Two code bits per message bit: rate 1/2.
c1[n] = u[n] ⊕ u[n−1] ⊕ u[n−2] taps 111 (octal 7) c2[n] = u[n] ⊕ u[n−2] taps 101 (octal 5) ⊕ = add mod 2: 0⊕0 = 0, 0⊕1 = 1, 1⊕1 = 0 n = 0: window [1, 0, 0] c1 = 1 ⊕ 0 ⊕ 0 = 1 c2 = 1 ⊕ 0 = 1 rate = 1 message bit / 2 code bits = 1/2Encoding is a convolution
Two 0s follow 1011 to flush the register. Out come 11 10 00 01 01 11. Each output stream is the message convolved with its taps, mod 2: that is the name. In Python, np.convolve(u, taps) % 2 gives each stream.
n window c1 c2 0 100 1 1 1 010 1 0 2 101 0 0 3 110 0 1 4 011 0 1 5 001 1 1 sent: 11 10 00 01 01 11 >>> c1 array([1, 1, 0, 0, 0, 1]) >>> c2 array([1, 0, 0, 1, 1, 1]) (Python 3 with numpy; mode "full" adds the tail)Noise flips two bits
Each bit is sent as a voltage: 1 → +1 V, 0 → −1 V. The channel adds Gaussian noise (σ = 0.7 V). The receiver decides 1 when r > 0. Samples r[3] and r[8] cross zero, so two bits arrive flipped.
sent 11 10 00 01 01 11 volts 1 -> +1 V, 0 -> −1 V noise Gaussian, σ = 0.7 V (seeded) r[3] = +0.26 V (sent −1 V) -> 1, wrong r[8] = +0.18 V (sent −1 V) -> 1, wrong received 11 11 00 01 11 11 2 of 12 bits flipped noise and SNR: see "Noise and decisions"States and the trellis
The register's two older bits are the encoder's state: 00, 01, 10 or 11, so 2^(K−1) = 4 states. A trellis shows every state at every time; each bit takes one branch. The message 1011 is one path through it.
state = u[n−1] u[n−2] (4 states) from each state two branches: solid = input bit 0 dashed = input bit 1 the encoder's path: 00 → 10 → 01 → 10 → 11 → 01 → 00 the 2 tail zeros end it at 00Branch metrics: count mismatches
Each branch has the pair the encoder would send on it. Its branch metric counts the bits that differ from what arrived: the Hamming distance. At n = 1 we received 11, but the true branch 10 → 01 sent 10: metric 1.
received at n = 1: 11 00→00 sent 00 metric 2 00→10 sent 11 metric 0 10→01 sent 10 metric 1 10→11 sent 01 metric 1 branch metric = Hamming distance (number of differing bits) the noise flipped bit 3, so the true branch costs 1, not 0Add, compare, select
A path metric adds up the branch metrics along a path. Two paths enter each state; the decoder keeps the one with the smaller total, the survivor. So only 4 survivors live at any time, not 2ⁿ paths.
path metric at each state, per time n (∞ = not reachable yet) n=0 00:0 01:∞ 10:∞ 11:∞ n=1 00:2 01:∞ 10:0 11:∞ n=2 00:4 01:1 10:2 11:1 n=3 00:3 01:2 10:1 11:2 n=4 00:3 01:2 10:3 11:1 n=5 00:2 01:2 10:3 11:2 n=6 00:2 01:3 10:2 11:3 add: old path metric + branch metric compare: the two paths into a state select: keep the smallerTrace back the survivor
The tail zeros end the encoder in state 00. Its survivor has path metric 2: the two flips. Followed backwards, its branches give the inputs 1011 (then 0 0): the message, with both errors corrected.
end state 00, path metric 2 survivor path: 00 → 10 → 01 → 10 → 11 → 01 → 00 input bits on it: 1 0 1 1 0 0 decoded 1011 sent 1011 ✓ free distance of this code: 5 -> corrects 2 errors that are spread outSoft decisions keep the voltages
Deciding 0 or 1 first discards how sure we were: r[3] = +0.26 V is a weak 1, r[0] = +1.47 V a strong one. A soft-decision decoder uses squared distances in volts. It picks the same path here and, on average, errs less.
hard metric: Hamming distance (bits) soft metric: Σ (r − expected volts)² soft Viterbi, same trellis: survivor 00 → 10 → 01 → 10 → 11 → 01 → 00 decoded 1011 path metric 4.57 V² weak samples (near 0 V) now count littleLDPC: many sparse parity checks
LDPC codes (Gallager, 1960) are block codes built from many parity checks, each on only a few bits. A Tanner graph draws bits as circles and checks as squares. Here 6 bits meet 4 checks; each check's bits must add to 0 mod 2.
H (rows = checks, columns = bits) 1 1 0 1 0 0 0 1 1 0 1 0 1 0 0 0 1 1 0 0 1 1 0 1 edge = a 1 in H codeword x = 0 0 1 0 1 1 H·x mod 2 = 0 0 0 0 (all checks pass) real LDPC codes: thousands of bits, only a few 1s per column ("low density")Bit flipping fixes the error
Bit 0 arrives flipped, so checks 0 and 2 fail. Flip the bit that sits in the most failed checks: bit 0, in both. Now every check passes. 5G NR data channels and Wi-Fi 6 use LDPC codes.
received 1 0 1 0 1 1 checks 1 0 1 0 (1 = fails) failed checks per bit: 2 1 0 1 1 1 bit 0 is in 2 failed checks -> flip it decoded 0 0 1 0 1 1 checks 0 0 0 0 ✓ real decoders pass soft beliefs (belief propagation)
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01The encoder uses c1 = u[n] ⊕ u[n−1] ⊕ u[n−2] and c2 = u[n] ⊕ u[n−2]. The window holds u[n] = 1, u[n−1] = 1, u[n−2] = 0. What does it send?
c1 = 1 ⊕ 1 ⊕ 0 = 0 and c2 = 1 ⊕ 0 = 1, so it sends 0 1. This is step n = 3 of the worked example, window 110.
02Why are these codes called convolutional?
c[n] = Σ g[j]·u[n−j] mod 2 is a discrete convolution of u with the generator g. That is why np.convolve(u, [1, 1, 1]) % 2 reproduces the c1 stream.
03A convolutional code has constraint length K = 7. How many states does its trellis have?
The state is the K − 1 = 6 older bits in the register, so there are 2⁶ = 64 states. Decoding work grows with the number of states, which is why K stays small.
04In add-compare-select, what does the decoder keep at each state and each time?
Two paths enter each state. Any later continuation adds the same amount to both, so the one with the larger total can never win: only the survivor is kept.
05An LDPC decoder finds checks 0 and 2 failing. Bit 0 is in both of them; every other bit is in at most one. What does bit flipping do?
Gallager's bit flipping flips the bit involved in the most unsatisfied checks and repeats. Here one flip of bit 0 makes every check pass.
Glossary
- Convolutional code
- A code that slides a window over the message stream and sends several parity bits per message bit, each a mod-2 sum of bits in the window.
- Constraint length K
- How many message bits the encoder's window holds: the new bit and the K − 1 before it.
- Code rate
- Message bits per sent bit. Two code bits per message bit gives rate 1/2.
- State and trellis
- The state is the K − 1 older bits in the register. A trellis draws every state at every time step, joined by the branches the encoder can take.
- Branch metric, path metric
- A branch metric scores one branch against what was received (Hamming distance for hard decisions). A path metric is the sum along a path.
- Viterbi algorithm
- Andrew Viterbi's 1967 decoder: at every state keep only the best path (add-compare-select), then trace the survivor back. It finds the most likely path.
- Free distance
- The smallest Hamming distance between two different encoded streams. For the (7, 5) code it is 5, so two spread-out errors can be corrected.
- LDPC code, Tanner graph
- A block code defined by a sparse parity-check matrix H. Its Tanner graph joins bit nodes to check nodes wherever H has a 1.
Notes and sources
- The code is the rate-1/2, K = 3 convolutional code with generators 111 and 101 (octal 7 and 5), free distance 5, the "(3, (7, 5))" code of MIT 6.02, ch. 8. Practical systems use longer codes, such as K = 7 with rate 1/2, used since the Voyager program.
- The noise is one repeatable draw of Gaussian noise with σ = 0.7 V from a seeded generator; a different draw flips different bits. Every number in the figure is computed from this model.
- The LDPC example is a toy: 6 bits are far too few for H to be truly sparse, and real decoders pass soft beliefs (belief propagation) instead of flipping hard bits. The idea (parity checks on few bits, decoded iteratively) is the same.
- MIT OCW 6.02 Digital Communication Systems (Balakrishnan, Verghese, Fall 2012), ch. 7: Convolutional Codes: Construction and Encoding
- MIT OCW 6.02, ch. 8: Viterbi Decoding of Convolutional Codes (hard and soft decisions, free distance of the (7, 5) code)
- MIT OCW 6.02 Introduction to EECS II: Digital Communication Systems (course page)
- R. G. Gallager, Low Density Parity Check Codes, Sc.D. thesis, MIT, 1960 (DSpace record)
- 3GPP TS 38.212, NR; Multiplexing and channel coding (LDPC for 5G NR data channels)
- MIT OCW 6.450 Principles of Digital Communications I (L. Zheng, R. Gallager)
- Convolutional code (Wikipedia): constraint length, free distance, K = 7 rate-1/2 code since Voyager
- Viterbi algorithm (Wikipedia): proposed by Andrew Viterbi in 1967 for convolutional codes
- Viterbi decoder (Wikipedia): branch metrics, add-compare-select, traceback, soft decisions
- Low-density parity-check code (Wikipedia): Gallager 1960, 5G NR data channels, Wi-Fi 802.11ax, DVB-S2
- Tanner graph (Wikipedia): bipartite graph of a parity-check matrix
- NumPy documentation: numpy.convolve (mode 'full', length N + M − 1)
Quiz Unlocks after the last step.