示例#1
0
    def decode(self, bit_arr: ba.bitarray) -> ba.bitarray:
        """
        :param bit_arr: bitarray that length multiple of self.n
        :param is_reshaped: flag is it array shape - into ndarray (l,self.k), where l - some value greater 0
        :return: decoded array multiple of self.k
        """
        # msg is being partited on pieces
        # print(bit_arr, len(bit_arr))
        bit_list = BaseCode.reshape_to_np_arr(bit_arr, self.n)
        decoded_arr = ba.bitarray()

        for i in range(len(bit_list)):

            w_edit = RM_Code.get_w_edit(bit_list[i])
            # w^~= w_edit = w_i
            for j in range(self.m):
                H_i_m = RM_Code.get_H_i_m(j + 1, self.m)
                w_edit = np.matmul(w_edit, H_i_m)

            max_index_j = np.argmax(np.abs(w_edit))
            vj: ba.bitarray = self.get_v_j(max_index_j)
            first_bit: tuple = (int(w_edit[max_index_j] > 0), )

            decoded_arr.extend(first_bit)
            decoded_arr.extend(vj)

        return decoded_arr
示例#2
0
    def decode(self, bit_arr: ba.bitarray):

        bit_list = BaseCode.reshape_to_np_arr(bit_arr, self.n)
        decoded_arr = ba.bitarray()

        for kij in range(len(bit_list)):
            i = self.r
            J = self.J
            w = bit_list[kij].copy()
            decoded = np.zeros(self.k, dtype=np.int8)
            v_i = np.zeros(self.n, dtype=np.int8)
            for j in range(len(J) - 1, -1, -1):
                J_j = J[j]
                if len(J_j) != i:
                    i -= 1
                    w = np.logical_xor(w, v_i)
                    v_i = np.zeros(self.n, dtype=np.int8)
                count = 0
                verifications = self.get_verification_vectors(J_j)
                for v in verifications:
                    count += RM_Code.scalar(w, v)
                size_half = len(verifications) // 2
                if count > size_half:
                    decoded[j] = 1
                    v_i = np.logical_xor(v_i, self.get_row_by_j(J_j))
                elif count == size_half:
                    print(f"Error. The word {w} can be fixed.")
            decoded_arr.extend(decoded)

        return decoded_arr
示例#3
0
 def encode(self, bit_arr: ba.bitarray, is_reshaped=False) -> ba.bitarray:
     """
     :param bit_arr: bitarray that length multiple of self.k
     :param is_reshaped: flag is it array shape - into ndarray (l,self.k), where l - some value greater 0
     :return: encoded array multiple of self.n
     """
     # msg is being partited on pieces
     bit_list = BaseCode.reshape_to_np_arr(bit_arr, self.k, is_reshaped)
     coded_arr = ba.bitarray()
     # encode
     log_xor = np.matmul(bit_list, self.g) % 2
     coded_arr.extend(log_xor.ravel())
     return coded_arr
示例#4
0
    def decode(self, bit_arr: ba.bitarray) -> ba.bitarray:
        """
        :param bit_arr: bitarray that length multiple of self.n
        :param is_reshaped: flag is it array shape - into ndarray (l,self.k), where l - some value greater 0
        :return: decoded array multiple of self.k
        """
        # msg is being partited on pieces
        print(bit_arr, len(bit_arr))
        bit_list = BaseCode.reshape_to_np_arr(bit_arr, self.n)
        decoded_arr = ba.bitarray()
        for i in range(len(bit_list)):
            # Algo from lecture
            # find syndrome s = wH
            s = np.matmul(bit_list[i], self.H) % 2
            u = None
            if GolayCode.wt(s, is_np_array=True) <= 3:
                u = np.concatenate((s, np.zeros(self.n_k, dtype=np.int8)))
            else:
                for j in range(len(self.B)):
                    log_xor = np.logical_xor(s, self.B[j])
                    if GolayCode.wt(log_xor, is_np_array=True) <= 2:
                        u = np.concatenate((log_xor, np.eye(self.n_k)[j]))
                        break

            # find syndrome b = sB
            sB = np.matmul(s, self.B) % 2
            if self.wt(sB) <= 3:
                u = np.concatenate((np.zeros(self.n_k, dtype=np.int8), sB))
            else:
                for j in range(len(self.B)):
                    log_xor = np.logical_xor(sB, self.B[j])
                    if GolayCode.wt(log_xor, is_np_array=True) <= 2:
                        u = np.concatenate((np.eye(self.n_k)[j], log_xor))
                        break
            if u is None:
                invalid_sequence = bit_arr[self.n * i:self.n * (i + 1)]
                err_str = f"""Can not decode the message {GolayCode.ba_to_str_bits(invalid_sequence)}.
Please repit the transfer of data."""
                raise ValueError(err_str)
            else:
                on_flush = np.logical_xor(bit_list[i], u)[0:self.k]
                decoded_arr.extend(on_flush)

        return decoded_arr