<< Chapter < Page | Chapter >> Page > |
The second solution requires more bookkeeping of indices,
but gives plots that more closely accord with continuous-timeintuition and graphs.
specsin2.m
exploits the built in
function
fftshift
,
which shuffles the output of the
FFT
command so that the
negative frequencies occur on the left, the positivefrequencies on the right, and DC in the middle.
f=100; Ts=1/1000; time=10.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=(-N/2:N/2-1)/(Ts*N); % frequency vector
fw=fft(w(1:N)); % do DFT/FFTfws=fftshift(fw); % shift it for plotting
plot(ssf,abs(fws)) % plot magnitude spectrum
[link] , which shows the complete magnitude spectrum
for both positive and negative frequencies. It is also easy toplot the phase spectrum by substituting
specsin2.m
spectrum of a sine wave via the FFT/DFT
(download file) phase
for
abs
in either of the preceding two programs.
Explore the limits of the FFT/DFT technique by choosing extreme values. What happens when:
f
becomes too large? Try
f
= 200, 300, 450, 550, 600,
800, 2200 Hz. Comment on the relationship between
f
and
Ts
.Ts
becomes too large? Try
Ts
= 1/500, 1/250, 1/50.
Comment on the relationship between
f
and
Ts
.
(You may have to increase
time
in order to have enough samples to operate on.)N
becomes too large or too small? What happens to the
location in the peak of the magnitude spectrum when
N
=
,
,
,
,
,
? What happens to the
width of the peak in each of these cases? (You may have to increase
time
in order to have enough samples to operate on).Replace the
sin
function with
. Use
What is the spectrum of
?
What is the spectrum of
?
Consider
. What is the largest
for which the results
make sense? Explain what limitations there are.
Replace the function with . What is the spectrum of the sinc function? What is the spectrum of ?
Plot the spectrum of
. Should you use the
technique of
specsin1.m
or of
specsin2.m
?
Hint: Think symmetry.
The FFT of a real sequence is typically complex, and sometimes it is important to look at the phase (as well asthe magnitude).
phi=
0, 0.2, 0.4, 0.8, 1.5, 3.14, find the phase
of the FFT output at the frequencies
f
.These are all examples of “simple” functions, which can be
investigated (in principle, anyway) analytically.The greatest strength of the FFT/DFT is that it can also
be used for the analysis of data when no functional form isknown. There is a data file on the website called
gong.wav
, which
is a sound recording of an Indonesian gong (a large struck metalplate). The following code reads in the waveform and
analyzes its spectrum using the FFT. Make sure that thefile
gong.wav
is in an active M
atlab path, or you will get
a “file not found” error. If there is a sound card (and speakers)attached, the
sound
command plays the
.wav
file at the sampling rate
.
filename='gong.wav'; % name of wave file goes here
[x,sr]=wavread(filename); % read in wavefile
Ts=1/sr; % sample interval and # of samplesN=2^15; x=x(1:N)'; % length for analysis
sound(x,1/Ts) % play sound, if sound card installedtime=Ts*(0:length(x)-1); % establish time base for plotting
subplot(2,1,1), plot(time,x) % and plot top figuremagx=abs(fft(x)); % take FFT magnitude
ssf=(0:N/2-1)/(Ts*N); % establish freq base for plottingsubplot(2,1,2), plot(ssf,magx(1:N/2)) % plot mag spectrum
specgong.m
find spectrum of the gong sound
(download file) specgong.m
results in the plot shown in
[link] . The top figure shows the time
behavior of the sound as it rises very quickly (when the gongis struck) and then slowly decays over about 1.5 seconds.
The variable
N
defines the window over which the
frequency analysis occurs. The middle plot shows the completespectrum, and the bottom plot zooms in on the low frequency portion
where the largest spikes occur. This sound consists primarilyof three major frequencies, at about 520, 630, and 660 Hz.
Physically, these represent the three largest resonant modesof the vibrating plate.
Notification Switch
Would you like to follow the 'Software receiver design' conversation and receive update notifications?