Convolution in Mat Lab

You might also like

You are on page 1of 5

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! ho are you!"#
name $ input (%lease enter your name en&losed bet'een (uotes #)
d $ date)
ans'er $ * Hello name Today is d ."+)
disp(ans'er#
In Matlab go to File New Script.
This will open a text editor into which you can enter the above program and save as hello.m. When
finished enter save as and note the path. It should be in
C!"ocuments and #ettings!studentid\My "ocuments!M$T%$& and appear in the left hand window of
Matlab.
'ou 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 (normally the path is already set).
Convolution in Matlab
Exercise 1
Convolve the following by hand
x1 =[4 2 6 3 8 1 5];
x2=[3 8 6 5 !];
4 2 6 3 8 1 5
3 8 6 6 !
*
Exercise 2
%og on to Matlab and enter
x1 =[2"4 3"6 #"2 5"3 1"4 4"4 3"6];
x2=[3"1 3"8 5"2 !"! 2"5 4"6];
$=conv%x1&x2'
Write down the output
'ou will note that there are no mar+er arrows in this se,uence, which mean that the se,uences start from
x - ..
/owever if the se,uences do not start at x - . then the problem is a little more complicated.
Consider the following se,uence
x*0n1 2 x30n1 where
x*0n1-43.5 6.7 ..3 8.6 *.5 5.5 6.79

x30n1-4 6.* 6.: 8.3 ;.; 3.8 5.79

<ote the ( in this context means =convolve with>


$s these are different lengths and the starting points are not the first number they need to be padded with
?ero>s as shown below
. . 3.5 6.7 ..3 8.6 *.5 5.5 6.7
6.* 6.: 8.3 ;.; 3.8 5.7 . . .
To solve this we need to pad these values out with ?ero>s to ma+e them all the same length. This can be
solved by writing a M$T%$& function.
3
Exercise 3
Copy the M$T%$& program from the handout and then using this function, plot the se,uences 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,0.7}
and their convolution sum
x1=["8 "8 "8 "8 "8 "8];n1=%2)!';
x2=["! "! "! "! "! "! "! "! "! ];n2=%-4)4';
convste*%x1&n1&x2&n2'
+ave t,e results o- Exercise 1& 2 an. 3 inclu.in/ a screen s,ot o- t,e out0ut -ro* Exercise 3" 1t
s,oul. loo2 li2e t,e one belo3 3,ic, clearl$ s,o3s $our 0at, containin/ $our stu.ent 14" Ensure
t,at $ou ,ave si/ne. o-- t,is assi/n*ent"
6
Convste* 5ro/ra*
% &on,stem.m is for plotting t'o se(uen&es and their &on,olution sum su&h that
% all three plots ha,e the same s&ale of indi&es and the inde- s&ale e-tends from
% the lo'est inde- amongst the three to the highest inde- among the three
function convstem(x1, n1, x2, n2)
y=conv(x1,x2);
% 'e no' need to find the lo'est inde- kmin of the &on,olution sum.
kmin=n1(1)+n2(1);
% e ne-t make the lo'est inde- of the plot e(ual to the lo'est inde- amongst
% -./ -0 and the &on,olution sum.
kmin_plot=min([n1(1) n2(1) kmin]);
% e need to find the highest inde- kma- of the &on,olution sum
kmax=max(n1)+max(n2);
% 'e ne-t make the highest inde-of the plot e(ual to one plus the highest inde-
% amongst -./ -0 and the &on,olution sum. The one is needed sin&e 'e shall later
% assign the last element of ea&h ,e&tor to 1ero and 'e must not destroy any data.
kmax_plot=max([max(n1) max(n2) kmax])+1;
% e ne-t generate a ,e&tor of indi&es that is appropriate to plts of -./ -0 and
% the &on,olution sum.
k_plot=(kmin_plot:kmax_plot);
% 2e-t &ome the plots themsel,es. Ea&h one must be padded 'ith 1eros outside the
% range of the original data.
xx1=[zeos(1,n1(1)!kmin_plot) x1];
xx1(len"t#(k_plot))=$; % This e-tension of the --. ,e&tor by meams of this
% assignment &ommand is 'hy 'e e-tended kma-3plot by one.
xx2=[zeos(1,n2(1)!kmin_plot) x2];
xx2(len"t#(k_plot))=$;
yy=[zeos(1,kmin!kmin_plot) y];
yy(len"t#(k_plot))=$;
% e no' plot --.
su&plot(',1,1)
stem(k_plot,xx1)
% e no'set the limits of the hori1ontal a-is of the plots a,oiding stems on the
% left or right boundaries beyond the lo'est and
% the highest timing inde-. e also redu&e the font si1e and spe&ify a ti&k mark for
% e,ery lag defined by the k3plot ,e&tor.
5
a=len"t#(k_plot)()$; % *#e #oizontal axis +ill &e exten,e, &y t#is muc#
set("ca,-./im-,[kmin_plot ! a kmax_plot + a],-fontsize-,1$,-.*ick-,[k_plot])
xla&el(-n-),yla&el(-x1[n]-)
% e no' plot --0
su&plot(',1,2)
stem(k_plot,xx2)
set("ca,-./im-,[kmin_plot ! a kmax_plot + a],-fontsize-,1$,-.*ick-,[k_plot])
xla&el(-n-),yla&el(-x2[n]-)
% 4inally 'e p*lot the &on,olution sum yy.
su&plot(',1,')
stem(k_plot,yy)
set("ca,-./im-,[kmin_plot ! a kmax_plot + a],-fontsize-,1$,-.*ick-,[k_plot])
xla&el(-/a" k of x2[!n] elative to x1[n], o of x1[!n] elative to x2[n]-)
yla&el(-x1[n]0x2[n]-)
8

You might also like