RF Wireless World

Browse articles, tutorials, tools, and vendors.

FSK Modulation Implementation in Python

This document provides Python code for Frequency Shift Keying (FSK) modulation. The script generates an FSK modulated waveform and a binary data waveform as output.

Introduction to FSK

FSK stands for Frequency Shift Keying. It’s a digital modulation scheme where digital data (binary 0s and 1s) are represented by different carrier frequencies. Binary 1 is represented by one frequency (f1), and binary 0 is represented by another frequency (f2).

In the provided Python script, f1 is set to 45 Hz, and f2 is set to 30 Hz. The script uses a separate Binarygen.py script to generate random binary data.

FSK modulation

Binary FSK can be represented by the following equations:

s(t)=Asin(2πf1t)for Binary 1, f1=45 Hz in the python code belows(t) = A \cdot \sin(2 \cdot \pi \cdot f_1 \cdot t) \quad \text{for Binary 1, } f_1 = 45 \text{ Hz in the python code below} s(t)=Asin(2πf2t)for Binary 0, f2=30 Hz in the python code belows(t) = A \cdot \sin(2 \cdot \pi \cdot f_2 \cdot t) \quad \text{for Binary 0, } f_2 = 30 \text{ Hz in the python code below}

Binary Data Generator (Binarygen.py)

The following Python script generates random binary data, used as the baseband data for FSK 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

FSK Python Script

The following FSK Python script generates an FSK modulated waveform for random binary data input.

# This python script generates FSK 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 = 30    # Carrier frequency 30 Hz, 30 cycles/sec
T = 1      # Total simulation time in seconds, 1sec
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)

# FSK waveform generation
f = fc + fc*sig/2
Xfsk = np.sin(2*pi*f*t)

# Binary waveform and FSK modulation waveform Plots
figure, axis = plt.subplots(2)
axis[0].plot(t, sig)
axis[0].set_title("Binary digital data")
axis[1].plot(t, Xfsk, 'r')
axis[1].set_title("FSK modulated signal")
plt.tight_layout()
plt.show()

Output Plots

The following are the output plots (Binary input data and FSK modulated waveform) generated by the FSK modulation Python script.

FSK modulation python plots