-
Notifications
You must be signed in to change notification settings - Fork 11
/
RSCode.py
87 lines (73 loc) · 2.53 KB
/
RSCode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# -*- coding: UTF-8 -*-
import numpy as np
from BCHCode import BCHCode
from GaloisField import X, degree
import math
def g(GF, t, verbose = True):
"""Construct generator polynomial of RS code with t.
(p 116)
Args:
GF: ExtendedGaloisField
t: number of correctable errors
verbose: printout how the generator polynomial is generated
Returns:
Generator polynomial of BCH code with t
"""
i_list = list(range(1, (2*t)+1, 2)) # only odd roots 1 <= i <= 2t
if verbose:
print("")
print('Generator polynomial g(X) of BCH code with t = ' + str(t) + ':')
print("")
print(u'g(X) = (X - \u03B1)(X - \u03B1^2)...(X - \u03B1^(n-k))')
print("")
g = np.ones(1)
for i in range(1, 2*t+1): # n - k = 2*t
root = GF.elementFromExp(i)
phi = np.array([root,1])
g = GF.multPoly(g, phi)
if verbose:
print('\u03A6_' + str(i) + '(X) = ' + GF.polyToString(phi))
if verbose:
print('g(X) = ' + GF.polyToString(g))
print()
return g
class RSCode(BCHCode):
"""RS Code
Attributes:
_GF: Galois Field the generator polynomial has roots in.
"""
_GF = None
_t = 0
def __init__(self, GF, t, verbose = False):
"""
Args:
GF: Galois Field the generator polynomial has roots in.
t: Number of errors to correct.
verbose: Print how the generator polynomial is constructed.
"""
m = GF.m()
self._GF = GF
self._g = g(GF, t, verbose)
self._n = 2**m - 1 # slide 5
self._t = t
def GF(self):
""" Return Galois Field.
"""
return self._GF
def printInfo(self):
GF = self.GF()
t = self._t
print()
print('Generator Polynomial: g(X) = ' + GF.polyToString(self.g()))
rootStr = ''
roots = GF.removeConjugateRoots(GF.roots(self.g()))
for root in roots:
rootStr += GF.elementToString(root) + ', '
rootStr = rootStr[:-2] # remove last comma
print('Roots of g(X) in GF(2^' + str(self.GF().m()) + '): ' + rootStr + ' and all conjugate roots')
print('Code length: n = q - 1 = ' + str(self.n()))
print('Message length: k = ' + str(self.k()))
print('Number of parity bits: n - k = 2t = ' + str(2*t))
print('Minimum Hamming distance: dmin = ' + str(2*t+1) + ' = 2t + 1')
print('Error correction capability: t = ' + str(t))
print()