def printAllCyclicCodes(factorPolynomials):#выводим все циклические коды, которые можем сделать s = '' product = np.array([]) for i in range(0, len(factorPolynomials)): if i == 0: product = factorPolynomials[i] else: product = GF2.multPoly(product, factorPolynomials[i]) s += '(' + GF2.polyToString(factorPolynomials[i]) + ') ' print(s + '= ' + GF2.polyToString(product)) print() numberCodes = 2**(len(factorPolynomials)) - 2 n = degree(product) print('Всего возможны', numberCodes, 'различных циклических кодов длиной', n) print('так как мы можем найти', numberCodes, 'различных неприводимых двоичных полиномов') print(GF2.polyToString(product)) print(np.bitwise_and(1, 3)) print('Code <- Generator polynomial') for i in range(0, numberCodes): s = '' gp = np.array([]) # generator polynomial for j in range(0, len(factorPolynomials)): if np.bitwise_and(i+1, 2**j) > 0: if s =='': gp = factorPolynomials[j] else: gp = GF2.multPoly(gp, factorPolynomials[j]) s += '(' + GF2.polyToString(factorPolynomials[j]) + ')' print('Ccyc(' + str(n) + ', ' + str(degree(gp)) + ') <- g' + str(i+1) + ' = ' + s + ' = ' + GF2.polyToString(gp))
def __init__(self, g, n): assert g[0] == 1, \ "g0 must equal to 1" assert n >= degree(g), \ "n=%i must be >= degree(g)=%i" % (n, degree(g)) self._g = g[:n]; #auto remove too much dangling zeros self._n = n;
def __init__(self, g, n): assert g[0] == 1, \ "g0 must equal to 1" assert n >= degree(g), \ "n=%i must be >= degree(g)=%i" % (n, degree(g)) self._g = g[:n]; #auto remove too much dangling zeros self._n = n
def encode(m, g, systematic = True): if systematic: r = degree(g) # r = n - k Xr = X(r) # X^(n-k) XrmX = GF2.multPoly(Xr, m) # X^(n-k) * m(X) p = GF2.modPoly(XrmX, g) # p(X) = (X^(n-k) * m(X)) mod g(X) c = GF2.addPoly(p, XrmX) # c(X) = p(X) + (X^(n-k) * m(X)) else: c = GF2.multPoly(m, g) return c.astype(int)
def gToG(g, n, systematic = True, verbose = False):#получаем образующую матрицу k = n - degree(g) g = padEnd(g, n) G = np.empty([k, n]) for i in range(0, k): G[i,:] = shift(g, i) # матрицу необходимо повернуть # -> systematic form if systematic: G = makeSystematic(G, verbose) return G.astype(int)
def printAllCyclicCodes(factorPolynomials): """Generates all cyclic codes that can be created from the given factor polynomials. (slide 28) Args: factorPolynomials: factor polynomials in a python array """ s = '' product = np.array([]) for i in range(0, len(factorPolynomials)): if i == 0: product = factorPolynomials[i] else: product = GF2.multPoly(product, factorPolynomials[i]) s += '(' + GF2.polyToString(factorPolynomials[i]) + ') ' print(s + '= ' + GF2.polyToString(product)) print() numberCodes = 2**(len(factorPolynomials)) - 2 n = degree(product) print('There are', numberCodes, 'different cyclic codes of length', n, 'as') print('we can find', numberCodes, 'different generator polynomials that are') print('the factors of', GF2.polyToString(product)) print(np.bitwise_and(1, 3)) print('Code <- Generator polynomial') for i in range(0, numberCodes): s = '' gp = np.array([]) # generator polynomial for j in range(0, len(factorPolynomials)): if np.bitwise_and(i + 1, 2**j) > 0: if s == '': gp = factorPolynomials[j] else: gp = GF2.multPoly(gp, factorPolynomials[j]) s += '(' + GF2.polyToString(factorPolynomials[j]) + ')' print('Ccyc(' + str(n) + ', ' + str(degree(gp)) + ') <- g' + str(i + 1) + ' = ' + s + ' = ' + GF2.polyToString(gp))
def printAllCyclicCodes(factorPolynomials): """Generates all cyclic codes that can be created from the given factor polynomials. (slide 28) Args: factorPolynomials: factor polynomials in a python array """ s = '' product = np.array([]) for i in range(0, len(factorPolynomials)): if i == 0: product = factorPolynomials[i] else: product = GF2.multPoly(product, factorPolynomials[i]) s += '(' + GF2.polyToString(factorPolynomials[i]) + ') ' print(s + '= ' + GF2.polyToString(product)) print() numberCodes = 2**(len(factorPolynomials)) - 2 n = degree(product) print('There are', numberCodes, 'different cyclic codes of length', n, 'as') print('we can find', numberCodes, 'different generator polynomials that are') print('the factors of', GF2.polyToString(product)) print(np.bitwise_and(1, 3)) print('Code <- Generator polynomial') for i in range(0, numberCodes): s = '' gp = np.array([]) # generator polynomial for j in range(0, len(factorPolynomials)): if np.bitwise_and(i+1, 2**j) > 0: if s =='': gp = factorPolynomials[j] else: gp = GF2.multPoly(gp, factorPolynomials[j]) s += '(' + GF2.polyToString(factorPolynomials[j]) + ')' print('Ccyc(' + str(n) + ', ' + str(degree(gp)) + ') <- g' + str(i+1) + ' = ' + s + ' = ' + GF2.polyToString(gp))
def encode(m, g, systematic = True): """Encoding of cyclic code (in systematic form) (slide 23) ATTENTION: Dangling zeros in returned codeword are cut away. """ if systematic: r = degree(g) # r = n - k Xr = X(r) # X^(n-k) XrmX = GF2.multPoly(Xr, m) # X^(n-k) * m(X) p = GF2.modPoly(XrmX, g) # p(X) = (X^(n-k) * m(X)) mod g(X) c = GF2.addPoly(p, XrmX) # c(X) = p(X) + (X^(n-k) * m(X)) else: c = GF2.multPoly(m, g) return c.astype(int)
def encode(m, g, systematic=True): """Encoding of cyclic code (in systematic form) (slide 23) ATTENTION: Dangling zeros in returned codeword are cut away. """ if systematic: r = degree(g) # r = n - k Xr = X(r) # X^(n-k) XrmX = GF2.multPoly(Xr, m) # X^(n-k) * m(X) p = GF2.modPoly(XrmX, g) # p(X) = (X^(n-k) * m(X)) mod g(X) c = GF2.addPoly(p, XrmX) # c(X) = p(X) + (X^(n-k) * m(X)) else: c = GF2.multPoly(m, g) return c.astype(int)
def gToG(g, n, systematic = True, verbose = False): """Builds Generator Matrix G from given Generator Polynomial. (slide 25) Args: g: generator polynomial n: code length systematic: generator matrix in systematic form (default: True) verbose: verbose output on how to gain systematic form (default: False) Returns: Generator Matrix """ k = n - degree(g) g = padEnd(g, n) G = np.empty([k, n]) for i in range(0, k): G[i,:] = shift(g, i) # row additions to gain embedded identity matrix on right side # -> systematic form if systematic: G = makeSystematic(G, verbose) return G.astype(int)
def gToG(g, n, systematic=True, verbose=False): """Builds Generator Matrix G from given Generator Polynomial. (slide 25) Args: g: generator polynomial n: code length systematic: generator matrix in systematic form (default: True) verbose: verbose output on how to gain systematic form (default: False) Returns: Generator Matrix """ k = n - degree(g) g = padEnd(g, n) G = np.empty([k, n]) for i in range(0, k): G[i, :] = shift(g, i) # row additions to gain embedded identity matrix on right side # -> systematic form if systematic: G = makeSystematic(G, verbose) return G.astype(int)
def k(self): # override return self.n() - degree(self.g())
def decode(self, r, verbose=True): """Decode received polynomial r(X) using the Euclidean Algorithm. (slide 26) Args: r: received polynomial verbose: print the algorithm steps. """ GF = self.GF() if verbose: print() print('Decode the received polynomial:') print('r(X) = ' + self.GF().polyToString(r)) S = self.S(r, verbose) if verbose: print('The Euclidean algorithm is applied by constructing the ' + \ 'following table:') ri, ti = GF.HCF(X(2 * self.t()), S, verbose) lamb = GF.monicMultiplier(ti) errorLocationPoly = GF.multPoly(lamb, ti) errorLocationPolyDerivative = GF.derivePoly(errorLocationPoly) errorEvalutationPoly = GF.multPoly(lamb, ri) if verbose: print(u'An element \u03BB \u2208 GF(' + str(GF.q()) + ') is conveniently selected to multiply') print( u't_i(X) by, in order to convert it into a monic polynomial.') print(u'This value of \u03BB is \u03BB = ' + GF.elementToString(lamb)) print('Therefore:') print() print('Error location polynomial:') print(u'\u03C3(X) = \u03BB * t_i(X) = ' + GF.elementToString(lamb) + '(' + GF.polyToString(ti) + ')') print(' = ' + GF.polyToString(errorLocationPoly)) print() print(u'\u03C3\'(X) = ' + GF.polyToString(errorLocationPolyDerivative)) print() print('Error evaluation polynomial:') print(u'W(X) = -\u03BB * r_i(X) = ' + GF.elementToString(lamb) + ' * ' + GF.polyToString(ri)) print(' = ' + GF.polyToString(errorEvalutationPoly)) print() print( u'Performing Chien search in the error location polynomial \u03C3(X):' ) print() errorLocations = [] for i, root in enumerate(GF.roots(errorLocationPoly)): j = GF.elementToExp(GF.elementFromExp(-GF.elementToExp(root))) errorLocations.append(j) if verbose: print(u'\u03B1^(-j_' + str(i+1) + ') = ' + GF.elementToString(root) + \ '\t-> j_' + str(i+1) + ' = ' + str(j)) if verbose: print('\nError values:') errorValues = [] for i, errorLocation in enumerate(errorLocations): alpha = GF.elementFromExp(-errorLocation) res_W = GF.substituteElementIntoPoly(errorEvalutationPoly, alpha) res_o = GF.substituteElementIntoPoly(errorLocationPolyDerivative, alpha) errorValue = GF.divElements(res_W, res_o) errorValues.append(errorValue) if verbose: print('e_j' + str(i+1) + ' = W(' + GF.elementToString(alpha) + \ u') / \u03C3\'(' + GF.elementToString(alpha) + ') = ' + \ GF.elementToString(res_W) + \ ' / ' + GF.elementToString(res_o) + ' = ' + \ GF.elementToString(errorValue)) e = np.zeros(degree(r) + 1) for i, errorLocation in enumerate(errorLocations): errorValue = errorValues[i] e[errorLocation] = errorValue c = GF.addPoly(r, e) if verbose: print() print('Error polynomial:') print('e(X) = ' + GF.polyToString(e)) print() print('Code vector:') print('c = r + e = ' + str(c)) return c.astype(int)
def decode(self, r, verbose = True): """Decode received polynomial r(X) using the Euclidean Algorithm. (slide 26) Args: r: received polynomial verbose: print the algorithm steps. """ GF = self.GF() if verbose: print() print('Decode the received polynomial:') print('r(X) = ' + self.GF().polyToString(r)) S = self.S(r, verbose) if verbose: print('The Euclidean algorithm is applied by constructing the ' + \ 'following table:') ri, ti = GF.HCF(X(2 * self.t()), S, verbose) lamb = GF.monicMultiplier(ti) errorLocationPoly = GF.multPoly(lamb, ti); errorLocationPolyDerivative = GF.derivePoly(errorLocationPoly) errorEvalutationPoly = GF.multPoly(lamb, ri); if verbose: print(u'An element \u03BB \u2208 GF(' + str(GF.q()) + ') is conveniently selected to multiply') print(u't_i(X) by, in order to convert it into a monic polynomial.') print(u'This value of \u03BB is \u03BB = ' + GF.elementToString(lamb)) print('Therefore:') print() print('Error location polynomial:') print(u'\u03C3(X) = \u03BB * t_i(X) = ' + GF.elementToString(lamb) + '(' + GF.polyToString(ti) + ')') print(' = ' + GF.polyToString(errorLocationPoly) ) print() print(u'\u03C3\'(X) = ' + GF.polyToString(errorLocationPolyDerivative)) print() print('Error evaluation polynomial:') print(u'W(X) = -\u03BB * r_i(X) = ' + GF.elementToString(lamb) + ' * ' + GF.polyToString(ri)) print(' = ' + GF.polyToString(errorEvalutationPoly) ) print() print(u'Performing Chien search in the error location polynomial \u03C3(X):') print() errorLocations = [] for i, root in enumerate(GF.roots(errorLocationPoly)): j = GF.elementToExp(GF.elementFromExp(-GF.elementToExp(root))) errorLocations.append(j) if verbose: print(u'\u03B1^(-j_' + str(i+1) + ') = ' + GF.elementToString(root) + \ '\t-> j_' + str(i+1) + ' = ' + str(j)) if verbose: print('\nError values:') errorValues = [] for i, errorLocation in enumerate(errorLocations): alpha = GF.elementFromExp(-errorLocation) res_W = GF.substituteElementIntoPoly(errorEvalutationPoly, alpha) res_o = GF.substituteElementIntoPoly(errorLocationPolyDerivative, alpha) errorValue = GF.divElements(res_W, res_o) errorValues.append(errorValue) if verbose: print('e_j' + str(i+1) + ' = W(' + GF.elementToString(alpha) + \ u') / \u03C3\'(' + GF.elementToString(alpha) + ') = ' + \ GF.elementToString(res_W) + \ ' / ' + GF.elementToString(res_o) + ' = ' + \ GF.elementToString(errorValue)) e = np.zeros(degree(r)+1) for i, errorLocation in enumerate(errorLocations): errorValue = errorValues[i] e[errorLocation] = errorValue c = GF.addPoly(r, e) if verbose: print() print('Error polynomial:') print('e(X) = ' + GF.polyToString(e)) print() print('Code vector:') print('c = r + e = ' + str(c)) return c.astype(int)