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

%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

Pcohask=[];
Pnoncask=[];
P4asksym=[];
P8asksym=[];
P16asksym=[];
xaxis1=[];
xaxis2=[];
xaxis3=[];
xaxis4=[];
xaxis5=[];

%Calculate BER Performance Curves

for j = 1:100 
i=j/5;
EbNo=10^(i/10);
cohask=0.5*(erfc(sqrt(EbNo)));
if cohask < 1e-6, ,break, end
Pcohask(j)=cohask;
xaxis1(j)=i;
end

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

M=8;
k=3;
for j = 1:100 
i=j/5;
EbNo=10^(i/10);
ask=((M-1)/M)*(erfc(sqrt((3/(M*M-1))*k*EbNo)));
if ask < 1e-6, ,break, end
P8asksym(j)=ask;
xaxis3(j)=i;
end

M=16;
k=4;
for j = 1:100 
i=j/5;
EbNo=10^(i/10);
ask=((M-1)/M)*(erfc(sqrt((3/(M*M-1))*k*EbNo)));
if ask < 1e-6, ,break, end
P16asksym(j)=ask;
xaxis4(j)=i;
end


for j = 1:100 
i=j/5;
EbNo=10^(i/10);
noncask=0.5*exp(-EbNo/8) + 0.5*erfc(sqrt(EbNo/4));
if noncask < 1e-6, ,break, end
Pnoncask(j)=noncask;
xaxis5(j)=i;
end


figure(1)
semilogy(xaxis1,Pcohask,'-b','linewidth',2);
hold on
semilogy(xaxis2,P4asksym,'-g','linewidth',2);
hold on
semilogy(xaxis3,P8asksym,'-r','linewidth',2);
hold on
semilogy(xaxis4,P16asksym,'-r','linewidth',2);
hold on
semilogy(xaxis5,Pnoncask,'-m','linewidth',2);
hold on
title('BER curves for M-ary ASK')

