comnum.LdpcCode#

class comnum.LdpcCode(n, rows)#

An LDPC code: LdpcCode.peg(n, k, var_degree=…, seed=…) (progressive edge growth, Hu, Eleftheriou & Arnold 2005) or LdpcCode(n, rows) from the rows of a parity-check matrix. Decoded by belief propagation (see set_decoder): rule is "sum_product" (the default, as in the specifications), "min_sum", "normalized_min_sum" (scaled by alpha, 0.8 by default) or "offset_min_sum" (offset alpha); layered selects the layered schedule (Hocevar 2004, the default); 50 iterations at most by default. The PEG construction uses the variable degree 3 and the seed 1 by default.

Parameters:
  • n (int) – coded bits per block.

  • rows (list[list[int]]) – the positions of the ones of every row of the parity-check matrix.

Raises:

ValueError – for an invalid parameter (the message names it).

static peg(n, k, *, var_degree=Ellipsis, seed=Ellipsis)#

A code built by progressive edge growth.

Parameters:
  • n (int) – coded bits per block.

  • k (int) – information bits per block.

  • var_degree (int) – the degree of every variable node.

  • seed (int) – the seed of the construction.

Returns:

the code.

Return type:

LdpcCode

Raises:

ValueError – for an invalid parameter (the message names it).

Examples

>>> import comnum
>>> code = comnum.LdpcCode.peg(96, 48)
>>> code.n, code.k, code.girth() >= 6
(96, 48, True)
decode(llr)#

Decode whole blocks of LLRs (belief propagation).

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 input that is not an array of numbers of the expected shape.

Examples

>>> import comnum, numpy as np
>>> code = comnum.LdpcCode.peg(96, 48)
>>> b = comnum.bits(48, comnum.Rng(1))
>>> bool(np.array_equal(code.decode(2.0 - 4.0 * code.encode(b)), b))
True
decode_llr(llr)#

Decode one block into the a posteriori LLRs of the coded bits.

Parameters:

llr (array_like) – n LLRs (positive for a 0).

Returns:

the n a posteriori LLRs and the iterations run.

Return type:

tuple[numpy.ndarray, int]

Raises:

ValueError – for an invalid parameter or input (the message names it).

encode(bits)#

Encode whole blocks.

Parameters:

bits (array_like) – a multiple of k bits.

Returns:

n coded bits per block (uint8).

Return type:

numpy.ndarray

Raises:

ValueError – for an input that is not an array of numbers of the expected shape.

Examples

>>> import comnum
>>> comnum.LdpcCode.peg(96, 48).encode(comnum.bits(96, comnum.Rng(1))).size
192
girth()#

The girth of the Tanner graph.

Returns:

the length of its shortest cycle.

Return type:

int

is_codeword(c)#

Whether H c = 0.

Parameters:

c (array_like) – n bits.

Returns:

every parity check holds.

Return type:

bool

Raises:

ValueError – unless c has n bits.

set_decoder(rule=Ellipsis, *, alpha=Ellipsis, layered=Ellipsis, max_iterations=Ellipsis)#

Choose the decoder.

Parameters:
  • rule (str) – the check-node rule, "sum_product", "min_sum", "normalized_min_sum" (scaled by alpha) or "offset_min_sum" (with the offset alpha), as the specifications.

  • alpha (float) – the scale or the offset of the min-sum rules.

  • layered (bool) – the layered schedule (else flooding).

  • max_iterations (int) – iterations at most.

Raises:

ValueError – for an invalid parameter (the message names it).

k#

Information bits per block.

Type:

int

n#

Coded bits per block.

Type:

int

rate#

Code rate.

Type:

float

rows#

The rows of the parity-check matrix (the positions of their ones).

Type:

list[list[int]]