Download Convolution in Matlab

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Mathematics of radio engineering wikipedia , lookup

Addition wikipedia , lookup

Sequence wikipedia , lookup

Series (mathematics) wikipedia , lookup

Transcript
Convolution in MATLAB
Convolve the following by hand
x1 =[4 2 6 3 8 1 5];
x2=[3 8 6 9 6 7];
4
2
6
3
8
1
3
8
6
9
6
7
5
Log on to MATLAB and enter:
x1 =[2.4 3.6 0.2 5.3 1.4 4.4 3.6];
x2=[3.1 3.8 5.2 7.7 2.5 4.6];
y=conv(x1,x2)
Write down the output
1
You will note that there are no marker arrows in this sequence, which mean that the sequences start from
x = 0.
However if the sequences do not start at x = 0 then the problem is a little more complicated.
Consider the following sequence:
x1[n] * x2[n] where:
x1[n]={2.4 3.6 0.2 5.3 1.4 4.4 3.6}
x2[n]={ 3.1 3.8 5.2 7.7 2.5 4.6}
Note the * in this context means ‘convolve with’
In this case x1[n] starts at-2 and x2[n] starts at -4.It appears that the commands must be modified to
accommodate this. Now consider the following sequences:
x1 =[4 2 6 3 8 1 5];n1=(-2:4);
x2=[3 8 6 9 5 7]; n1=(-4:1);
i.e. in sequence x1 time zero is 6 or at position n-2 and in sequence x2 time zero is the number 5 or
position n-4. Looking at the n1=(-2:4); and n1=(-4:1); commands it should be clear that the first number
in the brackets in each case is the position in the array and the second number, the number of digits to
follow. i.e in n1=(-2:4); the -2 is position n-2 (6) and there are 4 numbers to follow (3, 8, 1 and 5)
In general the lowest timing index of the convolution sum is the sum of the lowest timing indices of
x1[n] and x2[n], while the highest timing index is the sum of the highest timing indices of x1[n] and
x2[n]. These can be found automatically with MATLAB by:
kmin=n1(1)+n2(1); % the first value in each vector
kmax=max(n1)+max(n2); %the maximum value in each vector
We can next evaluate and display the convolution sum whilst simultaneously generating and displaying
an array of its samples indices. This is done using the command
y=conv(x1,x2), k=(kmin:kmax)
This last result can be plotted using the command stem(k,y). However it can be useful to see not only the
convolution sum but also the input and transfer function. This may be achieved with;
subplot(3, 1, 1)
stem(n1, x1)
subplot(3, 1, 2)
stem(n2, x2)
subplot(3, 1, 3)
stem(k, y)
2
Unfortunately, since x1[n], x2[n] and y[n] are different lengths the scales are different for the three plots.
To solve this we need to pad these values out with zero’s to make them all the same length. This can be
solved by writing a MATLAB function.
M-Files
Rather than writing and executing commands one line at a time, it is very common to write a complete
program using a text editor and then run it as a single entity.
e.g.
%The program in file HELLO.M greets you and asks for your name.
%Then greets you by name and tells you the date.
disp(‘Hello! Who are you?’)
name – input (‘Please enter your name enclosed between quotes ‘);
d = date;
answer = [ ‘Hello ‘ name ‘ Today is ‘ d ‘.’];
disp(answer)
Type in the above and then save as hello.m
You may now need to set a new path to the directory you are using. This may be done under file/set path
and select add folder then navigate to the folder you are using.
Using convstem
Copy the MATLAB program from the handout and then using this function, plot the sequences for
x1[n] = {0, 0, .8, .8, .8, .8, .8, .8, }
x2[n] = { 0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7}
and their convolution sum
x1=[.8 .8 .8 .8 .8 .8];n1=(2:7);
x2=[.7 .7 .7 .7 .7 .7 .7 .7 .7 ];n2=(-4:4);
convstem(x1,n1,x2,n2)
Save the results and ensure that you have signed off this assignment.
3