%% FFT Phase
% Generate a signal that consists of two sinusoids of frequencies 15 Hz and 40 Hz. The first sinusoid has a phase of -\pi /4. The second sinusoid has an equivalent phase of \pi /2. The signal is sampled at 100 Hz for one second.

fs = 100;
t = 0:1/fs:1-1/fs;
x = cos(2*pi*15*t - pi/4) - sin(2*pi*40*t);

%
% Compute the discrete Fourier transform of the signal. Plot the magnitude of the transform as a function of frequency.

y = fft(x);
ys = fftshift(y);

ly = length(y);
f = (-ly/2:ly/2-1)/ly*fs;

stem(f,abs(ys))
xlabel 'Frequency (Hz)'
ylabel '|y|'
grid

%
% Find the phase of the transform. Neglect small-magnitude transform values in the calculation. Plot the phase as a function of frequency.

th = 1e-6;
ys(abs(ys) < th) = 0;

phs = angle(ys);

stem(f,phs/pi)
xlabel 'Frequency (Hz)'
ylabel 'Phase / \pi'
grid

% Copyright 2012 The MathWorks, Inc.