galois.BCH.decode(codeword: ndarray | GF2, errors: False = False) ndarray | GF2
galois.BCH.decode(codeword: ndarray | GF2, errors: True) Tuple[ndarray | GF2, integer | ndarray]

Decodes the BCH codeword c into the message m.

Parameters
codeword: ndarray | GF2

The codeword as either a n-length vector or (N,n) matrix, where N is the number of codewords. For systematic codes, codeword lengths less than n may be provided for shortened codewords.

errors: False = False
errors: True

Optionally specify whether to return the number of corrected errors. The default is False.

Returns

  • The decoded message as either a k-length vector or (N,k) matrix.

  • Optional return argument of the number of corrected bit errors as either a scalar or n-length vector. Valid number of corrections are in [0,t]. If a codeword has too many errors and cannot be corrected, -1 will be returned.

Notes

The codeword vector c is defined as c=[cn1,,c1,c0]GF(2)n, which corresponds to the codeword polynomial c(x)=cn1xn1++c1x+c0. The message vector m is defined as m=[mk1,,m1,m0]GF(2)k, which corresponds to the message polynomial m(x)=mk1xk1++m1x+m0.

In decoding, the syndrome vector s is computed by s=cHT, where H is the parity-check matrix. The equivalent polynomial operation is s(x)=c(x) mod g(x). A syndrome of zeros indicates the received codeword is a valid codeword and there are no errors. If the syndrome is non-zero, the decoder will find an error-locator polynomial σ(x) and the corresponding error locations and values.

For the shortened BCH(ns,ks) code (only applicable for systematic codes), pass ns bits into decode() to return the ks-bit message.

Examples

Encode a single message using the BCH(15,7) code.

In [1]: bch = galois.BCH(15, 7)

In [2]: GF = galois.GF(2)

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

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

Corrupt t bits of the codeword.

In [5]: bch.t
Out[5]: 2

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

Decode the codeword and recover the message.

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

In [8]: np.array_equal(d, m)
Out[8]: True

Decode the codeword, specifying the number of corrected errors, and recover the message.

In [9]: d, e = bch.decode(c, errors=True); d, e
Out[9]: (GF([0, 0, 0, 1, 1, 1, 0], order=2), 2)

In [10]: np.array_equal(d, m)
Out[10]: True

Encode a single message using the shortened BCH(12,4) code.

In [11]: bch = galois.BCH(15, 7)

In [12]: GF = galois.GF(2)

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

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

Corrupt t bits of the codeword.

In [15]: bch.t
Out[15]: 2

In [16]: c[0:bch.t] ^= 1; c
Out[16]: GF([1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1], order=2)

Decode the codeword and recover the message.

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

In [18]: np.array_equal(d, m)
Out[18]: True

Decode the codeword, specifying the number of corrected errors, and recover the message.

In [19]: d, e = bch.decode(c, errors=True); d, e
Out[19]: (GF([0, 0, 1, 0], order=2), 2)

In [20]: np.array_equal(d, m)
Out[20]: True

Encode a matrix of three messages using the BCH(15,7) code.

In [21]: bch = galois.BCH(15, 7)

In [22]: GF = galois.GF(2)

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

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

Corrupt the codeword. Add zero errors to the first codeword, one to the second, and two to the third.

In [25]: c[1,0:1] ^= 1

In [26]: c[2,0:2] ^= 1

In [27]: c
Out[27]: 
GF([[1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0],
    [1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0],
    [0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1]], order=2)

Decode the codeword and recover the message.

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

In [29]: np.array_equal(d, m)
Out[29]: True

Decode the codeword, specifying the number of corrected errors, and recover the message.

In [30]: d, e = bch.decode(c, errors=True); d, e
Out[30]: 
(GF([[1, 1, 0, 1, 0, 0, 1],
     [0, 0, 1, 1, 0, 0, 1],
     [1, 0, 0, 0, 0, 0, 1]], order=2),
 array([0, 1, 2]))

In [31]: np.array_equal(d, m)
Out[31]: True

Encode a matrix of three messages using the shortened BCH(12,4) code.

In [32]: bch = galois.BCH(15, 7)

In [33]: GF = galois.GF(2)

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

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

Corrupt the codeword. Add zero errors to the first codeword, one to the second, and two to the third.

In [36]: c[1,0:1] ^= 1

In [37]: c[2,0:2] ^= 1

In [38]: c
Out[38]: 
GF([[1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1],
    [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1]], order=2)

Decode the codeword and recover the message.

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

In [40]: np.array_equal(d, m)
Out[40]: True

Decode the codeword, specifying the number of corrected errors, and recover the message.

In [41]: d, e = bch.decode(c, errors=True); d, e
Out[41]: 
(GF([[1, 1, 1, 1],
     [0, 0, 0, 0],
     [1, 0, 1, 1]], order=2),
 array([0, 1, 2]))

In [42]: np.array_equal(d, m)
Out[42]: True