clc;clear all;close all;
%% Part A. Generation of Modulated Signals
%% Amplitude-Shift Keying (ASK)
b = [1 0 0 1 0];
Rb=1000;% bit rate
n_sample=100;% samples per bit
[t,xu,f_sample] = unipolar_NRZ_2(b,Rb,n_sample);
fc= 8000;
sa= xu.*sin(2*pi*fc.*t);
figure(),plot(t,xu,'b','linewidth',2),hold on, plot(t,sa,'r','linewidth',1),title('ASK unipolar NRZ'),legend('xu','sa')
axis([0 5*10^-(3) -1.5 1.5])
grid on
XU = fft(xu);
L=length(xu);
P2_XU = abs(XU/L);% two sided PSD of xu
P1_XU = P2_XU(1:L/2+1);
P1_XU(2:end-1) = 2*P1_XU(2:end-1);% one sided PSD of xu
SA = fft(sa);
P2_SA = abs(SA/L);% two sided PSD of sa
P1_SA = P2_SA(1:L/2+1);
P1_SA(2:end-1) = 2*P1_SA(2:end-1);% one sided PSD of sa
f = f_sample*(0:(L/2))/L;
figure(),plot(f./1000,P1_XU,'b','linewidth',1),hold on, plot(f./1000,P1_SA,'r','linewidth',1),title('PSD of xu and sa'),legend('PSD(xu)','PSD(sa)')
xlabel('f (kHz)')
ylabel('PSD(f)')
axis([0 20 0 0.5])
grid on
%% Phase-Shift Keying (PSK)
b = [1 0 0 1 0 ];
Rb=1000;% bit rate
n_sample=100;% samples per bit
[t,xp,f_sample] = polar_NRZ_2(b,Rb,n_sample);
fc= 8000;
sp= xp.*sin(2*pi*fc.*t);
figure(),plot(t,xp,'b','linewidth',2),hold on, plot(t,sp,'r','linewidth',1),title('PSK polar NRZ'),legend('xp','sp')
axis([0 5*10^-(3) -1.5 1.5])
grid on
XP = fft(xp);
L=length(xp);
P2_XP = abs(XP/L);% two sided PSD of xp
P1_XP = P2_XP(1:L/2+1);
P1_XP(2:end-1) = 2*P1_XP(2:end-1);% one sided PSD of xp
SP = fft(sp);
P2_SP = abs(SP/L);% two sided PSD of sp
P1_SP = P2_SP(1:L/2+1);
P1_SP(2:end-1) = 2*P1_SP(2:end-1);% one sided PSD of sp
f = f_sample*(0:(L/2))/L;
figure(),plot(f./1000,P1_XP,'b','linewidth',1),hold on, plot(f./1000,P1_SP,'r','linewidth',1),title('PSD of xp and sp'),legend('PSD(xp)','PSD(sp)')
xlabel('f (kHz)')
ylabel('PSD(f)')
axis([0 20 0 1])
grid on
%% Frequency-Shift Keying (FSK)
b = [1 0 0 1 0 ];
Rb=1000;% bit rate
n_sample=100;% samples per bit
[t,xf,f_sample] = polar_NRZ_2(b,Rb,n_sample);
f0=6000; % Free-running frequency of VCO (Hz)
K0=-2000; % Frequency sensitivity of VCO (Hz/Volt)
f_VCO=f0+K0.*xf; % output of VCO (4kHz for input voltage of 1 Volt and 8kHz for -1 Volt)
f_osc=f_VCO(1:100:401);
sf1=sin(2*pi*f_osc(1).*t(1:100));
sf2=sin(2*pi*f_osc(2).*t(101:200));
sf3=sin(2*pi*f_osc(3).*t(201:300));
sf4=sin(2*pi*f_osc(4).*t(301:400));
sf5=sin(2*pi*f_osc(5).*t(401:500));
sf= [sf1 sf2 sf3 sf4 sf5]; % FSK signal
figure(),plot(t,xf,'b','linewidth',2),hold on, plot(t,sf,'r','linewidth',1),title('FSK polar NRZ'),legend('xf','sf')
axis([0 5*10^-(3) -1.5 1.5])
grid on
XF = fft(xf);
L=length(xf);
P2_XF = abs(XF/L);% two sided PSD of xf
P1_XF = P2_XF(1:L/2+1);
P1_XF(2:end-1) = 2*P1_XF(2:end-1);% one sided PSD of xf
SF = fft(sf);
P2_SF = abs(SF/L);% two sided PSD of sf
P1_SF = P2_SF(1:L/2+1);
P1_SF(2:end-1) = 2*P1_SF(2:end-1);% one sided PSD of sf
f = f_sample*(0:(L/2))/L;
figure(),plot(f./1000,P1_XF,'b','linewidth',1),hold on, plot(f./1000,P1_SF,'r','linewidth',1),title('PSD of xf and sf'),legend('PSD(xf)','PSD(sf)')
xlabel('f (kHz)')
ylabel('PSD(f)')
axis([0 20 0 1])
grid on
%% Part B. Coherent and Noncoherent Detection
%% B.1
fc= 8000;
ya= sa.*sin(2*pi*fc.*t);% ASK signal after mixer
figure(),plot(t,ya,'b','linewidth',2),title('ASK signal after mixer'),legend('ya')
axis([0 5*10^-(3) -1.5 1.5])
grid on
YA = fft(ya);
L=length(ya);
P2_YA = abs(YA/L);% two sided PSD of ya
P1_YA = P2_YA(1:L/2+1);
P1_YA(2:end-1) = 2*P1_YA(2:end-1);% one sided PSD of ya
f = f_sample*(0:(L/2))/L;
figure(),plot(f./1000,P1_YA,'b','linewidth',1),title('PSD of ya'),legend('PSD(ya)')
xlabel('f (kHz)')
ylabel('PSD(f)')
axis([0 20 0 0.5])
grid on
%% B.2
h_MF= fliplr(xu(1:100)); % matched filter impulse response
za= conv(ya,h_MF);
t_mf=0:1/f_sample:((length(za)-1)/f_sample);
figure(),plot(t_mf,za,'b','linewidth',2),title('Output of Matched Filter'),legend('za')
grid on
y_sample=za(100:100:500);
y_hold=zeros(1,500);
for w=0:length(b)-1
y_hold(1+100*w:100*(w+1))=y_sample(w+1);
end
figure(),plot(t,y_hold,'b','linewidth',2),title('Output of Sample & Hold'),legend('y Hold')
%% B.3 Phase deviation effect
fc= 8000;
phase_err= [0 (pi/180)*20 (pi/180)*60 (pi/180)*80 (pi/180)*120 ];
ya_err=zeros(5,length(sa));
za_err=zeros(5,length(t_mf));
peak_za=zeros(5,1);
for k=1:5
ya_err(k,:)= sa.*sin((2*pi*fc.*t) + phase_err(k));
za_err(k,:)= conv(ya_err(k,:),h_MF);
figure(),plot(t_mf,za_err(k,:),'b','linewidth',2),title('Output of Matched Filter '),legend(['za with phase error --> ',num2str(phase_err(k)*180/pi)]),ylim([-200 200])
peak_za(k,:)=max(za_err(k,:));
end
%% B.4
% Demodulation for phase error of 60 degrees and 120 degrees
y_sample3=za_err(3,100:100:500); %samples of za_err every Tb seconds for phase error of 60 degrees
y_hold3=zeros(1,500);
b_hat3=zeros(1,5);
y_sample5=za_err(5,100:100:500);%samples of za_err every Tb seconds for phase error of 120 degrees
y_hold5=zeros(1,500);
b_hat5=zeros(1,5);
for i=0:length(b)-1
y_hold3(1+100*i:100*(i+1))=y_sample3(i+1);
y_hold5(1+100*i:100*(i+1))=y_sample5(i+1);
if y_hold3(1+i*100)>0
b_hat3(i+1)=1;
else
b_hat3(i+1)=0;
end
if y_hold5(1+i*100)>0
b_hat5(i+1)=1;
else
b_hat5(i+1)=0;
end
end
%% B.5 Frequency deviation effect
fc= 8000;
ya= sa.*sin(2*pi*fc.*t);
fc1= 7900;
ya1= sa.*sin(2*pi*fc1.*t);
figure(),plot(t,ya,'b','linewidth',2),hold on,plot(t,ya1,'r','linewidth',2), title('Effect of frequency deviation , f=7900Hz'),legend('ya','ya1')
za1= conv(ya1,h_MF);
y_sample=za1(100:100:500);
y_hold=zeros(1,500);
b_hat_7900=zeros(1,5);
for i=0:length(b)-1
y_hold(1+100*i:100*(i+1))=y_sample(i+1);
if y_hold(1+i*100)>0
b_hat_7900(i+1)=1;
else
b_hat_7900(i+1)=0;
end
end
figure(),plot(t_mf,za1,'b','linewidth',2),title('Output of Matched Filter'),legend('za1')
grid on
fc2= 7985;
y2_mixed= sa.*sin(2*pi*fc2.*t);
ya2= conv(y2_mixed,h_MF);
figure(),plot(t_mf,ya2,'b','linewidth',2),title('Output of Matched Filter'),legend('ya2')
grid on
%% Part C. System Performance Under Noise
%% C.1
b = [1 0 0 1 0];
Rb=1000;% bit rate
n_sample=100;% samples per bit
[t,xu,f_sample] = unipolar_NRZ_2(b,Rb,n_sample);
fc= 8000;
sa= xu.*sin(2*pi*fc.*t);
%% C.2
y= sa + randn(1,length(sa));
figure(),plot(t,sa,'b','linewidth',2),hold on,plot(t,y,'r','linewidth',1), title('Effect of Noise on the ASK signal'),legend('sa','y')
%% C.3