Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

ASSIGMENT SIGNAL AND SYSTEM

3. For two discrete-time signals x[n] and h[n] that are both of finite length, you
can implement the convolution operation y[n] = x[n] * h[n] in Matlab using the
command conv.The resulting Matlab array that holds the values of y[n] will
have a length equal to the length of x[n] plus the length of h[n] minus one.

Here is an example problem: suppose that

x[n] = [n] – 2[n − 1] + 3[n − 2] – 4[n − 3] + 2[n − 5] + [n − 6]

and

h[n] = 3[n] + 2[n − 1] + [n − 2] − 2[n − 3] + [n − 4] − 4[n − 6] +


3[n − 8].

The problem is to use Matlab to compute the convolutions


y1 = x[n] *h[n] and y2[n] = x[n − 3] * h[n].

Here is the solution for the example problem:

The signal x[n] “goes” from n = 0 to n = 6, so the shifted signal x[n − 3] “goes”
from n = 0 to n = 9. This means that we need a Matlab array with 10 elements
to hold the values of x[n − 3] (for n = 0, 1, . . . , 9). To make the plotting easy,
we will store both x[n] and x[n − 3] in 10- element Matlab arrays. Since h[n]
“goes” from n = 0 to n = 8, we will need a nine-element Matlab array to hold
the values of h[n]. Each convolution result will have a length of 10 + 9 − 1 =
18, corresponding to the “times” n = 0, 1, . . . , 17.

Here is sample Matlab code to compute the two convolutions and plot them
both in the same figure using the subplot command:

x = [1 -2 3 -4 0 2 1 0 0 0]; % Input signal zero-padded to length 10


xm3 = [0 0 0 1 -2 3 -4 0 2 1]; % Shifted input signal x[n-3]

xm3 = [zeros(1,3) x(1:7)]; % An easier way to make x[n-3],same result

h = [3 2 1 -2 1 0 -4 0 3]; % Impulse response


y1 = conv(x,h); % Make y1[n] = x[n] * h[n]
y2 = conv(xm3,h); % Make y2[n] = x[n-3] * h[n]
n = 0:17; % "time" values for x-axis of plot
subplot(2,1,1); % Make a 2x1 array of graphs and make % the first graph the
"current" one

stem(n,y1); % Plot y1 against n


title(’y_1[n] = x[n] * h[n]’); % Title for top graph
xlabel(’n’); % X-axis label for top graph
ylabel(’y_1[n]’); % Y-axis label for top graph
subplot(2,1,2); % Select the second graph as "current"

stem(n,y2); % Plot y2 against n


title(’y_2[n] = x[n-3] * h[n]’); % Title for bottom graph
xlabel(’n’); % X-axis label for bottom graph
ylabel(’y_2[n]’); % Y-axis label for bottom graph

Now it’s your turn:


(a) Plot h1[n], h2[n], and x[n] together using the subplot function.

n = -8:8;
h1n = [zeros(1,8),1 1 -1 -1 zeros(1,5)];
for i=1:length(n)
%populating h1[n]
if n(i)>=-3 && n(i)<3
h2n(i) = (1/2)^n(i);
else
h2n(i) = 0;
end
%populating x[n]
if n(i)>0 && n(i)<6
xn(i) = (1/4)^n(i);
else
xn(i) = 0;
end
end
%plotting for part a
figure(1);
subplot(3,1,1);
stem(n,h1n);
subplot(3,1,2);
stem(n,h2n);
subplot(3,1,3);
stem(n,xn);
%part b
vn = conv(xn,h1n);
yn =conv(vn,h2n);
%plotting for part b
(b) Consider a system H formed from the series connection of H1 and H2,
where x[n] is input to H1, the output v[n] of H1 is input to H2, and the output of
H2 is y[n]. Use the conv function to find v[n] and y[n]. Plot v[n] and y[n] using
the subplot function.

subplot(211);
stem(-8:24,vn);
subplot(212);
stem(-8:40,yn);
%part c
vn = conv(xn,h2n);
yn =conv(vn,h1n);
%plotting for part c
(c) Now assume that the order of the systems is reversed, so that x[n] is input
to H2, the output v[n] of H2 is input to H1, and y[n] is the output of H1. Plot
v[n] and y[n]. Briefly explain why v[n] is different in parts (b) and (c), whereas
y[n] is the same in both parts.

subplot(211);
stem(-8:24,vn);
subplot(212);
stem(-8:40,yn);

You might also like