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(10) % Assaining the value of seed integer
N = 10000; % Number of bits to process
% Initialize SNR values and BER array
snr_dB = 0: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
% Compute BER
BER(snr_idx) = sum(InputSymbols_decimal ~= output') / N;
end
% Plot BER vs. SNR
figure;
semilogy(snr_dB, BER, 'o-');
grid on;
xlabel('SNR (dB)');
ylabel('Symbol Error Rate (SER)');
title('SER vs. SNR');
Output
Fig 1: SER vs SNR for 4-QAM