%% FFT Phase
% Create a signal that consists of two sinusoids of frequencies 15 Hz and 40 Hz. The first sinusoid has a phase of -\pi /4, and the second has a phase of \pi /2. Sample the signal 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 Fourier transform of the signal. Plot the magnitude of the transform as a function of frequency.

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

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

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

% Compute the phase of the transform, removing small-magnitude transform values. Plot the phase as a function of frequency.

tol = 1e-6;
z(abs(z) < tol) = 0;

theta = angle(z);

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

% Copyright 2018 The MathWorks, Inc.