echo off;clear;clc;close all;warning('off','comm:obsolete:randint')
n = 30000;MPSK = 8;MTCM = 16;ba = 3 / sqrt(5);sa = 1 / sqrt(5); 
trellisin = 'poly2trellis([3 2 1],[2 1 4 0;0 2 1 0;0 0 0 1])';tbdepthin = '4';
%%% The setting in the exponential shows where [000(0) 001(1) ... are
%%%%%%%%%%%%             0                 1              2             3                 4                 5                     
constptsin = ['[ba*exp(3*pi*j/4) sa*exp(7*pi*j/4) ba*exp(pi*j/4) sa*exp(3*pi*j/4) ba*exp(7*pi*j/4) sa*exp(5*pi*j/4) ' ....
               'ba*exp(5*pi*j/4) sa*exp(pi*j/4) exp(pi*j*0.3976) exp(pi*j*1.1024) exp(pi*j*1.8976) exp(pi*j*0.6024) ' ....
               'exp(pi*j*0.1024) exp(pi*j*1.3976) exp(pi*j*1.6024) exp(pi*j*0.8976)]'];
constptsin = ['[ba*exp(3*pi*j/4) sa*exp(7*pi*j/4) sa*exp(3*pi*j/4) ba*exp(7*pi*j/4) exp(pi*j*1.1024) exp(pi*j*0.3976) ' ....
               'exp(pi*j*1.3976) exp(pi*j*0.1024) exp(pi*j*0.6024) exp(pi*j*1.8976) exp(pi*j*0.8976) exp(pi*j*1.6024) ' ....
               'sa*exp(5*pi*j/4) ba*exp(pi*j/4) ba*exp(5*pi*j/4) sa*exp(pi*j/4)]'];           
%constptsin = '[exp(2*pi*j*[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]/16)]';
s_i = randint(n,1);sdec = [];
for i = 1:log2(MPSK):n;sdec1 = bi2de([s_i(i) s_i(i+1) s_i(i+2)],'left-msb');sdec = [sdec;sdec1];end
spsk = pskmod(sdec,MPSK);
set_param('TCM_Enc/General TCM Encoder','trellis',trellisin,'constpts',constptsin);
set_param('TCM_Dec/General TCM Decoder','trellis',trellisin,'constpts',constptsin,'tbdepth',tbdepthin);
sim('TCM_Enc');stricPSK = spsk;stricTCM = s_o;
%%%% Rician Channel. Note for Rayleigh channel we need DPSK, not used here
% Fddc = '5e-2';Fddy = '5e-2';gdB = '0';dVec = '0';KK1 = '100';KK2 = '30';KK3 = '1';
% set_param('ricchannelTCM/Rician Fading Channel','K',KK2);
% st1 = spsk;sim('ricchannelTCM');stricPSK = stc;st1 = s_o;sim('ricchannelTCM');stricTCM = stc;
%%%% End of channel

spsk = stricPSK;s_o = stricTCM;
E_stn = 1;E_stPSK = E_stn/log2(MPSK);E_stTCM = E_stn/log2(MTCM);
SNR_db = -2:12;dblen = length(SNR_db);
set_ntpPSK = 10*log10(E_stPSK) - SNR_db;SNRarr = 10.^(0.1*SNR_db);set_ntpTCM = 10*log10(E_stTCM) - SNR_db;
ntPSK = [];ntTCM = [];
for iSNR = 1:dblen
nt1 = wgn(1,n/log2(MPSK),set_ntpPSK(iSNR),'complex');ntPSK = [ntPSK nt1'];
nt1 = wgn(n/log2(MPSK),1,set_ntpTCM(iSNR),'complex');ntTCM = [ntTCM nt1];
end;clear nt1; 
spskmat = repmat(spsk,1,dblen);s_omat = repmat(s_o,1,dblen);

rtPSK = spskmat + ntPSK;rtTCM = s_omat + ntTCM;
P_expPSK = [];P_expTCM = [];
for iSNR = 1:dblen
r_detPSK = pskdemod(rtPSK(:,iSNR),MPSK);
P_exp1 = symerr(sdec,r_detPSK)*log2(MPSK)/n; P_expPSK = [P_expPSK P_exp1];
s_o = rtTCM(:,iSNR);sim('TCM_Dec');dTCM = [];
for i = 1:log2(MPSK):n;sdec1 = bi2de([cikis(i) cikis(i+1) cikis(i+2)],'left-msb');dTCM = [dTCM;sdec1];end
P_exp1 = log2(MPSK)*symerr(sdec,dTCM) / n; P_expTCM = [P_expTCM P_exp1];
end
semilogy(SNR_db,P_expPSK,'-k ','LineWidth',2);hold on
semilogy(SNR_db,P_expTCM,':k ','LineWidth',4);hold off
set(gcf,'Color','w');set(gca,'FontSize',16)
xlabel('Bit SNR (dB)','FontSize',16,'FontWeight','bold'); 
ylabel('\itP_e','FontSize',16,'FontWeight','bold'); 
leg1 = 'Uncoded 8 PSK';leg2 = '16 QAM TCM';
text(max(SNR_db)*0.5,max(P_expPSK)*.4,leg1,'FontSize',16,'FontWeight','bold','BackgroundColor','white');
text(max(SNR_db)*0.3,max(P_expPSK)*.6,leg2,'FontSize',16,'FontWeight','bold','BackgroundColor','white');