Signals and Systems 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 Signal Generat Generation ion and Plottin Plotting g
PQ1.1. Generate the following discrete discrete functions using MATLAB. MATLAB. Do not use for loops in your code. code. Plot the two functions functions in the same same figure figure using MATLAB MATLAB stem and subplot functions functions.. For x 2 [k ], plot the real and imaginary imaginary parts separately separately.. Therefore Therefore,, a total of three plots plots will be plotted in one figure. Also use MATLAB MATLAB hold function to plot all three plots in the same coordinate axis, i.e., all three plots superimposed.
sin(00.05πk 05πk + + π/ π/4) 4) + cos(0. cos(0.2πk) πk ), −20 ≤ k ≤ 80 (i) x1 [k ] = −6 sin( j )k/40e jπk /20 , 0 ≤ k ≤ 200. (ii) x2 [k ] = (−.5 j) PQ1.2. Are the sequences sequences x1[k ] and x2 [k ] periodic? If so, what are their periods? Mark them on the plots. PQ1.3. PQ1.3. Calculate Calculate the total energy of x1 [k ] and x2 [k ]. Hint: Familiari Familiarize ze yourself yourself with with Matlab Matlab functions sum and norm. ∗
The lab is partly based on the lab offered at the University of Alberta. Used with Prof Vicky Hong’s permission.
1
2
Convolution
PQ2.1. Consider the following two discrete functions:
x[k] =
�
k + 1, 0≤k 0, otherwise,
≤
4,
and
h[k] =
�
1 − k, 0, otherwise.
0≤k
≤
3,
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 F s = 44100Hz as follows:
x[k] = 0.5 ∗ sin(1000π
k ), F s
k = 0, . . . , 10F s ,
Using the MATLAB wavwrite function, write the signal into an output audio file. Use the sampling frequency F s . 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 F s 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.
2
Laboratory Exercise B: Convolution
LQ2.1. Consider a system with the following impulse response:
h[k] =
�
(
0.15sinc(0.15(k − 25)) 0.54 − 0.46cos 0,
2πk 50
( ��
, 0≤k
≤
50,
otherwise
LQ2.2. Plot the impulse response h[k]. 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
Laboratory Exercise C: Matched filter.
Radars operate by sending pulse signals and comparing them with an echo signals, which represent a delayed and highly distorted copy of the original pulse signal. In practice, the returned signal is much smaller than the transmitted signal, and is corrupted by noise. The reflected pulse is not visible by the naked eye from the response. However, it can be recovered using a matched filter which searches the response for a signal that looks like the 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τ
−∞
Here t plays the role of the delay time. As can be seen from the above equation, the cross-correlation is essentially a convolution of y and x(−t), the time-reversed version of x . Therefore, the cross-correlation can be computed using the Matlab function conv, by computing the convolution between y and x(−t). Note however that Matlab does not allow arrays with negative indexes. Therefore, instead of x(−t), one has to use a ‘flipped’ signal x(T − t), where T is the period of the ‘sent’ pulse signal (in fact, the duration of the signal x(t)). This essentially means that the result of cross correlation between y and x appears as a convolution operation whose result is delayed. Matlab has a special function for computing cross-correlation, xcorr. Given L-long sequences a and b , xcorr(a,b) produces 2L − 1 long sequence c according to the equation
∑ ∑
L−m+1
c[m + L] =
a[n + m]b [n], m = 1, 2, . . . , L; ∗
n=1 L+m
b[n − m]a [n],
m =
∗
−L + 1, −L + 2, . . . , 0.
n=1
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 ˆ(t) must be as follows: of x ∗
>> 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 (L − 1) 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
This will create a 10sec long ‘sent’ pulse signal,
x(t) =
�
1, 0 ≤ t ≤ 1, 0, 1 < t ≤ 10,
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:
PQ1.2 (attach the plot) and PQ1.3 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:
LQ1.3 and LQ1.4. 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.
5