TExcelTAD
Home › Information Theory › Parity-Check Matrices and Syndromes

Parity-Check Matrices and Syndromes

A linear code is the null space of its parity-check matrix HH. That one fact gives a decoding method for any linear code: compute the syndrome HrH\mathbf{r} 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 t\mathbf{t} is sent and r=t+n\mathbf{r} = \mathbf{t} + \mathbf{n} is received (mod 2), then

z=Hr=Ht+Hn=Hn\mathbf{z} = H\mathbf{r} = H\mathbf{t} + H\mathbf{n} = H\mathbf{n}

because Ht=0H\mathbf{t} = \mathbf{0} for every codeword. The decoding problem becomes: given z\mathbf{z}, find the most probable noise vector n\mathbf{n} with Hn=zH\mathbf{n} = \mathbf{z}. On a BSC with f<12f < \frac{1}{2}, that is the noise vector of smallest weight.

python · try it yourself
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 2n−k2^{n-k} possible syndromes. For each, store the lowest-weight noise vector that produces it (the coset leader). Decoding is then: compute z\mathbf{z}, look up n^\hat{\mathbf{n}}, output r+n^\mathbf{r} + \hat{\mathbf{n}}.

python · try it yourself
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

python · try it yourself
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 dd can:

  • detect any d−1d - 1 errors (a non-zero error pattern of weight below dd cannot turn one codeword into another);
  • correct any ⌊d−12⌋\lfloor \frac{d - 1}{2} \rfloor errors;
  • or do a mix: correct tt and detect up to ss errors when t+s<dt + s < d, with t≤st \le s.

When retransmission is possible, detection alone is often enough (Error Detection and CRCs).

The scaling problem

Syndrome tables have 2n−k2^{n-k} entries. For a rate-12\frac{1}{2} code of length 1000 that is 25002^{500} 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 HH 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 HH as an integer array and use H @ r % 2; for speed with many words, compute all syndromes at once as R @ 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

Ht=0H\mathbf{t} = \mathbf{0} 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 [23,12][23, 12] code have?

Answer
Solution

223−12=2112^{23 - 12} = 2^{11}. (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 2112^{11}.

Answer

Both are 2048.

Solution
python · try it yourself
from math import comb

print(sum(comb(23, w) for w in range(4)), 2**11)
2048 2048

1+23+253+1771=20481 + 23 + 253 + 1771 = 2048: the radius-3 spheres around the 2122^{12} codewords fill all 2232^{23} words.

Test yourself

The syndrome HrH\mathbf{r} depends on…

  1. only the noise vector
  2. only the codeword
  3. both codeword and noise
  4. neither

A coset leader is…

  1. the lowest-weight noise vector with a given syndrome
  2. the first codeword
  3. a row of G
  4. the most probable codeword

A code with minimum distance d can detect up to…

  1. d - 1 errors
  2. d errors
  3. (d - 1)/2 errors
  4. 2d errors