class galois.BCH

A primitive, narrow-sense binary BCH(n,k) code.

A 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 BCH(ns,ks) code, construct the full-sized BCH(n,k) code and then pass ks bits into encode() and ns 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, 0, 1, 0, 1, 1], order=2)

In [4]: c = bch.encode(m); c
Out[4]: GF([0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 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, 0, 1, 0, 1, 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, 0, 1, 0, 1, 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 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 c into the message m.

detect(codeword: ArrayLike) bool_ | ndarray

Detects if errors are present in the BCH codeword c.

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

Encodes the message m into the BCH codeword 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, dmind.

property field : Type[FieldArray]

The FieldArray subclass for the GF(2m) field that defines the BCH code.

property G : GF2

The generator matrix 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 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 α starting at 1, i.e. α,α2,,α2t.

property is_primitive : bool

Indicates if the BCH code is primitive, meaning n=2m1.

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 α, specifically α,α2,,α2t.

property t : int

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