Parity check matrices

In LDPC error correction codes are represented in terms of their parity check matrix stored in numpy.ndarray format. As an example, the parity check matrix for the repetition code can be loaded from the ldpc.codes submodule as follows:

[3]:
import numpy as np
from ldpc.codes import rep_code
n=5 #specifies the lenght of the repetition code
H=rep_code(n) #returns the repetition code parity check matrix
print(H)
[[1 1 0 0 0]
 [0 1 1 0 0]
 [0 0 1 1 0]
 [0 0 0 1 1]]

To compute the [n,k,d] code parameters we can use functions from the ldpc.mod2 and ldpc.code_util submodules. Below is an example showing how to calculate the code parameters of the Hamming code:

[2]:
from ldpc.codes import hamming_code #function for generating Hamming codes
from ldpc.mod2 import rank #function for calcuting the mod2 rank
from ldpc.code_util import compute_code_distance #function for calculting the code distance

H=hamming_code(3)
print(H)
n=H.shape[1] #block length of the code
k=n-rank(H) #the dimension of the code computed using the rank-nullity theorem.
d=compute_code_distance(H) #computes the code distance
print(f"Hamming code parameters: [n={n},k={k},d={d}]")
[[0 0 0 1 1 1 1]
 [0 1 1 0 0 1 1]
 [1 0 1 0 1 0 1]]
Hamming code parameters: [n=7,k=4,d=3]

Note that computing the code distance quickly becomes intractable for larger parity check matrices. The ldpc.code_util.compute_code_distance should therefore only be used for small codes.