TExcelTAD
Home › Information Theory › Linear Block Codes

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 F2\mathbb{F}_2, addition is XOR and multiplication is AND. Vectors of nn bits form the space F2n\mathbb{F}_2^n. Everything from linear algebra (span, rank, null space, Gaussian elimination) works, with % 2 after each operation.

Definition

An [n,k][n, k] linear code is a kk-dimensional subspace of F2n\mathbb{F}_2^n. It has 2k2^k codewords and rate kn\frac{k}{n}. It is described by:

  • a k×nk \times n generator matrix GG whose rows are a basis: codewords are sG\mathbf{s}G;
  • an (n−k)×n(n - k) \times n parity-check matrix HH whose null space is the code: t\mathbf{t} is a codeword iff Ht=0H\mathbf{t} = \mathbf{0}. Then GHT=0GH^T = 0.

If G=[Ik∣P]G = [I_k \mid P] (systematic form), then H=[PT∣In−k]H = [P^T \mid I_{n-k}].

python · try it yourself
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 a\mathbf{a} and b\mathbf{b} is the weight of a+b\mathbf{a} + \mathbf{b}, which is itself a codeword. So the minimum distance dd is the smallest weight of a non-zero codeword, which takes 2k2^k checks instead of 4k4^k. Equivalently, dd is the smallest number of columns of HH that sum to zero.

python · try it yourself
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 [7,3][7, 3] 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 [Ik∣P][I_k \mid P] (possibly after permuting columns) by row operations, which do not change the code:

python · try it yourself
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 HH generate another linear code, the dual C⊥C^\perp, of dimension n−kn - k. Every codeword of CC is orthogonal (mod 2) to every codeword of C⊥C^\perp. The Hamming [7,4][7, 4] code and the simplex [7,3][7, 3] 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 F28\mathbb{F}_{2^8}, where each symbol is a byte.

Exercises

Exercise 1: Size of a code

How many codewords does a [15,11][15, 11] linear code have, and what is its rate?

Answer

211=20482^{11} = 2048 codewords; rate 1115≈0.733\frac{11}{15} \approx 0.733.

Solution

A kk-dimensional space over F2\mathbb{F}_2 has 2k2^k elements.

Exercise 2: Is it linear?

Is the code {000,011,101,111}\{000, 011, 101, 111\} linear?

Answer

No.

Solution

011+101=110011 + 101 = 110 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 G=[I2∣P]G = [I_2 \mid P] with P=[110011]P = \begin{bmatrix} 1 & 1 & 0 \\ 0 & 1 & 1 \end{bmatrix}, build HH and check GHT=0GH^T = 0.

Answer

H=[101001101001001]H = \begin{bmatrix} 1 & 0 & 1 & 0 & 0 \\ 1 & 1 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 & 1 \end{bmatrix}.

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

  1. a subspace of F2n\mathbb{F}_2^n
  2. any set of 2k2^k words
  3. a code with all codewords of equal weight
  4. a code with distance 1

For a linear code, the minimum distance equals…

  1. the minimum weight of a non-zero codeword
  2. the length n
  3. the number of parity checks
  4. the rank of G

If G=[Ik∣P]G = [I_k \mid P], the parity-check matrix is…

  1. [PT∣In−k][P^T \mid I_{n-k}]
  2. [Ik∣PT][I_k \mid P^T]
  3. GTG^T
  4. [P∣Ik][P \mid I_k]