Saturday, November 20, 2010

Function for DFT Plot of Discrete Time Signal using Fast Fourier Transform Algorithm

function fftplot(signal_matrix, Fs)

%This function plots the two sided amplitude of the time varying signal
%sampled by frequency Fs
%Please note that this does not work if the two dimensional Image matrix is passed as signal_matrix.

%function to plot fft of the given signal
%input signal is passed in signal_matrix
%sampling frequency is given in Fs

signal_fft=fft(signal_matrix); %performing the fourier transform of the signal_matrix
Nx=length(signal_fft); % length of the FFT of signal
Ts=1./Fs; %Sample Period calculation
Fo= 1/(Ts*Nx); %frequency resolution factor
f=-Fs/2:Fo:(Fs/2)-1;  %defining the range of the frequency that has to be plotted
figure;
plot(f,2*fftshift(abs(signal_fft./Nx))); %2 sided amplitude spectrum plot of the signal_matrix

%Function ends
%Save this function to the current MATLAB directory to use