clear;clc;close;warning off all
n = 10; M = 32; Fs = 100; Fd = 1;t = 1:1:n*Fs;Mary = num2str(M);
STstr = num2str(n-1);
Fi1 = [ones(1,Fs/2) zeros(1,Fs/2) zeros(1,(n-1)*Fs)];
Fi2 = [zeros(1,Fs/2) ones(1,Fs/2) zeros(1,(n-1)*Fs)];
% Displaying the Basis Functions Fi1 and Fi2
figure(1)
subplot(3,1,1);plot(1:n*Fs,Fi1);title('\it\bf\Psi\rm\bf_1( \itt \rm\bf)','FontSize',16);set(gcf,'Color',[1 1 1])
axis ([min(t) max(t) -0.1 1.1]);set(gca,'FontSize',14)
set(gca,'XTick',[50 100 150 200 250 300 350 400])
set(gca,'XTickLabel',{'T/2';'T';'3T/2';'2T';'5T/2';'3T';'7T/2';'4T'},'FontWeight','bold');
subplot(3,1,2);plot(1:n*Fs,Fi2);title('\it\bf\Psi\rm\bf_2( \itt \rm\bf)','FontSize',16);
axis ([min(t) max(t) -0.1 1.1]);set(gca,'FontSize',14)
set(gca,'XTick',[50 100 150 200 250 300 350 400])
set(gca,'XTickLabel',{'T/2';'T';'3T/2';'2T';'5T/2';'3T';'7T/2';'4T'},'FontWeight','bold');
% PSK Signals
% Construct s_m(t) signal set vectors (Producing copies of s_m(t) for receiver)
sM = [];
for i = 1:M
sM1 = [cos(2*pi*i/M) sin(2*pi*i/M)];sM = [sM;sM1];
end
% Display of transmitted PSK Signal  
x = randint(n,1,M);
set_param('PSKMod_Exp7/M-PSK Modulator Baseband','M',Mary);
set_param('PSKMod_Exp7','StopTime',STstr);sim('PSKMod_Exp7');
rep1 = 100;
smtMaryarr = [];
for rept = 1:length(smtPSK)
    st = ones(rep1,1)*smtPSK(rept);smtMaryarr = [smtMaryarr;st];
end

s1psk = reshape(smtMaryarr,Fs/2,2*n);smtpsk = [];
for i = 1:2:2*n-1
    s3 = [real(s1psk(:,i));imag(s1psk(:,i+1))];
    smtpsk = [smtpsk;s3];
end
subplot(3,1,3);plot(t,smtpsk);title(['PSK Signal Transmitted - \bf\its_m \rm\bf( \itt \rm\bf) - \itM = ' num2str(M)],'FontSize',16);
axis ([min(t) max(t) -1.1 1.1]);set(gca,'FontSize',14);xlabel('\itt - \rmTime Axis')
set(gca,'XTick',[50 100 150 200 250 300 350 400])
set(gca,'XTickLabel',{'T/2';'T';'3T/2';'2T';'5T/2';'3T';'7T/2';'4T'},'FontWeight','bold');

% Choose the first one as s_m(t) and Find r1 and r2
smt = smtpsk(1:100);
noise_power = 0.1;nt = wgn(Fs,1,noise_power);rt = smt + nt;%rt = smt;% Adding noise to received signal
rt_received = [rt; zeros((n-1)*Fs,1)];
r1 = rt'*Fi1(1:100)'/(Fs/2);r2 = rt'*Fi2(1:100)'/(Fs/2);r = [r1;r2];
%% Detector - Decision on based on selecting the max of C(r,s_m) 
Crsm = [];
for i = 1:M
    Crs = 2*sM(i,:)*r - 1;% Finding the product of 2s_m(t)*r- length of s_m(t) which is always 1
    Crsm = [Crsm;Crs];
end
[row, col] = max(Crsm);
smt_received = [repmat(sM(col,1),Fs/2,1); repmat(sM(col,2),Fs/2,1); zeros((n-1)*Fs,1)];
smt_transmitted = [smt; zeros((n-1)*Fs,1)];
figure(2);subplot(3,1,1);plot(t,smt_transmitted);title(['PSK Signal Transmitted - \bf\its_m\rm\bf ( \itt \rm\bf) - \itM = ' num2str(M)],'FontSize',16);
set(gcf,'Color',[1 1 1]);axis ([min(t) max(t) -1.1 1.1]);set(gca,'FontSize',14)
set(gca,'XTick',[50 100 150 200 250 300 350 400])
set(gca,'XTickLabel',{'T/2';'T';'3T/2';'2T';'5T/2';'3T';'7T/2';'4T'},'FontWeight','bold');
subplot(3,1,2);plot(t,rt_received);title('PSK Signal Received with Noise - \bf\itr \rm\bf( \itt \rm\bf)','FontSize',16);
set(gcf,'Color',[1 1 1]);axis ([min(t) max(t) -3.1 3.1]);set(gca,'FontSize',14)
set(gca,'XTick',[50 100 150 200 250 300 350 400])
set(gca,'XTickLabel',{'T/2';'T';'3T/2';'2T';'5T/2';'3T';'7T/2';'4T'},'FontWeight','bold');
subplot(3,1,3);plot(t,smt_received);title(['PSK Signal Detected - \itM = ' num2str(M)],'FontSize',16);xlabel('\itt - \rmTime Axis')
axis ([min(t) max(t) -1.1 1.1]);;set(gca,'FontSize',14) 
set(gca,'XTick',[50 100 150 200 250 300 350 400])
set(gca,'XTickLabel',{'T/2';'T';'3T/2';'2T';'5T/2';'3T';'7T/2';'4T'},'FontWeight','bold');