Skip to main content

MATLAB Code for QAM (Quadrature Amplitude Modulation)

 

One of the best-performing modulation techniques is QAM [↗]. Here, we modulate the symbols by varying the carrier signal's amplitude and phase in response to the variation in the message signal (or voltage variation). So, we may say that QAM is a combination of phase and amplitude modulation. Additionally, it performs better than ASK or PSK [↗]. In fact, any constellation for any type of modulation, signal set (or, symbols) is structured in a way that prevents them from interacting further by being distinct by phase, amplitude, or frequency.


MATLAB Script (for 4-QAM)

% This code is written by SalimWirelss.Com
% This is an example of 4-QAM. Here constellation size is 4
% or total number of symbols/signals is 4
% We need 2 bits once to represent four constellation points
% QAM modulation is the combination of Amplitude modulation plus
% Phase Modulation. We map the decimal value of the input symbols, i.e.,
% 00, 01, 10, 11 to 1 + 1i, -1 + 1i, 1 - 1i, and -1 - 1i, respectively.


clc;clear all;close all;

M = 4; % Number of levels after quantization / size of signal constellation

k = log2(M); % Number of bits per symbol

rng(10) %assaining the value of seed integer

N =10000; % Number of bits to process

InputBits = randi([0 1],1,N); % Generating randon bits

InputSymbol_matrix = reshape(InputBits,length(InputBits)/k,k); % Reshape data into binary k-tuples, k = log2(M)

InputSymbols_decimal = bi2de(InputSymbol_matrix); % Convert binary to decimal

for n= 1:N/k

if InputSymbols_decimal(n)==0

QAM(n)= complex(1,1);

elseif InputSymbols_decimal(n)==1

QAM(n)= complex(-1,1);

elseif InputSymbols_decimal(n)==2

QAM(n)= complex(1,-1);

else

QAM(n)= complex(-1,-1);

end



end



%Transmission of 4QAM data over AWGN channel

snrdB = 10;

Y=awgn(QAM,snrdB); %received signal


%Threshold Detection

for n= 1:N/k

if (real(Y(n))>0 && imag(Y(n))>0)

Z(n)=complex(1,1);

elseif (real(Y(n))>0 && imag(Y(n))<0)

Z(n)=complex(1,-1);


elseif (real(Y(n))<0 && imag(Y(n))>0)

Z(n)=complex(-1,1);

else

Z(n)=complex(-1,-1);

end

end

figure(1)
scatter(real(QAM), imag(QAM))
xlim([-3, 3]);
ylim([-3, 3]);
legend('Transmitted Symbols')

figure(2)
scatter(real(Y), imag(Y))
xlim([-3, 3]);
ylim([-3, 3]);
legend('Received Symbols')
 

Output 

 
 
Fig 1: Constellation points of 4-QAM (Transmitted)


 
Fig 2: Constellation points of 4-QAM (Received)


Copy the MATLAB Code for 4-QAM


 

Another MATLAB Code (for 16-QAM)

%The code is developed by SalimWireless.Com

clc;
clear;
close all;

% Define parameters
M = 16; % Modulation order for 16-QAM
numSymbols = 10000; % Number of symbols to modulate

% Generate random data
data = randi([0 M-1], numSymbols, 1); % Ensure data is a column vector

% Modulate the data using 16-QAM
modData = qammod_custom(data, M);

snrdB = 15;
Y = awgn(modData,snrdB); %received signal

% Plot the constellation of the modulated signal
figure;
subplot(2,1,1);
scatter(real(modData), imag(modData), 'o');
grid on;
xlabel('In-phase');
ylabel('Quadrature');
title('Constellation Diagram of Modulated Signal (16-QAM)');
axis([-1.5 1.5 -1.5 1.5]); % Set axis limits for better visualization

subplot(2,1,2);
scatter(real(Y), imag(Y), 'o');
grid on;
xlabel('In-phase');
ylabel('Quadrature');
title('Constellation Diagram of Noisy Received Signal before demodulation');
axis([-1.5 1.5 -1.5 1.5]); % Set axis limits for better visualization

% Demodulate the received signal
receivedData = qamdemod_custom(modData, M);

% Ensure receivedData is a column vector for comparison
receivedData = receivedData(:);


% Custom 16-QAM Modulation Function
function modData = qammod_custom(data, M)
% QAMMOD_CUSTOM Modulate data using 16-QAM
% data - Column vector of integers (each element is between 0 and M-1)
% M - Modulation order (should be 16 for 16-QAM)

% Check if M is 16
if M ~= 16
error('This function is designed for 16-QAM modulation.');
end

% Define the 16-QAM constellation
constellation = [-3-3i, -3-1i, -1-3i, -1-1i, ...
-3+3i, -3+1i, -1+3i, -1+1i, ...
+3-3i, +3-1i, +1-3i, +1-1i, ...
+3+3i, +3+1i, +1+3i, +1+1i];

% Normalize constellation
constellation = constellation / sqrt(mean(abs(constellation).^2)); % Scale to unit average power

% Map data to constellation points
modData = constellation(data + 1);
end

% Custom 16-QAM Demodulation Function
function demodData = qamdemod_custom(modData, M)
% QAMDEMOD_CUSTOM Demodulate data using 16-QAM
% modData - Column vector of complex numbers (modulated symbols)
% M - Modulation order (should be 16 for 16-QAM)

% Check if M is 16
if M ~= 16
error('This function is designed for 16-QAM demodulation.');
end

% Define the 16-QAM constellation
constellation = [-3-3i, -3-1i, -1-3i, -1-1i, ...
-3+3i, -3+1i, -1+3i, -1+1i, ...
+3-3i, +3-1i, +1-3i, +1-1i, ...
+3+3i, +3+1i, +1+3i, +1+1i];

% Normalize constellation
constellation = constellation / sqrt(mean(abs(constellation).^2)); % Scale to unit average power

% Ensure modData is a column vector
modData = modData(:);

% Compute the distances from each modData point to all constellation points
numSymbols = length(modData);
numConstellations = length(constellation);
distances = zeros(numSymbols, numConstellations);
for k = 1:numConstellations
distances(:, k) = abs(modData - constellation(k)).^2;
end

% Find the closest constellation point for each modData point
[~, demodData] = min(distances, [], 2);

% Convert to zero-based index
demodData = demodData - 1;
end

Output  


 
 
 
 

Copy the MATLAB Code above from here (for 16-QAM)

 

MATLAB code for M-ary QAM (e.g., 4, 8, 16, 32, 64, 128, 256)

%The code is developed by SalimWireless.com
% M-ary QAM Modulation and Demodulation
clc;
clear;
close all;


% Parameters
M = 32; % Order of QAM (M-QAM)
N = 1000; % Number of symbols
SNR = 10; % Signal-to-Noise Ratio in dB


% Generate random data symbols
dataSymbols = randi([0 M-1], N, 1);


% Modulate using M-QAM
txSignal = qammod(dataSymbols, M);


% Add AWGN noise
rxSignal = awgn(txSignal, SNR, 'measured');


% Demodulate
demodulatedSymbols = qamdemod(rxSignal, M);


% Calculate symbol error rate
symbolErrors = sum(dataSymbols ~= demodulatedSymbols);
SER = symbolErrors / N;


% Display results
disp(['Symbol Error Rate (SER): ', num2str(SER)]);


% Plot constellation diagrams
figure;
subplot(2, 1, 1);
plot(real(txSignal), imag(txSignal), 'o');
grid on;
title('Transmitted Signal Constellation');
xlabel('In-Phase');
ylabel('Quadrature');


subplot(2, 1, 2);
plot(real(rxSignal), imag(rxSignal), 'o');
grid on;
title('Received Signal Constellation');
xlabel('In-Phase');
ylabel('Quadrature');

Output








Copy the MATLAB Code above from here (e.g., for QAM configurations such as 4, 8, 16, 32, 64, 128, and 256-QAM.)


MATLAB Code for BER vs SNR for 4-QAM, 16-QAM, 32-QAM, and so on

 
 


Also read about

Next>>

People are good at skipping over material they already know!

View Related Topics to







Admin & Author: Salim

profile

  Website: www.salimwireless.com
  Interests: Signal Processing, Telecommunication, 5G Technology, Present & Future Wireless Technologies, Digital Signal Processing, Computer Networks, Millimeter Wave Band Channel, Web Development
  Seeking an opportunity in the Teaching or Electronics & Telecommunication domains.
  Possess M.Tech in Electronic Communication Systems.


Contact Us

Name

Email *

Message *

Popular Posts

BER vs SNR for M-ary QAM, M-ary PSK, QPSK, BPSK, ...

Modulation Constellation Diagrams BER vs. SNR BER vs SNR for M-QAM, M-PSK, QPSk, BPSK, ... What is Bit Error Rate (BER)? The abbreviation BER stands for bit error rate, which indicates how many corrupted bits are received (after the demodulation process) compared to the total number of bits sent in a communication process. It is defined as,  In mathematics, BER = (number of bits received in error / total number of transmitted bits)  On the other hand, SNR refers to the signal-to-noise power ratio. For ease of calculation, we commonly convert it to dB or decibels.   What is Signal the signal-to-noise ratio (SNR)? SNR = signal power/noise power (SNR is a ratio of signal power to noise power) SNR (in dB) = 10*log(signal power / noise power) [base 10] For instance, the SNR for a given communication system is 3dB. So, SNR (in ratio) = 10^{SNR (in dB) / 10} = 2 Therefore, in this instance, the s...

Constellation Diagrams of ASK, PSK, and FSK

BASK (Binary ASK) Modulation: Transmits one of two signals: 0 or -√Eb, where Eb​ is the energy per bit. These signals represent binary 0 and 1.    BFSK (Binary FSK) Modulation: Transmits one of two signals: +√Eb​ ( On the y-axis, the phase shift of 90 degrees with respect to the x-axis, which is also termed phase offset ) or √Eb (on x-axis), where Eb​ is the energy per bit. These signals represent binary 0 and 1.  BPSK (Binary PSK) Modulation: Transmits one of two signals: +√Eb​ or -√Eb (they differ by 180 degree phase shift), where Eb​ is the energy per bit. These signals represent binary 0 and 1.  Key Points For Binary Amplitude Shift Keying (BASK), binary bit '0' can be represented as lower level voltage or no signal and bit '1' as higher level voltage.  For Binary Frequency Shift Keying (BFSK), you can map binary bit '0' to 'j' and bit '1' to '1'. So, signals are in phase.  A phase shift of 0 degrees could represent a binary '1...

Theoretical and simulated BER vs. SNR for ASK, FSK, and PSK

  BER vs. SNR denotes how many bits in error are received in a communication process for a particular Signal-to-noise (SNR) ratio. In most cases, SNR is measured in decibel (dB). For a typical communication system, a signal is often affected by two types of noises 1. Additive White Gaussian Noise (AWGN) 2. Rayleigh Fading In the case of additive white Gaussian noise (AWGN), random magnitude is added to the transmitted signal. On the other hand, Rayleigh fading (due to multipath) attenuates the different frequency components of a signal differently. A good signal-to-noise ratio tries to mitigate the effect of noise.  Calculate BER for Binary ASK Modulation The theoretical BER for binary ASK (BASK) in an AWGN channel is given by: BER  = (1/2) * erfc(0.5 * sqrt(SNR_ask));   Enter SNR (dB): Calculate BER BER vs. SNR curves for ASK, FSK, and PSK Calculate BER for Binary FSK Modulation The theoretical BER for binary FSK (BFSK) in a...

Theoretical BER vs SNR for BPSK

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 N0/2 (where N0 ​ 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 Rate (BER) The probability of error (BER) for BPSK is given by a function called the Q-function. The Q-function Q(x) measures the tail probability of the normal distribution, i.e., the probability that a Gaussian random variable exceeds a certain value x.  Understanding the Q...

MATLAB code for BER vs SNR for M-QAM, M-PSK, QPSk, BPSK, ...

Modulation Constellation Diagrams BER vs. SNR MATLAB code for BER vs SNR for M-QAM, M-PSK, QPSk, BPSK, ...   MATLAB Script for  BER vs. SNR for M-QAM, M-PSK, QPSk, BPSK %Written by Salim Wireless %Visit www.salimwireless.com for study materials on wireless communication %or, if you want to learn how to code in MATLAB clc; clear; close all; % Parameters num_symbols = 1e5; % Number of symbols snr_db = -20:2:20; % Range of SNR values in dB % PSK and QAM orders to be tested psk_orders = [2, 4, 8, 16, 32]; qam_orders = [4, 16, 64, 256]; % Initialize BER arrays ber_psk_results = zeros(length(psk_orders), length(snr_db)); ber_qam_results = zeros(length(qam_orders), length(snr_db)); % BER calculation for each PSK order and SNR value for i = 1:length(psk_orders) psk_order = psk_orders(i); for j = 1:length(snr_db) % Generate random symbols data_symbols = randi([0, psk_order-1], 1, num_symb...

OFDM in MATLAB

  MATLAB Script % The code is written by SalimWireless.Com 1. Initialization clc; clear all; close all; 2. Generate Random Bits % Generate random bits numBits = 100; bits = randi([0, 1], 1, numBits); 3. Define Parameters % Define parameters numSubcarriers = 4; % Number of subcarriers numPilotSymbols = 3; % Number of pilot symbols cpLength = ceil(numBits / 4); % Length of cyclic prefix (one-fourth of the data length) 4. Add Cyclic Prefix % Add cyclic prefix dataWithCP = [bits(end - cpLength + 1:end), bits]; 5. Insert Pilot Symbols % Insert pilot symbols pilotSymbols = ones(1, numPilotSymbols); % Example pilot symbols (could be any pattern) dataWithPilots = [pilotSymbols, dataWithCP];   6. Perform OFDM Modulation (IFFT) % Perform OFDM modulation (IFFT) dataMatrix = reshape(dataWithPilots, numSubcarriers, []); ofdmSignal = ifft(dataMatrix, numSubcarriers); ofdmSignal = reshape(ofdmSignal, 1, []); 7. Display the Generated Data % Display the generated data disp("Original Bits:"); ...

Analog and Digital Communication Mini Projects | FM, Telecommunication, Mod...

  Mini Project Ideas 1. You can do your mini project on analog communication topic such as FM, walkie-talkie, etc. [1.1]  Analog Communication Based Project [1.2] MATLAB Code for Frequency Modulation (FM) 2. Compare the ASK, FSK, and PSK systems' relative performances. ( Include an introduction, concise descriptions of ASK, FSK, and PSK, MATLAB, and Simulink . You can then compare ASK, FSK, and PSK by creating BER vs. SNR graphs for each of those modulations, as well as by comparing their bandwidth, noise resistivity, complexity, and other characteristics. ) 3. M-ary Modulation Based Mini Projects (You can go for this project if you are interested in doing projects based on frequently used and modern modulation techniques. You can compare the performance analysis of various modulation schemes, like, bit rate, complexity, SNR v/s BER graph. You know frequently used modulation technique is m ary QPSK. But now QAM is also becoming popular due to its less complexity. But there is...