QPSK mapper python code | QPSK demapper python script

This page covers QPSK mapper python code and QPSK demapper python script which is used for QPSK mapping and QPSK demapping respectively.

Introduction: QPSK stands for Quadrature Phase Shift Keying. This digital modulation technique is used to increase data rate by two. It maps two binary digits on single RF carrier.

QPSK mapper

The figure depicts QPSK data constellation and QPSK mapping process. QPSK demapper does the reverse functions of the QPSK mapper. Refer QPSK modulation >> for more information.

QPSK mapping and QPSK demapping python script

Following mapper/demapper python code can be used to map digital binary data to QPSK constellation points and vice versa.

import random
import numpy as np
import matplotlib.pyplot as plt
len1 = 48 # length of input data
mode = 2 # QPSK 2 bits per sub carrier
mapping_table = {
0: 0.7071 + 0.7071j,
1: - 0.7071 + 0.7071j,
2: 0.7071 - 0.7071j,
3: - 0.7071 - 0.7071j}
dec_binary = {
0: '00',
1: '01',
2: '10',
3: '11'}
# Binary random data generator (1s and 0s)
input_data = ""
for i in range(len1):
  temp1 = str(random.randint(0, 1))
  input_data += temp1
print("Mapper IN, Input Binary string :", input_data)
# Conversion of bits to dual bits and integer list
s1 = input_data
s2_user1 = []
chunks = [s1[i:i+mode] for i in range(0, len(s1), mode)]
for piece in chunks:
  temp = int(piece, 2)
  s2_user1.append(temp)
print("Input integer list :", s2_user1)
# QPSK mapping part
Q = np.zeros((24,), dtype=complex)
j = 0
for val in s2_user1:
  Q[j] = mapping_table[val]
  j += 1
print("QPSK mapper output complex list", Q)
# QPSK de-mapping part
Q1 = np.zeros((24,), dtype=int)
j = 0
for i in Q:
  if i == 0.7071+0.7071j:
    Q1[j] = 0
  elif i == -0.7071+0.7071j:
    Q1[j] = 1
  elif i == 0.7071-0.7071j:
    Q1[j] = 2
  elif i == -0.7071-0.7071j:
    Q1[j] = 3
  else:
    print("improper i value")
  j += 1
print("De-mapper Output integer list:", Q1)
# Converting integer to bits
output_data = ""
j = 0
for val in Q1:
  output_data += dec_binary[val]
  j += 1
print("De-mapper OUT, Output Binary String:", output_data)
plt.plot(s2_user1-Q1)
plt.title("Difference between input and output integer data")
plt.show()

QPSK mapper python output and its plot


Following are the respective outputs at different points in the QPSK python code. It mentions mapper binary input, its corresponding interger list, QPSK mapped output, de-mapper integer output, de-mapper binary output.

QPSK mapper and demapper python output

Following plot mentions difference between mapper input (integer array list) and demapper output (integer array list). As shown, output is at zero level meaning both the outputs are equal in value.

Difference between QPSK input and output integer data

Other useful DSP codes in Python

Useful Links to MATLAB codes

RF and Wireless tutorials