Skip to main content

MATLAB Code for ASK, FSK, and PSK (with Online Simulator)


MATLAB Code for ASK, FSK, and PSK

Comprehensive implementation of digital modulation and demodulation techniques with simulation results.




MATLAB Code for ASK Modulation and Demodulation

% The code is written by SalimWireless.Com clc; clear all; close all; % Parameters Tb = 1; fc = 10; N_bits = 10; Fs = 100 * fc; Ts = 1/Fs; samples_per_bit = Fs * Tb; rng(10); binary_data = randi([0, 1], 1, N_bits); t_overall = 0:Ts:(N_bits * Tb) - Ts; message_signal_overall = zeros(1, length(t_overall)); ask_signal_overall = zeros(1, length(t_overall)); carrier_template = sqrt(2/Tb) * sin(2*pi*fc*(0:Ts:Tb-Ts)); for i = 1:N_bits current_bit = binary_data(i); t_segment_indices = ((i-1)*samples_per_bit + 1) : (i*samples_per_bit); if current_bit == 1 message_segment = ones(1, samples_per_bit); else message_segment = zeros(1, samples_per_bit); end ask_segment = carrier_template .* message_segment; message_signal_overall(t_segment_indices) = message_segment; ask_signal_overall(t_segment_indices) = ask_segment; end
ASK Output

Fig 1: ASK Modulation and Demodulation

MATLAB Code for FSK Modulation and Demodulation

% The code is written by SalimWireless.Com clc; clear; close all; % Parameters Fs = 1000; fc1 = 20; fc2 = 50; Tb = 1; num_bits = 10; Ts = 1/Fs; samples_per_bit = Fs * Tb; rng(20); bits = randi([0, 1], 1, num_bits); t_bit = 0:Ts:Tb-Ts; modulated_signal = []; for bit = bits if bit == 0 modulated_signal = [modulated_signal sin(2*pi*fc1*t_bit)]; else modulated_signal = [modulated_signal sin(2*pi*fc2*t_bit)]; end end
FSK Output

Fig 2: FSK Modulation and Demodulation

MATLAB Code for PSK Modulation and Demodulation

% The code is written by SalimWireless.Com clc; clear all; close all; carrier_frequency = 10; bit_duration = 1; sampling_frequency = 1000; Ts = 1/sampling_frequency; samples_per_bit = sampling_frequency * bit_duration; rng(30); bit_stream = randi([0, 1], 1, 8); t_bit = 0:Ts:bit_duration-Ts; psk_signal = []; for bit = bit_stream if bit == 0 psk_signal = [psk_signal sin(2*pi*carrier_frequency*t_bit + 0)]; else psk_signal = [psk_signal sin(2*pi*carrier_frequency*t_bit + pi)]; end end
PSK Output

Fig 3: PSK Modulation and Demodulation

Understanding the MATLAB Implementation

Key Variables

  • Fs: Sampling frequency (must be > 2x carrier frequency).
  • Tb: Bit duration (defines how long each symbol lasts).
  • rng(10): Ensures your random bit generation is reproducible.

The Logic

The code uses a for loop to iterate through the bitstream. Depending on the bit value (0 or 1), it selects the appropriate carrier frequency or phase, effectively performing Hard-Decision Mapping.

Common MATLAB Errors & Fixes

1. "Undefined function or variable": Ensure you are running the script in the same folder where your variables are saved. Use clc; clear all; at the top.

2. Aliasing in Plots: If your waveforms look "jagged," increase the Fs (Sampling Frequency) to at least 10 times the carrier frequency.

3. Toolboxes: These codes run on base MATLAB, but for advanced BER analysis, you may need the Communication Toolbox.

Interactive Modulation Simulator

Launch the web-based tool to simulate digital modulation techniques.

Simulator Launch Simulator →

Modulation Parameter Changed Noise Immunity Complexity
ASK Amplitude Low (Highly susceptible) Simplest
FSK Frequency High (Robust) Moderate
PSK Phase Very High Highest (Requires Coherent Detection)

From Simulation to Hardware

While these MATLAB scripts simulate digital modulation in a vacuum, real-world deployment requires considering AWGN (Additive White Gaussian Noise). If you are moving to hardware like USRP (Universal Software Radio Peripheral) or RTL-SDR, you must implement Pulse Shaping (like Root-Raised Cosine) to limit bandwidth occupancy. Want to read more about raised cosine filter? Click here.

Frequently Asked Questions

Which is better: ASK, FSK, or PSK?

PSK is generally superior for high-speed data because it is the most bandwidth-efficient and noise-resistant, though it requires more complex receiver hardware.

How do I calculate BER in MATLAB?

You can use the biterr function in MATLAB to compare the transmitted bitstream with the demodulated bitstream to calculate the Bit Error Rate.


📚 Further Reading


Effect of Noise (AWGN) on ASK, FSK, and PSK

The modulated signal, x(t), is propagated through a physical communication medium—such as a wireless interface or fiber-optic cabling—where it is subject to various channel impairments. Consequently, the resulting received signal, y(t), constitutes a degraded representation of the original transmission.

This relationship is mathematically characterized by the general received signal model in the presence of fading and noise:

y(t) = h(t) · x(t) + n(t)

Where:

  • x(t) denotes the transmitted modulated signal;
  • h(t) represents the multiplicative fading characteristics of the channel;
  • n(t) signifies the Additive White Gaussian Noise (AWGN) superimposed on the signal.

Read More: Effect of AWGN on ASK→   Effect of AWGN on FSK→   Effect of AWGN on PSK→   GET MATLAB Code 


Effect of Rayleigh Fading on ASK, FSK, and PSK

This technical analysis details the lifecycle of a signal within a digital communication system, focusing on the transition from the message signal to final recovery. To facilitate long-distance transmission, information is modulated onto high-frequency carriers. However, as the signal traverses a physical medium, it encounters significant degradation modeled by the following time-domain relationship:

y(t) = [h(t)] ∗ s(t) + w(t)

In this model, h(t) represents the Channel Impulse Response—specifically Rayleigh Fading—while w(t) signifies Additive White Gaussian Noise (AWGN). The text distinguishes between these two impairments: AWGN is a constant thermal noise that reduces the signal-to-noise ratio (SNR) across all frequencies, whereas Rayleigh Fading is a stochastic process caused by multipath propagation. In urban or indoor environments, signal reflections create "deep fades," resulting in rapid fluctuations of signal strength.

The performance impact is most visible in the Bit Error Rate (BER). While BER in an AWGN channel decreases exponentially with higher power, fading channels exhibit a much slower, linear decay. For instance, at 0 dB SNR using BPSK modulation, the BER effectively doubles from 0.078 (AWGN) to 0.16 (Rayleigh).

To combat these effects, systems employ Equalization to reverse channel distortion and Diversity Techniques (spatial, temporal, or frequency) to ensure redundancy.

Read More: Impact of Rayleigh Fading on BPSK



Contact Us

Name

Email *

Message *

Popular Posts

MIMO Channel Matrix | Rank and Condition Number

MIMO / Massive MIMO MIMO Channel Matrix | Rank and Condition...   The channel matrix in wireless communication is a matrix that describes the impact of the channel on the transmitted signal. The channel matrix can be used to model the effects of the atmospheric or underwater environment on the signal, such as the absorption, reflection or scattering of the signal by surrounding objects. When addressing multi-antenna communication, the term "channel matrix" is used. Let's assume that only one TX and one RX are in communication and there's no surrounding object. Here, in our case, we can apply the proper threshold condition to a received signal and get the original transmitted signal at the RX side. However, in real-world situations, we see signal path blockage, reflections, etc.,  (NLOS paths [↗]) more frequently. The obstruction is typically caused by building walls, etc. Multi-antenna communication was introduced to address this issue. It makes diversity app...

BER vs SNR for M-ary QAM, M-ary PSK, QPSK, BPSK, ...(MATLAB Code + Simulator)

Bit Error Rate (BER) & SNR Guide Analyze communication system performance with our interactive simulators and MATLAB tools. 📘 Theory 🧮 Simulators 💻 MATLAB Code 📚 Resources BER Definition SNR Formula BER Calculator MATLAB Comparison 📂 Explore M-ary QAM, PSK, and QPSK Topics ▼ 🧮 Constellation Simulator: M-ary QAM 🧮 Constellation Simulator: M-ary PSK 🧮 BER calculation for ASK, FSK, and PSK 🧮 Approaches to BER vs SNR Calculation What is Bit Error Rate (BER)? The BER indicates how many corrupted bits are received compared to the total number of bits sent. It is the primary figur...

UGC NET Electronic Science Previous Year Question Papers with Solutions

Home / Engineering & Other Exams / UGC NET 2026 PYQ ⬇️ Download Papers and Solutions 📋 Exam Pattern 💡 Preparation Tips ❓ FAQs 📊 Exam Highlights: Electronic Science (88) Feature Details Junior Research Fellowship (JRF) ₹37,000 + HRA per month Eligibility M.Sc/M.Tech in Electronics (55%) Validity of Certificate JRF (3 Years) | Lectureship (Lifetime) 📥 Download UGC NET Electronics PDFs Complete collection of previous year question papers, answer keys and explanations for Subject Code 88. Start Downloading 📂 View All Question Papers June 2025 - Question Paper Download PDF June 2025 - Solved Paper + Explanation ...

How to Mount Google Drive in Google Colab

How to Mount Google Drive in Google Colab Google Colab provides temporary storage during a session. Any files stored in the /content directory will be deleted when the runtime disconnects. To store datasets, trained models, and results permanently, it is recommended to mount your Google Drive in Colab. Mounting Google Drive allows your notebook to access files directly from your Drive and save outputs there so they remain available even after the Colab session ends. Step 1: Import the Drive Module First import the Google Colab drive module. from google.colab import drive Step 2: Mount Google Drive Run the following command to mount your Google Drive. from google.colab import drive drive.mount('/content/drive') After running the command: A link will appear in the output. Click the link and log in to your Google account. Copy the authentication code provided. Paste the code back into the notebook. Or, a Google authentication page will a...

Wiener Filter in MATLAB

  MATLAB Code  % Wiener Filter Based on Wiener-Hopf Equation % This script demonstrates how to apply the Wiener filter to recover % a reference signal from a noisy signal using the Wiener-Hopf equation. % The filter minimizes the mean squared error between the noisy signal and the reference signal. clear; close all; clc; % Signal Parameters fs = 4000; % Sampling frequency (Hz) T = 1; % Total recording time (seconds) L = T * fs; % Signal length (samples) tt = (0:L-1) / fs; % Time vector ff = (0:L-1) * fs / L; % Frequency vector % Generate Reference Signal (a sinusoid) y = sin(2 * pi * 120 * tt); % Reference sinusoidal signal y = y(:); % Ensure column vector % Create Noisy Signal by Adding Gaussian Noise x = 0.50 * randn(L, 1) + y; % Noisy signal x = x(:); % Ensure column vector % Define Filter Order (Number of Coefficients) N = 200; % Apply Wiener Filter using custom function [xest, b, MSE] = wienerFilt(x, y, N); % Plot Results figure; subplot(411); plot(tt, x, 'k'), hold on, p...

Overmodulation & Distortion in AM

Overmodulation in AM and How It Causes Distortion 1. AM Signal Equation s(t) = A c [1 + μ m(t)] cos(2Ï€ f c t) A c = carrier amplitude m(t) = normalized modulating signal (|m(t)| ≤ 1) μ = modulation index 2. Modulation Index μ = A m / A c - Normal AM: 0 < μ ≤ 1 → no distortion - Overmodulation: μ > 1 → distortion occurs 3. Envelope and Overmodulation A(t) = A c [1 + μ m(t)] - For undistorted AM: 1 + μ m(t) ≥ 0 at all times - If μ > 1: 1 + μ m(t) < 0 at negative peaks → carrier flips Example: Let m(t) = cos(2Ï€ f m t), A c = 1 V, μ = 1.2 Minimum envelope: A min = A c [1 - 1.2] = -0.2 V Negative amplitude → envelope crosses zero → 180° phase flip 4. Mathematical Consequence -A c cos(θ) = A c cos(θ + Ï€) This phase reversal is what causes distortion in the demodulated signal. 5. Instantaneous AM Signal s...

Phase Modulation (PM) & Demodulation

Advanced Analysis of Phase Modulation Phase Modulation (PM): Theoretical Foundations and Spectral Dynamics 1. Analytical Characterization Phase Modulation (PM) is a subset of Angle Modulation , where the information residing in the message signal \( m(t) \) is mapped linearly onto the instantaneous phase of a high-frequency carrier. Unlike Amplitude Modulation (AM), PM is a non-linear modulation process, resulting in an expansion of the signal bandwidth into an infinite dimensional Hilbert space. \[ S_{PM}(t) = A_c \cos\left[ 2\pi f_c t + \phi(t) \right] = A_c \cos\left[ 2\pi f_c t + K_p m(t) \right] \] The instantaneous frequency \( f_i(t) \) is defined as the time derivative of the total angle: \[ f_i(t) = \frac{1}{2\pi} \frac{d\theta_i(t)}{dt} = f_c + \frac{K_p}{2\pi} \frac{dm(t)}{dt} \] ...

QPSK Online Simulator (Signal Generation)

Simulator for QPSK Modulation Quadrature (4-PSK) Bitstream (Even length) Carrier Freq (Hz) Samples Per Symbol Run QPSK Simulation The Math Behind QPSK Quadrature Phase Shift Keying (QPSK) is a form of digital modulation that transmits two bits per symbol by changing the phase of a carrier wave. s(t) = A cos(2Ï€f c t + θ n ) Phase (θ n ): Each pair of bits (dibit) corresponds to a specific phase shift. In Gray coding, we use: "00" → Ï€/4 (45°) "01" → 3Ï€/4 (135°) "11" → 5Ï€/4 (225°) "10" → 7Ï€/4 (315°) Efficiency: Since 4 phases are used, QPSK carries double the data of BPSK in the same bandwidth. ...