<< Chapter < Page | Chapter >> Page > |
While the FFT/IFFT commands are easy to invoke, their meaning is not always instantly transparent.The intent of this section is to provide some examples that show how to interpret (andhow not to interpret) the frequency analysis commands in M atlab .
Begin with a simple sine wave of frequency
sampled every
seconds, as is familiar from previous
programs such as
speccos.m
.
The first step in any frequency analysis is todefine the window over which the analysis will take place,
since the FFT/DFT must operate on a finite data record.The program
specsin0.m
defines the length of the analysis
with the variable
N
(powers of two make for fast calculations),
and then analyzes the first
N
samples of
w
.
It is tempting to simply invoke the M
atlab commands
fft
and to plot the results. Typing
plot(fft(w(1:N)))
gives a meaningless answer (try it!) because the output
of the
fft
command is a vector of complex numbers.
When M
atlab plots complex numbers, it plots
the imaginary vs. the real parts. In order to view themagnitude spectrum, first use the
abs
command, as shown in
specsin0.m
.
f=100; Ts=1/1000; time=5.0; % freq, sampling interval, time
t=Ts:Ts:time; % define a time vectorw=sin(2*pi*f*t); % define the sinusoid
N=2^10; % size of analysis windowfw=abs(fft(w(1:N))); % find magnitude of DFT/FFT
plot(fw) % plot the waveform
specsin0.m
naive and deceptive spectrum of a sine wave via the FFT
(download file)
Running this program results in a plot of the magnitude of the output
of the FFT analysis of the waveform
w
.
The top plot in
[link] shows two large spikes, one near “100” and one near “900”.
What do these mean? Try a simple experiment. Change the value of
N
from
to
. This is shown in the bottom
plot of
[link] , where the two spikes now occur
at about “200” and at about “1850”. But the frequency of thesine wave hasn't changed! It does not seem reasonable that the window
over which the analysis is done should change the frequencies inthe signal.
There are two problems. First,
specsin0.m
plots the magnitude data
against the index of the vector
fw
, and this index (by itself)
is meaningless. The discussion surrounding
[link] shows that
each element of
represents a scaling of the complex sinusoid
with frequency
. Hence, these indices must be scaled by
the time over which the analysis is conducted, which involvesboth the sampling interval and the number of points in the FFT analysis.
The second problem is the ordering of the frequencies.Like the columns
of the DFT matrix
[link] ,
the frequencies represented by the
are the negative
of the frequencies represented by
.
There are two solutions. The first is appropriate only when the
original signal is real valued. In this case, the
's are
symmetric and there is no extra information contained inthe negative frequencies. This suggests plotting only
the positive frequencies, a strategy that is followed in
specsin1.m
.
f=100; Ts=1/1000; time=5.0; % freq, sampling interval, time
t=Ts:Ts:time; % define a time vectorw=sin(2*pi*f*t); % define the sinusoid
N=2^10; % size of analysis windowssf=(0:N/2-1)/(Ts*N); % frequency vector
fw=abs(fft(w(1:N))); % find magnitude of DFT/FFTplot(ssf,fw(1:N/2)) % plot for positive freq. only
specsin1.m
spectrum of a sine wave via the FFT/DFT
(download file) specsin1.m
is shown in the top
plot of
[link] .
The magnitude spectrum shows a single spikeat 100 Hz, as is expected. Change
f
to other values, and observe
that the location of the peak in frequency moves accordingly.Change the width and location of the analysis window
and verify that the location of the peak does not change.
Change the sampling interval
Ts
and verify that the
analyzed peak remains at the same frequency.
Notification Switch
Would you like to follow the 'Software receiver design' conversation and receive update notifications?