def decode_SDD(r, param): code = komm.HammingCode(param['n'] - param['k']) #mod = komm.PAModulation(2) #b = mod.demodulate(r) u_hat = code.decode(r, method='exhaustive_search_soft') return u_hat
def hamming_decode(self, encoded_parts, parameter): """Decoding method for Hamming code """ code = komm.HammingCode(parameter) decoded_parts = [] for i in range(0, len(encoded_parts)): decoded_part = code.decode(encoded_parts[i]) decoded_parts.append(decoded_part) decoded_parts = np.array(decoded_parts) decoded_parts = np.concatenate(decoded_parts) if (len(self.image_bits) % code.dimension != 0): for i in range(0, self.calculate_zeros_addition_Hamming(parameter)): decoded_parts = np.delete(decoded_parts, len(decoded_parts) - 1) return decoded_parts
class TestHammingCode: code = komm.HammingCode(3) def test_parameters(self): n, k, d = self.code.length, self.code.dimension, self.code.minimum_distance assert (n, k, d) == (7, 4, 3) def test_weight_distributions(self): assert np.array_equal(self.code.codeword_weight_distribution, [1, 0, 0, 7, 7, 0, 0, 1]) assert np.array_equal(self.code.coset_leader_weight_distribution, [1, 7, 0, 0, 0, 0, 0, 0]) def test_GH_orthogonality(self): k, m = self.code.dimension, self.code.redundancy G = self.code.generator_matrix H = self.code.parity_check_matrix assert np.array_equal( np.dot(G, H.T) % 2, np.zeros((k, m), dtype=np.int)) def test_encoding(self): assert np.array_equal(self.code.encode([1, 0, 0, 1]), [1, 0, 0, 1, 0, 0, 1]) assert np.array_equal(self.code.encode([1, 0, 1, 1]), [1, 0, 1, 1, 0, 1, 0]) assert np.array_equal(self.code.encode([1, 1, 1, 1]), [1, 1, 1, 1, 1, 1, 1]) def test_codewords(self): n, k = self.code.length, self.code.dimension codewords = self.code.codeword_table assert codewords.shape == (2**k, n) def test_decoding(self): assert np.array_equal(self.code.decode([1, 1, 1, 1, 1, 1, 1]), [1, 1, 1, 1]) assert np.array_equal(self.code.decode([1, 1, 1, 1, 1, 1, 0]), [1, 1, 1, 1]) assert np.array_equal(self.code.decode([1, 0, 1, 1, 1, 1, 0]), [1, 0, 1, 1])
def hamming_encode(self, parameter): """ Hamming code encoding method for image pixels """ bits = np.array(self.image_bits) code = komm.HammingCode(parameter) if (len(bits) % code.dimension > 0): bits = np.append(bits, [ np.zeros(self.calculate_zeros_addition_Hamming(parameter), dtype=np.uint8) ]) number_of_arrays = int(len(bits) / code.dimension) parts_to_encode = np.reshape(bits, (number_of_arrays, -1), order='C') encoded_parts = [] for i in range(0, len(parts_to_encode)): encoded_part = code.encode(parts_to_encode[i]) encoded_parts.append(encoded_part) encoded_parts = np.array(encoded_parts) return encoded_parts elif (len(bits) % code.dimension == 0): number_of_arrays = int(len(bits) / code.dimension) parts_to_encode = np.reshape(bits, (number_of_arrays, -1), order='C') encoded_parts = [] for i in range(0, len(parts_to_encode)): encoded_part = code.encode(parts_to_encode[i]) encoded_parts.append(encoded_part) encoded_parts = np.array(encoded_parts) return encoded_parts
def calculate_zeros_addition_Hamming(self, parameter): bits = np.array(self.image_bits) code = komm.HammingCode(parameter) additional_zeros = (int(len(bits) / code.dimension + 1) * code.dimension) - len(bits) return additional_zeros
# coding: utf-8 import numpy as np import sindrome import komm import matplotlib.pyplot as plt N_cw = 100000 code = komm.HammingCode(3) EbN0_list = np.arange(-1.0, 8.0, step = 1.0) k = code.dimension n = code.length R = code.rate u = np.random.randint(2, size = k*N_cw) u_reshaped = np.reshape(u,newshape = (N_cw,k)) c_reshaped = np.apply_along_axis(code.encode, axis = 1, arr = u_reshaped) c = np.reshape(c_reshaped, newshape = (n*N_cw,)) mod = komm.PAModulation(2) s = mod.modulate(c) Pb = [] for EbN0 in EbN0_list: EbN0_linear = 10**(EbN0/10) Eb = np.linalg.norm(s)**2 #Eb = total de energia transmitida (cada elem. ao quadrado somado, que dá 700000)/ número de bits de informação (400000) N0 = Eb/EbN0_linear w = np.random.randn(n*N_cw) * np.sqrt(N0/2) #vetor gaussiano com média zero e variância 1, multiplica pela raiz da variância do canal awgn (N0) para mudar a variância 1 r = s + w b = mod.demodulate(r) b_reshaped = np.reshape(b, newshape = (N_cw, n))
def decode_HDD(b, param): code = komm.HammingCode(param['n'] - param['k']) u_hat = code.decode(b, method='exhaustive_search_hard') return u_hat