ASK Modulation in Python: Code and Explanation
Advertisement
This document details Amplitude Shift Keying (ASK) modulation using Python. We’ll cover the code implementation, explain the underlying principles, and show the resulting waveforms.
Introduction to ASK Modulation
ASK, or Amplitude Shift Keying, is a digital modulation technique where binary data is represented by varying the amplitude of a carrier signal. A key aspect of ASK is the encoding of binary 1s and 0s as distinct amplitude levels. A common form of ASK is ON-OFF keying (OOK), where a binary ‘1’ is represented by the presence of the carrier signal, and a binary ‘0’ by its absence.
Figure 1 illustrates this concept. The Python script below implements this form of ASK. The script uses a randomly generated binary data stream (created using a separate Binarygen.py script) as the input.

Mathematically, ASK modulation can be expressed as follows:
-
For Binary Logic-1:
-
For Binary Logic-0:
Where:
- is the amplitude of the carrier signal.
- is the carrier frequency.
- is time.
Binary Data Generator Script (Binarygen.py)
The following Python script generates random binary data, serving as the baseband data for the ASK modulator.
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
This script generates a random binary sequence. The `sym` parameter controls the number of symbols, and `sym_len` determines the length of each symbol. It returns a NumPy array representing the binary data stream.
## ASK Modulation Python Script
This Python script generates the ASK modulated waveform using the random binary data generated by `Binarygen.py`.
```python
# This python script generates ASK 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 waveform configuration parameters
Fs = 1000 # Samples per second
fc = 25 # Carrier frequency 25 Hz
T = 1 # Total simulation time in seconds
t = np.arange(0, T, 1/Fs)
x = np.sin(2*pi*fc*t)
Td = 0.1 # Bit duration
Nsamples = int(Td*Fs) # Samples in one bit duration
Nsym = int(np.floor(np.size(t)/Nsamples))
# Python code to generate binary stream of data
sig = binary(Nsym, Nsamples)
# ASK waveform generation
Xask = x * sig
# Binary waveform and ASK waveform Plots
figure, axis = plt.subplots(2)
axis[0].plot(t,sig)
axis[0].set_title("Binary digital data")
axis[1].plot(t, Xask)
axis[1].set_title("ASK modulated signal")
plt.tight_layout()
plt.show()
This script first defines the parameters for the carrier wave and the binary data. It then generates the binary data using the binary() function from Binarygen.py. The ASK modulation is achieved by multiplying the carrier wave x with the binary data sig. Finally, the script plots both the original binary data and the resulting ASK modulated waveform.
Output Plots
The following plots show the binary input data and the ASK modulated waveform generated by the script.

The top plot shows the random binary data, and the bottom plot shows the ASK modulated signal. Notice how the amplitude of the carrier signal corresponds to the binary data (presence of carrier for ‘1’, absence for ‘0’).
Explore Python Codes on Wireless Communication & Modulation Simulations
- AM Modulation Simulation in Python
- FM Modulation in Python
- PM Modulation with Python
- FSK Modulation in Python
- PSK Modulation in Python
- BPSK, QPSK, QAM Modulation in Python
- QPSK Mapper / Demapper Python Code
- OFDM Transmitter Simulation in Python (WiMAX)
- Generalized OFDMA Simulation with Python
Continue Learning Modulation Types
- AM Vs. FM Vs. PM : Difference between Analog Modulation Techniques
- ASK Vs. FSK Vs. PSK : Difference between Digital Modulation Techniques
- PAM Vs. PWM Vs. PPM : Difference between Pulse Modulation Techniques
- BPSK Vs. QPSK : Key Differences
- 16-QAM Vs. 64-QAM Vs. 256-QAM : Key Differences
- 512-QAM Vs. 1024-QAM Vs. 2048-QAM Vs. 4096-QAM
- 4096QAM Modulation Used in Wi-Fi 7
- MSK Vs. GMSK : Modulation Technique used in GSM
- LORa Modulation Used in LoRaWAN
- 10% ASK & 100% ASK Modulation used in NFC
- OOK Vs. VPPM Vs. CSK : Visible Light Communication (VLC) Modulation Techniques
- QPSK Vs. OQPSK Vs. PI/4 QPSK
- PAM4 Modulation Used in Gigabit Ethernet
- CCK Vs. DSSS Vs. OFDM Used in WLAN (Wi-Fi) Standards
