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 to n bits: 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: encode maps whole blocks of k bits to n coded bits, decode whole blocks of n LLRs to k bits (by their signs, a block whose decoding fails keeping its message bits as received); decode_bits decodes one word of hard bits with the number of corrections.

Parameters:
  • m (int) – degree of the field, the code length being 2^m - 1 before shortening.

  • t (int) – bit errors corrected per word.

  • n (int | None) – word length in bits (2^m - 1 by default).

  • poly (int | None) – the primitive polynomial of the field, bit i the coefficient of x^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 n LLRs (positive for a 0).

Returns:

k bits per block (uint8).

Return type:

numpy.ndarray

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) – n received bits.

Returns:

the k message bits and the number of corrected errors (None for 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 k bits.

Returns:

n coded bits per block (uint8).

Return type:

numpy.ndarray

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:

numpy.ndarray

k#

Message bits per word, n - deg g.

Type:

int

n#

Word length in bits.

Type:

int

rate#

Code rate k / n.

Type:

float

t#

Bit errors corrected per word.

Type:

int