Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Convolutional codes and the Viterbi algorithm

ProblemEncode a continuous bit stream with a short shift register, and decode it with maximum likelihood using soft channel information
Devised byPeter Elias (1955), convolutional codes; Andrew Viterbi (1967), the decoding algorithm; Jim Omura (1969) and David Forney (1973), its interpretation as dynamic programming on a trellis
AccuracyViterbi decoding is maximum-likelihood over the whole sequence; the K = 7 code with soft decisions reaches at about 4 dB, 5.5 dB better than uncoded BPSK (figure below)
Cost add-compare-select operations per information bit, and memory for bits
Read firstLinear block codes and Hamming codes, Dynamic programming
APIConvolutionalCode (new, nasa_k7, encode, decode_soft, decode_hard, outputs, tail_len)

Overview

A block code encodes each block of data separately. A convolutional code has no blocks: it encodes a continuous stream, and each output bit depends on the current input bit and on several previous ones.

The encoder is a shift register. It remembers the last input bits, and for each new input bit it emits output bits (so the rate is ). Each output bit is the parity (XOR) of a fixed subset of the current input and the remembered bits; the subsets are specified by the generator polynomials. The number is the constraint length. Because every input bit influences successive groups of outputs, the information about each bit is spread over a stretch of the transmitted stream, and an error can be corrected from the bits around it.

The decoder exploits the encoder’s structure. The encoder is a finite-state machine with states (the contents of its memory), and its possible behaviours over time can be drawn as a graph called a trellis. Every possible message is a path through the trellis. The Viterbi algorithm finds the path whose output is closest to what was received. It works equally well on hard bit decisions or on soft reliabilities.

Where it is used

Convolutional codes with Viterbi decoding were the workhorse of power-limited links for decades, because the soft-decision decoder squeezes several decibels out of a noisy channel.

  • Deep-space missions. Deep-space probes (from the later phases of Voyager onward) used constraint-length-7 convolutional codes (often followed by a Reed-Solomon outer code) to send pictures home at a few kilobits per second from billions of kilometres.
  • Mobile phones and satellite TV. GSM and 3G voice channels, digital satellite broadcasting (DVB-S) and Wi-Fi (802.11a/g) all protect their bits with convolutional codes, trading a little bandwidth for a much lower error rate.
  • Beyond communications. The same Viterbi recursion finds the best path through any trellis, which is how speech recognisers, gene finders and the hidden Markov models of the learning chapter decode their most likely state sequence.

History

Peter Elias introduced convolutional codes in 1955. Early decoders were sequential (Wozencraft 1957, Fano 1963). Andrew Viterbi’s 1967 algorithm was first presented as a proof device. Jim Omura recognised it as dynamic programming, and David Forney (1973) showed it was maximum-likelihood. With the NASA standard , rate-1/2 code, generators 171 and 133 octal, it flew on Voyager (concatenated with Reed-Solomon) and became the inner code of satellite TV, 802.11a/g Wi-Fi and GSM. Viterbi co-founded Qualcomm. Turbo codes (1993) and LDPC codes later surpassed it, but the trellis idea lives on in their decoders and in equalisers.

The idea

The encoder

For constraint length , the register holds the current input and the previous ones. Generator is a -bit mask, whose most significant bit multiplies the newest input; output is the parity of the register bits selected by the mask. Generators are conventionally written in octal.

An example. The classic code has generators in octal, that is and in binary. With input and the two previous inputs (most recent) and , the two outputs are and . Starting from an empty register, the input is encoded as follows:

input memory outputs
10011
01010
10100
11001

The four states are the four possible contents of the memory. From each state there are two transitions, one for each input bit, each labelled with its pair of output bits; drawing the states at successive times, joined by these transitions, gives the trellis.

Distance. The measure of a convolutional code’s strength is its free distance: the smallest number of positions in which two different output sequences can differ. For the code it is 5, so two errors within a constraint span can be corrected. The code has free distance 10.

Termination. Appending zeros to the message returns the register to the all-zero state. This costs a little rate, but the decoder then knows where the path ends, which protects the last bits of the message as well as the rest.

Maximum-likelihood decoding on the trellis

What to minimise. The best decoder chooses the codeword that is most likely to have produced the received signal. For each transmitted bit , the demodulator supplies a log-likelihood ratio , positive when the bit looks like a 0. Compared with the all-zero codeword, a candidate codeword changes the log-likelihood by for every position where it has a 1. So the most likely codeword is the one that minimises the cost

the total evidence it contradicts. With hard decisions, , this is equivalent to minimising the Hamming distance from the received bits.

Why dynamic programming applies. A message of bits corresponds to one of paths through the trellis, too many to try. But the cost is a sum of contributions from the successive transitions of the path. If two paths arrive at the same state at the same time, their futures are identical, so whichever has the higher cost so far can never win and may be discarded. This is the principle of optimality, and the resulting procedure is the Viterbi algorithm (the same one that decodes hidden Markov models).

The algorithm. For each state, keep only the cheapest path reaching it, the survivor, with its cost. At each time step:

  1. Add. Extend every survivor by both possible input bits, adding the cost of the output bits on each transition.
  2. Compare. Two candidate paths enter each new state.
  3. Select. Keep the cheaper as the new survivor and record which it was.

At the end, trace back from the zero state (or from the cheapest state, if the code was not terminated) through the recorded choices to read off the message. The work per bit is proportional to the number of states, , which is why practical constraint lengths are small.

Figure 19.4. Viterbi BER.

Soft decisions are worth about 2 dB over hard ones, a classic rule of thumb that the figure confirms. The larger constraint length gains almost 2 dB more, at 16 times the decoding work. Soft Viterbi decoding of the K = 7 code (64 states) ran at 1.83 Mbit/s here.

Algorithm

For a code of constraint length with generators.

Encoding. The register holds the previous input bits, initially zero. For each input bit, and then for each of the tail zeros if the code is terminated:

  1. form the -bit word consisting of the new bit followed by the register;
  2. for each generator, output the parity of that word ANDed with the generator’s mask;
  3. shift the new bit into the register, discarding the oldest.

Viterbi decoding, from the LLRs of the received bits. Each of the states has a path metric, the cost of the best path reaching it.

  1. Initialise the metric of state 0 to zero and all the others to infinity, since the encoder starts in state 0.
  2. For each time step, with its received LLRs: for each state and each input bit (0 or 1), find the next state and the output bits of that transition; the branch cost is the sum of the LLRs of the output bits that are 1; add it to the state’s metric to get a candidate. Each next state receives two candidates: keep the smaller as its new metric, and record which predecessor and input bit it came from.
  3. Trace back. Start at state 0 if the code was terminated, otherwise at the state of smallest metric. Follow the recorded predecessors back to the start, collecting the input bits, and reverse them.
  4. Drop the tail bits.

The work is additions and comparisons per decoded bit, and the recorded decisions take bits per step.

Implementation

            for (state, &m) in metric.iter().enumerate() {
                if !m.is_finite() {
                    continue;
                }
                for input in 0..2u32 {
                    let reg = (input << (k - 1)) | state as u32;
                    // Branch cost: sum of llr over coded bits equal to 1.
                    let cost: f64 = self.outputs_for(reg).zip(obs).map(|(bit, &l)| if bit == 1 { l } else { 0.0 }).sum();
                    let ns = (reg >> 1) as usize;
                    if m + cost < next[ns] {
                        next[ns] = m + cost;
                        surv[ns] = (state as u32, input as u8);
                    }
                }
            }
  • decode_hard maps bits to LLRs of ±1 and calls decode_soft.
  • The decoder keeps the full survivor history and traces back once at the end. Streaming decoders instead trace back over a fixed window of about steps.

In practice

//! Convolutional encoding and Viterbi decoding, with hard and soft decisions.

use lib_jsl_information::forward_error_correction::ConvolutionalCode;

fn main() {
    let code = ConvolutionalCode::new(3, &[0o7, 0o5]).unwrap();
    let data = [1, 0, 1, 1, 0, 0, 1];
    let coded = code.encode(&data, true);
    println!("K = 3 (7, 5): {} bits -> {} coded bits {:?}", data.len(), coded.len(), coded);
    // Flip two well-separated coded bits: the free distance 5 corrects them.
    let mut rx = coded.clone();
    rx[2] ^= 1;
    rx[11] ^= 1;
    println!("hard Viterbi with 2 bit errors: {:?}", code.decode_hard(&rx, true).unwrap());
    // Soft decisions: unreliable bits (small |LLR|) count for less.
    let mut llr: Vec<f64> = coded.iter().map(|&b| if b == 0 { 4.0 } else { -4.0 }).collect();
    for i in [0, 1, 2, 5] {
        llr[i] = -0.3 * llr[i].signum(); // four weakly wrong values
    }
    println!("soft Viterbi with 4 weak errors: {:?}", code.decode_soft(&llr, true).unwrap());
    let nasa = ConvolutionalCode::nasa_k7();
    println!("NASA K = 7: {} outputs per bit, {} tail bits", nasa.outputs(), nasa.tail_len());
}
K = 3 (7, 5): 7 bits -> 18 coded bits [1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1]
hard Viterbi with 2 bit errors: [1, 0, 1, 1, 0, 0, 1]
soft Viterbi with 4 weak errors: [1, 0, 1, 1, 0, 0, 1]
NASA K = 7: 2 outputs per bit, 6 tail bits

Four wrong but weak values are outvoted by their reliable neighbours in the soft decoder.

Strengths

  • Simple encoders; optimal, soft-input decoding at a cost that is linear in the length.
  • Natural for streams; well understood performance (union bounds from the distance spectrum).

Weaknesses

  • Decoding cost grows as , which limits the distance achievable; several dB from capacity.
  • Errors at the output come in bursts, which an outer code (such as Reed-Solomon) must handle.

Exercises

  1. Draw the state diagram and trellis of the code, and find its free distance.
  2. Show that minimising is equivalent to maximising the likelihood of the codeword.
  3. Encode 1011 with the code, flip one bit, and run the Viterbi algorithm by hand.
  4. Puncture the code to rate 2/3 by deleting every fourth coded bit, and decode by setting the missing LLRs to zero.
  5. Estimate the bit error rate at high SNR from the free distance: .
Hints and answers
  1. The code has generators and with 4 states; its free distance is 5 (the shortest path leaving and returning to the zero state has output weight 5).
  2. For the BSC (or AWGN), maximising the likelihood of a codeword is minimising its Hamming (or Euclidean) distance to the received word; with log-likelihood ratios the cost is when .
  3. Encode the input bits through the shift register to get the coded pairs, flip one coded bit, and compute the branch metrics (Hamming distances) along the four-state trellis; the minimum-cost path recovers 1011 (plus the termination zeros).
  4. Puncturing deletes coded bits according to a pattern; the decoder inserts a zero LLR (no information) at each deleted position and decodes with the mother code’s trellis.
  5. At high SNR the first-event error probability is dominated by the free-distance paths; counts the information bit errors on those paths, and appears with the code rate because the energy per coded bit is .

Further reading

  • A. J. Viterbi, “Error bounds for convolutional codes and an asymptotically optimum decoding algorithm”, IEEE Transactions on Information Theory 13 (1967) 260–269.
  • G. D. Forney, “The Viterbi algorithm”, Proceedings of the IEEE 61 (1973) 268–278.
  • P. Elias, “Coding for noisy channels”, IRE Convention Record 3, part 4 (1955) 37–46.
  • S. Lin and D. J. Costello, Error Control Coding, 2nd ed., Pearson, 2004, chapter 12.