Exp 3
Exp 3
Experiment-3
Objective: Explain the implementation of linear convolution as an operation to analyze
discrete time LTI system using MATLAB functions.
Theory: Consider two finite duration sequences x (n) and h (n), the duration of x (n) is
n1 samples in the interval 0 n (n1 −1) . The duration of h (n) is n2 samples; that is h
(n) is non – zero only in the interval 0 n (n2 −1) . The linear or a periodic convolution
of x (n) and h (n) yields the sequence y (n) defined as,
Step1: Choose an initial value of n, the starting time for evaluating the output sequence
y (n). If x (n) starts at n=n1 and h (n) starts at n= n2 then n = n1+ n2-1 is a good
Choice.
Step3: Fold h (m) about m=0 to obtain h (-m) and shift by n to the right if n is positive and
left if n is negative to obtain h (n-m).
Step4: Multiply two sequences x (n-m) and h (m) element by element and sum the
products to get y (n).
Step5: Increment the index n, shift the sequence x (n-m) to right by one sample and repeat
Step4.
Step6: Repeat step5 until the sum of products is zero for all remaining values of n.
23102107 Saniya Walavalkar
IN LAB EXERCISES:
Q1
a)
x = [1, 3, -2, 1];
h = [1, 1, 0, 0];
n_start = 0;
n_length = length(x) + length(h) - 1;
y = zeros(1, n_length);
for n = 1:n_length
y(n) = 0;
for m = 1:length(h)
if (n-m+1 >= 1)&& (n-m+1 <= length(x))
y(n) = y(n) + x(n-m+1) * h(m);
End end end
disp('Convolution of x[n] and h[n]:');
disp(y);
stem(0:n_length-1,y,'filled','red');
grid on;
title("23102107")
xlabel("n --->");
ylabel("y --------->");
OUTPUT:
Convolution of x[n] and h[n]:
1 4 1 -1 1 0 0
b)
x = [1, 1, 1, 0 ,0 , 0];
h = [0.5, 2, 2.5, 1];
n_start = 0;
n_length = length(x) + length(h) - 1;
y = zeros(1, n_length);
for n = 1:n_length
y(n) = 0;
for m = 1:length(h)
if (n-m+1 >= 1) && (n-m+1 <= length(x))
y(n) = y(n) + x(n-m+1) * h(m);
end End end
disp('Convolution of x[n] and h[n]:');
disp(y);
y1 = conv(x, h);
disp('Convolution of x[n] and h[n] using conv:');
disp(y1);
stem(0:n_length-1,y,'filled','red');
grid on;
title("23102107")
xlabel("n --->");
ylabel("y --------->");
OUTPUT :
Convolution of x[n] and h[n]:
0.5000 2.5000 5.0000 5.5000 3.5000 1.0000 0 0
0
C)
a = 0.5;
n = 0:20;
x = ones(1, length(n));
h = exp(-a * n);
n_start = 0;
n_length = length(x) + length(h) - 1;
y = zeros(1, n_length);
for n = 1:n_length
y(n) = 0;
for m = 1:length(h)
if (n-m+1 >= 1) && (n-m+1 <= length(x))
y(n) = y(n) + x(n-m+1) * h(m);
end
end
end
disp('Convolution of x[n] and h[n]:');
disp(y);
y1 = conv(x, h);
disp('Convolution of x[n] and h[n] using conv:');
disp(y1);
stem(0:n_length-1, y, 'filled', 'red');
grid on;
title('23102107');
xlabel('n --->');
ylabel('y[n] --------->');
Output:
Columns 18 through 34
Columns 35 through 41
Columns 18 through 34
Columns 35 through 41
Q2
f1 = [1, 0, 0, 0, 0];
f2 = [1, 1, 1];
f3 = [1, 1, 1, 0, 0];
y1 = conv(f1, f2);
y2 = conv(f2, f1);
disp("(a) f1[n]*f2[n]=f2[n]*f1[n]")
disp(y1);
disp(y2);
max_len = max(length(f2), length(f3));
f2padded = padarray(f2, [0, max_len - length(f2)], 'post');
f3padded = padarray(f3, [0, max_len - length(f3)], 'post');
f2plusf3 = f2padded + f3padded;
disp("(b) f1[n]*(f2[n]+f3[n])= f1[n]*f2[n] + f1[n]*f2[n]")
23102107 Saniya Walavalkar
disp(conv(f1, f2plusf3));
y3=conv(f1,f2);
y4=conv(f1,f3);
max_len = max(length(y3), length(y4));
y3padded = padarray(y3, [0, max_len - length(y3)], 'post');
y4padded = padarray(y4, [0, max_len - length(y4)], 'post');
y3plusy4 = y3padded + y4padded;
disp(y3plusy4);
disp("(c) f1[n]*(f2[n]*f3[n])=(f1[n]*f2[n])*f3[n]")
Y=conv(f2,f3);
disp(conv(y1,f3));
disp(conv(f1,Y));
disp("23102107");
Output:
(a) f1[n]*f2[n]=f2[n]*f1[n]
1 1 1 0 0 0 0
1 1 1 0 0 0 0
2 2 2 0 0 0 0 0 0
(c) f1[n]*(f2[n]*f3[n])=(f1[n]*f2[n])*f3[n]
1 2 3 2 1 0 0 0 0 0 0
1 2 3 2 1 0 0 0 0 0 0
23102107
Q1
n = 0:20;
x = [1, zeros(1, length(n)-1)];
h1 = ones(1, length(n));
h2 = [ones(1, 5), zeros(1, length(n)-5)];
w = conv(x, h1);
y1 = conv(w, h2);
g = conv(h1, h2);
y2 = conv(x, g);
subplot(3,1,1);
stem(0:length(y1)-1, y1, 'filled');
title('y[n] = w[n] * h2[n] (Part a) - 23102107');
xlabel('n');
ylabel('y[n]');
subplot(3,1,2);
stem(0:length(y2)-1, y2, 'filled');
title('y[n] = x[n] * g[n] (Part b) - 23102107');
23102107 Saniya Walavalkar
xlabel('n');
ylabel('y[n]');
subplot(3,1,3);
stem(0:length(g)-1, g, 'filled');
title('g[n] = h1[n] * h2[n] - 23102107');
xlabel('n');
ylabel('g[n]');
LEARNING OUTCOMES :