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, , and Hamming (7,4)-coded, .
- 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?
A code of distance d corrects errors, so 2 for d = 5. For a convolutional code that holds per error event: two errors close together are corrected, three close together may not be, but errors spread over the codeword are each repaired locally, so a long message can survive many errors in total.
Name it
What the lab computes
- Hamming (7,4): data and parity , , (mod 2), sent as . 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 (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 , 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?
Two paths that reach the same state at the same time have the same future: from that state on, every continuation adds the same output bits to both, and so the same distance to the received sequence. Whichever path is already farther behind can never catch up, so it can be dropped. That leaves one survivor per state at each step, and each step costs a fixed amount of work per state, so decoding is linear in the message length and proportional to the number of states, 2 to the power K − 1. That's why constraint lengths stay around 7 in practice: each extra stage of memory doubles the decoder.