---
title: Convolutional codes and Viterbi decoding
heading: Encoding with shift registers, generators, constraint length and the trellis, free distance and coding gain, the Viterbi algorithm, soft decisions and puncturing
eyebrow: S8 · Lesson 35 · Convolutional codes and Viterbi decoding
xp: 50
---

::: hook
Block codes chop data into independent pieces; convolutional codes run continuously, each output bit a parity of the last few input bits, produced by a shift register a few stages long. They pair naturally with soft decisions, and Andrew Viterbi's 1967 algorithm decodes them optimally with work that grows only linearly in the message length. The constraint-length-7 code that Voyager used still appears in Wi-Fi, satellite links and deep-space missions, gaining about 5 dB at a 10⁻⁵ error rate, roughly a factor of three in transmitter power.
:::

::: play
- Bit error probability against Eb/N0 for uncoded BPSK and for rate-1/2 convolutional codes with soft-decision Viterbi decoding, from the leading term of the union bound $P_b \approx B_{d}Q\left(\sqrt{2Rd_\text{free}E_b/N_0}\right)$.
- The K = 3 code (generators 7, 5 octal, $d_\text{free}$ = 5) reaches 10⁻⁵ near 5.6 dB; the K = 7 code (171, 133 octal, $d_\text{free}$ = 10) near 4.0 dB by this estimate, and about 4.2 to 4.4 dB in measurements.
- Against uncoded BPSK's 9.6 dB, that's a coding gain of about 5 dB.
:::
:::

```sim plot
view: graph
x: E = 1..12 dB
y: P = 1e-9..0.1 log
plot: Q(sqrt(2*10^(E/10))) | samples: 400 | dashed | color: ch3 | label: uncoded BPSK
plot: Q(sqrt(2*0.5*5*10^(E/10))) | samples: 400 | color: ch2 | label: K = 3, d_free = 5
plot: 36*Q(sqrt(2*0.5*10*10^(E/10))) | samples: 400 | label: K = 7, d_free = 10
plot: 1e-5 | dashed | label: 10⁻⁵
```

::: predict
## How many states does the Viterbi decoder for a constraint-length-7 convolutional code track?

- [ ] 7
- [ ] 14
- [x] 64
- [ ] 128

::: reveal
The encoder's state is the contents of its memory, K − 1 = 6 bits, so there are $2^6$ = 64 states. At each step the decoder extends 2 branches into each state, 128 branch metrics, and keeps the better path into each state. The work per decoded bit is constant, which made Viterbi decoding practical in 1970s hardware.
:::
:::

::: nameit
## The encoder

A rate-1/n convolutional code shifts each input bit into a register of length K (the **constraint length**) and outputs n bits, each the modulo-2 sum of register taps given by a **generator** polynomial, written in octal. The (7, 5) code: $g_1$ = 111, $g_2$ = 101. The encoder is a finite-state machine with $2^{K-1}$ states; unrolling it in time gives the **trellis**.

## Free distance and coding gain

The **free distance** $d_\text{free}$ is the least Hamming weight of any coded sequence that leaves the all-zero path and returns. Best rate-1/2 codes: K = 3, 5; K = 5, 7; K = 7, 10; K = 9, 12. With soft decisions,

$$P_b \approx B_{d_\text{free}}Q\left(\sqrt{\frac{2Rd_\text{free}E_b}{N_0}}\right), \qquad G_\text{asym} = 10\log(Rd_\text{free})\text{ dB}$$

where $B_d$ counts the information-bit errors on those paths (1 for the (7,5) code, 36 for the K = 7 code). Hard decisions lose about 2 dB.

## The Viterbi algorithm

At each time step, for every state, add each incoming branch's metric (squared Euclidean distance for soft decisions, Hamming distance for hard) to the survivor metric of the state it came from, and keep only the best. After a **traceback depth** of about 5K, all survivors agree on old decisions, which are output. Complexity: $2^K$ branch metrics per bit.

## Puncturing

Deleting some coded bits in a fixed pattern raises the rate with one decoder: keeping 4 of every 6 coded bits from a rate-1/2 code gives rate 3/4, as in Wi-Fi's 54 Mb/s mode.
:::

::: example
## Encode 1, 0, 1, 1 with the (7, 5) code starting from the zero state.

::: solution
1. **First input, 1.** The register (newest first) becomes 100, so the outputs are
   $$\begin{aligned} 1 \oplus 0 \oplus 0 &= 1 \\ 1 \oplus 0 &= 1 \end{aligned}$$
   giving 11.
2. **Second input, 0.** The register is 010, giving 1 and 0: 10.
3. **Third input, 1.** The register is 101, giving 0 and 0: 00.
4. **Fourth input, 1.** The register is 110, giving 0 and 1: 01.
5. **Flush.** Two flushing zeros would follow to return to state 0.

**Answer:** the coded stream is 11 10 00 01.
:::
:::

::: example
## What asymptotic gains do the K = 3 and K = 7 codes promise, and what do they deliver at 10⁻⁵?

::: solution
1. **K = 3, asymptotically.**
   $$10\log(0.5 \times 5) = 4.0\ \text{dB}$$
2. **K = 3 at 10⁻⁵.** The estimate needs 5.6 dB against 9.6, a gain of
   $$9.6 - 5.6 = 4.0\ \text{dB}$$
3. **K = 7, asymptotically.**
   $$10\log(5) = 7.0\ \text{dB}$$
4. **K = 7 at 10⁻⁵.** The many error paths ($B_d$ = 36) make the real gain at 10⁻⁵ about 5 to 5.6 dB.
5. **When the asymptote arrives.** The asymptotic figure is reached only at very low error rates.

**Answer:** K = 3 promises 4.0 dB and delivers 4.0 dB at 10⁻⁵; K = 7 promises 7.0 dB and delivers about 5 to 5.6 dB.
:::
:::

::: example
## The K = 7 rate-½ code ($d_\text{free}$ = 10) is punctured to rate ⅔ ($d_\text{free}$ = 6) and rate ¾ ($d_\text{free}$ = 5). What asymptotic coding gains do the three versions promise?

::: solution
1. **The asymptotic gain.**
   $$G = 10\log_{10}(Rd_\text{free})$$
2. **Rate ½.**
   $$10\log_{10}5 = 7.0\ \text{dB}$$
3. **Rate ⅔.**
   $$10\log_{10}4 = 6.0\ \text{dB}$$
4. **Rate ¾.**
   $$10\log_{10}3.75 = 5.7\ \text{dB}$$
5. **The trade.** Puncturing gives up about a decibel of gain for half again the throughput, with the same Viterbi decoder.

**Answer:** 7.0 dB at rate ½, 6.0 dB at rate ⅔ and 5.7 dB at rate ¾.
:::
:::

::: check
given: K = 3 | 5 | 7 | 9
answer: 2^(K - 1)
tolerance: 0

## A convolutional code has constraint length {K}. How many states does its trellis have?

::: right
Right. $2^{K-1}$ = {answer}.
:::

::: misses
- [2^K] The state is the memory, K − 1 bits; the newest bit is the input.
- [any] Not yet. The encoder remembers K − 1 bits.
:::
:::

::: check
given: d = 5 | 7 | 10 | 12; R = 0.5 | 0.333
answer: 10*log(R*d)
unit: dB
tolerance: 0.05

## A rate-{R} convolutional code has $d_\text{free}$ = {d}. What asymptotic soft-decision coding gain does it promise?

::: right
Right. $10\log(Rd_\text{free})$ = {answer} dB.
:::

::: misses
- [10*log(d)] The energy per coded bit is only R times the energy per data bit.
- [any] Not yet. $G = 10\log(Rd_\text{free})$.
:::
:::

::: check
given: B = 4 | 36; d = 5 | 10; E = 3 | 5
answer: B*Q(sqrt(2*0.5*d*10^(E/10)))
tolerance: 1%

## A rate-1/2 code with $d_\text{free}$ = {d} and $B_d$ = {B} is decoded with soft decisions at Eb/N0 = {E} dB. Roughly what bit error probability results?

::: right
Right. $B_dQ(\sqrt{2Rd_\text{free}E_b/N_0})$ = {answer}.
:::

::: misses
- [Q(sqrt(2*0.5*d*10^(E/10)))] Multiply by the number of information-bit errors on the minimum-distance paths.
- [any] Not yet. The leading term of the union bound.
:::
:::

::: check
given: K = 3 | 7 | 9
answer: 2^K
tolerance: 0

## How many branch metrics does a Viterbi decoder for constraint length {K} compute per decoded bit (rate 1/n)?

::: right
Right. $2^{K-1}$ states × 2 branches = {answer}.
:::

::: misses
- [2^(K - 1)] Each state has two incoming branches.
- [any] Not yet. States times branches per state.
:::
:::

::: check
given: b = 2 | 3 | 5 | 7; let a = b + 1
answer: b/a
tolerance: 0.5%

## A rate-1/2 code is punctured so that, for every {b} data bits, only {a} coded bits are sent. What's the new code rate?

::: right
Right. {b} data bits per {a} sent bits: {answer}.
:::

::: misses
- [a/(2*b)] That's the fraction of coded bits kept, not the code rate.
- [any] Not yet. Data bits over transmitted bits.
:::
:::

::: check
given: K = 3 | 7 | 9
answer: 5*K
tolerance: 0

## About how deep should a Viterbi decoder's traceback be for constraint length {K}?

::: right
Right. About 5K = {answer} steps, after which survivors almost always agree.
:::

::: misses
- [K] Survivors usually merge only after several constraint lengths.
- [any] Not yet. The rule of thumb is about five constraint lengths.
:::
:::

::: check
kind: parts
given: K = 3 | 5 | 7 | 9; R = 0.5 | 0.75; d = 5 | 10

## A rate-{R} convolutional code with constraint length {K} has $d_\text{free}$ = {d}.

::: part
answer: 2^(K - 1)
unit: states
tolerance: 0

### How many trellis states does it have?

::: misses
- [2^K] The state is the K − 1 previous bits.
- [any] $2^{K-1}$.
:::
:::

::: part
answer: 10*log(R*d)
unit: dB
tolerance: 0.5%

### What asymptotic soft-decision gain does it promise?

::: misses
- [any] $10\log_{10}(Rd_\text{free})$.
:::
:::
:::

::: check
kind: derivation
vars: R = 0.25..0.9, d = 3..20, B = 1..100, E = 1..30

## A code of rate R and free distance d has $B_d$ = B and runs at Eb/N0 = E (linear) with soft decisions.

::: step
answer: 10*log(R*d)

### What is its asymptotic gain in dB?
:::

::: step
answer: B*Q(sqrt(2*R*d*E))

### What bit error probability does the dominant term give?
:::

::: right
Right. The factor $B_d$ is why real gains at moderate error rates fall short of the asymptotic figure.
:::
:::

::: explain
## Why does the Viterbi algorithm only need to keep one path into each state?

::: model
The encoder's future behavior depends only on its current state, not on how it got there, and the total path metric is a sum of independent branch metrics. If two candidate paths arrive at the same state at the same time, whatever continuation lies ahead adds exactly the same future metric to both. The one that's already worse can therefore never overtake the other, so it can be discarded immediately without losing the optimal path. Keeping only the best survivor into each of the $2^{K-1}$ states at each step finds the maximum-likelihood sequence exactly, with work that grows linearly in the message length instead of exponentially in the number of possible sequences.
:::
:::

::: practice

::: check
given: K = 3 | 4 | 5 | 7 | 8 | 9
answer: 2^(K - 1)
tolerance: 0

## States for constraint length {K}?

::: misses
- [any] $2^{K-1}$.
:::
:::

::: check
given: d = 6 | 8; R = 0.5 | 0.25
answer: 10*log(R*d)
unit: dB
tolerance: 0.05

## Asymptotic gain: rate {R}, $d_\text{free}$ {d}?

::: misses
- [any] $10\log(Rd_\text{free})$.
:::
:::

::: check
given: B = 2 | 20; d = 6 | 9; E = 4 | 6
answer: B*Q(sqrt(2*0.5*d*10^(E/10)))
tolerance: 1%

## Bit error estimate: $B_d$ {B}, $d_\text{free}$ {d}, {E} dB, rate 1/2?

::: misses
- [any] $B_dQ(\sqrt{2Rd_\text{free}E_b/N_0})$.
:::
:::
:::
