LAB 5 Filtering Periodic Signals PDF
LAB 5 Filtering Periodic Signals PDF
LAB 5 Filtering Periodic Signals PDF
UW EE235 TA UW EE235 TA
This work is produced by The Connexions Project and licensed under the
Creative Commons Attribution License ∗
Abstract
This development of these labs was supported by the National Science Foundation under Grant No.
DUE-0511635. Any opinions, conclusions or recommendations expressed in this material are those of the
authors and do not necessarily reect the views of the National Science Foundation.
1 Introduction
In this lab, we will look at the eect of ltering signals using a frequency domain implementation of an LTI
system, i.e., multiplying the Fourier transform of the input signal with the frequency response of the system.
In particular, we will lter sound signals, and investigate both low-pass and high-pass lters. Recall that a
low-pass lter lters out high frequencies, allowing only the low frequencies to pass through. A high-pass
lter does the opposite.
All of the sounds for this lab can be downloaded from the Sound Resources page. 1
When working in MATLAB, the continuous-time Fourier transform cannot be done by the computer exactly,
but a digital approximation is done instead. The approximation uses the discrete Fourier transform (more
on that in EE 341). There are a couple important dierences between the discrete Fourier transforms on the
computer and the continuous Fourier transforms you are working with in class: nite frequency range and
discrete frequency samples. The frequency range is related to the sampling frequency of the signal. In the
example below, where we nd the Fourier transform of the "fall" signal, the sampling frequency is Fs=8000,
∗ http://creativecommons.org/licenses/by/2.0/
1 "Sound resources" <http://cnx.org/content/m13854/latest/>
http://cnx.org/content/m14481/1.8/
Connexions module: m14481 2
so the frequency range is [-4000,4000] Hz (or 2*pi times that for w in radians). The frequency resolution
depends on the length of the signal (which is also the length of the frequency representation).
The MATLAB command for nding the Fourier transform of a signal is fft (for Fast Fourier Transform
(FFT)). In this class, we only need the default version.
The fft command in MATLAB returns an uncentered result. To view the frequency content in the same
way as we are used to seeing it in class, you need to plot only the rst half of the result (positive frequencies
only) OR use the MATLAB command fftshift which toggles between centered and uncentered versions of
the frequency domain. The code below will allow you to view the frequency content both ways.
N = length(x);
pfreq = [0:N/2]*Fs/N;
Xpos=X(1:N/2+1);
plot(pfreq,abs(Xpos));
figure;
freq = [-(N/2-1):N/2]*Fs/N;
plot(freq,abs(fftshift(X)));
Note that we are using abs in the plot to view the magnitude since the Fourier transform of the signal is
complex valued. (Type X(2) to see this. Note that X(1) is the DC term, so this will be real valued.)
Try looking at the frequency content of a few other signals. Note that the fall signal happens to have an
even length, so N/2 is an integer. If the length is odd, you may have indexing problems, so it is easiest to
just omit the last sample, as in x=x(1:length(x)-1);.
After you make modications of a signal in the frequency domain, you typically want to get back to the
time domain. The MATLAB command ifft will accomplish this task.
xnew = real(ifft(X));
You need the real command because the inverse Fourier transform returns a vector that is complex-valued,
since some changes that you make in the frequence domain could result in that. If your changes maintain
complex symmetry in the frequency domain, then the imaginary components should be zero (or very close),
but you still need to get rid of them if you want to use the sound command to listen to your signal.
4 Low-pass Filtering
http://cnx.org/content/m14481/1.8/
Connexions module: m14481 3
This low-pass lter can be implemented in MATLAB using what we know about the Fourier transform.
Remember that multiplication in the Frequency domain equals convolution in the time domain. If our signal
and lter are both in the frequency domain, we can simply multiply them to produce the result of the
system.
y (t) = x (t) ∗ h (t)
Y (ω) = X (ω) H (ω)
Below is an example of using MATLAB to perform low-pass ltering on the input signal x with the FFT
and the lter denition above.
The cuto of the low-pass lter is dened by the constant a. The low-pass lter equation above denes
the lter H in the frequency domain. Because the denition assumes the lter is centered around w = 0, the
vector w is dened as such.
load fall %load in the signal
x = fall;
X = fft(x); % get the Fourier transform (uncentered)
N = length(X);
a = 100*2*pi;
w = (-N/2+1:(N/2)); % centered frequency vector
H = a ./ (a + i*w); % centered version of H
plot(w*Fs/N,abs(H))
The plot will show the form of the frequency response of a system that we are used to looking at, but we
need to shift it to match the form that the fft gave us for x.
Note: If you are having problems multiplying vectors together, make sure that the vectors are
the exact same size. Also, even if two vectors are the same length, they may not be the same size.
For example, a row vector and column vector of the same length cannot be multiplied element-wise
unless one of the vectors is transposed. The ' operator transposes vectors/matrices in MATLAB.
Now that we have the output of the system in the frequency domain, it must be transformed back to the
time domain using the inverse FFT. Play the original and modied sound to see if you can hear a dierence.
Remember to use the sampling frequency Fs.
y = real(ifft(Y));
sound(x, Fs) % original sound
sound(y, Fs) % low-pass-filtered sound
The lter reduced the signal amplitude, which you can hear when you use the sound command but not
with the soundsc which does automatic scaling. Replay the sounds with the soundsc and see what other
dierences there are in the ltered vs. original signals. What changes could you make to the lter to make
a greater dierence?
Note: Sometimes, you may want to amplify the signal so that it has the same height as the
original, e.g., for plotting purposes.
http://cnx.org/content/m14481/1.8/
Connexions module: m14481 4
y = y * (max(abs(x))/max(abs(y)))
Exercise 1
Low-pass Filtering with Sound Use wavread to load the sound castanets44m.wav. Perform low-pass
ltering with the lter dened above, starting with a = 500*2*pi, but also try dierent values.
Play the original and the low-passed version of the sound. Plot their frequency content (Fourier
transforms) as well.
Exercise 2 OPTIONAL: Low-pass Filtering
1. Create an impulse train as the input signal x(t) using the following MATLAB command,
2. Use the low-pass lter dened earlier to low-pass the impulse train. Choose a cuto of 20.
3. Plot the two signals x(t) and y(t) separately using the subplot command. These should be
plotted versus the time vector. Label the axes and title each graph appropriately.
4. Look at the plots. Can you explain what is happening to the spike train?
5 High-pass Filtering
A real high-pass lter typically has low but non-zero values for |HL (ω) | at low frequencies, and a gradual
(rather than an immediate) rise in magnitude as frequency increases. The simplest (and least eective)
high-pass lter is given by (e.g. using an RC circuit):
α
HH (ω) = 1 − HL (ω) = 1 − , α = cuto frequency.
α + jω
This lter can be implemented in the same way as the low pass lter above.
Exercise 3
High-pass Filtering with Sound The high-pass lter can be implemented in MATLAB much the
same way as the low-pass lter. Perform high-pass ltering with the lter dened above on the
sound castanets44m.wav. Start with a = 2000*2*pi, but also try dierent values.
Play the original and the high-passed version of the sound. The ltered signal may be to be
scaled so that both have the same range on the Y-axis. Plot their frequency responses as well.
6 Sound Separation
http://cnx.org/content/m14481/1.8/
Connexions module: m14481 5
• Kick'n Retro 235 Inc. recorded a session of a trumpet and drum kit together for their new
release. The boss doesn't like the bass drum in the background and wants it out. Unfortu-
nately, there was a malfunction in the mixing board and instead of having two separate tracks
for the drums and the trumpet, the sounds mixed together in one track. In order to get this
release out on time you will have to use some ltering to eliminate the bass drum from the
sound. There is not enough time to bring the drummer and trumpet player back in the studio
to rerecord the track.
• Click here to download the mixed.wav sound (Fs = 8000 Hz). The mixed sound is created
2
2 http://cnx.org/content/m14481/latest/mixed.wav
http://cnx.org/content/m14481/1.8/