Linear Block Codes
The Hamming code has a property that makes it practical: the sum (XOR) of any two codewords is again a codeword. Codes with this property are linear codes. Linearity turns coding into linear algebra over the two-element field, so encoding is a matrix multiplication, and many properties follow from rank and null-space arguments (Linear Algebra & Matrix Theory).
Arithmetic modulo 2
Over the binary field , addition is XOR and multiplication is AND. Vectors of bits form the space . Everything from linear algebra (span, rank, null space, Gaussian elimination) works, with % 2 after each operation.
Definition
An linear code is a -dimensional subspace of . It has codewords and rate . It is described by:
- a generator matrix whose rows are a basis: codewords are ;
- an parity-check matrix whose null space is the code: is a codeword iff . Then .
If (systematic form), then .
import numpy as np
def gf2_rank(M):
M = M.copy() % 2; r = 0
for c in range(M.shape[1]):
piv = np.nonzero(M[r:, c])[0]
if len(piv) == 0:
continue
p = r + piv[0]; M[[r, p]] = M[[p, r]]
for i in range(M.shape[0]):
if i != r and M[i, c]:
M[i] ^= M[r]
r += 1
if r == M.shape[0]:
break
return r
P = np.array([[1, 1, 0, 1], [1, 0, 1, 1], [0, 1, 1, 1]]) # a [7, 3] code
G = np.hstack([np.eye(3, dtype=int), P])
H = np.hstack([P.T, np.eye(4, dtype=int)])
print("G H^T mod 2 is zero:", not np.any(G @ H.T % 2))
print("rank of G:", gf2_rank(G), " rank of H:", gf2_rank(H))
C = np.array([np.array(s) @ G % 2 for s in np.ndindex(2, 2, 2)])
print("closed under XOR:", all(any(np.array_equal((a + b) % 2, c) for c in C) for a in C for b in C))G H^T mod 2 is zero: True
rank of G: 3 rank of H: 4
closed under XOR: True
Minimum distance equals minimum weight
For linear codes, the distance between codewords and is the weight of , which is itself a codeword. So the minimum distance is the smallest weight of a non-zero codeword, which takes checks instead of . Equivalently, is the smallest number of columns of that sum to zero.
import numpy as np
P = np.array([[1, 1, 0, 1], [1, 0, 1, 1], [0, 1, 1, 1]])
G = np.hstack([np.eye(3, dtype=int), P])
C = np.array([np.array(s) @ G % 2 for s in np.ndindex(2, 2, 2)])
w = C.sum(axis=1)
print("weights of the 8 codewords:", sorted(w.tolist()))
print("minimum distance d =", int(w[w > 0].min()), "-> corrects", (int(w[w > 0].min()) - 1) // 2, "errors")weights of the 8 codewords: [0, 4, 4, 4, 4, 4, 4, 4]
minimum distance d = 4 -> corrects 1 errors
This code is the simplex code, the dual of the Hamming code: every non-zero codeword has weight 4.
Systematic form by Gaussian elimination
Any generator matrix can be brought to systematic form (possibly after permuting columns) by row operations, which do not change the code:
import numpy as np
def systematic(G):
G = G.copy() % 2; k, n = G.shape; cols = list(range(n))
for r in range(k):
c = next(c for c in range(r, n) if np.any(G[r:, cols[c]]))
cols[r], cols[c] = cols[c], cols[r]
p = r + np.nonzero(G[r:, cols[r]])[0][0]
G[[r, p]] = G[[p, r]]
for i in range(k):
if i != r and G[i, cols[r]]:
G[i] ^= G[r]
return G[:, cols], cols
G = np.array([[1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 1, 1, 1, 1],
[0, 1, 1, 0, 0, 1, 1],
[1, 0, 1, 0, 1, 0, 1]])
Gs, cols = systematic(G)
print(Gs)
print("column order:", cols)[[1 0 0 0 0 1 1]
[0 1 0 0 1 0 1]
[0 0 1 0 1 1 0]
[0 0 0 1 1 1 1]]
column order: [0, 1, 2, 3, 4, 5, 6]
The dual code
The rows of generate another linear code, the dual , of dimension . Every codeword of is orthogonal (mod 2) to every codeword of . The Hamming code and the simplex code are duals of each other.
In software
- Encoding a linear code is a bit-matrix product; with bits packed into integers it becomes XORs of rows selected by the message bits.
- Reed-Solomon codes (CDs, QR codes, RAID 6) are linear codes over larger fields , where each symbol is a byte.
Exercises
Exercise 1: Size of a code
How many codewords does a linear code have, and what is its rate?
Answer
codewords; rate .
Solution
A -dimensional space over has elements.
Exercise 2: Is it linear?
Is the code linear?
Answer
No.
Solution
is not in the code. (A linear code must also contain 000, which this one does, but that is not enough.)
Exercise 3: Parity-check matrix from G (code)
For with , build and check .
Answer
.
Solution
import numpy as np
P = np.array([[1, 1, 0], [0, 1, 1]])
G = np.hstack([np.eye(2, dtype=int), P])
H = np.hstack([P.T, np.eye(3, dtype=int)])
print(H)
print("G H^T = 0:", not np.any(G @ H.T % 2))[[1 0 1 0 0]
[1 1 0 1 0]
[0 1 0 0 1]]
G H^T = 0: True
Test yourself
A binary linear code is…
- a subspace of
- any set of words
- a code with all codewords of equal weight
- a code with distance 1
For a linear code, the minimum distance equals…
- the minimum weight of a non-zero codeword
- the length n
- the number of parity checks
- the rank of G
If , the parity-check matrix is…