% Fourier_Transform_Basics 
% Author C. Prati
% February 17, 2020
clear
close all
%--------------------------------------------------
% 1
% Let us generate a discrete cosinusoidal signal x with
% frequency of fo=40Hz, N=50 samples using a sampling interval
% T=1/1000 second

N=50;
fo=40;
T=1/1000;
t=(0:N-1)*T;
x=cos(2*pi*fo*t);

%----------------------------------------------------
% 2
% Let us plot the discrete signal x in figure 1
% as a sequence of samples ... from 0 to N-1
figure(1)
subplot(211)
stem((0:N-1),x)
xlabel('samples')
pause
% as a sequence of samples along a time axis t
figure(1)
subplot(212)
stem(t,x)
xlabel('time (seconds)')
pause

%----------------------------------------------------
% 3
% Let us generate the Fourier Transform X of the discrete signal x 
X=fft(x);

%----------------------------------------------------
% 4
% Let us plot the absolute value and the phase of X in figure 2
% as a sequence of samples from 0 to N-1

figure(2)
subplot(211)
stem((0:N-1),abs(X))
xlabel('samples')
title('absolute value')
pause

figure(2)
subplot(212)
stem((0:N-1),angle(X))
xlabel('samples')
title('phase')
pause

%----------------------------------------------------
% 5
% Let us plot the absolute value of X in figure 3
% as a sequence of samples and as a sequence along a 
% normalized frequency axis and as sequence along a 
% frequency axis

figure(3)
subplot(311)
stem((0:N-1),abs(X))
xlabel('samples')
pause

subplot(312)
stem((0:N-1)/N,abs(X))
xlabel('normalized frequency')
pause

subplot(313)
stem((0:N-1)/N/T,abs(X))
xlabel('frequency (Hz)')
pause

%----------------------------------------------------
% 6
% Let us generate the FFT sequence centered around 
% the zero frequency 

Xshift=fftshift(X);

%----------------------------------------------------
% 7
% Let us plot the absolute value of X in figure 4
% as a sequence of samples from 0 to N-1 and as a sequence
% of samples from -N/2 to N/2-1

figure(4)
subplot(211)
stem((0:N-1),abs(X))
xlabel('samples')
pause

subplot(212)
stem((-N/2:N/2-1),abs(Xshift))
xlabel('samples')
pause

%----------------------------------------------------
% 8
% Let us plot the absolute value of X in figure 5
% as a sequence of samples from -N/2 to N/2-1, as a sequence 
% along a normalized frequency axis from -1/2 to 1/2 and 
% as a sequence of samples along a frequency axis from
% -1/(2T) to 1/(2T)

figure(5)
subplot(311)
stem((-N/2:N/2-1),abs(Xshift))
xlabel('samples')
pause
subplot(312)
stem((-N/2:N/2-1)/N,abs(Xshift))
xlabel('normalized frequency')
pause
subplot(313)
stem((-N/2:N/2-1)/N/T,abs(Xshift))
xlabel('frequency (Hz)')

%----------------------------------------------------
% 9
% Now repeat the same analysis on a diferent signal x
% e.g. x=sinc((-N/2:N/2-1)/4); or x=ifftshift(gausswin(N,12));



