6. Convolutional Codes & Viterbi
Codes with memory, decoded by finding the best path
Block codes like Hamming and Reed-Solomon encode fixed-size blocks independently. Convolutional codes are fundamentally different: each output bit depends on the current input and several previous inputs. The encoder has memory — a sliding window over the input stream. This seemingly simple change, combined with a brilliant decoding algorithm called the Viterbi algorithm, produces codes that outperform block codes for many applications.
Block Codes vs Convolutional Codes
Block Codes
- Encode fixed-size blocks (e.g., 4 data bits → 7 coded bits)
- No memory between blocks — each is independent
- Algebraic decoding (solve equations to find errors)
- Examples: Hamming, Reed-Solomon, BCH
Convolutional Codes
- Encode a continuous stream of bits
- Encoder has memory (internal state) — output depends on recent history
- Decoded by searching for the most likely sequence (Viterbi)
- Examples: NASA K=7, GSM K=5, Wi-Fi 802.11a/g
The Convolutional Encoder
A convolutional encoder is built from a shift register (the memory) and XOR gates (the taps). Two key parameters define the code:
- Rate R = k/n — for every k input bits, the encoder produces n output bits. A rate 1/2 code doubles the data.
- Constraint length K — the number of shift register stages plus one. K determines how far back the code "remembers."
Our running example: a rate 1/2, K=3 encoder. It takes 1 input bit and produces 2 output bits. The shift register has 2 stages (4 possible states).
Rate 1/2, K=3 Convolutional Encoder
Input ---->[S1]---->[S2] (shift register: 2 stages)
| | |
| +--XOR--+ |
+----| c1 |------+ c1 = input XOR S2 (polynomial 101)
| +-------+
| +--XOR--+
+----| c2 |---+--+ c2 = input XOR S1 XOR S2 (polynomial 111)
+-------+ | |
S1 S2
The constraint length K determines how powerful the code is, but also decoding complexity: the Viterbi decoder tracks 2(K-1) states. K=7 (64 states) is common; K=9 (256 states) is the practical upper limit for real-time hardware.
Encoding: Worked Example
Encode the input 1 0 1 1 with our rate 1/2, K=3 encoder, starting in
state 00. Rules: c1 = input XOR S2, c2 = input XOR S1 XOR S2.
| Step | Input | State (S1,S2) | c1 | c2 | Output | New State |
|---|---|---|---|---|---|---|
| 1 | 1 | 0,0 | 1^0=1 | 1^0^0=1 | 11 | 1,0 |
| 2 | 0 | 1,0 | 0^0=0 | 0^1^0=1 | 01 | 0,1 |
| 3 | 1 | 0,1 | 1^1=0 | 1^0^1=0 | 00 | 1,0 |
| 4 | 1 | 1,0 | 1^0=1 | 1^1^0=0 | 10 | 1,1 |
Encoded output: 11 01 00 10 — 4 input bits became 8 output bits (rate 1/2).
The Trellis Diagram
The encoder's behavior over time is drawn as a trellis: columns are time steps, rows are states, and edges are transitions labeled with output bits. Every input sequence traces a unique path through the trellis.
Transition table (rate 1/2, K=3)
From 00: input 0 → 00, out 00 | input 1 → 10, out 11 From 10: input 0 → 01, out 10 | input 1 → 11, out 01 From 01: input 0 → 00, out 11 | input 1 → 10, out 00 From 11: input 0 → 01, out 01 | input 1 → 11, out 10Trellis (4 time steps):
State t=0 t=1 t=2 t=3 t=4 00 --00-----00-----00-----00---* |\ |\ |\ |
| \11 | \11 | \11 | \11 10 | --10+----10+----10+-- | |\ | |\ | |\ | | | 01 | | 01 | | 01 | 01 | | --+--+---+--+---+--+- | |/11 | |/11 | |/11 | | |/ | |/ | |/ | 11 | --01+----01+----01+--Our path for input 1,0,1,1: 00 → 10 → 01 → 10 → 11 Edge labels: 11, 01, 00, 10
The Viterbi Algorithm
Given a received (possibly corrupted) sequence, Viterbi finds the trellis path closest to the received sequence. It uses dynamic programming: at each time step, for each state, keep only the best path reaching it (the "survivor"). This avoids the exponential blowup of trying all 2n possible input sequences.
- Initialize: State 00 gets metric 0; all others get ∞.
- Branch metrics: For each received pair, compute Hamming distance to each possible transition's output.
- Add-Compare-Select: For each state, compare incoming paths, keep the one with the lowest cumulative metric.
- Repeat for every received pair.
- Traceback: From the best final state, trace back through survivors to recover the decoded input.
Viterbi Decoding: Worked Example
We transmitted 11 01 00 10 but the channel corrupted the second pair.
Received: 11 **11** 00 10
(the 01 became 11).
Initialization: t=0 metrics: 00=0, others=INF
t=0→t=1: received "11"
From 00: in=0→00 out=00 dist(00,11)=2 metric=0+2=2
in=1→10 out=11 dist(11,11)=0 metric=0+0=0
t=1 metrics: 00=2 10=0 01=INF 11=INF
t=1→t=2: received "11"
From 00(2): in=0→00 out=00 d=2 m=4 | in=1→10 out=11 d=0 m=2 From 10(0): in=0→01 out=10 d=1 m=1 | in=1→11 out=01 d=1 m=1 t=2 metrics: 00=4 10=2 01=1 11=1 Survivors: 00←00 10←00 01←10 11←10
t=2→t=3: received "00"
From 00(4): in=0→00 out=00 d=0 m=4 | in=1→10 out=11 d=2 m=6 From 10(2): in=0→01 out=10 d=1 m=3 | in=1→11 out=01 d=1 m=3 From 01(1): in=0→00 out=11 d=2 m=3 | in=1→10 out=00 d=0 m=1 From 11(1): in=0→01 out=01 d=1 m=2 | in=1→11 out=10 d=1 m=2 t=3 metrics: 00=3 10=1 01=2 11=2 Survivors: 00←01 10←01 01←11 11←11
t=3→t=4: received "10"
From 00(3): in=0→00 out=00 d=1 m=4 | in=1→10 out=11 d=1 m=4 From 10(1): in=0→01 out=10 d=0 m=1 | in=1→11 out=01 d=2 m=3 From 01(2): in=0→00 out=11 d=1 m=3 | in=1→10 out=00 d=1 m=3 From 11(2): in=0→01 out=01 d=2 m=4 | in=1→11 out=10 d=0 m=2 t=4 metrics: 00=3 10=3 01=1 11=2 Survivors: 00←01 10←10 01←10 11←11
Traceback from best final state (01, metric=1):
t=4 state 01 ← from 10 at t=3 (input 0) t=3 state 10 ← from 01 at t=2 (input 1) t=2 state 01 ← from 10 at t=1 (input 0) t=1 state 10 ← from 00 at t=0 (input 1) Decoded: 1, 0, 1, 0
The last bit differs from our intended 1 0 1 1 because we did not flush
the encoder. In practice, K-1 zero tail bits are appended to drive
the encoder back to state 00, so the traceback starts from the known final state and
correctly recovers 1 0 1 1. The full transmitted sequence with tail bits
would be 11 01 00 10 01 11 (6 pairs for 4 data + 2 tail bits).
Hard vs Soft Decisions
Hard Decision
- Received signals mapped to definite 0 or 1
- Branch metric = Hamming distance
- Simple to implement
- Throws away confidence information
Soft Decision
- Preserves analog confidence (e.g., 0.9 = "probably 1")
- Branch metric = Euclidean distance
- More complex, requires multi-bit values
- ~2 dB improvement — nearly "free" performance gain
Soft-decision Viterbi decoding is used in virtually all modern systems. The 2 dB gain means you can transmit with less power or tolerate more noise for the same error rate. The only cost is a slightly more complex receiver — a trade-off every engineer happily makes.
Puncturing: Adjustable Rate
A rate 1/2 code doubles the data, which wastes bandwidth when the channel is good. Puncturing systematically deletes some output bits to increase the rate.
Puncturing rate 1/2 to rate 2/3
Original output (rate 1/2): c1: 1 0 0 1 c2: 1 1 0 0 Puncture pattern (1=keep): c1: 1 1 c2: 1 0 After puncturing: c1: 1 0 0 1 c2: 1 _ 0 _ Transmitted: 3 bits per 2 input bits = rate 2/3
The decoder treats deleted positions as erasures with neutral confidence. Common punctured rates: 2/3, 3/4, 5/6, 7/8. Higher rate = less redundancy = less error correction.
Real-World Usage
| System | Code Parameters | Notes |
|---|---|---|
| NASA Voyager (1977) | Rate 1/2, K=7 | Decoded signals from Jupiter/Saturn; concatenated with Reed-Solomon |
| GSM mobile | Rate 1/2, K=5 | Protects voice and control channels in 2G cellular |
| 802.11a/g Wi-Fi | Rate 1/2, punctured to 2/3, 3/4 | Rate adapts to channel quality |
| DVB-S satellite TV | Rate 1/2, K=7, punctured | Concatenated with Reed-Solomon for satellite links |
| Dial-up modems (V.32, V.34) | Various | Trellis-coded modulation: coding + signal constellation combined |
Convolutional codes with Viterbi decoding were a major leap: continuous encoding, efficient maximum-likelihood decoding, and flexible rate adjustment. But they still fall short of Shannon's limit by several decibels. In 1993, two French researchers showed that two simple convolutional codes, connected in parallel and decoded iteratively, come within a fraction of a decibel of the limit. These are turbo codes, and alongside LDPC codes, they represent the next revolution in error correction. The next page explores how they work.
Try It Yourself — Rate 1/2, K=3 Encoder & Viterbi Decoder
Part 1 — Encoding
Enter input bits (0s and 1s, up to 8). The encoder steps through the shift register, producing two output bits per input.
Part 2 — Channel Errors & Viterbi Decoding
Click any received bit to flip it, simulating channel noise. The Viterbi decoder searches the trellis for the path with the lowest Hamming distance to the received sequence.
Trellis — path metrics at each time step
Key Takeaways
- Convolutional codes have memory: each output depends on the current input and several previous inputs, producing a continuous coded stream.
- The encoder is a shift register with XOR taps, defined by rate (k/n) and constraint length (K). More memory = more power but exponentially more decoding.
- The trellis diagram represents all possible encoder states over time. Every input sequence is a path through the trellis.
- The Viterbi algorithm uses dynamic programming to find the trellis path closest to the received sequence, avoiding brute-force search.
- Soft-decision decoding preserves analog confidence and gives ~2 dB of "free" improvement over hard decisions.
- Puncturing lets one encoder/decoder support multiple code rates by systematically deleting output bits.