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

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 ...

MUSIC Algorithm Explained (with MATLAB + Simulator)

Practical Implementation of the MUSIC Algorithm The focus is on how the algorithm works computationally , not just theory, and it explains the denominator (a H E n E n H a) mathematically and intuitively. 1. Introduction The MUSIC (Multiple Signal Classification) algorithm is a high-resolution method used in signal processing and array processing to estimate the Direction of Arrival (DOA) of signals received by a sensor array. Unlike classical beamforming methods, MUSIC uses eigenvector decomposition of the covariance matrix to separate the signal subspace and noise subspace , allowing it to achieve much higher angular resolution. In practical implementations, MUSIC works by: Simulating or collecting array signals Computing the covariance matrix Performing eigenvalue decomposition Separating signal and noise subspaces Scanning possible angles using a steering vector Constructing a pseudo-spectrum where peaks indicate signal directions 2. Signal Mo...

PSD Calculation with FFT: MATLAB Tutorial for Signal Analysis

  Implementation Steps 1. FFT Computes the Frequency Content of a Signal FFT converts a time-domain signal to the frequency domain. If: The signal is sampled at rate $f_s$ You compute an $N_{\text{FFT}}$-point FFT Then each FFT bin corresponds to a frequency resolution of: $$\Delta f = \frac{f_s}{N_{\text{FFT}}}$$ So the FFT gives you accurate frequency content, assuming the signal is stationary and adequately sampled (Nyquist criterion met).  2. Magnitude Squared Gives Power (Not Amplitude) $$P[k] = |X[k]|^2$$ This gives power at each frequency bin, not just amplitude. It represents how much energy is present at each frequency. It's a key step for PSD.  3. Normalization Makes the PSD Physically Meaningful The equation: $$\text{PSD}[k] = \frac{|X[k]|^2}{N_{\text{FFT}} \cdot f_s \cdot U}$$ is derived from first principles and ensures that the u...

MATLAB code for BER vs SNR for M-QAM, M-PSK, QPSK, BPSK (with Simulation)

🧮 MATLAB Code for BPSK, M-ary PSK, and M-ary QAM Together 🧮 MATLAB Code for M-ary QAM 🧮 MATLAB Code for M-ary PSK 📚 Further Reading MATLAB Script for BER vs. SNR for M-QAM, M-PSK, QPSK, BPSK % Written by Salim Wireless clc; clear; close all; snr_db = -5:2:25; psk_orders = [2, 4, 8, 16, 32]; qam_orders = [4, 16, 64, 256]; ber_psk_results = zeros(length(psk_orders), length(snr_db)); ber_qam_results = zeros(length(qam_orders), length(snr_db)); for i = 1:length(psk_orders) ber_psk_results(i, :) = berawgn(snr_db, 'psk', psk_orders(i), 'nondiff'); end for i = 1:length(qam_orders) ber_qam_results(i, :) = berawgn(snr_db, 'qam', qam_orders(i)); end figure; semilogy(snr_db, ber_psk_results(1, :), 'o-', 'LineWidth', 1.5, 'DisplayName', 'BPSK'); hold on; for i = 2:length(psk_orders) semilogy(snr_db, ber_psk_results(i, :), 'o-', 'DisplayName', sprintf('%d-PSK', psk_or...

Theoretical BER vs SNR for BPSK

Theoretical Bit Error Rate (BER) vs Signal-to-Noise Ratio (SNR) for BPSK in AWGN Channel Let’s simplify the explanation for the theoretical Bit Error Rate (BER) versus Signal-to-Noise Ratio (SNR) for Binary Phase Shift Keying (BPSK) in an Additive White Gaussian Noise (AWGN) channel. Key Points Fig. 1: Constellation Diagrams of BASK, BFSK, and BPSK [↗] BPSK Modulation Transmits one of two signals: +√Eb or −√Eb , where Eb is the energy per bit. These signals represent binary 0 and 1 . AWGN Channel The channel adds Gaussian noise with zero mean and variance N₀/2 (where N₀ is the noise power spectral density). Receiver Decision The receiver decides if the received signal is closer to +√Eb (for bit 0) or −√Eb (for bit 1) . Bit Error Rat...

Constellation Diagrams of ASK, PSK, and FSK (with MATLAB Code + Simulator)

Constellation Diagrams: ASK, FSK, and PSK Comprehensive guide to signal space representation, including interactive simulators and MATLAB implementations. 📘 Overview 🧮 Simulator ⚖️ Theory 📈 Q-function 📚 Resources BASK Modulation Transmits one of two signals: 0 or $\sqrt{E_b}$, representing binary 0 and 1. Simple but sensitive to noise. BFSK Modulation Transmits one of two signals: $\sqrt{E_b}$ on the Y-axis or $\sqrt{E_b}$ on the X-axis. These are orthogonal signals. BPSK Modulation Transmits $+\sqrt{E_b}$ or $-\sqrt{E_b}$ (antipodal signaling). Most efficient binary scheme. ...