You are on page 1of 5

OCE 310 - Basic Ocean Measurements

Filtering Mini-Lab

Filtering Mini-Lab

Objectives

The objective of this lab is to become familiar with filtering by utilizing filtering tools in the
Matlab signal processing toolbox. You will be given several signals to analyze and filter to try
to reconstruct a clean signal. You will also familiarize yourself with using Fourier transforms to
analyze the frequency content of a signal.

Lab Materials
Laptop computer running Matlab and the signal processing toolbox.
Provided signals.

Lab Assignment Tasks

3.1

TASK 1: Familiarize yourself with filtering tools

The Matlab signal processing toolbox has many dierent functions and options for filtering signals.
These functions can be called through a script file or they can be accessed using Matlabs built-in
GUI called fdatool. We will first explore dierent types of filters using the Filter Design and
Analysis Tool (fdatool). Type fdatool into the command line and press enter. The Filter
Design & Analysis Tool GUI will pop-up on the screen. This tool allows you to look at the
frequency response of a variety of types of filters and allows you to change the characteristics of
the filter depending on how you want to use the filter. For example, the buttons on the left allow
you to choose between a lowpass, high pass, bandpass, and bandstop filter. You can specify the
order of the filter, cuto frequencies, sampling frequency, passband and stop band gains, etc. When
you have chosen the parameters defining the filter, you can click the design filter button and the
display will show the frequency response of the filter in Magnitude (dB) vs. Frequency (Hz). The
passband should appear as a flat region with high magnitude, while the stop band will have a rapid
drop o in frequency response.
1. Create a lowpass Butterworth filter, assuming a sampling frequency (Fs) of 1000 Hz, set Fpass
as 30 Hz and Fstop as 200 Hz. Set Apass as 3 and Astop as 6. Choose the filter to have the
minimum order possible. Click design filter to see the frequency response. Why does the
frequency response only show values up to 500 Hz? What is the order of this filter? Does the
shape of the filters magnitude make sense since this is a lowpass filter? Is the slope (rollo)
of the filter steep or gradual in the transition region?
2. The previous filter has a very large transition region (30 to 200 Hz). Change the cuto
frequency (Fstop) to 40 Hz. What happens to the order of the filter? What happens to the
shape of the frequency response? Change the cuto frequency to 31 Hz. What happens?

OCE 310 - Basic Ocean Measurements

Filtering Mini-Lab

3. Try changing the amplitude in the passband and stop band. The passband amplitude refers to
the maximum amount of attenuation allowed in the passband, while the stop band amplitude
refers to the minimum amount of attenuation allowed in the stop band. How does altering
these amplitudes aect the frequency response of the filter?
4. Butterworth filters are designed to allow maximal smoothness in the passband, but because
of this, they tend to have large transition regions. Chebyshev filters are better suited to have
more rapid transition regions. A Chebyshev Type I filter is designed to have no ripple in the
stop band, but will have ripple in the passband. A Chebyshev Type II filter is designed to
have no ripple in the passband, but will have ripple in the stop band. Since we are typically
not concerned with the signal in the stop band for low pass filters, it is common to use a
Chebyshev Type II filter. Set Fpass to 30, Fstop to 40, Apass to 3, and Astop to 80. Try
changing the filter type from Butterworth to Chebyshev type I and Chebyshev type II while
keeping the other parameters the same. How does this change the frequency response?
5. Investigate the phase response of several types of filters. You can do this by clicking on the
phase response graphing button in the top bar of the GUI. Since filters are causal, they will
inherently shift your signal in time. The phase response describes how much your signal will
shift in time for dierent frequencies. Ideally, you would want to design a filter that has little
or no phase shift over the frequency range that you are interested in keeping.

3.2

TASK2: Filter a Noisy Signal

1. Using Matlab, create a sine wave with amplitude 1, a frequency of 1 Hz and a sampling rate
of 1000 Hz, that lasts for 20 seconds.
2. We will first create noise by generating a single frequency noise source. Generate another
sine wave that is the same length as your original sine wave, but has frequency of 60 Hz and
amplitude of 0.1. Create the noisy signal by adding your noise to the original signal.
3. We can view our noise by looking at the spectrum of our signals. The Fast Fourier Transform
(FFT) is an algorithm that computes the discrete Fourier transform of a signal and allows
us to look at the frequency content of a signal. The command in Matlab is called FFT. The
FFT algorithm typically requires the length of the resulting spectrum to be a power of 2 (this
is inherent in the FFT algorithm), therefore, we will define the length of the FFT as NFFT.
Use the following commands to generate the spectrum, g, and frequency, f, assuming a
sampling frequency of fs.
NFFT = 2nextpow2(length(signal));
g = t(signal,NFFT);
f = fs/2*linspace(0,1,NFFT/2+1);
The Fast Fourier Transform defines a signal for frequencies from minus the Nyquist frequency
to plus the Nyquist frequency. Since we are not interested in negative frequencies, you would
only like to plot half of the Fourier transform that you calculate. In addition, the Fast Fourier
Transform computes a complex number (the angle between the real and imaginary parts of
the signal give information about the phase of the signal), so in order to plot g, you want
to look at the absolute value of g (magnitude of g). Plot f vs. g using the following
command:
2

OCE 310 - Basic Ocean Measurements

Filtering Mini-Lab

plot(f,abs(g(1:NFFT/2+1)));
You can now view the spectrum by plottingf vs. g. Change the plotting axes if
necessary to better view your spectrum. Plot the spectrum of the clean signal and the noisy
signal on the same graph (in dierent colors or with dierent line styles). Determine the peak
associated with the noise.
4. Using the fdatool generate a filter that can be used to filter out the noise in this signal.
The filter tool will generate a filter that is defined by coefficients in the denominator (B) and
coefficients in numerator (A). For instance, Matlabs command-line command to generate a
low-pass Butterworth filter is:
[B,A] = butter(N, Wn, low)
where N is the order of the filter and Wn is the cut-o frequency of the filter normalized by
the Nyquist frequency (sampling frequency/2). The fdatool will generate these coefficients
for you however (you dont need to use this command-line function). After you have designed
your filter, you can export the filter to your Matlab workspace or you can save it to a .mat
file. In order to export your filter, go to File and choose Export. Choose the Export
to workspace option or Export to .mat file option, depending on your preference. If your
filter is defined as a Second Order Section, you will want to convert it to a Single Section
filter in order to put in in numerator and denominator format to use for filtering. Before
exporting, you can do this conversion by going to Edit and choosing Convert to single
section.
In order to actually filter a signal, one must use the filter command or filtfilt command,
which convolves the filter with your signal. When you filter a signal using a causal filter,
there is an inherent shift in the phasing of the signal as the filter will cause a phase shift. If
you are not filtering the signal in real time, however, a trick to get rid of this phase shift is
to filter the signal twice. You can generate a filtered signal with phase shift by convolving
the signal with your filter, but you can then take the mirror image of your new signal and
convolve it again with your filter. The result is that the filtered signal will remain unchanged
in the second filtering, except that it will have a phase shift equal and opposite to the original
phase shift, resulting in a filtered signal with no phase shift. This is easily done in Matlab
using the filtfilt command.
Use the filtfilt command to filter your noisy signal using the filter you have designed.
Compare your original signal, noisy signal, and filtered signal on a plot.
5. In real life, noise is not always a single frequency. We will create some broad-band noise
(white noise) by generating a random number that we will add to the signal. Create the noise
using the rand command:
noise = A*(rand(1,length(t))-0.5);
The Matlab command rand generates a random number uniformly distributed between 0
and 1. The above command will shift the number to be between -0.5 and 0.5 and will scale
the number by A. The series of numbers should have the same length as your signal vector.
Define the amplitude of the noise to be 0.3 for now. You can now generate a noisy signal by
adding your sine wave to the randomly generated noise.
6. Look at the frequency spectrum for the new noisy signal. Is it easy to distinguish the frequency
content of the noise?
3

OCE 310 - Basic Ocean Measurements

Filtering Mini-Lab

7. Try using your previous filter to filter the new noisy signal. Compare your original signal,
noisy signal, and filtered signal. Does your filtered signal look anything like your original
signal? Try altering your filter or try using dierent filters to see if you can improve filtering
this new noisy signal.
8. Try changing the amplitude of the randomly generated noise to 0.5 and then try filtering the
signal. How does this change your ability to filter the signal?
9. Signal to noise ratio is very important when analyzing a signal as it describes how large white
noise is relative to your signal. Based on your analysis with adding white noise to the signal,
if you have a signal with a low signal to noise ratio, can you simply filter the signal to remove
the noise?

3.3

TASK 3: What is that ringing?

1. You can find an audio file named tones.mat located in the Resources section on the Sakai
site under the Filter Lab folder, Filtering Discrete Tones document. The file contains my
voice saying something and several single frequency tones that make the recording difficult
to listen to. You can listen to the recording in Matlab using the sound function, however
make sure your speakers are not on full volume as the tones are very annoying. The recording
was made with a sampling frequency of 44100 Hz and the data is recorded in 8 bit format.
You can replay the signal as a sound with the following command:
sound(signal, 44100, 8);
2. Using the same analysis as in TASK 2, plot the spectrum of the signal in order to determine
the frequencies of the noise tones. Create a filter to remove the tones from the signal. Replay
your new filtered sound signal to identify if you have removed the tones. When you are
confident that you have removed the tones, save a .mat file with your filtered signal in it.

3.4

TASK 4: Fun with Fourier and phones (no phones allowed!)

1. An additional audio file named phone.mat is located in the Resources section of the Sakai
site. This file contains an audio recording of me dialing a phone number. Phone number tones
are standard specific combinations of audible frequencies. Using what you have learned above
about using Matlab for spectrum analysis, determine the phone number that I am dialing.

Deliverables

Please answer the following questions and provide appropriate plots in a Mini-lab (homework) style
format.
1. Give a short one paragraph description of what you did in this lab.
2. Please answer all questions from TASKS 1, 2, and 3. Include figures and graphs where
appropriate in answering the questions. Answer TASK 4 as well if you choose to take on the
Bonus question.

OCE 310 - Basic Ocean Measurements

Filtering Mini-Lab

3. For TASK 3, upload your save .mat file to the dropbox on Sakai. Send an e-mail to the
instructor indicating the location of the .mat file (only one file is needed per group). In the
e-mail, also indicate the name of your filtered variable in the workspace.
4. For TASK 4, determine the phone number and describe your process for reaching this conclusion.
5. Upload any matlab script files used in this lab with other uploaded documents in the Sakai
dropbox.

You might also like