##### Personal tools
•
You are here: Home Lecture 9: Examples

# Lecture 9: Examples

##### Document Actions
Schedule :: The DFT :: Aliasing & Leakage :: Examples :: The FFT :: Convolution

In this section some examples of FFT usage are presented by means of M ATLAB .
```            f = 100;    % frequency of the signal
fs = 400;   % sampling rate
N0 = 128;   % choose the number of points in the FFT
f0 = fs/N0; % compute the frequency resolution, f0 = 3.125 Hz
k = (0:N0-1); % range of index values
fin = cos(2*pi*f/fs*k);  % input function
plot(k,fin); % plot the function
```
file=f1.ps,width=.6
```            % Note that the function ends at the end of a cycle: its periodic
% extension is the same as the function.
%
fout = fft(fin,N0);  % compute the FFT
plot(k,abs(fout);
% we have to plot abs(fout), since fout is a complex vector.
```
file=f2.ps,width=.6
```            % note that there are exactly two peaks.  This is because the
% frequency was a multiple of the resolution.  The bin with the
% frequency peaks is bin 32 and bin 96 (128-32=96)
% since f/f0 = 32.
% Note: the indexing in Matlab starts from index=1.  This means that
% index 1 corresponds to bin 0.  Hence, bin 32 is in fout(33):
% fout(33) = 64 + 0i
% fout(97) = 64 + 0i
%
% Now let's change the frequency and do it again.
f = f0;      % set the frequency to the lowest resolvable frequency
fin = cos(2*pi*f/fs*k);  % function
plot(k,fin); % plot the function
fout = fft(fin,N0);  % compute the FFT
plot(k,abs(fout);
```
file=f3.ps,width=.6
file=f4.ps,width=.6
```            %
% Now let's try a frequency that is not a multiple of the
% frequency resolution
f = 51;
% Note that f/f0 = 16.32, which is not an integer bin number
%
fin = cos(2*pi*f/fs*k);  % function
plot(k,fin); % plot the function
fout = fft(fin,N0);  % compute the FFT
plot(k,abs(fout);
```
file=f5.ps,width=.6
file=f6.ps,width=.6
```            % Note that the time function does not end at the end of a period
% The transform does not occupy just one bin -- there is spectral
% leakage.  The peak occurs at bin 16 -- the nearest integer to the
% true (real) frequency bin.
%
% Now let's examine the effect of zero padding on this problem
% (increasing N0).  Using the same frequency, add on 128 zeros to the
% set of samples:
fin1 = [fin zeros(1,128)];
fout1 = fft(fin1,256);
plot(0:255,abs(fout1));
```
file=f7.ps,width=.6
```            % Note that the zero padding did nothing to improve the spectral
% leakage.

%
% Now let's examine the effect of the number of samples on the spectral
% resolution.  Suppose that we have a signal that consists of two
% sinusoids, f1 = 51 Hz and f2 = 53 Hz.  We will keep the same
% sampling rate.  To begin with, we will take 128 points of data:
fin = cos(2*pi*k*f1/fs) + cos(2*pi*k*f2/fs);
plot(k,fin)
```
file=f8.ps,width=.6
```            fout = fft(fin,128);
plot(k,abs(fout));
```
file=f9.ps,width=.6
```            % The frequency resolution is f0=3.125 Hz.  The frequencies we wish to
% distinguish are too close to both show up.
%
% Now let's increase the number of sample points up to 512.  This gives
% a frequency resolution of f0 = 0.7813
k2 = (0:511)
fin2 = cos(2*pi*k2*f1/fs) + cos(2*pi*k2*f2/fs);
plot(k2,fin2);
```
file=f10.ps,width=.6
```            fout2 = fft(fin2,512);
plot(k2,abs(fout2));
```
file=f11.ps,width=.6
```            % In this case, we are able to distinguish between the two
% frequencies.
%
% Now, suppose that instead of taking more data, we had simply decided
% to zeropad out to 512 points.
%
fin1 = [fin zeros(1,384)];    % 512 points
plot(k1,fin1)
fout1 = fft(fin1,512);
plot(k1,fout1);
```
file=f12.ps,width=.6
file=f13.ps,width=.6
```            % Note that we have not increased the spectral resolution.  We have
% only provided a more finely-sampled version of the spectrum we saw
% with 128 points.
```
Copyright 2008, by the Contributing Authors. Cite/attribute Resource . admin. (2006, June 07). Lecture 9: Examples. Retrieved January 07, 2011, from Free Online Course Materials — USU OpenCourseWare Web site: http://ocw.usu.edu/Electrical_and_Computer_Engineering/Signals_and_Systems/9_3node3.html. This work is licensed under a Creative Commons License