Ejemplo n.º 1
0
def polarcodes(n, rate, snr, channel_con, decode_method, decode_para,
               information_pos, frozen_bit, crc_n):
    N = 2**n
    information_num = int(N * rate)
    # 信息-information_bit生成
    information_bit = np.random.randint(0, 2, size=(1, information_num))

    if crc_n == 0:
        pass
    else:
        informationbit = information_bit.copy()
        informationbit.resize(information_num, )
        information_bit_list = list(informationbit)
        crc_info = CRC.CRC(information_bit_list, crc_n)
        crc_information_bit = crc_info.code
        crc_information_bit = np.array(crc_information_bit)
        crc_information_bit.resize(1, information_num + crc_n)

    # 编码前序列-u生成
    u = np.ones((1, N)) * frozen_bit
    j = 0
    #print(u.size)
    #print(information_bit.size)
    if crc_n == 0:
        for i in information_pos:
            u[0][i] = information_bit[0][j]
            j += 1
    else:
        for i in information_pos:
            u[0][i] = crc_information_bit[0][j]
            j += 1

    #print(information_pos)
    # 生成矩阵-G生成
    G = function.generate_matrix(n)

    # 编码比特-x生成
    x = u * G
    x = np.array(x % 2)

    # 经过信道生成y
    y = function.channel(x, channel_con, snr, rate)

    # y进入译码器生成u_d
    u_d = decoder.decoder(y, decode_method, decode_para, information_pos,
                          frozen_bit, channel_con, snr, rate, crc_n)
    #print(u_d)
    # 计算错误数
    information_pos = information_pos[0:information_num]
    information_bit_d = u_d[information_pos]
    error_num = int(np.sum((information_bit_d + information_bit) % 2))
    if error_num != 0:
        decode_fail = 1
    else:
        decode_fail = 0
    r_value = np.array([error_num, decode_fail])

    return r_value
Ejemplo n.º 2
0
def scf_decoder(y_llr, information_pos, frozen_bit,decode_para,crc_n):
    # 第一次用SC译码的过程
    N = y_llr.size
    n = int(np.log2(N))
    u_d_1_list = sc_decoder(y_llr,information_pos,frozen_bit)
    u_d_1_llr=u_d_1_list[1].copy()
    u_d_1=u_d_1_list[0].copy()
    u_d_1 = np.array([0 if u_d_1[i] == 0 else 1 for i in range(u_d_1.size)])
    u_d_1_info=u_d_1[information_pos]
    u_d_1_llr_info=u_d_1_llr[information_pos]
    #print(u_d_1_llr_info)
    u_d_1_info=list(u_d_1_info)
    #print(u_d_1)
    crc_c = CRC.CRC(u_d_1_info, crc_n)
    flag = crc_c.detection()
    #print(flag)
    flip_info_pos1 = list(np.argsort(np.abs(u_d_1_llr_info)))
    flip_info_pos = flip_info_pos1[0:decode_para[0]]
    information_pos_array=np.array(information_pos)
    flip_pos=information_pos_array[flip_info_pos]
    #print(flip_pos)
    if flag == 0:
        T=1
        while T <= decode_para[0]:
            #print(T)
            u_d_2_list=sc_flip1_decoder(y_llr,information_pos,frozen_bit,flip_pos[T-1])
            u_d_2_llr = u_d_2_list[1].copy()
            u_d_2 = u_d_2_list[0].copy()
            u_d_2 = np.array([0 if u_d_2[i] == 0 else 1 for i in range(u_d_2.size)])
            u_d_2_info=u_d_2[information_pos]
            u_d_2_info=list(u_d_2_info)
            crc_c = CRC.CRC(u_d_2_info, crc_n)
            flag1 = crc_c.detection()
            if flag1 == 1:
                u_d = np.array(u_d_2)
                #print('The scf decoder flip: ',T,' times and it works.')
                break
            else:
                T += 1
                u_d = np.array(u_d_1)
    else:
        #print('no flip and success')
        u_d=np.array(u_d_1)
    return u_d
Ejemplo n.º 3
0
    def process_data(self, Data):
        data = ""
        for x in Data:
            crc = CRC.CRC(x, 4)
            for y in crc.code:
                if y == 1:
                    data += "1"
                else:
                    data += "0"

        return data
Ejemplo n.º 4
0
def encoder(bin_path, out_path, time_lim, width=960, highth=960):
    time_lim = int(time_lim)
    fra = 10
    pixel_size = 20
    bin_in = dec2bin(bin_path)
    x = len(bin_in)
    row_num = int(highth / pixel_size) - 1
    col_num = int(width / pixel_size / 8)
    y = int(x // (row_num * col_num))  # 生成的图片数
    if (y > time_lim * fra):
        y = time_lim * fra
    for i in range(y):
        m = i * row_num * col_num
        n = (i + 1) * row_num * col_num
        bin_slice = bin_in[m:n]
        list_CRC = for_CRC(bin_slice)
        crc = CRC.CRC(list_CRC, 32)
        #n=crc.code_list
        #m=CRC.check(n)
        #print(m)
        c = form_CRC(crc)
        img = np.zeros((highth + 12 * pixel_size, width + 12 * pixel_size),
                       dtype=np.uint8)
        for j in range(row_num):

            img[j * pixel_size + 120:(j + 1) * pixel_size + 120,
                120:width + 120] = made_row(
                    bin_slice[j * col_num:(j + 1) * col_num], pixel_size,
                    width)
        img[1060:1080, 120:1080] = c
        img[100:1100, 100:120] = 255
        img[100:1100, 1080:1100] = 255
        img[1080:1100, 100:1100] = 255
        img[100:120, 100:1100] = 255

        im = Image.fromarray(img)
        im.save(str(i) + ".png")

    ff = FFmpeg(inputs={'': '-f image2 -r ' + str(fra) + ' -i %d.png'},
                outputs={out_path: '-vcodec mpeg4'})
    print(ff.cmd)
    ff.run()
    return y
        counter += 1
        if (counter == wielkoscPakietu):  # pakiet po 8 bitow
            packets.append(packet)  # tworzy liste pakietow
            packet = []
            counter = 0

    if (
            len(packet) > 0
    ):  # mozliwe tworzenie pakietow wiekszych niz 8 bitow. po prostu ostatni pakiet, bedzie malutki
        packets.append(packet)

    # DODANIE BITU PARZYSTOSCI DO KAZDEGO Z PAKIETOW
    tmr = TMR()
    hamming = Hamming()
    crc = CRC()
    parity = ParityBit()
    channel = Channel(prawdopodobienstwoBSC, channelS0, channelS1, channelP01,
                      channelP10)

    # print(packets)
    packetsWithParityBit = []
    for pack in packets:
        pack = tmr.codeTMR(pack)  # DODANIE TMR
        pack = parity.addParityBit(pack)  # DODANIE PARITY BIT
        # pack = hamming.codeHamming(pack)   # DODANIE Hamminga
        # pack = crc.addCRC(pack)            # DODANIE CRC

        packetsWithParityBit.append(pack)

    # print(packetsWithParityBit)
Ejemplo n.º 6
0
def bp_decoder(y_llr,information_pos,frozen_bit,decode_para,crc_n):
    N = y_llr.size
    n = int(np.log2(N))

    if decode_para[1] == 'max_iter':

        bp_max_iter=int(decode_para[0])
        #初始化左传播和右传播矩阵
        left_matrix=np.zeros([N,n+1])
        right_matrix=np.zeros([N,n+1])
        left_matrix[:,n]=y_llr
        temp_value=(1-2*frozen_bit)*np.infty
        temp=[temp_value if i not in information_pos else 0 for i in range(N)]
        right_matrix[:,0]=temp

        for iter in range(bp_max_iter):
            for i in range(n):
                left_matrix[:,n-i-1]=function.bp_update_left(left_matrix[:,n-i],right_matrix[:,n-i-1],n-i)

            for i in range(n):
                right_matrix[:,i+1]=function.bp_update_right(left_matrix[:,i+1],right_matrix[:,i],i+1)


        u_d_llr=left_matrix[:,0]+right_matrix[:,0]
        u_d=[0 if u_d_llr[i] >= 0 else 1 for i in range(N)]
        u_d=np.array(u_d)


    elif decode_para[1] == 'g_matrix':

        # 初始化左传播和右传播矩阵
        left_matrix = np.zeros([N, n + 1])
        right_matrix = np.zeros([N, n + 1])
        left_matrix[:, n] = y_llr
        temp_value = (1 - 2 * frozen_bit) * np.infty
        temp = [temp_value if i not in information_pos else 0 for i in range(N)]
        right_matrix[:, 0] = temp
        flag=0
        iter_num=1

        while flag == 0 and iter_num <= decode_para[0]:
            for i in range(n):
                left_matrix[:, n - i - 1] = function.bp_update_left(left_matrix[:, n - i], right_matrix[:, n - i - 1],n - i)

            for i in range(n):
                right_matrix[:, i + 1] = function.bp_update_right(left_matrix[:, i + 1], right_matrix[:, i], i + 1)

            u_d_llr = left_matrix[:, 0] + right_matrix[:, 0]
            u_d = [0 if u_d_llr[i] >= 0 else 1 for i in range(N)]
            x_d_llr = left_matrix[:, n] + right_matrix[:, n]
            x_d = [0 if x_d_llr[i] >= 0 else 1 for i in range(N)]
            G = function.generate_matrix(n)
            x_g = u_d * G
            x_g = np.array(x_g % 2)
            #print(x_g)
            cor = [0 if x_g[0][i]==x_d[i] else 1 for i in range(N)]
            if sum(cor) == 0:
                flag = 1
            else:
                flag = 0
            iter_num += 1
        u_d = np.array(u_d)


    elif decode_para[1] == 'crc_es':

        # 初始化左传播和右传播矩阵
        left_matrix = np.zeros([N, n + 1])
        right_matrix = np.zeros([N, n + 1])
        left_matrix[:, n] = y_llr
        temp_value = (1 - 2 * frozen_bit) * np.infty
        temp = [temp_value if i not in information_pos else 0 for i in range(N)]
        right_matrix[:, 0] = temp
        flag = 0
        iter_num = 1

        while flag == 0 and iter_num <= decode_para[0]:
            for i in range(n):
                left_matrix[:, n - i - 1] = function.bp_update_left(left_matrix[:, n - i], right_matrix[:, n - i - 1],n - i)

            for i in range(n):
                right_matrix[:, i + 1] = function.bp_update_right(left_matrix[:, i + 1], right_matrix[:, i], i + 1)

            u_d_llr = left_matrix[:, 0] + right_matrix[:, 0]
            u_d = np.array([0 if u_d_llr[i] >= 0 else 1 for i in range(N)])
            u_d_info = u_d[information_pos]
            u_d_info = list(u_d_info)
            crc_c=CRC.CRC(u_d_info,crc_n)
            flag=crc_c.detection()
            # if flag == 1:
            #     print('It work!',iter_num)

            iter_num += 1
        u_d = np.array(u_d)
    else:
        print('This is not a early stopping method of BP decoder or s have not added it to the endecoder.bp_decoder !')
        quit()
    return u_d
Ejemplo n.º 7
0
def scl_decoder(y_llr, information_pos, frozen_bit, decode_para, crc_n):
    N = y_llr.size
    n = int(np.log2(N))
    llr_matrix = np.ones((n + 1, N))
    llr_matrix[llr_matrix == 1] = float('nan')
    bit_matrix = llr_matrix.copy()
    llr_matrix[0] = y_llr
    list_max= decode_para[0]
    pm_method=decode_para[1]
    split_pos=information_pos                                               #采用传统的方法,在所有信息位分裂,未裁剪和减复杂度
    llr_list = [llr_matrix]
    bit_list = [bit_matrix]
    #print(llr_list)
    #print(bit_list)
    pm_list = [0]
    split_loc=0
    split_len=len(split_pos)
    l_now=1

    while split_len-1 >= split_loc:
        for i in range(l_now):
            llr_matrix_temp = llr_list[i]
            bit_matrix_temp = bit_list[i]
            pm_temp = pm_list[i]

            matrix_temp = sc_stepping_decoder(llr_matrix_temp, bit_matrix_temp, information_pos, frozen_bit,
                                              split_pos[split_loc])

            llr_list[i] = matrix_temp[0]
            bit_list[i] = matrix_temp[1]
            right_pm_update = function.get_pm_update(
                matrix_temp[0][n][split_pos[split_loc - 1] + 1:split_pos[split_loc] + 1],
                matrix_temp[1][n][split_pos[split_loc - 1] + 1:split_pos[split_loc] + 1], pm_method)
            pm_list[i] = pm_temp + right_pm_update
            llr_list.append(matrix_temp[0].copy())
            bit_matrix_wrong = matrix_temp[1].copy()
            bit_matrix_wrong[n][split_pos[split_loc]] = 1 - bit_matrix_wrong[n][split_pos[split_loc]]
            bit_list.append(bit_matrix_wrong.copy())
            wrong_pm_update = function.get_pm_update(
                matrix_temp[0][n][split_pos[split_loc - 1] + 1:split_pos[split_loc] + 1],
                bit_matrix_wrong[n][split_pos[split_loc - 1] + 1:split_pos[split_loc] + 1], pm_method)
            pm_list.append(pm_temp + wrong_pm_update)

        if l_now > list_max/2:
            pm_list_arg=np.argsort(pm_list)
            del_list_arg=pm_list_arg[list_max:]
            #删减多余的列表分支
            pm_list = [pm_list[i] for i in range(len(pm_list)) if i not in del_list_arg]
            llr_list = [llr_list[i] for i in range(len(llr_list)) if i not in del_list_arg]
            bit_list = [bit_list[i] for i in range(len(bit_list)) if i not in del_list_arg]
        l_now = len(pm_list)
        split_loc += 1

    if split_pos[-1] != N-1:
        for i in range(l_now):
            llr_matrix_temp = llr_list[i]
            bit_matrix_temp = bit_list[i]
            pm_temp = pm_list[i]
            matrix_temp = sc_stepping_decoder(llr_matrix_temp, bit_matrix_temp, information_pos, frozen_bit,N-1)
            llr_list[i] = matrix_temp[0]
            bit_list[i] = matrix_temp[1]
            right_pm_update = function.get_pm_update(
                matrix_temp[0][n][split_pos[split_loc - 1]+1:N],
                matrix_temp[1][n][split_pos[split_loc - 1]+1:N], pm_method)
            pm_list[i] = pm_temp + right_pm_update

    #接下来提取list中的译码值经过crc校验,采取的是排序pm,从最小的开始经过CRC,第一个通过CRC的即作为译码值输出
    pm_argsort=np.argsort(pm_list)
    for i in pm_argsort:
        u_d_temp=bit_list[i][n]
        u_d_temp = np.array([0 if u_d_temp[i] == 0 else 1 for i in range(u_d_temp.size)])
        u_d_temp_info = u_d_temp[information_pos]
        u_d_temp_info = list(u_d_temp_info)
        crc_c = CRC.CRC(u_d_temp_info, crc_n)
        flag = crc_c.detection()
        if flag == 1:
            u_d=u_d_temp
            break
        elif flag == 0 and i == pm_argsort[-1]:
            u_d_temp=bit_list[pm_argsort[0]][n]
            u_d_temp = np.array([0 if u_d_temp[i] == 0 else 1 for i in range(u_d_temp.size)])
            u_d=u_d_temp

    return u_d