clear all
close all
clc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
T=0.2; %%% sampling period
ws=2*pi/T; %%%% sampling frequency/rate/speed
disp(['T = ',num2str(T)])
disp(['ws = ',num2str(ws)])
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ts=0:T:10;
W=5; %%%% frequency of the signal
xn=cos(W*ts);
%xn=cos(W*ts)+sin(3*W*ts);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
stem(ts,real(xn),'r')
title('DATA x(nT)')
set(gca,'fontSize',20,'fontWeight','Bold')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
L=length(xn); %%% length of the signal
N=L; %%% you can choose N>=L 
FXs=fft(xn,N);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
stem((0:N-1),abs(FXs).^2)
hold on
plot([N/2 N/2],[0 max(abs(FXs).^2)],'r--','LineWidth',2)
set(gca,'fontSize',20,'fontWeight','Bold')
xlabel('k')
xlim([0 N-1])
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
Omega=(0:N-1)*(2*pi/N);
stem(Omega,abs(FXs).^2)
hold on
plot([pi pi],[0 max(abs(FXs).^2)],'r--','LineWidth',2)
set(gca,'fontSize',20,'fontWeight','Bold')
xlabel('\Omega')
xlim([0 2*pi])
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
w=0:(ws/2)/(N/2):ws-(ws/2)/(N/2);
stem(w,abs(FXs).^2)
hold on
plot([pi/T pi/T],[0 max(abs(FXs).^2)],'r--','LineWidth',2)
set(gca,'fontSize',20,'fontWeight','Bold')
xlabel('w=\Omega/T')
xlim([0 2*pi/T])
title(['T = ', num2str(T)])
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
FXs2=FXs(1:fix(N/2));
w=0:(ws/2)/(N/2):ws/2-(ws/2)/(N/2);
stem(w,abs(FXs2),'r')
hold on
set(gca,'fontSize',20,'fontWeight','Bold')
xlabel('w=\Omega/T')
xlim([0 pi/T])
title(['T = ', num2str(T)])


xlim([0 ws/2])