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:
-
Data=dlmread('SensorSignal1.txt','\t');
-
Brings the data from the
file SensorSignal1.txt into the Matlab workspace.
-
The file is "tab-delimited"
- denoted by '\t'. Other delimiters are possible, e.g. commas, but
they don't specify how to treat them in the help within Matlab.
-
The file should be a pure
text file, and that's the kind of file many data acquisition programs generate,
even if they use other extensions like ".dat"
-
You need to set the "path"
so that the m-file is at a location where Matlab can find it.
-
Time = Data(:,1);
-
VData = Data(:,2);
-
Separates the data into
two one-dimensional arrays. This assumes that the original data file,
SensorSignal1.txt, has both time and data in two columns.
-
You need the data in a
separate array because the FFT function can only handle a one-dimensional
array.
-
Then, plot the data vs
time. This gives you a chance to look to see if things are OK.
-
SigFFT = fft(VData);
-
This is the place where
the FFT is calculated.
-
There are other "versions"
of the FFT algorithm, IFFT, FFT2, IFFT2, and FFTSHIFT. Check the
help for information about them.
-
The fft function returns
a complex array. The reals are the a's, and the complex values are
the b's in the Fourier expansion.
-
plot(abs(SigFFT))
-
This plots the c's for
the FFT that was calculated. If you want the a's and b's, you can
extract the real and the imaginary parts to get what you want. There
are two notes that give you info on the c's.
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).
-
The biggest problem with
doing the FFT in Matlab is getting the correlation between the results
that are returned and the actual numbers you want.