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.
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.
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.
Through a noisy channel
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.
Viterbi decoding
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.
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.
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.
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.
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.
LDPC codes
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.
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.
The working
# step 1/11 · window
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"
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.