-
galois.BCH.encode(message: ndarray | GF2, parity_only: bool =
False
) ndarray | GF2 Encodes the message \(\mathbf{m}\) into the BCH codeword \(\mathbf{c}\).
- Parameters¶
- message: ndarray | GF2¶
The message as either a \(k\)-length vector or \((N, k)\) matrix, where \(N\) is the number of messages. For systematic codes, message lengths less than \(k\) may be provided to produce shortened codewords.
- parity_only: bool =
False
¶ Optionally specify whether to return only the parity bits. This only applies to systematic codes. The default is
False
.
- Returns¶
The codeword as either a \(n\)-length vector or \((N, n)\) matrix. The return type matches the message type. If
parity_only=True
, the parity bits are returned as either a \(n - k\)-length vector or \((N, n-k)\) matrix.
Notes¶
The message vector \(\mathbf{m}\) is defined as \(\mathbf{m} = [m_{k-1}, \dots, m_1, m_0] \in \mathrm{GF}(2)^k\), which corresponds to the message polynomial \(m(x) = m_{k-1} x^{k-1} + \dots + m_1 x + m_0\). The codeword vector \(\mathbf{c}\) is defined as \(\mathbf{c} = [c_{n-1}, \dots, c_1, c_0] \in \mathrm{GF}(2)^n\), which corresponds to the codeword polynomial \(c(x) = c_{n-1} x^{n-1} + \dots + c_1 x + c_0\).
The codeword vector is computed from the message vector by \(\mathbf{c} = \mathbf{m}\mathbf{G}\), where \(\mathbf{G}\) is the generator matrix. The equivalent polynomial operation is \(c(x) = m(x)g(x)\). For systematic codes, \(\mathbf{G} = [\mathbf{I}\ |\ \mathbf{P}]\) such that \(\mathbf{c} = [\mathbf{m}\ |\ \mathbf{p}]\). And in polynomial form, \(p(x) = -(m(x) x^{n-k}\ \textrm{mod}\ g(x))\) with \(c(x) = m(x)x^{n-k} + p(x)\). For systematic and non-systematic codes, each codeword is a multiple of the generator polynomial, i.e. \(g(x)\ |\ c(x)\).
For the shortened \(\textrm{BCH}(n-s, k-s)\) code (only applicable for systematic codes), pass \(k-s\) bits into
encode()
to return the \(n-s\)-bit codeword.Examples¶
Encode a single message using the \(\textrm{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([1, 1, 0, 1, 1, 0, 1], order=2) In [4]: c = bch.encode(m); c Out[4]: GF([1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0], order=2)
Compute the parity bits only.
In [5]: p = bch.encode(m, parity_only=True); p Out[5]: GF([1, 0, 1, 1, 0, 1, 1, 0], order=2)
Encode a single message using the shortened \(\textrm{BCH}(12, 4)\) code.
In [6]: bch = galois.BCH(15, 7) In [7]: GF = galois.GF(2) In [8]: m = GF.Random(bch.k - 3); m Out[8]: GF([1, 0, 0, 1], order=2) In [9]: c = bch.encode(m); c Out[9]: GF([1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0], order=2)
Compute the parity bits only.
In [10]: p = bch.encode(m, parity_only=True); p Out[10]: GF([1, 1, 0, 0, 1, 1, 0, 0], order=2)
Encode a matrix of three messages using the \(\textrm{BCH}(15, 7)\) code.
In [11]: bch = galois.BCH(15, 7) In [12]: GF = galois.GF(2) In [13]: m = GF.Random((3, bch.k)); m Out[13]: GF([[0, 1, 1, 1, 0, 0, 1], [1, 0, 1, 0, 1, 1, 0], [0, 1, 0, 1, 1, 0, 1]], order=2) In [14]: c = bch.encode(m); c Out[14]: GF([[0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0], [1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1], [0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0]], order=2)
Compute the parity bits only.
In [15]: p = bch.encode(m, parity_only=True); p Out[15]: GF([[1, 0, 0, 0, 0, 0, 1, 0], [0, 1, 0, 0, 0, 1, 1, 1], [0, 1, 0, 1, 1, 1, 1, 0]], order=2)
Encode a matrix of three messages using the shortened \(\textrm{BCH}(12, 4)\) code.
In [16]: bch = galois.BCH(15, 7) In [17]: GF = galois.GF(2) In [18]: m = GF.Random((3, bch.k - 3)); m Out[18]: GF([[1, 0, 0, 1], [0, 0, 1, 0], [1, 1, 1, 1]], order=2) In [19]: c = bch.encode(m); c Out[19]: GF([[1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0], [0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1], [1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1]], order=2)
Compute the parity bits only.
In [20]: p = bch.encode(m, parity_only=True); p Out[20]: GF([[1, 1, 0, 0, 1, 1, 0, 0], [0, 1, 1, 1, 0, 0, 1, 1], [0, 1, 0, 1, 1, 0, 0, 1]], order=2)