% This is differential for Mary PSK. Works OK, reference graph Fig. 2 of Biglieri IEEE Vehic 1998 
% Note that in a Rayleigh channel, only DPSK (not phase coherent PSK) will function as the following
% demostrates. In this configuration a Rician channel approaches AWGN when
% K >> 1, and Rayleigh channel when K << 1
clear;clc;close all;clf reset
M = 8;n = 10000;Kc = 0.1;
Mstr = num2str(M);Phstr = num2str(pi/M);KK1 = '100';KK2 = '10';KK3 = '1';
x_set = randint(n,1,M); % Original signal
set_param('M_DPSK_Mod/M-DPSK Modulator Baseband','M',Mstr,'Ph',Phstr);
%set_param('M_DPSK_Mod/M-DPSK Modulator Baseband','Ph',Phstr);
set_param('M_DPSK_deMod/M-DPSK Demodulator Baseband','M',Mstr,'Ph',Phstr);
%set_param('M_DPSK_deMod/M-DPSK Demodulator Baseband','Ph',Phstr);
%set_param('Ricchannel/Rician Fading Channel','Fd',Fdd);
sim('M_DPSK_Mod'); st = [real(stD) imag(stD)];st1 = stD; % DPSK Modulated signal

Kfac1 = '[1 0.2 0.3 0.1]';Kfac2 = '[10 0.2 0.3 0.1]';Kfac3 = '[50 0.2 0.3 0.1]';Tss = 'n*1e-0';
FDLOS = '[1e-3 1e-3 1e-3 1e-3]';TetaLOS = '[0 0 0 0]';Spektrum = 'Jakes';PathD = '[0 1e-5 1e-5 1e-5]';AvepathG = '[0 -3 -6 -9]';MaxDS = '1e-3';
set_param('Ricchannelc/DSP Constant','Ts',Tss);set_param('Ricchannelc/DSP Constant1','Ts',Tss);
set_param('Ricchannelc/Multipath Rician Fading Channel','K',Kfac1,'fdLOS',FDLOS,'thetaLOS',TetaLOS,'spectrumType',Spektrum,'pathDelays',PathD, ....
    'avgPathGaindB',AvepathG,'maxDopplerShift',MaxDS);
set_param('Ricchannelc/Multipath Rayleigh Fading Channel','spectrumType',Spektrum,'pathDelays',PathD, ....
    'avgPathGaindB',AvepathG,'maxDopplerShift',MaxDS);
sim('Ricchannelc');stric1 = [real(stc) imag(stc)];stray = [real(str) imag(str)];
set_param('Ricchannelc/Multipath Rician Fading Channel','K',Kfac2);
sim('Ricchannelc');stric2 = [real(stc) imag(stc)];
set_param('Ricchannelc/Multipath Rician Fading Channel','K',Kfac3);
sim('Ricchannelc');stric3 = [real(stc) imag(stc)];

%%%%% Setting noise power, Average symbol signal power is unity, bit SNR is used 
E_stn = 1;E_st = E_stn/log2(M);SNR_db = 0:12;dblen = length(SNR_db);
set_ntp = 10*log10(E_st) - SNR_db - 3.0103;SNRarr = 10.^(0.1*SNR_db);
P_exp = [];P_expric1 = [];P_expric2 = [];P_expric3 = [];P_expray = [];tic;
for iSNR = 1:dblen
nt = wgn(length(st),2,set_ntp(iSNR));
rt = st + nt; rtric1 = stric1 + nt;rtric2 = stric2 + nt;rtric3 = stric3 + nt; rtray = stray + nt;% signal with noise added
rtD = rt(:,1) + j*rt(:,2);sim('M_DPSK_deMod');P_exp1 = symerr(x_set,r_det) / n; % DPSK deModulated signal

rtD = rtric1(:,1) + j*rtric1(:,2); sim('M_DPSK_deMod');P_exprc1 = symerr(x_set,r_det) / n;
rtD = rtric2(:,1) + j*rtric2(:,2); sim('M_DPSK_deMod');P_exprc2 = symerr(x_set,r_det) / n;
rtD = rtric3(:,1) + j*rtric3(:,2); sim('M_DPSK_deMod');P_exprc3 = symerr(x_set,r_det) / n;

rtD = rtray(:,1) + j*rtray(:,2); sim('M_DPSK_deMod');P_expray1 = symerr(x_set,r_det) / n;

P_exp = [P_exp P_exp1];P_expric1 = [P_expric1 P_exprc1];
P_expric2 = [P_expric2 P_exprc2];P_expric3 = [P_expric3 P_exprc3];
P_expray = [P_expray P_expray1];end

semilogy(SNR_db,P_exp,'-k ','LineWidth',2);hold on;semilogy(SNR_db,P_expric1,'--k ','LineWidth',3);hold on
semilogy(SNR_db,P_expric2,'-.k ','LineWidth',2);hold on;semilogy(SNR_db,P_expric3,':k ','LineWidth',3);hold on
semilogy(SNR_db,P_expray,'-ok ','LineWidth',2)

set(gcf,'Color','w');set(gca,'FontSize',16);toc;grid on
xlabel('Bit SNR = \it\xi_b\rm\bf / \itN\rm\bf_0 in dB','FontSize',14,'FontWeight','bold'); 
ylabel('\itSER \rm\bf for M ary signals in Rician channel','FontSize',14,'FontWeight','bold');
Mstr = [' \itM = \rm\bf' num2str(M) ' (DPSK) '];nstr = [' \itn = \rm\bf' num2str(n)];
Kstr1 = [' \its\rm\bf^2 = ' Kfac1(2)];Kstr2 = [' \its\rm\bf^2 = ' Kfac2(2) Kfac2(3)];
Kstr3 = [' \its\rm\bf^2 = ' Kfac3(2) Kfac3(3)];leg1 = {Mstr;' ';nstr};
legA = 'AWGN channel ';legRAY = ' Rayleigh channel ';
legric1 = [' Rician channel with ' Kstr1];legric2 = [' Rician channel with ' Kstr2];legric3 = [' Rician channel with ' Kstr3];

diffx = max(SNR_db)-min(SNR_db);diffy = max(P_exp)-min(P_exp);
text(diffx*0.2,diffy*.01,leg1,'FontSize',16,'FontWeight','bold','Edgecolor','black','BackgroundColor','white');
text(diffx*0.2,diffy*.04,legA,'FontSize',16,'FontWeight','bold','BackgroundColor','white');
text(diffx*0.2,diffy*.06,legRAY,'FontSize',16,'FontWeight','bold','BackgroundColor','white');
text(diffx*0.2,diffy*.08,legric1,'FontSize',16,'FontWeight','bold','BackgroundColor','white');
text(diffx*0.3,diffy*.08,legric2,'FontSize',16,'FontWeight','bold','BackgroundColor','white');
text(diffx*0.4,diffy*.08,legric3,'FontSize',16,'FontWeight','bold','BackgroundColor','white');