% DEMO 3hs OSS 28/01/2024
% HLE.46 Nikos Dimitriou
clear
fo=10;     % Syxnothta shmatos plhroforias
fc=100;    %Syxnothta Ferontos
fs=100.*fc; %syxnothta deigmatolhyias
Ts=1./fs;
t=-2./fo:Ts:2./fo;
x_m=cos(2.*pi.*fo.*t);   %shma plhroforias

%%%%%%%%%%%%%%%%%%%%%%%% DSB Diamorfwsh
figure;

plot(t,x_m);
hold;
x_DSB=cos(2.*pi.*fc.*t).*x_m;
plot(t,x_DSB,'r');
grid;
legend('x_m','x_{DSB}','Fontsize',20);
title('DSB modulation','Fontsize',20);
%%%%%%%%%%%%%%%%%%%%%%%%% DSB Sygxronh Apodiamorfwsh
x_rec=x_DSB.*cos(2.*pi.*fc.*t);
figure;
plot(t,x_m,'b');
hold;
plot(t,x_rec,'r');

x_filtered=conv(x_rec,30.*sinc(30.*t),"same"); %% Bathyperato Filtro H(f)=rect (f/30) syxnothta apokophs 15Hz
x_filtered=x_filtered./max(x_filtered);
plot(t,x_filtered,'kp-',"markersize", 3);
grid;
legend('x_m','x_{demod}','x{rec}','Fontsize',20);
title('DSB Synchronous Demodulation','Fontsize',20);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% AM Diamorfwsh

x_AM=cos(2.*pi.*fc.*t).*(1+x_m);
figure;
grid;
plot(t,1+x_m,'b');
hold;
plot(t,x_AM,'r-');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%Prosomoiwsh Fwrath Periballousas
clear x_c,t_c
count=0;
value_hold=0;
for i=1:size(x_AM,2)-1,
  if x_AM(i)>x_AM(i+1) && x_AM(i)>value_hold,
    count=count+1;
    t_c(count)=t(i);
    x_c(count)=x_AM(i);
    value_hold=x_c(count);
  endif
  if x_AM(i)<=0,
    value_hold=0;
  endif
end
plot(t_c,x_c,'k--');



grid;
legend('1+x_m','x_{AM}','1+x_{rec}','Fontsize',20);
title('AM modulation','Fontsize',20);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Paradeigma Yperdiamorfwshs AM
x_m_1=x_m.*2;  %% Parallagh shmatos plhroforias

x_AM_1=cos(2.*pi.*fc.*t).*(1+x_m_1);
figure;
plot(t,1+x_m_1);
hold;

plot(t,x_AM_1,'r');

clear x_c t_c
count=0;
value_hold=0;
for i=1:size(x_AM_1,2)-1,
  if x_AM_1(i)>x_AM_1(i+1) && x_AM_1(i)>value_hold,
    count=count+1;
    t_c(count)=t(i);
    x_c(count)=x_AM_1(i);
    value_hold=x_c(count);
  endif
  if x_AM_1(i)<=0,
    value_hold=0;
  endif
end
plot(t_c,x_c,'k--');

grid;
legend('1+x_m','x_{AM}','1+x_{rec}','Fontsize',20);
title('AM Overmodulation','Fontsize',20);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Diamorfwsh PM


kf=20.*pi;

x_FM=cos(2.*pi.*fc.*t+kf.*(1./(2.*pi.*fo)).*sin(2.*pi.*fo.*t));
figure;
plot(t,x_FM,'r');
hold;
plot(t,cos(2.*pi.*fc.*t),'b');

grid;
legend('x_{FM}','carrier','Fontsize',20);
title('FM Modulation','Fontsize',20);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Diamorfwsh FM
kp=20;

x_PM=cos(2.*pi.*fc.*t+kp.*x_m);
figure;
plot(t,x_PM,'r');
hold
plot(t,cos(2.*pi.*fc.*t),'b');

grid;
legend('x_{PM}','carrier','Fontsize',20);
title('PM Modulation','Fontsize',20);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Deigmatolhyia
fs1=20.*fo; %Syxnothta Deigmatolhyias
Ts1=1./fs1;
tn=-2./fo:Ts1:2./fo;
x_m_sampled=cos(2.*pi.*fo.*tn); %Deigmatismeno shma

x_q_1=quantizer(4,x_m_sampled); %Kbantismemo shma L=4

x_q_2=quantizer(16,x_m_sampled); %Kbantismeno shma L=16

figure

plot(t,x_m,'b');
hold;
plot(tn,x_m_sampled,'rs',"markersize", 6);

plot(tn,x_q_1,'kp--',"markersize",6);


grid;
legend('Analog','Sampled','Quantized L=4','Fontsize',20);
title('Sampling - Quantization L=4','Fontsize',20);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

figure

plot(t,x_m,'b');
hold;
plot(tn,x_m_sampled,'rs',"markersize", 6);

plot(tn,x_q_2,'gp--',"markersize",6);


grid;
legend('Analog','Sampled','Quantized L=16','Fontsize',20);
title('Sampling - Quantization - L=16','Fontsize',20);
