I want to write a program which simulates the modulation and demodulation of 16-QAM. First, this program creates an array of 1000000 randomly generated bits, it groups them as 4-bit values and converts into numerical values. Then it enters 16-QAM modulation, AWGN noise is added for SNRs 0 to 20dB. Then it is demodulated and each result is converted to binary. When I compare the BER for theoritical and experimental result, I see that my results are much worse than theoritical values. This program worked well for 4-QAM but it got not-so-well results for 16-QAM. Can you help please?
clear all
clc
a = randi([0 1], 1, 1000000);%random binary array
b = zeros(1,250000);
for i=1:250000%4-bits grouped into one
b(i) = 8*a(4*i-3) + 4*a(4*i-2) + 2*a(4*i-1) + a(4*i);
end
c = qammod(b,16,'UnitAveragePower',true);%16-QAM modulation
k = 0:20;%SNR
d = zeros(length(k),250000);
for i=1:length(k)
d(i,1:250000) = awgn(c,k(i),'measured');%noise is added for different SNR values
end
e = qamdemod(d,16);%demodulation of 16-QAM
f = zeros(length(k),1000000);
for j=1:length(k)%results of numerical values are converted to 4-bit binary
for i=1:250000
temp = e(j,i);
f(j,4*i-3) = floor(temp/8);
temp = temp - 8;
f(j,4*i-2) = floor(temp/4);
temp = temp - 4;
f(j,4*i-1) = floor(temp/2);
temp = temp - 2;
f(j,4*i) = rem(e(j,i),2);
end
end
r = zeros(1,length(k));%BER is calculated
for i=1:length(k)
for j=1:1000000
if(a(1,j) ~= f(i,j))
r(i) = r(i) + 1/1000000;
end
end
end
t = zeros(1,length(k));%Theoritical result for BER is calculated
M = 16;
for i=1:length(k)
t(1,i) = 1-(1-2*sqrt(M-1)/sqrt(M)*qfunc(sqrt(3*k(1,i)/(M-1)))).^2;
end
plot(k,t)%results are plotted
hold on
plot(k,r)