PSK modulation python code | PSK python script

This page covers PSK modulation python code. PSK python script plots PSK modulated waveform and binary data waveform as output.

Introduction: PSK stands for Phase Shift Keying. This is one of the digital modulation scheme in which binary data are represented by phase of carrier frequency. Usually 180 degree of phase shift is applied between binary 1 and binary 0. In following PSK python script, phase shift of pi radian (i.e. 90 degree) is being used.

PSK modulation

PSK modulation output can be represented by following equation :
sig (t) = sin(2*pi*fc*t + phase), phase is 3*pi/2 for binary '1'
sig (t) = sin(2*pi*fc*t + phase), phase is pi for binary '0'

Binary data generator Python script | Binarygen.py

Following python script can be used to generate random binary data. This is used as baseband data in ASK modulation.

def binary(sym, sym_len):

  import numpy as np
  rand_n = np.random.rand(sym)
  rand_n[np.where(rand_n >= 0.5)] = 1
  rand_n[np.where(rand_n <= 0.5)] = 0

  sig = np.zeros(int(sym*sym_len))

  # generating symbols
  id1 = np.where(rand_n == 1)

  for i in id1[0]:
    temp = int(i*sym_len)
    sig[temp:temp+sym_len] = 1
  return sig

PSK Python script

Following PSK python script can be used to generate PSK modulated waveform for random binary data given as input.

# This python script generates PSK modulated waveforms
# Library files
import matplotlib.pyplot as plt
import numpy as np
from Binarygen import binary
from math import pi
plt.close('all')
# Carrier wave and binary signal configuration parameters
Fs = 1000 # Samples per second
fc = 50 # Carrier frequency 50 Hz, 50 cycles/sec
T = 1 # Total simulation time in seconds
t = np.arange(0, T, 1/Fs)
Td = 0.1 # Bit duration
Nsamples = int(Td*Fs) # Samples in one bit duration
Nsym = int(np.floor(np.size(t)/Nsamples))
# Binary waveform generation
sig = binary(Nsym,Nsamples)
# PSK waveform generation
phase= pi + pi*sig/2
Xpsk = np.sin(2*pi*fc*t + phase)
# Binary waveform and PSK modulation waveform Plots
figure, axis = plt.subplots(2)
axis[0].plot(t, sig)
axis[0].set_title("Binary digital data")
axis[1].plot(t, Xpsk, 'r')
axis[1].set_title("PSK modulated signal")
plt.tight_layout()
plt.show()

Output plots of PSK modulation python code

Following are the output plots (Binary input data and PSK modulated waveform) of above PSK modulation python script.

PSK modulation python plots

Other useful DSP codes in Python

Useful Links to MATLAB codes

RF and Wireless tutorials