Exemplo n.º 1
0
 def shiftSyndrome(self, S, i = 1):
     """Shift syndrome i times (slide 35)
     """
     for i in range(0, i):
         # S1(X) = XS(X) mod g(X)
         S = GF2.modPoly(GF2.multPoly(X(1), S), self.g())
     return S
Exemplo n.º 2
0
 def shiftSyndrome(self, S, i=1):
     """Shift syndrome i times (slide 35)
     """
     for i in range(0, i):
         # S1(X) = XS(X) mod g(X)
         S = GF2.modPoly(GF2.multPoly(X(1), S), self.g())
     return S
Exemplo n.º 3
0
 def shift(self, c, i = 1):
     """Cyclic right shift of c using division (slide 11)
     """
     Xi = X(i) # X^i polynomial
     XiCX = GF2.multPoly(Xi, c) # X^i * c(X) polynomial
     Xn1 = GF2.addPoly(X(self.n()), X(0)) # X^n + 1 polynomial
     ci = GF2.modPoly(XiCX, Xn1) # i times shifted c
     return padEnd(ci, self.n())
Exemplo n.º 4
0
 def shift(self, c, i=1):
     """Cyclic right shift of c using division (slide 11)
     """
     Xi = X(i)  # X^i polynomial
     XiCX = GF2.multPoly(Xi, c)  # X^i * c(X) polynomial
     Xn1 = GF2.addPoly(X(self.n()), X(0))  # X^n + 1 polynomial
     ci = GF2.modPoly(XiCX, Xn1)  # i times shifted c
     return padEnd(ci, self.n())
Exemplo n.º 5
0
 def printMessageCodewordTable(self, systematic = True): # override
     """Print all messages and their corresponding codewords.
     Args:
         systematic: print codewords in systematic form (default: True)
     """
     M = self.M()
     print('Messages -> Codewords')
     for m in M:
         c = self.c(m, systematic)
         print(m, c, 'm(X) =', GF2.polyToString(m), '\tc(X) =', GF2.polyToString(c) )
Exemplo n.º 6
0
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) 
Exemplo n.º 7
0
 def printMessageCodewordTable(self, systematic = True): # override
     """
     Print all messages and their corresponding codewords.
     Args:
         systematic: print codewords in systematic form (default: True)
     """
     M = self.M()
     print('Messages -> Codewords')
     for m in M:
         c = self.c(m, systematic)
         print(m, c, 'm(X) =', GF2.polyToString(m), '\tc(X) =', GF2.polyToString(c) )
Exemplo n.º 8
0
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)
Exemplo n.º 9
0
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)
Exemplo n.º 10
0
 def S(self, r):
     """Calculate Syndrome polynomial from receive or error polynomial.
     Args:
         r: receive or error polynomial
     Returns:
         Syndrome polynomial
     """
     return GF2.modPoly(r, self.g())
Exemplo n.º 11
0
 def S(self, r):
     """Calculate Syndrome polynomial from receive or error polynomial.
     Args:
         r: receive or error polynomial
     Returns:
         Syndrome polynomial
     """
     return GF2.modPoly(r, self.g())
Exemplo n.º 12
0
 def dmin(self, verbose = False): # override (LinearBlockCode dmin would work, but is slower)
     dmin = lbc.w(self.g())
     if verbose:
         print()
         print('Minimum Hamming distance (d_min) equals weight of generator polynomial g(X):')
         print('g(X) =', GF2.polyToString(self.g()))
         print('d_min =', dmin)
         print()
     return dmin
Exemplo n.º 13
0
def exam2011problem3():
    G = np.array([[1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0],
                  [0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0],
                  [0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0],
                  [0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0],
                  [0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1]])

    g = np.array([1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1])
    X15 = np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
    print(GF2.polyToString(GF2.divPoly(X15, g)))

    G = cyccode.makeSystematic(G, True)

    lbc = LinearBlockCode()
    lbc.setG(G)
    r = np.array([1, 0, 1, 1, 0])
    c = lbc.c(r)
    print(c)
Exemplo n.º 14
0
 def dmin(self, verbose = False): # override
     #(LinearBlockCode dmin would work, but is slower)
         dmin = lbc.w(self.g())
         if verbose:
             print()
             print('Minimum Hamming distance (d_min) equals weight of generator polynomial g(X):')
             print('g(X) =', GF2.polyToString(self.g()))
             print('d_min =', dmin)
             print()
         return dmin 
Exemplo n.º 15
0
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)) 
Exemplo n.º 16
0
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))
Exemplo n.º 17
0
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))
Exemplo n.º 18
0
 def printg(self):
     print(GF2.polyToString(self.g())) 
Exemplo n.º 19
0
def exam2014problem3():
    g1 = np.array([1, 0, 0, 1])
    g2 = np.array([1, 1, 1, 1])
    r, t = GF2.HCF(g1, g2, True)
    print(GF2.polyToString(t))
Exemplo n.º 20
0
 def printg(self):
     print(GF2.polyToString(self.g()))