% This is finding Pe in an arbitrary ASk, PSK, QAM using model modulators
% and demodulators - 2012
clear;clc;close all;warning off all
sitil = ['-k  ';'--k ';':k  ';'-.k ';'-xk ';'--*k';':*k ';'-.*k';'-ok ';'--ok';':ok ';'-.ok';'-xk ';'--xk';':xk ';'-.xk'];
linwid = [2 3 3 3 2 2 2 2 2 2 2 2 2 2 2 2];
Mary = '32';M = str2num(Mary);ST = '10000';
%%%% Setting the Mary and stoptime (number of symbols to be transmitted and received) in modulator and demodulator
set_param('Allmodd/Random Integer Generator','mul',Mary);set_param('Allmodd/Random Integer Generator1','mul',Mary);
set_param('Allmodd/Random Integer Generator2','mul',Mary);
set_param('Allmodd/M-ASK Modulator Baseband','M',Mary);set_param('AllDmodd/M-ASK Demodulator Baseband','M',Mary);
set_param('Allmodd/M-PSK Modulator Baseband','M',Mary);set_param('Allmodd/Rectangular QAM Modulator Baseband','M',Mary);
set_param('AllDmodd/M-PSK Demodulator Baseband','M',Mary);set_param('AllDmodd/Rectangular QAM Demodulator Baseband','M',Mary);
set_param('Allmodd','StopTime',ST);set_param('AllDmodd','StopTime',ST);

%%% Generating transmitted signals %%%%%%%%%%%%
sim('Allmodd');n = length(smtMaryPSK);
%%% Setting SNR, scaling noise. E_st indicates the energy in one binary waveform
E_stn = 1;E_st = 1/log2(M);SNR_db = -2:12;dblen = length(SNR_db);
set_ntp = 10*log10(E_st) - SNR_db;SNRarr = 10.^(0.1*SNR_db);
nt = [];
for iSNR = 1:dblen
nt1 = wgn(length(smtPSK),1,set_ntp(iSNR),'complex');
nt = [nt nt1];end;
rtmatASK = repmat(smtASK,1,dblen) + nt; %%% Adding noise to ASK modulated signal
rtmatPSK = repmat(smtPSK,1,dblen) + nt; %%% Adding noise to PSK modulated signal
rtmatQAM = repmat(smtQAM,1,dblen) + nt; %%% Adding noise to QAM modulated signal

smtdmatPSK = [];smtdmatQAM = [];smtdmatASK = [];
for iSNR = 1:dblen
rtASK = rtmatASK(:,iSNR);rtPSK = rtmatPSK(:,iSNR);rtQAM = rtmatQAM(:,iSNR);
sim('AllDmodd');
smtdmatASK = [smtdmatASK smtdASK];smtdmatPSK = [smtdmatPSK smtdPSK];smtdmatQAM = [smtdmatQAM smtdQAM];
end
clear smtdPSK smtdQAM smtdASK
P_expASK = symerr(smtMaryASK,smtdmatASK) / length(smtMaryASK);% Experimental error for ASK
P_expPSK = symerr(smtMaryPSK,smtdmatPSK) / length(smtMaryPSK);% Experimental error for PSK
P_expQAM = symerr(smtMaryQAM,smtdmatQAM) / length(smtMaryQAM);% Experimental error for PSK

P_thASK = (M - 1)*erfc(sqrt(3*SNRarr/(M^2 - 1)*log2(M)))/M; % From my notes of 3.05.2004
P_thPSK = PeMaryPSK(SNR_db, M);% Theoretical error from function PeMaryPSK for equivalent PSK
P_thQAM = 1 - erf(sqrt(1.5*log2(M)*SNRarr/(M-1))).^2; % Eq. (4) of IKECCO paper

semilogy(SNR_db,P_expASK,sitil(1,:),'LineWidth',2);set(gcf,'Color','w');hold on
semilogy(SNR_db,P_expPSK,sitil(2,:),'LineWidth',2);set(gcf,'Color','w');hold on
semilogy(SNR_db,P_expQAM,sitil(3,:),'LineWidth',2);set(gcf,'Color','w');hold on
semilogy(SNR_db,P_thASK,sitil(5,:),'LineWidth',3);hold on
semilogy(SNR_db,P_thPSK,sitil(6,:),'LineWidth',3);hold on
semilogy(SNR_db,P_thQAM,sitil(7,:),'LineWidth',3);hold on

hold off
set(gca,'FontSize',16)
titlestr = ['Graph of SER versus bit SNR (in dB) for an M = ' num2str(M) ' ary Modulations'];
%title(titlestr)
xlabel('Bit SNR, \fontsize{24}\it\epsilon\fontsize{16}_b\rm\bf / \itN\rm\bf_0 in dB','FontSize',16,'FontWeight','bold'); 
ylabel('\itP_e\rm\bf - symbol error rate (SER)','FontSize',16,'FontWeight','bold');
Mstr = [' \itM = \rm\bf' num2str(M)];
nstr = [' \itn_s = \rm\bf' ST ' '];Mnstr = {Mstr;' ';nstr};
legASKe = '\bf(experimental) \itP_e - ASK';legASKt = '\bf(theoretical) \itP_e - ASK';
legPSKe = '\bf(experimental) \itP_e - PSK';legPSKt = '\bf(theoretical) \itP_e - PSK';
legQAMe = '\bf(experimental) \itP_e - QAM';legQAMt = '\bf(theoretical) \itP_e - QAM';
text(max(SNR_db)*0.4,max(P_expPSK)*.2,Mnstr,'FontSize',16,'FontWeight','bold','Edgecolor','black');
text(max(SNR_db)*0.6,max(P_expPSK)*.04,legASKe,'FontSize',16,'FontWeight','bold','Edgecolor','white','BackgroundColor','white');
text(max(SNR_db)*0.7,max(P_expPSK)*.1,legASKt,'FontSize',16,'FontWeight','bold','Edgecolor','white','BackgroundColor','white');
text(max(SNR_db)*0.6,max(P_expPSK)*.04,legPSKe,'FontSize',16,'FontWeight','bold','Edgecolor','white','BackgroundColor','white');
text(max(SNR_db)*0.7,max(P_expPSK)*.1,legPSKt,'FontSize',16,'FontWeight','bold','Edgecolor','white','BackgroundColor','white');
text(max(SNR_db)*0.6,max(P_expPSK)*.04,legQAMe,'FontSize',16,'FontWeight','bold','Edgecolor','white','BackgroundColor','white');
text(max(SNR_db)*0.7,max(P_expPSK)*.1,legQAMt,'FontSize',16,'FontWeight','bold','Edgecolor','white','BackgroundColor','white');