comnum.ReedSolomonCode#
- class comnum.ReedSolomonCode(m, t, *, n=None, first_root=1, root_step=1, poly=None)#
A Reed–Solomon code over GF(2^m) (symbols of
mbits) correctingtsymbols per word, with systematic encoding, errors-and-erasures decoding (Berlekamp–Massey, Chien search, Forney 1965) and shortening (Lin & Costello 2004, ch. 7).The generator polynomial has the roots
β^b, …, β^(b+2t-1)withβ = α^cfor the primitive elementαof the field of the primitive polynomialpoly:first_rootisb(1 in the textbooks, 0 in DVB, 112 in CCSDS) androot_stepisc(11 in CCSDS). The code of length2^m - 1symbols is shortened ton_symbols; a word carriesk_symbols = n_symbols - 2tinformation symbols.The code works on symbols (
encode_symbols,decode_symbols) and on bits,mper symbol, the most significant first. As for every code of the package,nandkcount bits (n = m n_symbols,k = m k_symbols):encodeanddecodetake whole blocks ofkbits andnLLRs (hard decisions),decode_bitsone block ofnhard bits with the number of corrections.- Parameters:
m (int) – bits per symbol, 2 to 16.
t (int) – symbol errors corrected per word.
n (int | None) – word length in symbols,
n_symbols(2^m - 1by default, less for a shortened code).first_root (int) – exponent
bof the first root of the generator.root_step (int) – exponent
cofβ = α^c, coprime with2^m - 1.poly (int | None) – the primitive polynomial of the field, bit
ithe coefficient ofx^i(the conventional one of Lin & Costello, Table 2.7, by default:0x11Dform = 8).
- Raises:
ValueError – for an invalid parameter (the message names it).
Examples
The RS(204, 188) outer code of DVB-T and DVB-S (ETSI EN 300 744 V1.6.1, Sec. 4.3.2; EN 300 421, Sec. 4.4):
t = 8, rootsλ^0, …, λ^15over the field ofx^8 + x^4 + x^3 + x^2 + 1, shortened from (255, 239):>>> import comnum, numpy as np >>> dvb = comnum.ReedSolomonCode(8, 8, n=204, first_root=0) >>> (dvb.n_symbols, dvb.k_symbols, dvb.t, dvb.n, dvb.k) (204, 188, 8, 1632, 1504) >>> msg = np.arange(188) % 256 >>> c = dvb.encode_symbols(msg) >>> c[[3, 50, 200]] ^= 0x5A >>> decoded, corrected = dvb.decode_symbols(c) >>> bool(np.array_equal(decoded, msg)), corrected (True, 3)
The RS(255, 223) code of CCSDS 131.0-B-5, Sec. 4.2:
t = 16over the field ofx^8 + x^7 + x^2 + x + 1with the rootsα^(11 j),j = 112, …, 143:>>> ccsds = comnum.ReedSolomonCode(8, 16, poly=0x187, first_root=112, root_step=11) >>> (ccsds.n_symbols, ccsds.k_symbols, round(ccsds.rate, 4)) (255, 223, 0.8745) >>> g = ccsds.generator >>> bool(np.array_equal(g, g[::-1])) # self-reciprocal True
- 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.ReedSolomonCode(4, 2) >>> b = comnum.bits(code.k, comnum.Rng(1)) >>> c = code.encode(b) >>> c[[0, 9]] ^= 1 # two wrong bits, in two symbols >>> bool(np.array_equal(code.decode(1.0 - 2.0 * c), b)) True
- decode_bits(bits)#
Decode one block of hard bits.
- Parameters:
bits (array_like) –
nreceived bits.- Returns:
the
kmessage bits and the number of corrected symbols (Nonefor a detected decoding failure).- Return type:
tuple[numpy.ndarray, int | None]
- Raises:
ValueError – for an invalid parameter or input (the message names it).
- decode_symbols(received, *, erasures=None)#
Decode a word of received symbols, some positions known to be unreliable. The decoder corrects
eerrors andserasures whenever2e + s <= 2t.- Parameters:
received (array_like) – the
n_symbolsreceived symbols.erasures (array_like, optional) – the erased positions (0 to
n_symbols - 1).
- Returns:
the
k_symbolsmessage symbols and the number of corrected symbols (erasures included),Nonefor a detected decoding failure (the message symbols are 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 of bits (
mper symbol, most significant first).- 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 >>> code = comnum.ReedSolomonCode(4, 2) >>> code.encode(comnum.bits(2 * code.k, comnum.Rng(1))).size 120
- encode_symbols(msg)#
The word of a message of symbols: the message, then the
2tparity symbols.- Parameters:
msg (array_like) –
k_symbolsmessage symbols, below2^m.- Returns:
the
n_symbolssymbols of the word (int64).- Return type:
- Raises:
ValueError – for an invalid parameter or input (the message names it).
- generator#
The generator polynomial
g(x), lowest degree first (2t + 1symbols, int64).- Type: