Unformatted text preview:

Handout 2 – ConvolutionThe conv functionSPHSC 503 – Speech Signal Processing UW – Summer 2006 Handout 2 – Convolution In the lecture, we have seen that we can implement an LTI system by convolving the input sequence with the system’s impulse response, using the convolution sum [] [] [][][kyn hn xnhn kxk∞=−∞=∗=−∑]In this handout, we will study ways to evaluate the convolution sum in Matlab. The conv function Matlab has a built-in conv function that computes the convolution between two sequences. Using the example from the lecture notes: >> h = [1 1]; % define the system’s impulse response >> nh = [0 1]; % define the index associated with h >> x = [0 1 2 0 1 2]; % define the input sequence >> nx = [0 1 2 3 4 5]; % define the index associated with x >> y = conv(h,x) % evaluate the convolution sum y = 0 1 3 2 1 3 2 We observe several things here: • The conv function takes two parameters, namely the two sequences we want to convolve. What about nh and nx? • The conv function does not return a ny vector, the index associated with y. How do we know what ny should be? • The first parameter is the impulse response, and the second parameter is the input sequence. Does it have to be in that order? • The output y is longer than the input x, how come? To answer the first to questions: the conv function has been kept as simple as possible, and it doesn’t keep track of the index vectors associated with the sequences. When both the impulse response and the input sequence start at 0 —as is the case in our example— then the output sequence also starts at 0, and we can define the index for the output ourselves: >> yn = 0:length(y)-1 % define the index associated with y yn = 0 1 2 3 4 5 6 In all other cases, we will need to keep track of the indexes ourselves, as we will do in a few moments. By defining the output index ourselves, we can now plot all sequences, as follows: >> figure >> subplot(1,3,1), stem(nh,h), title(‘h[n]’) >> subplot(1,3,2), stem(nx,x), title(‘x[n]’) >> subplot(1,3,3), stem(ny,y), title(‘y[n] = h[n]*x[n]’) – 1 –SPHSC 503 – Speech Signal Processing UW – Summer 2006 which results in the following figure: 0 5 100123y[n] = h[n]*x[n]0 0.5 100.51h[n]0 2 4 6012x[n] To answer the third question: the order of the parameters doesn’t matter. We can switch them, and still get the same result: >> y = conv(x,h) % evaluate the convolution sum y = 0 1 3 2 1 3 2 The reason that the output sequence is longer than the input sequence is the following. In the convolution sum, the impulse response is shifted over the entire length of the input sequence. As a result, the output sequence will have non-zero samples beyond the last non-zero sample of the input sequence. See also the ‘scaling and shifting’ diagram in today’s lecture notes. The length of the output is always the length of the input sequence plus the length of the impulse response minus 1. Or in Matlab-speak: >> length(x) + length(h) – 1 ans = 7 >> length(y) ans = 7 Keeping track of the index Now what would happen to the convolution in the example above if the sequence x[n] didn’t start at 0, but instead was defined for n=-3,…,2? How would that affect the output sequence? Well, you may recall that the system we are working with here is a linear, time-invariant system. And because it is time-invariant, we know that if the input is shifted by some amount, the output is shifted by the same amount. So if the input shifts from n=0,…,4 to n=-3,…,2, the output will shift from n=0,…,5 to n=-3,…,3: >> nx = [0:5]-3 % redefine the index associated with x nx = -3 -2 -1 0 1 2 >> y = conv(h,x) % evaluate the convolution sum y = 0 1 3 2 1 3 2 >> ny = [0:6]-3; % redefine the index associated with y 0 0.5 100.51h[n]-4 -2 0 2012x[n]-4 -2 0 2 40123y[n] = h[n]*x[n] – 2 –SPHSC 503 – Speech Signal Processing UW – Summer 2006 Or what if the impulse response shifted from n=0,1 to n=4,5? That’s a little harder, but we can use the fact that we can switch the role of the impulse response and the input sequence and still get the same result out of the convolution. So a shift of the impulse response also causes a similar shift in the output sequence. >> nx = [0:5]-3; % redefine the index associated with x >> nh = [0 1]+4; % AND redefine the index associated with h >> y = conv(h,x) % evaluate the convolution sum y = 0 1 3 2 1 3 2 >> ny = [0:6]-3+4 % redefine the index associated with y ny = 1 2 3 4 5 6 7 0 2 4 6 80123y[n] = h[n]*x[n]-4 -2 0 2012x[n]4 4.5 500.51h[n] Exercise 2.1 Download the file conv_m.m from the class website and save it in C:\temp\SPHSC503\. Change the current directory in Matlab to C:\temp\SPHSC503\. This m-file contains the function conv_m, which is a modified version of the conv function that keeps track of the index vectors. You can use it as follows: >> [y,ny] = conv_m(x,nx,h,nh) Type help conv_m for details. Use the conv_m function to determine the convolution between the input sequence x[n]={1, 1, 1, 1, 1} for n=0,…,4 and each of the following systems. Plot the output sequences using stem. a. h[n]={1} for n={0} b. h[n]={0,1} for n={0,1} c. h[n]={1,0} for n={-1,0} d. h[n]={1,-1} for n={0,1} Exercise 2.2 Download the ex1_3.wav file from the class website, and save it in C:\temp\SPHSC503\. Change the current directory in Matlab to C:\temp\SPHSC503\. This WAV-file contains a speech signal. a. Load the ex1_3.wav file into Matlab. You can either use Matlab’s Import Wizard, by double-clicking on the filename in Matlab’s current directory window, or use the wavread command (see help wavread for details). You will need to load both the signal’s samples, as well as its sampling rate. – 3 –SPHSC 503 – Speech Signal Processing UW – Summer 2006 b. Play the speech file in Matlab, using the soundsc command. If you’ve stored the signal’s samples in a vector called y, and its sampling frequency in a variable called fs, you would use the command >> soundsc(y,fs) c. Convolve the speech signal with the impulse response h[n]={-1,2,-1}. Don’t worry about the index vectors here. Store the result in a new variable, say y1.


View Full Document

UW SPHSC 503 - Convolution

Download Convolution
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Convolution and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Convolution 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?