BER versus SNR for QPSK modulation. I have written following code. But BER not reducing with increasing SNR in case of simulation. Please check the above code and rectify it.
42 views (last 30 days)
Show older comments
My MATLAB code is given as follows:
% Simulate the BER versus SNR curve for QPSK under AWGN communication channel.
% Assignment 3: Problem 1
% Empty workspace and close figures
clc; clearvars; close all;
% SNR in dB: snr_db = 10*log10(snr)
snr_db = -5 : 2 : 25;
% dB to linear scale conversion: snr = 10.^(snr_db/10)
snr = 10.^(snr_db/10);
% Define parameters and different numBits values for simulation
numBits = 1000 ;
numSimulations = 100;
%% Theoretical BER calculation for BPSK
qpsk_ber_theoretical = 2 * erfc(sqrt(snr/2)) - ( erfc(sqrt(snr/2))).^2;
qpsk_ber_simulations = zeros(size(snr_db));
%% Calculate BER for each SNR value
for j = 1:length(snr_db)
% Repeat simulations (as random quantities are involved) for numSimulations times
for sim = 1:numSimulations
% Generate Random Bits (0's and 1's) of size 1XnumBits, and it is a row vector
binaryBits_transmitted = randi([0, 1], 1, numBits);
% Make the binary sequence length a multiple of 2 (pad with zeros if needed)
numPaddingBits = mod(length(binaryBits_transmitted), 2);
if numPaddingBits > 0
binaryBits_transmitted = [binaryBits_transmitted, zeros(1, 2 - numPaddingBits)];
end
% Divide the binary sequence into groups of 2 bits each
qpsk_transmitted = reshape(binaryBits_transmitted,2,[])';
% Map each group of 2 bits to the corresponding QPSK symbol
% Initialize QPSK symbols (complex numbers)
qpsk_symbols = [exp(1i*0), exp(1i*pi/2), exp(1i*pi), exp(1i*3*pi/2)];
% Define the mapping from QPSK symbols to their corresponding 2-bit binary representation
qpsk_mapping = [0 0; 0 1; 1 0; 1 1];
% Map binary groups to the corresponding QPSK symbol
qpsk_modulated = qpsk_symbols (bin2dec(num2str(qpsk_transmitted)) + 1) ;
% Energy per bit = Symbol energy / 2
symbolEnergy = mean(abs(qpsk_modulated).^2);
Energy_bit = symbolEnergy / 2;
% Generate complex Gaussian noise samples
% Relate snr to the noise power (This is very important in this entire code)
Noise_Power = 1 / snr(j); % This holds true as Symbol energy is always 1
gaussian_noise = sqrt(Noise_Power) * (randn(1, numBits/2) + 1i * randn(1, numBits/2));
% Add noise to the QPSK modulated symbols to get noisy received signal
received_symbol = qpsk_modulated + gaussian_noise;
%% QPSK Demodulation with threshold as pi/4 in case of QPSK modulation scheme
% Initialize the decision regions (thresholds) for each quadrant
threshold = pi / 4; % ±45 degrees (±π/4 radians)
% Initialize a variable to store the demodulated symbols
qpsk_demodulated = zeros(1, length(received_symbol));
% Perform QPSK demodulation
for i = 1:length(received_symbol)
% Calculate the phase of the received signal
phase = angle(received_symbol(i));
% Perform threshold-based demodulation
if phase >= -3*threshold && phase < -threshold
qpsk_demodulated(i) = qpsk_symbols(1); % QPSK symbol at 0 degrees
elseif phase >= -threshold && phase < threshold
qpsk_demodulated(i) = qpsk_symbols(2); % QPSK symbol at 90 degrees
elseif phase >= threshold && phase < 3*threshold
qpsk_demodulated(i) = qpsk_symbols(3); % QPSK symbol at 180 degrees
else
qpsk_demodulated(i) = qpsk_symbols(4); % QPSK symbol at 270 degrees
end
end
%% QPSK received symbols using inverse process
qpsk_received = zeros(size(qpsk_demodulated));
% Find the closest QPSK symbol for each demodulated symbol
for i = 1: length(qpsk_demodulated)
% Calculate the distance b/w the demodulated symbol and each QPSK symbol
distances = abs(qpsk_demodulated(i) - qpsk_symbols) ;
% Find the index of the closest QPSK symbol
[~, index] = min(distances);
% Convert the index to its corresponding binary representation
binaryRepresentation = qpsk_mapping(index,:);
% Store the binary representation in the 'originalBinarySymbols' array
qpsk_received(2*i-1 : 2*i) = binaryRepresentation;
end
%% Number of bit errors
ErrorBits = xor( binaryBits_transmitted, qpsk_received);
Number_errorBits = sum(ErrorBits == 1); % bitErrors = sum(binaryBits ~= decodedBits)
Bit_Error_Rate = Number_errorBits / (numBits * 2) ;
% Store the calculated BER for this simulation
qpsk_ber_simulations(j) = qpsk_ber_simulations(j) + Bit_Error_Rate;
end
% Calculate Average Bit error rate for all the simulations for this SNR value and numBits
qpsk_simulation(j) = qpsk_ber_simulations (j) / numSimulations ;
end
%end
disp('binaryBits_transmitted');
disp(binaryBits_transmitted);
disp('qpsk_received');
disp(qpsk_received);
disp('qpsk_ber_simulations');
disp(qpsk_ber_simulations);
disp('qpsk_ber_theoretical');
disp(qpsk_ber_theoretical);
% Plot BER versus SNR curve for different numBits values
figure;
semilogy(snr_db, qpsk_simulation, '*-');
hold on;
semilogy(snr_db, qpsk_ber_theoretical, '*-');
hold on;
xlabel('Signal-to-Noise Ratio (SNR) [dB]');
ylabel('Bit Error Rate (BER)');
title('BER vs. SNR for QPSK Modulation');
legend('Simulation', 'Theoretical'); % Add more legends if needed
grid on;
hold off;
Graph is shown below:
0 Comments
Answers (1)
vidyesh
on 29 Sep 2023
Hi KAGANA SARATH,
I see that you are currently facing a challenge during the simulation of the Bit Error Rate (BER) versus Signal-to-Noise Ratio (SNR) graph for QPSK modulation.
Please note that the ‘angle’ function, used to obtain the phase of the received symbol returns a value within the range of [- π, π].
To get the desired graph, you should modify the conditions within the if-else loop in the "Perform threshold-based demodulation" section as shown.
% Perform threshold-based demodulation
if phase >= -1*threshold && phase <threshold % phase between -45 and 45 degrees
qpsk_demodulated(i) = qpsk_symbols(1); % QPSK symbol at 0 degrees
elseif phase >= threshold && phase <= 3* threshold % phase between 45 and 135 degrees
qpsk_demodulated(i) = qpsk_symbols(2); % QPSK symbol at 90 degrees
elseif phase >=-3*threshold && phase < -1*threshold % phase between -135 and -45 degrees
qpsk_demodulated(i) = qpsk_symbols(4); % QPSK symbol at 270 degrees
else % phase greater thna 135 or lesster than -135 degrees
qpsk_demodulated(i) = qpsk_symbols(3); % QPSK symbol at 180 degrees
end
You can refer to the below documentation for more information on the ‘angle’ function and QPSK.
0 Comments
See Also
Categories
Find more on QPSK in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!