UOMOP

16/64QAM 본문

Wireless Comm./Python

16/64QAM

Happy PinGu 2023. 5. 2. 16:51
class Tx:
    def Modulation(input_sig, mode):
        # mode : {M-ASK, M-FSK, M-PSK}

        if mode[-3] == 'P':
            M = int(mode[0])
            k = int(math.log2(M))

            start_phase = np.pi * (1 / M)

            phase_list = list([start_phase])
            phase_step = start_phase * 2

            for i in range(int(M / 2) - 1):
                start_phase = phase_list[i] + phase_step
                phase_list.append(start_phase)

            phase_list_rev = list(reversed(phase_list))

            for i in range(len(phase_list_rev)):
                phase_list_rev[i] *= -1

            phase_list = phase_list + phase_list_rev

            result = list()
            table_for_demod = list()

            for i in range(M):
                inphase = np.cos(phase_list[i])
                quadrature = np.sin(phase_list[i])

                table_for_demod.append(complex(inphase, quadrature))

            for i in range(int(len(input_sig) / k)):
                bin_sig = "".join(map(str, input_sig[k * i: k * i + k]))
                # print('0b' + bin_sig)

                sig = int('0b' + bin_sig, 2)

                inphase = np.cos(phase_list[sig])
                quadrature = np.sin(phase_list[sig])

                result.append(complex(inphase, quadrature))

            return result



        elif mode[-3] == 'Q' :

            if mode[0] == '1' :
                M = 16
            elif mode[0] == '6' :
                M = 64

            k = int(math.log2(M))
           
            list_gray = list()
            list_bbb = list()
            output_sig = list()

            for i in range( int(sqrt(M)) ) :
                list_gray.append(   format(graycode.tc_to_gray_code(i), 'b').zfill(int(k/2))      )

            for i in range( int(sqrt(M)) ) :
                start_num = 1 - int(sqrt(M))
                list_bbb.append(start_num + 2 * i  )

            for i in range( int(len(input_sig)/k) ) :
                cut_sig = input_sig[k*i : k*(i+1)]
                inphase_val    = str("")
                quadrature_val = str("")

                for j in range(int(len(cut_sig)/2)) :
                    inphase_val    = inphase_val + str(cut_sig[j])
                    quadrature_val = quadrature_val + str( cut_sig[j + int(len(cut_sig)/2)]   )
                
                check_inphase    = 0
                check_quadrature = 0

                for t in range( int(sqrt(M)) ) :
                    if inphase_val == list_gray[t] :
                        inphase = list_bbb[t]
                    if quadrature_val == list_gray[t] :
                        quadrature = list_bbb[t]

                output_sig.append(complex(inphase, quadrature))

            return output_sig



class Rx:
    def DeModulation(input_sig, mode):

        if mode[2] == 'P':
            M = int(mode[0])
            k = int(math.log2(M))
            bit_list = list()

            start_phase = np.pi * (1 / M)

            phase_list = list([start_phase])
            phase_step = start_phase * 2

            for i in range(int(M / 2) - 1):
                start_phase = phase_list[i] + phase_step
                phase_list.append(start_phase)

            phase_list_rev = list(reversed(phase_list))

            for i in range(len(phase_list_rev)):
                phase_list_rev[i] *= -1

            phase_list = phase_list + phase_list_rev

            result = list()
            table_for_demod = list()

            for i in range(M):
                inphase = np.cos(phase_list[i])
                quadrature = np.sin(phase_list[i])

                table_for_demod.append(complex(inphase, quadrature))


            for i in range(len(input_sig)):
                input_sig_x = float(input_sig[i].real)
                input_sig_y = float(input_sig[i].imag)

                distance = 1000
                output = 0

                for j in range(M):
                    if distance > sqrt(
                            (input_sig_x - table_for_demod[j].real) ** 2 + (input_sig_y - table_for_demod[j].imag) ** 2):
                        distance = sqrt(
                            (input_sig_x - table_for_demod[j].real) ** 2 + (input_sig_y - table_for_demod[j].imag) ** 2)
                        output = j

                bin_output = format(output, 'b').zfill(k)

                for q in range(k):
                    bit_list.append(int(bin_output[q]))

            return bit_list



        elif mode[-3] == 'Q' :

            if mode[0] == '1' :
                M = 16
            elif mode[0] == '6' :
                M = 64

            k = int(math.log2(M))

            print(k)

            list_gray = list()
            list_bbb = list()
            output_sig = list()
            all_candidate = list()
            demodulated_list = list()

            for i in range( int(sqrt(M)) ) :
                list_gray.append(   format(graycode.tc_to_gray_code(i), 'b').zfill(int(k/2))      )

            for i in range( int(sqrt(M)) ) :
                start_num = 1 - int(sqrt(M))
                list_bbb.append(start_num + 2 * i  )

            for i in range(len(list_bbb)) :
                for j in range(len(list_bbb)) :
                    all_candidate.append(complex(list_bbb[i], list_bbb[j]))

            print(list_gray)
            print(list_bbb)
            print(all_candidate)
                    

            for i in range(len(input_sig)) :

                d_min = 100
                save_index = 0

                for j in range(len(all_candidate)) :
                    if sqrt(((float(input_sig[i].real) - float(all_candidate[j].real))**2) + ((float(input_sig[i].imag) - float(all_candidate[j].imag))**2)) < d_min :
                        d_min = sqrt(((float(input_sig[i].real) - float(all_candidate[j].real))**2) + ((float(input_sig[i].imag) - float(all_candidate[j].imag))**2))
                        inphase_val    = int(all_candidate[j].real)
                        quadrature_val = int(all_candidate[j].imag)

                for k in range(len(list_bbb)) :
                    if (inphase_val == list_bbb[k]) :
                        output_first = list_gray[k]

                    if (quadrature_val == list_bbb[k]) :
                        output_second = list_gray[k]

                result = output_first + output_second

                for q in range( len(result) ) :
                    demodulated_list.append(int(result[q]))


            return demodulated_list
input_sig = Bit_Gen(120000)
moded_sig = Tx.Modulation(input_sig, mode = "64-QAM")
demoded_sig = Rx.DeModulation(moded_sig, mode = "64-QAM")

BER_Check(input_sig, demoded_sig)

BER = 0.0

'Wireless Comm. > Python' 카테고리의 다른 글

LPDC (n, k) comparison  (0) 2023.05.03
save2  (0) 2023.05.03
save1  (0) 2023.05.02
Linear Block Coding Comparison (6, 3) vs (7, 4)  (0) 2023.05.02
def Restore_Image(input, c_or_g)  (0) 2023.05.01
Comments