class galois.BCH

A primitive, narrow-sense binary \(\textrm{BCH}(n, k)\) code.

A \(\textrm{BCH}(n, k)\) code is a \([n, k, d]_2\) linear block code with codeword size \(n\), message size \(k\), minimum distance \(d\), and symbols taken from an alphabet of size 2.

To create the shortened \(\textrm{BCH}(n-s, k-s)\) code, construct the full-sized \(\textrm{BCH}(n, k)\) code and then pass \(k-s\) bits into encode() and \(n-s\) bits into decode(). Shortened codes are only applicable for systematic codes.

Examples

Construct the BCH code.

In [1]: galois.bch_valid_codes(15)
Out[1]: [(15, 11, 1), (15, 7, 2), (15, 5, 3), (15, 1, 7)]

In [2]: bch = galois.BCH(15, 7); bch
Out[2]: <BCH Code: [15, 7, 5] over GF(2)>

Encode a message.

In [3]: m = galois.GF2.Random(bch.k); m
Out[3]: GF([0, 1, 1, 0, 1, 0, 1], order=2)

In [4]: c = bch.encode(m); c
Out[4]: GF([0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1], order=2)

Corrupt the codeword and decode the message.

# Corrupt the first bit in the codeword
In [5]: c[0] ^= 1

In [6]: dec_m = bch.decode(c); dec_m
Out[6]: GF([0, 1, 1, 0, 1, 0, 1], order=2)

In [7]: np.array_equal(dec_m, m)
Out[7]: True
# Instruct the decoder to return the number of corrected bit errors
In [8]: dec_m, N = bch.decode(c, errors=True); dec_m, N
Out[8]: (GF([0, 1, 1, 0, 1, 0, 1], order=2), 1)

In [9]: np.array_equal(dec_m, m)
Out[9]: True

Constructors

BCH(n: int, k: int, primitive_poly: PolyLike | None = None, ...)

Constructs a primitive, narrow-sense binary \(\textrm{BCH}(n, k)\) code.

String representation

__repr__() str

A terse representation of the BCH code.

__str__() str

A formatted string with relevant properties of the BCH code.

Methods

decode(codeword: ArrayLike, errors: False = False) GF2
decode(codeword: ArrayLike, errors) Tuple[GF2, integer | ndarray]

Decodes the BCH codeword \(\mathbf{c}\) into the message \(\mathbf{m}\).

detect(codeword: ArrayLike) bool_ | ndarray

Detects if errors are present in the BCH codeword \(\mathbf{c}\).

encode(message: ArrayLike, parity_only: bool = False) GF2

Encodes the message \(\mathbf{m}\) into the BCH codeword \(\mathbf{c}\).

Properties

property d : int

The design distance \(d\) of the \([n, k, d]_2\) code. The minimum distance of a BCH code may be greater than the design distance, \(d_{min} \ge d\).

property field : Type[FieldArray]

The FieldArray subclass for the \(\mathrm{GF}(2^m)\) field that defines the BCH code.

property G : GF2

The generator matrix \(\mathbf{G}\) with shape \((k, n)\).

property generator_poly : Poly

The generator polynomial \(g(x)\) whose roots are roots.

property H : FieldArray

The parity-check matrix \(\mathbf{H}\) with shape \((2t, n)\).

property is_narrow_sense : bool

Indicates if the BCH code is narrow sense, meaning the roots of the generator polynomial are consecutive powers of \(\alpha\) starting at 1, i.e. \(\alpha, \alpha^2, \dots, \alpha^{2t}\).

property is_primitive : bool

Indicates if the BCH code is primitive, meaning \(n = 2^m - 1\).

property is_systematic : bool

Indicates if the code is configured to return codewords in systematic form.

property k : int

The message size \(k\) of the \([n, k, d]_2\) code

property n : int

The codeword size \(n\) of the \([n, k, d]_2\) code

property roots : FieldArray

The \(2t\) roots of the generator polynomial. These are consecutive powers of \(\alpha\), specifically \(\alpha, \alpha^2, \dots, \alpha^{2t}\).

property t : int

The error-correcting capability of the code. The code can correct \(t\) bit errors in a codeword.


Last update: Aug 27, 2022