%Programme to plot the BER performance curves for M-ary QAM

%Written by Andrew Bateman.

%set up figure numbers and location on the screen

figure(1)
close
h=figure('position',[90 341 394 369],'color',[1,1,0]);
h=axes('position',[100,331,404,379],'color',[0,1,0]);
clf

%initialise vectors

P4qamsym=[];
P16qamsym=[];
P64qamsym=[];
xaxis1=[];
xaxis2=[];
xaxis3=[];


%Calculate BER curves

M=4;
for j = 1:100 
i=j/5;
EbNo=10^(i/10);
qamsym= (erfc(sqrt(EbNo)))*(1-0.25*(erfc(sqrt(EbNo))));
if qamsym < 1e-6, ,break, end
P4qamsym(j)=qamsym;
xaxis1(j)=i;
end

M=16;
k=4;
for j = 1:100 
i=j/5;
EbNo=10^(i/10);
qam=(2*(1-1/sqrt(M))*erfc(sqrt(3*k*EbNo/(2*(M-1)))))*(1-0.5*(1-1/sqrt(M))*erfc(sqrt(3*k*EbNo/(2*(M-1)))));
if qam < 1e-6, ,break, end
P16qamsym(j)=qam;
xaxis2(j)=i;
end

M=64;
k=6;
for j = 1:100 
i=j/5;
EbNo=10^(i/10);
qam=(2*(1-1/sqrt(M))*erfc(sqrt(3*k*EbNo/(2*(M-1)))))*(1-0.5*(1-1/sqrt(M))*erfc(sqrt(3*k*EbNo/(2*(M-1)))));
if qam < 1e-6, ,break, end
P64qamsym(j)=qam;
xaxis3(j)=i;
end


figure(1)
semilogy(xaxis1,P4qamsym,'-g','linewidth',2);
hold on
semilogy(xaxis2,P16qamsym,'-b','linewidth',2);
hold on
semilogy(xaxis3,P64qamsym,'-r','linewidth',2);
hold on
title('BER curves for M-ary QAM')

