Fast Fourier Transform in MATLAB: Magnitude of The Complex Amplitude
Fast Fourier Transform in MATLAB: Magnitude of The Complex Amplitude
I
n sound, frequency (pitch) and amplitude (related to volume) are the principle elements
perceived by humans—both of which usually vary with time. Most sounds are composed of
more than one frequency at a time. In music, this is known as a chord. If this were not the
case, then we would be left with those annoying monophonic ringtones from the first cell phones.
You know, the ones where there were only 10 ringtones from which to choose and the odds of
more than one person having that annoying beeping rendition of Für Elise were very high. But I
digress.
In MATLAB, if we plot the amplitude data vector against time we get a plot similar to the one
below.
This plot shows us how long the sound is (2 seconds) and how the amplitude (effectively related
to volume) varies over time. But it also can be very useful to know how each frequency
component of the sound contributes to the overall amplitude. For example, in most music the
melody line has larger amplitude than the other frequency components of the sound. Think of it
as if 3 different instruments played 3 different notes (frequencies) each at a different volume. We
can distinguish which instrument was playing which frequency at what volume using MATLAB.
This can be done by doing a Fourier Analysis of the sound data—a transform from the time
domain to the frequency domain.
y=2*abs(fft(data))/length(data);
From this code, y will be a vector of the magnitudes of each frequency. However, the second half
of this vector contains data not relevant to the course. So the next line we run is:
y = y(1:end/2);
Nyquist Frequency
The frequencies that correspond to these magnitudes can be made by the following line of code
(where Fs is the sampling frequency of the sound).
x=linspace(0,Fs/2,length(y));
These frequencies vary from hertz to Fs/2 Hz. Fs/2 is also known as the Nyquist Frequency—or
the maximum frequency that can be perceived in the sound at a sampling frequency of Fs. In
other words, if you try to record a sound with a frequency of 10 kHz in it at a sampling frequency
of 15 kHz, the sound will not be accurately played back, according to Nyquist’s Theorem.
plot(x,y)
We can modify our plot command above to include a circle around the Principle Frequency:
plot(x,y,f_principle,y_max,’o’)
Summary
All of the above can be difficult to understand. Some of it is even beyond the scope of our course.
However, it provides a basis for understanding frequency domain analysis of sounds in MATLAB.
Below is an outline of what you should “take home” about the FFT:
Note: the sampling frequency for this sound was 44,100 Hz. The Principle Frequency was about
415Hz which is approximately a “G#” note, the loudest note played in this recording of the intro to
“Sweet Child of Mine” by Guns N’ Roses.