Lab No. 5
Lab No. 5
S O U R C E C O D E
clc;
clear;
close all;
subplot(3,1,1);
stem(n, x, 'filled');
title('Original Sequence');
xlabel('n');
ylabel('x[n]');
grid on;
% Plot the even component
subplot(3,1,2);
title('Even Component');
xlabel('n');
ylabel('x_{even}[n]');
grid on;
subplot(3,1,3);
title('Odd Component');
xlabel('n');
ylabel('x_{odd}[n]');
grid on;
Task No.2:
You should have noticed that ‘conv’ command calculates convolution assuming
both input sequences are starting from origin (i-e no values on –ve t-axis). This is not always the
case, we do have sequences which have values for t.
Code:
function [y, ny] = conv_m(x1, n1, x2, n2)
y = conv(x1, x2);
End.
Example Code:
clc; clear; close all;
% Display results
disp('Convolution Result:');
disp(y);
disp('Resulting Indices:');
disp(ny);
% Plot results
stem(ny, y, 'filled');
xlabel('n');
ylabel('y[n]');
grid on;
Plot:
Task No.3:
Convolve following sequences using MATLAB Function “conv” and “conv_m”
and plot the input, impulse response and output in one figure using “subplot”: x[n] = [1 2 1],
n=[0 1 2] h[n] = [1 1 1], n= [0 1 2] x[n] = [-1 4 -3 -2 1 0 2], n=[-2:4] h[n] = [1 1 1], n= [-1 0
1].
Code:
clc; clear; close all;
y1 = conv(x1, h1);
y2 = conv(x2, h2);
figure;
subplot(3,2,1);
xlabel('n'); ylabel('x_1[n]');
grid on;
subplot(3,2,3);
stem(n_h1, h1, 'filled');
xlabel('n'); ylabel('h_1[n]');
grid on;
subplot(3,2,5);
xlabel('n'); ylabel('y_1[n]');
grid on;
subplot(3,2,2);
xlabel('n'); ylabel('x_2[n]');
grid on;
subplot(3,2,4);
xlabel('n'); ylabel('h_2[n]');
grid on;
subplot(3,2,6);
xlabel('n'); ylabel('y_2[n]');
Plot:
Task No.4:
Write a function convolution in MATLAB that performs 1D linear convolution by
the steps mentioned above. You can use any method.
Code:
function y = my_conv(x, h)
L = length(x);
M = length(h);
N = L + M - 1; % Output length
% Compute convolution
for n = 1:N
end
end
End