%%%% Comparison of Pe for CPM and PSK on equal basis
%%%% Do not use Signal from work space blocks for input. Use instead DSP
%%%% Constant blocks as done in GModulator and GDModulator
echo off;clear;clc;close all
sitil = ['-k  ';'--k ';':k  ';'-.k ';'-*k ';'--*k';':*k ';'-.*k';'-ok ';'--ok';':ok ';'-.ok';'-xk ';'--xk';':xk ';'-.xk'];
linwid = [2 3 3 2 2 2 2 2 2 2 2 2 2 2 2 2];
Mary = '16';M = str2num(Mary);ST = '100000';ST1 = '0.9';tBL = '4';tBLnum = str2num(tBL); 
h = '2/3';gtshape = 'Rectangular';L = '3';preH = '[1 1]';sPS = '4';gtshape = 'Raised Cosine';%preH = '[1 1 1]';%preH = '1';
SD = '12';SD = '20';
%%% Setting parameters for the model file
set_param('MaryGen','StopTime',ST);set_param('MaryGen/Random Integer Generator','mul',Mary,'seed',SD);
set_param('MaryGen/Unipolar to Bipolar Converter','M',Mary);

sim('MaryGen');
set_param('GModulator','StopTime',ST1);set_param('GDModulator','StopTime',ST1);
set_param('GModulator/CPM Modulator Baseband','Mnum',Mary,'modIdx',h,'pulseShape',gtshape,'pulseLength',L,'preHistory',preH,'samplesPerSymbol',sPS);
set_param('GDModulator/CPM Demodulator Baseband','Mnum',Mary,'modIdx',h,'pulseShape',gtshape,'pulseLength',L,'preHistory', ....
    preH,'samplesPerSymbol',sPS,'traceBack',tBL);
set_param('GModulator/M-PSK Modulator Baseband','M',Mary);set_param('GDModulator/M-PSK Demodulator Baseband','M',Mary);
set_param('GModulator/Rectangular QAM Modulator Baseband','M',Mary);set_param('GDModulator/Rectangular QAM Demodulator Baseband','M',Mary);
sim('GModulator');
n = length(stMaryCPM);
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);
ntPSK = [];ntCPM = [];
for iSNR = 1:dblen
nt1 = wgn(length(stPSK),1,set_ntp(iSNR),'complex');ntPSK = [ntPSK nt1];
nt1 = wgn(length(stCPM),1,set_ntp(iSNR),'complex');ntCPM = [ntCPM nt1];
end
rtmatCPM = repmat(stCPM,1,dblen) + ntCPM; %%% Adding noise to CPM modulated signal
rtmatPSK = repmat(stPSK,1,dblen) + ntPSK; %%% Adding noise to PSK modulated signal
rtmatQAM = repmat(stQAM,1,dblen) + ntPSK; %%% Adding noise to PSK modulated signal
stdmatCPM = [];stdmatPSK = [];stdmatQAM = [];
for iSNR = 1:dblen
rtCPM = rtmatCPM(:,iSNR);rtPSK = rtmatPSK(:,iSNR);rtQAM = rtmatQAM(:,iSNR);
sim('GDModulator');
stdmatCPM = [stdmatCPM stdCPM];stdmatPSK = [stdmatPSK stdPSK];stdmatQAM = [stdmatQAM stdQAM];
end
stBCPM = stBCPM(1:end - str2num(tBL));
stdmatCPM = stdmatCPM(tBLnum + 1:end,:);
%clear stdCPM stdPSK 
P_expCPM = symerr(stBCPM,stdmatCPM) / (n - tBLnum);% Experimental error for CPM
P_expPSK = symerr(stMaryCPM,stdmatPSK) / n;% Experimental error for PSK
P_expQAM = symerr(stMaryCPM,stdmatQAM) / n;% Experimental error for QAM
semilogy(SNR_db,P_expCPM,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
hold off
set(gca,'FontSize',16)
titlestr = ['Graph of SER versus bit SNR (in dB) for an M = ' num2str(M) ' ary Modulations'];

xlabel('Symbol SNR, \it\gamma = E_b\rm\bf / \itN\rm\bf_0 in dB','FontSize',14,'FontWeight','bold'); 
ylabel('\itP_e\rm\bf (SER) for \itM\rm\bf-ary PSK','FontSize',14,'FontWeight','bold');
Mstr = [' \itM = \rm\bf' num2str(M)];
nstr = [' \itn_s = \rm\bf' ST];Mnstr = {Mstr;' ';nstr};
leg1 = [' \itP_e \rm\bf(experimental) '];
leg2 = [' \itP_e \rm\bf(theoretical) '];
text(max(SNR_db)*0.4,max(P_expCPM)*.2,Mnstr,'FontSize',14,'FontWeight','bold','Edgecolor','black');
text(max(SNR_db)*0.6,max(P_expPSK)*.04,leg1,'FontSize',14,'FontWeight','bold','Edgecolor','white');