You are on page 1of 3

# FFT (MATLAB)

There are many ways to define FFT in MATLAB in this tutorial is one of them:

## we have a continuous signal(ex. audio) in time domain and we want to represent it in

the frequency domain so we want to use Fourier transform but in MATLAB there is FFT
(Fast Fourier Transform) which deals with sampled signals so the first step is to sample
our signal:

##  To sample any signal we must define ( ) and

( ) which is and from shannon’s sampling theorem must be
greater than twice the highest frequency component of the signal.

##  Second step we define something called frequency resolution and time

resolution( ) represent minimum change in the signal to represent it in freq.
domain better and help us to define the frequency vector and represent a
large period of time so if we divide by we get the number of the samples in
this period of signal.

## N=To/TS; %number of samples in the period I take.

 Third step is to define the frequency vector and time vector (why?)
We define the time vector to represent our signal in the time domain and the
frequency vector because we will draw the signal’s frequency components
In the freq domain.

## t=(0:N-1)*TS; %we take N points to make the time vector definite

%(why?) to make it equal in size with the frequency
% %vector (why? :D) we will know later why. We multiply
% %by TS to make time points at TS ,2TS,3TS…. (times of
sdsdsd %samples).

1
f=(1:N)*df; %begin the vector from 1 or 0 not important but make
%sure it the same size as time vector. We multiply by df as
%we multiply time vector by TS .This issue to be clear lets
%give example if FM=16Khz,df=FM/4=4khz,N=256,so if
%we don’t multiply by df vector frequency will be [1 2 3
% ….255] represent nothing of the freq. components of
%the signal. But if we multiply by df so frequency vector
%will be [1,2……..255]*4khz ,so the first point at 4khz
%the second point at 8khz etc….

 Fourth step is to represent our Xt signal using time vector for example our signal
is sine wave with frequency FM.

Xt=sin(2*pi*FM*t);

## Plot(t,Xt); %if we want to draw it in time domain

 The last step is to define FFT the function is fft(my signal)/no.of samples
We divide by N to be normalized fft.

Xf=fft(Xt)/N;

## % from the beginning if we don’t make the time vector

%not the same size as freq. vector we will not be able to
%draw Xf with the freq. vector because Xf is the same
%size as Xt as it’s came from it and Xt is the same size as
%the time vector and in stem we draw f(freq. vector)
%with Xf which they must be equal in size to be drawn in
%MATLAB so size(t)=size(f)=size(Xt)=size(Xf).

2
Example:

Code:

## clear all;close all;

fm=3e3; fs=32*fm; ts=1/fs;
df=fm/64; to=1/df; n=to/ts;
t=(0:n-1)*ts;
f=(1:n)*df;
xt=sin(2*pi*fm*t);
figure(1);plot(t,xt);axis([0 0.6e-3 -1 1]);
xf=fft(xt)/n;
figure(2);stem(f,abs(xf));axis([0 6e3 0 0.7]);

Results:

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6
-4
x 10

0.6

0.5

0.4

0.3

0.2

0.1

0
0 1000 2000 3000 4000 5000 6000