def get_decoded_polynomial(self, codeword): if self.code_is_valid(codeword): return codeword else: for i in range(0, self.k): syndrome = self.get_syndrome(codeword) weight = syndrome.get_hamming_weight() # print('Code: ' + str(codeword) + ' len=' + str(len(codeword))) # print('Syndrome: ' + str(syndrome) + ', Weight: ' + str(weight)) if weight <= self.t: newcodeword = codeword + syndrome if i > 0: new_elements = newcodeword.elements[ i:] + newcodeword.elements[0:i] return Polynomial(new_elements) else: return newcodeword else: codes = [ *[codeword.elements[len(codeword) - 1]], *(self.n - len(codeword.elements)) * [GF.Alpha(-1, 0, self.gf_index)], *codeword.elements[0:len(codeword) - 1] ] codeword = Polynomial(codes) raise CannotDetectErrorException
def get_coding_polynomial(self): result = Polynomial([ GF.Alpha(0, 1, self.gf.index), GF.Alpha(1, GF.alpha_elements_by_index[1], self.gf.index) ]) for i in range(2, self.r + 1): result = result * Polynomial([ GF.Alpha(0, 1, self.gf.index), GF.Alpha(i, self.gf.alpha_elements_by_index[i], self.gf.index) ]) return result
def generate_alpha_elements(self): all_alpha_elements = pow(2, self.index) - 1 self.alpha_elements.append( self.Alpha(index=-1, value=0, gf_index=self.index)) poly = Polynomial([Bit(1)]) for i in range(all_alpha_elements): a = poly % self.generating_polynomial self.alpha_elements.append( self.Alpha(index=i, value=int(a.get_string_representation(), 2), gf_index=self.index)) poly = Polynomial(poly.elements + [Bit(0)])
def encode(self, word): word2encode = [ord(c) for c in word] word2bits = '' word2alphas = [] for i in range(len(word2encode)): word2bits = word2bits + format(word2encode[i], '08b') for i in range(0, len(word2bits), 7): if len(word2bits[i:i + 7]) == 7: w = int(word2bits[i:i + 7], 2) else: additional_zeros = 7 - len(word2bits[i:i + 7]) last_bits = word2bits[i:i + 7] + additional_zeros * '0' w = int(last_bits, 2) word2alphas.append( GF.Alpha(index=GF.alpha_elements_by_value[w], value=w, gf_index=self.gf.index)) word2poly = Polynomial(word2alphas + self.r * [GF.Alpha(-1, 0, self.gf.index)]) coding_reminder = word2poly % self.coding_polynomial code = word2poly + coding_reminder return code
def __init__(self, generating_polynomial=Polynomial([ Bit(1), Bit(1), Bit(1), Bit(1), Bit(1), Bit(1), Bit(0), Bit(1) ])): self.generating_polynomial = generating_polynomial self.index = len(self.generating_polynomial) - 1 self.alpha_elements = [] self.generate_alpha_elements() self.assign_alphas_to_dicts()
def __init__(self, generating_polynomial=Polynomial([ Bit(1), Bit(0), Bit(0), Bit(0), Bit(1), Bit(0), Bit(0), Bit(1) ]), k=73, s=7): self.gf = GF(generating_polynomial=generating_polynomial) self.n = 2**self.gf.index - 1 self.k = k # self.s = s self.r = self.n - self.k self.coding_polynomial = self.get_coding_polynomial()
def decode(self, codeword, algorithm): full_decoded_polynomial = self.decoders[algorithm](codeword) decoded_polynomial = Polynomial( full_decoded_polynomial.elements[:-(self.n - self.k)]) binary_result = '' result = [] for i in range(len(decoded_polynomial)): w = format(decoded_polynomial.elements[i].value, '07b') binary_result = binary_result + w last_bits = len(binary_result) % 8 binary_result = binary_result[:-last_bits] for i in range(0, len(binary_result), 8): w = int(binary_result[i:i + 8], 2) result.append(w) logging.info(result) return ''.join([chr(x) for x in result])
def code_is_valid(self, codeword): return self.get_syndrome(codeword) == Polynomial( [GF.Alpha(-1, 0, self.gf_index)])
'a': Bit(1), 'b': Bit(1), 'c': Bit(1) }] divide_params_exceptions = [{ 'a': Bit(0), 'b': Bit(0) }, { 'a': Bit(1), 'b': Bit(0) }] add_poly = [{ 'a': Polynomial([Bit(0), Bit(1), Bit(0), Bit(1), Bit(1), Bit(1)]), 'b': Polynomial([Bit(1), Bit(1), Bit(0), Bit(1), Bit(0), Bit(1)]), 'c': Polynomial([Bit(1), Bit(0), Bit(0), Bit(0), Bit(1), Bit(0)]) }, { 'a': Polynomial([Bit(0), Bit(0), Bit(0), Bit(0), Bit(0), Bit(0)]), 'b': Polynomial([Bit(1), Bit(1), Bit(1), Bit(1), Bit(1), Bit(1)]), 'c': Polynomial([Bit(1), Bit(1),