%   MATLAB fft demo

%	Download and run this file
%   This demo takes the fft of a multitone signal and demonstrates
%   that the fft has the correct amplitude and phase in the right
%   fft bins

disp(' ')
disp('        fft MATLAB Demo')
disp(' ')

N = 256;    %   Use 256 samples
M1 = 19;    %   Create a signal in bin 19
M2 = 9;     %   Create a signal in bin  9

n = 1:N;

%   Create a multitone signal with frequencies in bins 9 and 19
%   The signal in bin 9 has a 15 volt amplitude, the signal in bin
%   19 has a 5 volt amplitude and there is a a 4 Volt DC term in bin zero.

v = 4+5*cos(2*pi*M1*(n-1)/N) + 15*cos(2*pi*M2*(n-1)/N + pi/4);
vf = 2*fft(v)/N;    % vf is the fourier transform of v. Note that the
                    % fft is multiplied by 2 and divided by N.

vf(1) = vf(1)/2;    % the DC term is treated differently

bin=n-1;            % Shift MATLAB index so the lowest bin is bin zero.

            %   Plot the first 51 spectral bins (bins 0 through 50)
plot(bin(1:51),abs(vf(1:51))),xlabel('Bin'),ylabel('Signal Peak Amplitude'), title('Magnitude in Spectral bins')

%   Note that bin 9 corresponds to MATLAB index 10 and bin 19 corresponds to
%   matlab index 20.  The MATLAB index starts at 1, while the bin numbers start at zero.
%   You have to add one to the bin to get the corresponding MATLAB index.

disp(' The signal in bin 0 should have an amplitude of 4 and a  zero phase shift.')
vf(1)
disp(' The signal in 9 should have an amplitude of 15 and a  45 degree phase shift.')
vf(10)
disp(' The signal in in 19 should have an amplitude of 5 and zero phase. ')
vf(20)
disp([' The amplitude of the signal in bin 0 is ' num2str(abs(vf(1)))])
disp([' The amplitude of the signal in bin 9 is ' num2str(abs(vf(10)))])
disp([' The amplitude of the signal in bin 19 is ' num2str(abs(vf(20)))])



