FFT Algorithms - Matlab |
Using the FFT Algorithm(s)
in Matlab
If you use Matlab to get
the FFT you could use an m-file like the one below.
Data=dlmread('SensorSignal1.txt','\t');
Time = Data(:,1);
VData = Data(:,2);
plot(Time,VData)
pause
SigFFT = fft(VData);
plot(abs(SigFFT))
This m-file does the
following:
Precautions:
The m-file code above can
be copied directly from the page, pasted into an editor and saved. We
have put it on the left edge of the page to minimize possible problems
with indentations, etc.
When you save the
m-file be sure that you have the path to the m-file included in the path
settings. Use File-Set Path
to add the path if necessary.
Points to note:
Matlab indices start at
1, but we start with a coefficient, ao. The first
coefficient is ao and it will be stored in the first
element of the array (SigFFT in the example m-file), i.e, SigFFT(1).
Every other element in the array is also off by 1. For example,
SigFFT(12) will contain the 11th harmonic.
The calculation will be
done in terms of the harmonics of the fundamental frequency of the data
set. If you have a square wave that shows two periods in the data set,
the first harmonic of the square wave will be the second harmonic of the
fundamental frequency of the data set, and - given the first point just
above - will appear in the third element of the FFT array.
When you do the
calculation, you will often have data that has many periods in one
data set. Those frequencies - in the periodice signal - will be at
exact multiples of the fundamental frequency of the data set (a
sharp line in the computed FFT) or near a multiple (a spread-out
line). You shouldn't expect the fundamental of a periodice signal
to be the fundamental frequency of the data set unless you have
exactly one period of your periodic signal in the data set.
Example: You have a
data set that is 2 milliseconds long. The fundamental frequency of
the data set is 0.5 kHz (= 500 Hz). If you have a one kHz signal
embedded in that data, it will be at the second harmonic of the 500
Hz. (In Matlab, that will be at the index = 3.)
The second biggest
problem with doing the FFT in Matlab is getting the index straight.
The calculation
does not include the 2/N term. You have to compensate for that. In a
five volt square wave with 4000 data points, the first harmonic of the
square wave will calculate out at a magnitude of 12,813. The actual
first harmonic (i.e., the fundamental) of a square wave of amplitude A
is 4A/p,
and that would calculate out to about 6.4. To get from 12,813 to 6.4
you need to multiply 12,813 by 2/4000 (that's 2/N).
|