def test_code(self): config = yaml.safe_load(open('config-test.yml')) for i in range(2 ** self.k): code, distorted_code, error = encode( coder_file=config['coder-generator-test'], message=i, m_length=self.k, error=74) # 000000000000001001010 assert transpose_matrix( matrix=multiply_matrices( matrix1=read_file_to_list(config['decoder-parity-check-test']), columns_count1=self.n, matrix2=transpose_matrix([code], self.n), columns_count2=1 ), columns_count=1)[0] == 0 for i in range(2 ** self.k): code, distorted_code, error = encode( coder_file=config['coder-generator-test'], message=i, m_length=self.k) if get_hamming_weight(error) <= self.t: assert transpose_matrix( matrix=multiply_matrices( matrix1=read_file_to_list(config['decoder-parity-check-test']), columns_count1=self.n, matrix2=transpose_matrix([code], self.n), columns_count2=1 ), columns_count=1)[0] == 0
def decode(parity_check_file, n, syndrome_file, distorted_code): parity_check_matrix = read_file_to_list(name=parity_check_file) syndrome_decoding_table = read_file_to_dict(name=syndrome_file) syndrome = transpose_matrix(matrix=multiply_matrices( matrix1=parity_check_matrix, columns_count1=n, matrix2=transpose_matrix(matrix=[distorted_code], columns_count=n), columns_count2=1), columns_count=1)[0] return syndrome_decoding_table[0].index( distorted_code ^ syndrome_decoding_table[syndrome][0])
def test_multiply_matrices(self): parity_check_matrix, a_matrix_transposed = fill_parity_check_matrix_and_a_matrix_transposed( n=self.n, r=self.r, d=self.d) generator_matrix = fill_generator_matrix_from_a_matrix_transposed( a_matrix_transposed=a_matrix_transposed, k=self.k, r=self.r) multiplied_matrix = multiply_matrices(matrix1=parity_check_matrix, num_col_m1=self.n, matrix2=transpose_matrix( generator_matrix, self.n), num_col_m2=self.k) for i in multiplied_matrix: assert i == 0
def encode(coder_file, message, m_length, error=0): generator_matrix = read_file_to_list(name=coder_file) max_line = max(generator_matrix) number_of_columns_g_m = len(bin(max_line)) - 2 gen_matrix_length = len(generator_matrix) if gen_matrix_length != m_length: raise ValueError('The given len(generator_matrix) == {0}' ' and m_length == {1} are incompatible.' ' {0} != {1}.'.format(gen_matrix_length, m_length)) if error == 0: error = random.randrange(2**number_of_columns_g_m) code = multiply_matrices(matrix1=[message], columns_count1=m_length, matrix2=generator_matrix, columns_count2=number_of_columns_g_m)[0] return code, code ^ error, error
def test_multiply_matrices_exception(self): with self.assertRaises(ValueError): multiply_matrices([0, 0], 2, [0], 1)