1 minute read

QPSK (Quadrature Phase Shift Keying) Modulation

import numpy as np
import math
from random import randrange
from math import pi as PI
Num_Bit = 2
BER = []
BitPerNoiseDB_iter = range(0,11)

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):
      symbol = complex(math.cos(PI/4), math.sin(PI/4))

    elif (Bit[0] == 0 and Bit[1] == 1):
      symbol = complex(math.cos(3*PI/4), math.sin(3*PI/4))
    
    elif (Bit[0] == 1 and Bit[1] == 1):
      symbol = complex(math.cos(-3*PI/4), math.sin(-3*PI/4))

    else : # Bit[0] == 1 and Bit[1] == 0
      symbol = complex(math.cos(-1*PI/4), math.sin(-1*PI/4))

    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_Phase >= 0) and (Received_Signal_Phase < PI/2):
      Detected_Array = 0, 0

    elif (Received_Signal_Phase >= PI/2) and (Received_Signal_Phase < PI):
      Detected_Array = 0, 1

    elif (Received_Signal_Phase >= -1*PI) and (Received_Signal_Phase < -1*PI/2):
      Detected_Array = 1, 1

    else: 
      Detected_Array = 1, 0

    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("---------------------------------")
%matplotlib inline 
import matplotlib.pyplot as plt

plt.figure(figsize=(8,7))
plt.plot(BitPerNoiseDB_iter, QPSK_BER, '-v', label = 'QPSK', color="pink")
plt.plot(BitPerNoiseDB_iter, BPSK_BER, '-*', label = 'BPSK', color="green")
plt.title('QPSK & BPSK BER', fontsize = 20) 
plt.xlabel("Eb/N0 [dB]", fontsize=15) 
plt.ylabel("BER", fontsize=15)
plt.grid(True, color='blue', alpha=0.3, linestyle='--')
plt.yscale('log')
plt.legend(fontsize=15)

download

Categories:

Updated: