Professional Documents
Culture Documents
Laboratory Exercise 2
Signals and Convolution
February 7, 2013
Objective
In this Lab, you will learn how to (i) plot multiple 1-D functions, (ii) read and write audio signals
in .wav format, and (iii) calculate output of a system by convolving the input signal with the
system impulse response function. An application to pulse detection using matched filters is also
discussed in the lab.
Prelab
1 Signal Generation and Plotting
PQ1.1. Generate the following discrete functions using MATLAB. Do not use for loops in your
code. Plot the two functions in the same figure using MATLAB stem and subplot
functions. For x2 [k], plot the real and imaginary parts separately. Therefore, a total of
three plots will be plotted in one figure. Also use MATLAB hold function to plot all
three plots in the same coordinate axis, i.e., all three plots superimposed.
PQ1.2. Are the sequences x1 [k] and x2 [k] periodic? If so, what are their periods? Mark them on
the plots.
PQ1.3. Calculate the total energy of x1 [k] and x2 [k]. Hint: Familiarize yourself with Matlab
functions sum and norm.
The lab is partly based on the lab offered at the University of Alberta. Used with Prof Vicky Hongs permission.
1
2 Convolution
PQ2.1. Consider the following two discrete functions:
{ {
k + 1, 0 k 4, 1 k, 0 k 3,
x[k] = and h[k] =
0, otherwise, 0, otherwise.
Plot the two functions in the same figure using the Matlab stem.
PQ2.2. Using the MATLAB conv function, find the convolution of the two functions. Plot the
convolution output using the stem.
PQ2.3. Verify the result obtained in the previous question using hand calculations. Use the
graphical method discussed in class.
3 Digital Audio
There exist a number of formats for storing audio files. Some of the most popular ones are the
.wav and .mp3 formats. The .wav format (used in typical Audio CD) stores audio signals as
raw audio data, and requires larger storage space, while, the .mp3 format stores audio signals in
compressed form, and requires less storage space. In this lab, you will learn how to read an audio
signal from a .wav file into MATLAB workspace and create an audio signal and save it as .wav
file.
PQ3.1. Generate a 10s long 500Hz signal sampled at rate of Fs = 44100Hz as follows:
k
x[k] = 0.5 sin(1000 ), k = 0, . . . , 10Fs ,
Fs
Using the MATLAB wavwrite function, write the signal into an output audio file. Use
the sampling frequency Fs . Play the output audio signal using a media player. Using the
MATLAB wavread function, read the audio file back into the workspace. Familiarize
yourself with different formats of this command. Take note of the parameters such as
sampling frequency and quantization level.
PQ3.2. Generate a signal x2 [k] by recording every 2nd sample of the original signal. What is the
sampling frequency of the signal x2 [k]? Write this signal into output audio files with the
sampling frequency Fs and the correct sampling frequency. Play both signals through a
media player and note the difference.
PQ3.3. Try repeating this exercise using some other .wav files you may have on your computer.
2
Laboratory Exercise
1 Laboratory Exercise A: Digital Audio
LQ1.1. Locate the audio file song.wav in the lab directory on drive W and copy it to your
working directory. Play the audio signal using the system media player and listen to it
through the headphones. Time the signal duration in seconds.
LQ1.2. Using the MATLAB wavread function, read the above audio file. Take note of the
sampling frequency and quantization level used to encode this signal. Store the signal in
the matrix mz.
LQ1.3. From the size of the matrix mz and its duration, determine the number of samples in
the signal and estimate its sampling frequency. Compare it with the sampling frequency
recorded in the previous question.
LQ1.4. Downsample the file mz by recording every 2nd sample of the original signal and also
every 6th sample of the original signal. What is the sampling frequency of the resulting
signals? Write them to output audio files with these sampling rates. Play the signals
using a media player and note any differences.
LQ2.3. Using the MATLAB conv function, convolve h[k] with the mz signal obtained earlier.
LQ2.4. Export the convolution output as a wav file in the working directory using the same
sampling frequency and play the .wav file using the media player. Comment on the
qualitative difference between the original and resulting signals. How can you explain
the difference?
3
transmitted signal but is delayed. Mathematically, the search is performed by computing the
cross-correlation between the transmitted signal x and the received reflected signal y,
+ +
x(t) = x( )y(t + )d = x( t)y( )d
where means complex conjugate. Note the difference between this definition and the definition
of the matched filter. According to these definitions, the Matlab command to compute samples
of x(t) must be as follows:
>> xhat=xcorr(y,x);
Furthermore, the first (L 1) points of the sequence xhat correspond to negative t. For pulse
detection, we are only interested in positive time, of course. Hence we can safely disregard the
first (L 1) samples of the output sequence.
Given L samples long sequences x and y, the function xcorr produces a (2L 1) samples
long sequence, whose first (L1) points correspond to negative delay time t. For pulse detection,
we are only interested in positive time, of course. Hence we can safely disregard the first (L 1)
samples of the output sequence.
LQ3.1. Locate in the Lab directory on Drive W the file xy.mat and copy it to your working
directory. Load it in Matlab using the command
>> load xy
4
sampled at a sampling rate of 1kHz. Also this will create a noisy returned signal y
sampled at the same rate. The returned signal contains a 1 sec long reflected rectangular
pulse of amplitude 0.1 corrupted by noise. The signals will appear in the workspace as
x and y, respectively.
Plot both signals and note whether the reflected pulse is visible in the plot of y.
LQ3.2. Compute the output of the matched filter using one of the two methods
(a) Using the Matlab functions conv and fliplr; the latter function flips arrays left
to right; or
(b) Directly computing cross correlation between two arrays of data using the Matlab
command xcorr.
Plot the output sequence and comment on the result. Comment on the shape of the
output. Can you determine time between emitting the pulse and receiving the echo
pulse?
Report
Prelab
In no more than one page, answer the following questions:
PQ2.3. Attach the hand calculations for at least three sample points of the convolution. Also
attach the plot obtained in PQ2.2 and mark the results of your calculations in the figure.
PQ3.2. Answer the question about the sampling frequency of the downsampled signal.
Lab Exercise
In no more than one page, answer the following questions:
LQ2.4
LQ3.2
75% of the grade will be awarded for correct answers, and up to 25% will be awarded for the
quality of answers.