Parity-Check Matrices and Syndromes
A linear code is the null space of its parity-check matrix . That one fact gives a decoding method for any linear code: compute the syndrome of the received word. It depends only on the error, not on which codeword was sent, so the decoder can look up the most likely error for each syndrome.
The syndrome depends only on the noise
If is sent and is received (mod 2), then
because for every codeword. The decoding problem becomes: given , find the most probable noise vector with . On a BSC with , that is the noise vector of smallest weight.
import numpy as np
P = np.array([[1, 0, 1], [1, 1, 0], [1, 1, 1], [0, 1, 1]])
G = np.hstack([np.eye(4, dtype=int), P]); H = np.hstack([P.T, np.eye(3, dtype=int)])
n = np.array([0, 0, 1, 0, 0, 0, 0])
for s in [(0, 0, 0, 0), (1, 0, 1, 1), (1, 1, 1, 1)]:
r = (np.array(s) @ G + n) % 2
print("sent data", s, " syndrome", H @ r % 2)
print("H n =", H @ n % 2)sent data (0, 0, 0, 0) syndrome [1 1 1]
sent data (1, 0, 1, 1) syndrome [1 1 1]
sent data (1, 1, 1, 1) syndrome [1 1 1]
H n = [1 1 1]
Syndrome decoding with a table
There are possible syndromes. For each, store the lowest-weight noise vector that produces it (the coset leader). Decoding is then: compute , look up , output .
import numpy as np
from itertools import combinations
def syndrome_table(H):
m, n = H.shape
table = {}
for w in range(n + 1):
for pos in combinations(range(n), w):
e = np.zeros(n, dtype=int); e[list(pos)] = 1
z = tuple(int(v) for v in H @ e % 2)
if z not in table:
table[z] = e
if len(table) == 2 ** m:
return table
return table
# a [6, 3] code with minimum distance 3
P = np.array([[1, 1, 0], [0, 1, 1], [1, 0, 1]])
G = np.hstack([np.eye(3, dtype=int), P]); H = np.hstack([P.T, np.eye(3, dtype=int)])
table = syndrome_table(H)
for z, e in sorted(table.items()):
print("syndrome", z, "-> coset leader", e, "weight", int(e.sum()))syndrome (0, 0, 0) -> coset leader [0 0 0 0 0 0] weight 0
syndrome (0, 0, 1) -> coset leader [0 0 0 0 0 1] weight 1
syndrome (0, 1, 0) -> coset leader [0 0 0 0 1 0] weight 1
syndrome (0, 1, 1) -> coset leader [0 1 0 0 0 0] weight 1
syndrome (1, 0, 0) -> coset leader [0 0 0 1 0 0] weight 1
syndrome (1, 0, 1) -> coset leader [0 0 1 0 0 0] weight 1
syndrome (1, 1, 0) -> coset leader [1 0 0 0 0 0] weight 1
syndrome (1, 1, 1) -> coset leader [1 0 0 0 0 1] weight 2
Six syndromes have single-error leaders, one is zero, and one needs a weight-2 leader: this code corrects all single errors and one double-error pattern.
Measuring the decoder
import numpy as np
from itertools import combinations
P = np.array([[1, 1, 0], [0, 1, 1], [1, 0, 1]])
G = np.hstack([np.eye(3, dtype=int), P]); H = np.hstack([P.T, np.eye(3, dtype=int)])
leader = {}
for w in range(7):
for pos in combinations(range(6), w):
e = np.zeros(6, dtype=int); e[list(pos)] = 1
leader.setdefault(tuple(H @ e % 2), e)
rng = np.random.default_rng(0)
f, blocks = 0.05, 50_000
S = rng.integers(0, 2, (blocks, 3))
R = (S @ G % 2) ^ (rng.random((blocks, 6)) < f)
Z = R @ H.T % 2
E = np.array([leader[tuple(z)] for z in Z])
decoded = (R ^ E)[:, :3]
print(f"block error rate: {np.mean(np.any(decoded != S, axis=1)):.4f}")
print(f"uncoded 3 bits: {1 - (1 - f)**3:.4f}")block error rate: 0.0300
uncoded 3 bits: 0.1426
Detection versus correction
A non-zero syndrome always means that something went wrong. A code with minimum distance can:
- detect any errors (a non-zero error pattern of weight below cannot turn one codeword into another);
- correct any errors;
- or do a mix: correct and detect up to errors when , with .
When retransmission is possible, detection alone is often enough (Error Detection and CRCs).
The scaling problem
Syndrome tables have entries. For a rate- code of length 1000 that is entries. Finding the lowest-weight error for a given syndrome is NP-hard in general. Practical codes need structure that allows efficient decoding: algebraic structure (BCH, Reed-Solomon) or sparse with iterative decoding (Low-Density Parity-Check Codes).
In software
- Small codes (Hamming, extended Hamming, Golay) are decoded with syndrome tables in hardware; ECC memory controllers do this in a single clock cycle.
- Keep as an integer array and use
H @ r % 2; for speed with many words, compute all syndromes at once asR @ H.T % 2.
Exercises
Exercise 1: Syndrome of a codeword
What is the syndrome of a received word that happens to be a codeword?
Answer
Zero.
Solution
for every codeword. Note that an undetected error is exactly a noise vector that is itself a non-zero codeword.
Exercise 2: Table size
How many entries does the syndrome table of a code have?
Answer
Solution
. (This is the Golay code, which is perfect: its 2048 coset leaders are exactly all error patterns of weight at most 3.)
Exercise 3: Golay check (code)
Count the binary vectors of length 23 with weight at most 3 and compare with .
Answer
Both are 2048.
Solution
from math import comb
print(sum(comb(23, w) for w in range(4)), 2**11)2048 2048
: the radius-3 spheres around the codewords fill all words.
Test yourself
The syndrome depends on…
- only the noise vector
- only the codeword
- both codeword and noise
- neither
A coset leader is…
- the lowest-weight noise vector with a given syndrome
- the first codeword
- a row of G
- the most probable codeword
A code with minimum distance d can detect up to…
- d - 1 errors
- d errors
- (d - 1)/2 errors
- 2d errors