galois.ReedSolomon.decode(codeword: ArrayLike, errors: False = False) FieldArray
galois.ReedSolomon.decode(codeword: ArrayLike, errors: True) Tuple[FieldArray, integer | ndarray]

Decodes the Reed-Solomon codeword \(\mathbf{c}\) into the message \(\mathbf{m}\).

Parameters
codeword: ArrayLike

The codeword as either a \(n\)-length vector or \((N, n)\) matrix, where \(N\) is the number of codewords. For systematic codes, codeword lengths less than \(n\) may be provided for shortened codewords.

errors: False = False
errors: True

Optionally specify whether to return the number of corrected errors. The default is False.

Returns

  • The decoded message as either a \(k\)-length vector or \((N, k)\) matrix.

  • Optional return argument of the number of corrected symbol errors as either a scalar or \(n\)-length vector. Valid number of corrections are in \([0, t]\). If a codeword has too many errors and cannot be corrected, -1 will be returned.

Notes

The codeword vector \(\mathbf{c}\) is defined as \(\mathbf{c} = [c_{n-1}, \dots, c_1, c_0] \in \mathrm{GF}(q)^n\), which corresponds to the codeword polynomial \(c(x) = c_{n-1} x^{n-1} + \dots + c_1 x + c_0\). The message vector \(\mathbf{m}\) is defined as \(\mathbf{m} = [m_{k-1}, \dots, m_1, m_0] \in \mathrm{GF}(q)^k\), which corresponds to the message polynomial \(m(x) = m_{k-1} x^{k-1} + \dots + m_1 x + m_0\).

In decoding, the syndrome vector \(\mathbf{s}\) is computed by \(\mathbf{s} = \mathbf{c}\mathbf{H}^T\), where \(\mathbf{H}\) is the parity-check matrix. The equivalent polynomial operation is the codeword polynomial evaluated at each root of the generator polynomial, i.e. \(\mathbf{s} = [c(\alpha^{c}), c(\alpha^{c+1}), \dots, c(\alpha^{c+2t-1})]\). A syndrome of zeros indicates the received codeword is a valid codeword and there are no errors. If the syndrome is non-zero, the decoder will find an error-locator polynomial \(\sigma(x)\) and the corresponding error locations and values.

For the shortened \(\textrm{RS}(n-s, k-s)\) code (only applicable for systematic codes), pass \(n-s\) symbols into decode() to return the \(k-s\)-symbol message.

Examples

Encode a single message using the \(\textrm{RS}(15, 9)\) code.

In [1]: rs = galois.ReedSolomon(15, 9)

In [2]: GF = rs.field

In [3]: m = GF.Random(rs.k); m
Out[3]: GF([10,  1, 15, 12,  8,  1,  0,  7, 12], order=2^4)

In [4]: c = rs.encode(m); c
Out[4]: GF([10,  1, 15, 12,  8,  1,  0,  7, 12,  4, 12,  8,  6,  2,  5], order=2^4)

Corrupt \(t\) symbols of the codeword.

In [5]: e = GF.Random(rs.t, low=1); e
Out[5]: GF([12, 15, 11], order=2^4)

In [6]: c[0:rs.t] += e; c
Out[6]: GF([ 6, 14,  4, 12,  8,  1,  0,  7, 12,  4, 12,  8,  6,  2,  5], order=2^4)

Decode the codeword and recover the message.

In [7]: d = rs.decode(c); d
Out[7]: GF([10,  1, 15, 12,  8,  1,  0,  7, 12], order=2^4)

In [8]: np.array_equal(d, m)
Out[8]: True

Decode the codeword, specifying the number of corrected errors, and recover the message.

In [9]: d, e = rs.decode(c, errors=True); d, e
Out[9]: (GF([10,  1, 15, 12,  8,  1,  0,  7, 12], order=2^4), 3)

In [10]: np.array_equal(d, m)
Out[10]: True

Encode a single message using the shortened \(\textrm{RS}(11, 5)\) code.

In [11]: rs = galois.ReedSolomon(15, 9)

In [12]: GF = rs.field

In [13]: m = GF.Random(rs.k - 4); m
Out[13]: GF([15,  9, 11,  9,  3], order=2^4)

In [14]: c = rs.encode(m); c
Out[14]: GF([15,  9, 11,  9,  3, 11, 12,  9,  8, 15,  3], order=2^4)

Corrupt \(t\) symbols of the codeword.

In [15]: e = GF.Random(rs.t, low=1); e
Out[15]: GF([15,  6,  3], order=2^4)

In [16]: c[0:rs.t] += e; c
Out[16]: GF([ 0, 15,  8,  9,  3, 11, 12,  9,  8, 15,  3], order=2^4)

Decode the codeword and recover the message.

In [17]: d = rs.decode(c); d
Out[17]: GF([15,  9, 11,  9,  3], order=2^4)

In [18]: np.array_equal(d, m)
Out[18]: True

Decode the codeword, specifying the number of corrected errors, and recover the message.

In [19]: d, e = rs.decode(c, errors=True); d, e
Out[19]: (GF([15,  9, 11,  9,  3], order=2^4), 3)

In [20]: np.array_equal(d, m)
Out[20]: True

Encode a matrix of three messages using the \(\textrm{RS}(15, 9)\) code.

In [21]: rs = galois.ReedSolomon(15, 9)

In [22]: GF = rs.field

In [23]: m = GF.Random((3, rs.k)); m
Out[23]: 
GF([[ 0,  4, 15,  2,  7,  3,  8, 14,  7],
    [10, 14,  5,  0, 13,  0,  8,  7,  0],
    [10,  4,  5,  1, 14,  1,  4,  8,  3]], order=2^4)

In [24]: c = rs.encode(m); c
Out[24]: 
GF([[ 0,  4, 15,  2,  7,  3,  8, 14,  7, 10,  4,  5,  6,  5,  1],
    [10, 14,  5,  0, 13,  0,  8,  7,  0,  0,  4, 14,  0,  8,  0],
    [10,  4,  5,  1, 14,  1,  4,  8,  3,  7,  1,  8,  0,  6, 15]],
   order=2^4)

Corrupt the codeword. Add one error to the first codeword, two to the second, and three to the third.

In [25]: c[0,0:1] += GF.Random(1, low=1)

In [26]: c[1,0:2] += GF.Random(2, low=1)

In [27]: c[2,0:3] += GF.Random(3, low=1)

In [28]: c
Out[28]: 
GF([[13,  4, 15,  2,  7,  3,  8, 14,  7, 10,  4,  5,  6,  5,  1],
    [ 8,  8,  5,  0, 13,  0,  8,  7,  0,  0,  4, 14,  0,  8,  0],
    [ 7,  9, 15,  1, 14,  1,  4,  8,  3,  7,  1,  8,  0,  6, 15]],
   order=2^4)

Decode the codeword and recover the message.

In [29]: d = rs.decode(c); d
Out[29]: 
GF([[ 0,  4, 15,  2,  7,  3,  8, 14,  7],
    [10, 14,  5,  0, 13,  0,  8,  7,  0],
    [10,  4,  5,  1, 14,  1,  4,  8,  3]], order=2^4)

In [30]: np.array_equal(d, m)
Out[30]: True

Decode the codeword, specifying the number of corrected errors, and recover the message.

In [31]: d, e = rs.decode(c, errors=True); d, e
Out[31]: 
(GF([[ 0,  4, 15,  2,  7,  3,  8, 14,  7],
     [10, 14,  5,  0, 13,  0,  8,  7,  0],
     [10,  4,  5,  1, 14,  1,  4,  8,  3]], order=2^4),
 array([1, 2, 3]))

In [32]: np.array_equal(d, m)
Out[32]: True

Encode a matrix of three messages using the shortened \(\textrm{RS}(11, 5)\) code.

In [33]: rs = galois.ReedSolomon(15, 9)

In [34]: GF = rs.field

In [35]: m = GF.Random((3, rs.k - 4)); m
Out[35]: 
GF([[11,  7,  9,  6,  2],
    [ 5,  7,  3,  8,  2],
    [ 2,  5,  7,  1,  5]], order=2^4)

In [36]: c = rs.encode(m); c
Out[36]: 
GF([[11,  7,  9,  6,  2, 13,  8, 11,  3, 12, 11],
    [ 5,  7,  3,  8,  2, 14, 11,  6, 11, 10, 12],
    [ 2,  5,  7,  1,  5, 13,  0, 11,  9, 12,  6]], order=2^4)

Corrupt the codeword. Add one error to the first codeword, two to the second, and three to the third.

In [37]: c[0,0:1] += GF.Random(1, low=1)

In [38]: c[1,0:2] += GF.Random(2, low=1)

In [39]: c[2,0:3] += GF.Random(3, low=1)

In [40]: c
Out[40]: 
GF([[ 1,  7,  9,  6,  2, 13,  8, 11,  3, 12, 11],
    [ 4,  2,  3,  8,  2, 14, 11,  6, 11, 10, 12],
    [14,  4, 14,  1,  5, 13,  0, 11,  9, 12,  6]], order=2^4)

Decode the codeword and recover the message.

In [41]: d = rs.decode(c); d
Out[41]: 
GF([[11,  7,  9,  6,  2],
    [ 5,  7,  3,  8,  2],
    [ 2,  5,  7,  1,  5]], order=2^4)

In [42]: np.array_equal(d, m)
Out[42]: True

Decode the codeword, specifying the number of corrected errors, and recover the message.

In [43]: d, e = rs.decode(c, errors=True); d, e
Out[43]: 
(GF([[11,  7,  9,  6,  2],
     [ 5,  7,  3,  8,  2],
     [ 2,  5,  7,  1,  5]], order=2^4),
 array([1, 2, 3]))

In [44]: np.array_equal(d, m)
Out[44]: True

Last update: Aug 27, 2022