Exemplo n.º 1
0
Arquivo: bch.py Projeto: thmis/BCH
def encode(args):
    random.seed()
    code = construct_code_from_file(args.in_file)
    message = input('Input the message for encoding: \n >')
    encoded_blocks = []

    binary = code.str_to_bits(message)
    while len(binary) % code.k != 0:
        binary.append(0)

    for i in range(int(len(binary) / code.k)):
        src_line = binary[i * code.k:(i + 1) * code.k]
        encoded = code.encode_block(src_line)
        encoded_blocks.append(encoded)

        print(src_line, list(encoded), sep=' ')

    with open(args.out_file, 'w') as out_file:
        for block in encoded_blocks:
            error_vector = np.zeros(code.n, dtype=int)
            for i in range(len(error_vector)):
                random_number = random.randint(1, 100)
                if random_number < 100 * code.p:
                    error_vector[i] = 1
            block_with_errors = math_utils.xor(block, error_vector, code.n)
            print(block, error_vector, block_with_errors, sep=' ')
            for bit in block_with_errors:
                out_file.write(str(bit))
Exemplo n.º 2
0
Arquivo: bch.py Projeto: thmis/BCH
 def encode_block(self, block):
     power = math_utils.greatest_bit(self.generator)
     encoded = np.concatenate((block, np.zeros(power - 1, dtype=int)))
     encoded = math_utils.xor(
         encoded,
         math_utils.divide_polynomials(encoded, self.generator)[1],
         len(encoded))
     return encoded
Exemplo n.º 3
0
    def get_minimal_polynomial(self, cycl_class):
        '''Calculates the minimal polynomial for the given cyclotomic class'''
        polynomial = 1 << len(cycl_class)
        polynomial = np.concatenate(([1], np.zeros(len(cycl_class),
                                                   dtype=int)))
        for i in range(len(cycl_class)):
            coefficient = np.zeros(self.power, dtype=int)
            for combination in combinations(cycl_class, i + 1):
                coefficient = math_utils.xor(
                    coefficient,
                    self.logarithm_table[sum(combination) % self.field_power],
                    self.power)
            if (sum(coefficient) != 0):
                coefficient = coefficient[-math_utils.greatest_bit(coefficient
                                                                   ):]
                coefficient = np.concatenate(
                    (coefficient, np.zeros(len(cycl_class) - i - 1,
                                           dtype=int)))
                polynomial = math_utils.xor(coefficient, polynomial,
                                            len(cycl_class) + 1)

        return polynomial
Exemplo n.º 4
0
    def find_roots_of_polynomial(self, polynomial):
        '''Calculates the roots of the given polynomial'''
        roots = []
        for candidate in range(self.field_power):
            result = self.logarithm_table[polynomial[0]]
            for polynomial_power in range(1, len(polynomial)):
                if polynomial[polynomial_power] >= 0:
                    temp = self.logarithm_table[(polynomial[polynomial_power] +
                                                 candidate * polynomial_power)
                                                % (self.field_power)]
                    result = math_utils.xor(temp, result,
                                            max(len(result), len(temp)))
            if sum(result) == 0:
                roots.append(candidate)

        return roots
Exemplo n.º 5
0
    def get_logarithm_table(self):
        '''Calculates powers of the primitive element in the given field'''
        size = math_utils.greatest_bit(self.primitive_polynomial) - 1
        logarithm_table = {}

        for i in range(size):
            logarithm_table[i] = np.zeros(i + 1, dtype=int)
            logarithm_table[i][0] = 1

        logarithm_table[size] = self.primitive_polynomial[1:]

        for i in range(size + 1, 2**size - 1):
            tmp_pol = np.concatenate(
                (logarithm_table[i - 1], np.zeros(1, dtype=int)))
            if tmp_pol[len(tmp_pol) - size - 1]:
                tmp_pol = math_utils.xor(tmp_pol,
                                         self.primitive_polynomial[1:],
                                         size + 1)
            logarithm_table[i] = tmp_pol[1:]
        return logarithm_table