S841 of 56
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

S8 · Lesson 41 · Coding lab

A Hamming (7,4) encoder and syndrome decoder, its block error rate by simulation, a Viterbi decoder for the rate-1/2 K = 3 code, and free distances found by searching the trellis

Lessons 34 to 37 described codes by their distances and their decoders by what they can correct. In this lab you'll build two of them and test those claims exhaustively or statistically: a Hamming code that must correct every single error in every codeword, a Viterbi decoder that must undo scattered errors in a convolutional codeword, and a search that finds the free distance of any small convolutional code, the number that sets its coding gain.

  • The curves are the block error probability of 4 data bits over a binary symmetric channel with crossover p: sent raw, 1−(1−p)4, and Hamming (7,4)-coded, 1−(1−p)7−7p(1−p)6.
  • At the same crossover probability the code wins for every p below one half. A fair comparison also charges it for its extra bits: at equal energy per data bit, each coded bit gets only 4/7 of the energy and flips more often.
  • At p = 10⁻³ it cuts the block error rate from 4.0 × 10⁻³ to 2.1 × 10⁻⁵.

Predict

The rate-1/2, K = 3 convolutional code (generators 7 and 5 in octal) has free distance 5. How many channel errors can a hard-decision Viterbi decoder always correct, if they're spread far enough apart?

Name it

What the lab computes

  • Hamming (7,4): data d1…d4 and parity p1=d1+d2+d4, p2=d1+d3+d4, p3=d2+d3+d4 (mod 2), sent as d1d2d3d4p1p2p3. The syndrome of a received word matches the column of the parity-check matrix at the position of a single error.
  • Block error rate on a binary symmetric channel: the code fails when two or more of the seven bits flip.
  • Convolutional encoding with constraint length K and generators gi (octal): shift each input bit into a K-bit register, newest bit highest, and send the parity of the register ANDed with each generator. Two zeros flush the K = 3 encoder back to state 0.
  • Viterbi decoding: for each state, keep the path with the smallest Hamming distance to the received bits; at the end, take the survivor in state 0.
  • Free distance: the smallest output weight of a path that leaves state 0 and returns to it, found by a shortest-path search over the trellis with output weights as edge lengths.

Check

Write ham_encode(d) for a list of 4 bits, returning 7 bits in the order d1d2d3d4p1p2p3, and ham_decode(r), which corrects any single error by its syndrome and returns the 4 data bits.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write ham_block_error(p, nblocks, seed): send nblocks random 4-bit messages through your Hamming code and a binary symmetric channel with crossover p (random.Random(seed)), and return the fraction decoded wrongly.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write conv_encode(bits) for the rate-1/2, K = 3 code with generators 7 and 5 (octal), appending two flushing zeros, and viterbi(r), a hard-decision decoder that returns the message bits (without the flush).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write free_distance(g, K): the free distance of a rate-1/n convolutional code with generators g (a tuple of integers) and constraint length K, by a shortest-path search from the first step out of state 0 back to state 0.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why does a Viterbi decoder need to keep only one path per state, and what does that make its cost?