2 minute read

16QAM (16 Quadrature Amplitude Modulation)

import numpy as np
import math
from random import randrange
from math import pi as PI
Num_Bit = 4
BER = []
BitPerNoiseDB_iter = range(0,15)
A = 1/math.sqrt(10) # Es = 1로 조정하기 위함

for BitPerNoiseDB in BitPerNoiseDB_iter:
  SymbolPerNoiseRATIO = Num_Bit*10**(BitPerNoiseDB/10)
  AWGN_sigma = math.sqrt(1/(2*SymbolPerNoiseRATIO))
  
  Error_Count = 0
  Total_Error_Count = 0
  Bit_Count = 0

  while Total_Error_Count < 100:

    Information_Generator = np.empty(Num_Bit, dtype='int')
    Bit = np.empty(Num_Bit, dtype='int')
 
    for generator in range(0, Num_Bit):
      Information_Generator[generator] = randrange(2)

    Detected_Signal = np.empty(Num_Bit, dtype='int')
    Detected_Array = np.empty(Num_Bit, dtype='int')
    Bit = Information_Generator
  

    if (Bit[0] == 0 and Bit[1] == 0 and Bit[2] == 0 and Bit[3] == 0):
      symbol = complex(3*A, 3*A)

    elif (Bit[0] == 0 and Bit[1] == 0 and Bit[2] == 0 and Bit[3] == 1):
      symbol = complex(3*A, A)
    
    elif (Bit[0] == 0 and Bit[1] == 0 and Bit[2] == 1 and Bit[3] == 0):
      symbol = complex(A, 3*A)

    elif (Bit[0] == 0 and Bit[1] == 0 and Bit[2] == 1 and Bit[3] == 1):
      symbol = complex(A, A)

    elif (Bit[0] == 0 and Bit[1] == 1 and Bit[2] == 0 and Bit[3] == 0):
      symbol = complex(3*A, -3*A)

    elif (Bit[0] == 0 and Bit[1] == 1 and Bit[2] == 0 and Bit[3] == 1):
      symbol = complex(3*A, -A)

    elif (Bit[0] == 0 and Bit[1] == 1 and Bit[2] == 1 and Bit[3] == 0):
      symbol = complex(A, -3*A)

    elif (Bit[0] == 0 and Bit[1] == 1 and Bit[2] == 1 and Bit[3] == 1):
      symbol = complex(A, -A)

    elif (Bit[0] == 1 and Bit[1] == 0 and Bit[2] == 0 and Bit[3] == 0):
      symbol = complex(-3*A, 3*A)

    elif (Bit[0] == 1 and Bit[1] == 0 and Bit[2] == 0 and Bit[3] == 1):
      symbol = complex(-3*A, A)

    elif (Bit[0] == 1 and Bit[1] == 0 and Bit[2] == 1 and Bit[3] == 0):
      symbol = complex(-A, 3*A)

    elif (Bit[0] == 1 and Bit[1] == 0 and Bit[2] == 1 and Bit[3] == 1):
      symbol = complex(-A, A)

    elif (Bit[0] == 1 and Bit[1] == 1 and Bit[2] == 0 and Bit[3] == 0):
      symbol = complex(-3*A, -3*A)

    elif (Bit[0] == 1 and Bit[1] == 1 and Bit[2] == 0 and Bit[3] == 1):
      symbol = complex(-3*A, -A)

    elif (Bit[0] == 1 and Bit[1] == 1 and Bit[2] == 1 and Bit[3] == 0):
      symbol = complex(-A, -3*A)
    
    else:
      symbol = complex(-A, -A)

    AWGN = np.random.normal(size = (1,2))
    AWGN = complex(AWGN[0][0], AWGN[0][1]) * AWGN_sigma

    Received_Signal = symbol + AWGN
    Received_Signal_Phase = math.atan2(Received_Signal.imag, Received_Signal.real)

    if (Received_Signal.real > 0):
      Detected_Array[0] = 0
    else:
      Detected_Array[0] = 1

    if (Received_Signal.imag > 0):
      Detected_Array[1] = 0
    else:
      Detected_Array[1] = 1

    if (abs(Received_Signal.real) > 2*A):
      Detected_Array[2] = 0
    else:
      Detected_Array[2] = 1

    if (abs(Received_Signal.imag) > 2*A):
      Detected_Array[3] = 0
    else:
      Detected_Array[3] = 1

    Error_Count = 0
    for index in range(0, Num_Bit):
      if Bit[index] != Detected_Array[index]:
        Error_Count += 1

    
    Total_Error_Count += Error_Count
    Bit_Count += Num_Bit

    
  BER.append(Total_Error_Count/Bit_Count)

  print("Bit_Count :", Bit_Count)
  print("Eb/N0 :{} [dB]".format(BitPerNoiseDB))
  print("The number of bits with error :", Total_Error_Count)
  print("BER :", BER[BitPerNoiseDB] )
  print("---------------------------------")

download

Categories:

Updated: