MATLAB Script
% This code is written by SalimWirelss.Comclc;
clear all;
close all;
M = 4; % Number of levels after quantization / size of signal constellation
k = log2(M); % Number of bits per symbol
rng(1) % Assaining the value of seed integer
N = 1000000; % Number of bits to process
% Initialize SNR values and BER array
snr_dB = -20:1:20; % SNR values in dB
BER = zeros(size(snr_dB));
for snr_idx = 1:length(snr_dB)
snrdB = snr_dB(snr_idx);
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
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
for n = 1:N/k
if Z(n) == complex(1,1)
output(n) = 0;
elseif Z(n) == complex(-1,1)
output(n) = 1;
elseif Z(n) == complex(1,-1)
output(n) = 2;
else
output(n) = 3;
end
end
% Decimal values of 4QAM constellation
% Convert decimal to binary symbols
% Reshape binary symbols into k-tuples
ReceivedBits = reshape(output, [], 2);
ReceivedSymbol_matrix = de2bi(ReceivedBits);
%ReceivedSymbol_matrix = InputSymbol_matrix;
A = ReceivedSymbol_matrix(:,1)';
B = ReceivedSymbol_matrix(:,2)';
ReceivedBits1 = [A B];
% Compute BER
BER(snr_idx) = sum(InputBits ~= ReceivedBits1) / N;
end
% Plot BER vs. SNR
figure;
semilogy(snr_dB, BER, 'o-');
grid on;
xlabel('SNR (dB)');
ylabel('Symbol Error Rate (BER)');
title('BER vs. SNR');
Output
Fig 1: BER vs SNR graph for 4-QAM