Delta_t=1e-3          % Pas de Calcul
N=1/Delta_t           % Nombre de points
A=1                   % Amplitude
F=10                  % Frequence
T=1/F                 % Periode
Nb_per=3              % Nombre de Periodes
t=[-N*Nb_per*T/2:(N-1)*Nb_per*T/2]/N;

F_analyze = 3*F
%===============================================================================
% 1 %                    SAME FREQUENCY ANALYSIS
%===============================================================================
re=cos(-2*pi*F_analyze*t);
im=sin(-2*pi*F_analyze*t);
y=cos(2*pi*F*t);

%...............................................................................
figure()
subplot(2,2,1)

plot3(t,re,im,"b", "linewidth", 2)
hold on
plot3(t,y,"r","linewidth", 5)
hold on

xlabel("t(s)")
ylabel('real : cos\omega t')
zlabel('imag : sin\omega t')

str = {strcat('y(t)=cos(2\piFt) with F = ' , num2str(F),'Hz, analyzed with e^{-j2\pi F_at} with F_a=', num2str(F_analyze),'Hz')} 
title(str)
% str = {strcat('e^{-j2\pi Fa t} with Fa=' , num2str(F_analyze),'Hz')} 
str = {strcat('e^{-j2\pi Fa t} with Fa=' , num2str(F_analyze),'Hz')} 
legend(str, 'y(t)=cos(2\piFt)',"location", "northeast")
grid
%===============================================================================
% 2 %                COS BECOMES A SUM OF EXP
%===============================================================================
re=cos(-2*pi*F_analyze*t);
im=sin(-2*pi*F_analyze*t);
y_1_re=0.5*cos(2*pi*F*t);
y_1_im=0.5*sin(2*pi*F*t);
y_2_re=0.5*cos(-2*pi*F*t);
y_2_im=0.5*sin(-2*pi*F*t);

%...............................................................................
subplot(2,2,2)

plot3(t,re,im,"b", "linewidth", 2)
hold on
plot3(t,y_1_re,y_1_im,"m","linewidth", 5)
hold on
plot3(t,y_2_re,y_2_im,"g","linewidth", 5)
hold on

xlabel("t(s)")
ylabel('real : cos\omega t')
zlabel('imag : sin\omega t')
str = {strcat('y(t)=(e^{j\omega t}+e^{-j\omega t})/2 with F = ' , num2str(F),'Hz')} 
title(str)
str = {strcat('e^{-j2\pi Fa t} with Fa=' , num2str(F_analyze),'Hz')} 
legend (str,'y(t)=e^{j2\pi Ft}','y(t)=e^{-j2\pi Ft}',"location", "northeast");
grid
%===============================================================================
% 3 %                PRODUCT FOR EACH t : exp(+jwt)
%===============================================================================
re=cos(-2*pi*F_analyze*t);
im=sin(-2*pi*F_analyze*t);
y_1_re=0.5*cos(2*pi*F*t);
y_1_im=0.5*sin(2*pi*F*t);

product=0.5*exp(j*2*pi*F*t).*exp(-j*2*pi*F_analyze*t);

nd=100
product_area = product.*[zeros(1,(length(t)-nd)/2), ones(1, nd), zeros(1, (length(t)-nd)/2)] ; 

%...............................................................................
subplot(2,2,3)

plot3(t,re,im,"b", "linewidth", 2)
hold on
plot3(t,y_1_re,y_1_im,"m","linewidth", 5)
hold on
plot3(t,product,"y","linewidth", 5)
area(t,product_area)

xlabel("t(s)")
ylabel('real : cos\omega t')
zlabel('imag : sin\omega t')
str = {strcat('y_1(t)=e^{j\omega t}/2 with F = ' , num2str(F),'Hz')} 
title(str)
str = {strcat('e^{-j2\pi Fa t} with Fa=' , num2str(F_analyze),'Hz')} 
legend (str,'y_1(t)=0.5.e^{j2\pi Ft}','y_1(t).e^{-j2\pi Ft}','\int_{-T/2}^{T/2}y_1(t).e^{-j2\pi Ft}dt',"location", "northeast");
grid
%===============================================================================
% 4 %                PRODUCT FOR EACH t : exp(+jwt)
%===============================================================================
re=cos(-2*pi*F_analyze*t);
im=sin(-2*pi*F_analyze*t);
y_2_re=0.5*cos(-2*pi*F*t);
y_2_im=0.5*sin(-2*pi*F*t);

product=0.5*exp(-j*2*pi*F*t).*exp(-j*2*pi*F_analyze*t);

nd=100
product_area = product.*[zeros(1,(length(t)-nd)/2), ones(1, nd), zeros(1, (length(t)-nd)/2)] ; 

%...............................................................................
subplot(2,2,4)

plot3(t,re,im,"b", "linewidth", 2)
hold on
plot3(t,y_2_re,y_2_im,"g","linewidth", 5)
hold on
plot3(t,product,"y","linewidth", 5)
area(t,product_area)

xlabel("t(s)")
ylabel('real : cos\omega t')
zlabel('imag : sin\omega t')
str = {strcat('y_2(t)=e^{-j\omega t}/2 with F = ' , num2str(F),'Hz')} 
title(str)
str = {strcat('e^{-j2\pi Fa t} with Fa=' , num2str(F_analyze),'Hz')} 
legend (str,'y_2(t)=0.5.e^{-j2\pi Ft}','y_2(t).e^{-j2\pi Ft}','\int_{-T/2}^{T/2}y_2(t).e^{-j2\pi Ft}dt',"location", "northeast");
grid

%===============================================================================
%                 FREQUENCY ANALYSIS
%===============================================================================
figure()
freq = [0:1:100];
amplitudes=[zeros(1,length(freq))];

Delta_t=1e-3          % Pas de Calcul
N=1/Delta_t           % Nombre de points
A=1                   % Amplitude
F=10                  % Frequence
T=1/F                 % Periode
Nb_per=1              % Nombre de Periodes
t=[-N*Nb_per*T/2:(N-1)*Nb_per*T/2]/N;

y=cos(2.*pi.*F.*t);
TF=exp(-i.*2.*pi.*F_analyze.*t).*y;

acc_TF=0;
for n=1:length(t)
        acc_TF = acc_TF+TF(n).*Delta_t;
end
acc_TF=acc_TF/T;

amplitudes(F_analyze)=abs(acc_TF);
%-------------------------------------------------------------------------
stem(freq,amplitudes)
axis([0 max(freq) 0 1])
hold on
%===============================================================================



