-
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
into the message .- Parameters¶
- codeword: ndarray | GF2¶
The codeword as either a
-length vector or matrix, where is the number of codewords. For systematic codes, codeword lengths less than 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
-length vector or matrix.Optional return argument of the number of corrected bit errors as either a scalar or
-length vector. Valid number of corrections are in . If a codeword has too many errors and cannot be corrected, -1 will be returned.
Notes¶
The codeword vector
is defined as , which corresponds to the codeword polynomial . The message vector is defined as , which corresponds to the message polynomial .In decoding, the syndrome vector
is computed by , where is the parity-check matrix. The equivalent polynomial operation is . 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 and the corresponding error locations and values.For the shortened
code (only applicable for systematic codes), pass bits intodecode()
to return the -bit message.Examples¶
Encode a single message using the
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
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
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
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
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
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