comnum.BchCode#
- class comnum.BchCode(m, t, *, n=None, poly=None)#
A binary BCH code of GF(2^m), narrow sense, of designed distance
2t + 1(Lin & Costello, Error Control Coding, 2nd ed., 2004, ch. 6), shortened tonbits: the outer code of DVB-S2 (ETSI EN 302 307-1 V1.4.1, Sec. 5.3.1). Systematic encoding; decoding of hard decisions by Berlekamp–Massey and a Chien search.The code follows the contract of every code of the package:
encodemaps whole blocks ofkbits toncoded bits,decodewhole blocks ofnLLRs tokbits (by their signs, a block whose decoding fails keeping its message bits as received);decode_bitsdecodes one word of hard bits with the number of corrections.- Parameters:
m (int) – degree of the field, the code length being
2^m - 1before shortening.t (int) – bit errors corrected per word.
n (int | None) – word length in bits (
2^m - 1by default).poly (int | None) – the primitive polynomial of the field, bit
ithe coefficient ofx^i(the conventional one by default).
- Raises:
ValueError – for an invalid parameter (the message names it).
Examples
The (15, 7) code correcting two errors,
g(x) = 1 + x^4 + x^6 + x^7 + x^8(Lin & Costello, ch. 6):>>> import comnum, numpy as np >>> code = comnum.BchCode(4, 2) >>> (code.n, code.k, code.generator.tolist()) (15, 7, [1, 0, 0, 0, 1, 0, 1, 1, 1]) >>> msg = np.array([1, 0, 1, 1, 0, 0, 1], dtype=np.uint8) >>> c = code.encode(msg) >>> c[[2, 11]] ^= 1 >>> decoded, corrected = code.decode_bits(c) >>> bool(np.array_equal(decoded, msg)), corrected (True, 2)
- decode(llr)#
Decode whole blocks of LLRs by their signs (hard decisions); a block whose decoding fails keeps its message bits as received.
- Parameters:
llr (array_like) – a multiple of
nLLRs (positive for a 0).- Returns:
kbits per block (uint8).- Return type:
- Raises:
ValueError – for an invalid parameter or input (the message names it).
Examples
>>> import comnum, numpy as np >>> code = comnum.BchCode(4, 2) >>> b = comnum.bits(14, comnum.Rng(1)) >>> c = code.encode(b) >>> c[[1, 20]] ^= 1 # one error in each word >>> bool(np.array_equal(code.decode(1.0 - 2.0 * c), b)) True
- decode_bits(bits)#
Decode one word of hard bits.
- Parameters:
bits (array_like) –
nreceived bits.- Returns:
the
kmessage bits and the number of corrected errors (Nonefor a detected decoding failure, the message bits then returned as received).- Return type:
tuple[numpy.ndarray, int | None]
- Raises:
ValueError – for an invalid parameter or input (the message names it).
- encode(bits)#
Encode whole blocks: every word is its message, then its parity bits.
- Parameters:
bits (array_like) – a multiple of
kbits.- Returns:
ncoded bits per block (uint8).- Return type:
- Raises:
ValueError – for an invalid parameter or input (the message names it).
Examples
>>> import comnum >>> comnum.BchCode(4, 2).encode(comnum.bits(14, comnum.Rng(1))).size 30
- generator#
The generator polynomial
g(x), lowest degree first (uint8).- Type: