0% found this document useful (0 votes)
2 views

Linear Convolution-1

Linear convolution program

Uploaded by

BACHU AKSHITHA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Linear Convolution-1

Linear convolution program

Uploaded by

BACHU AKSHITHA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Linear Convolution(without buitin function)

clc;
clear;

% Input sequences
x = input('Enter the first sequence x[n]: ');
h = input('Enter the second sequence h[n]: ');

% Manual computation of linear convolution


lx = length(x);
lh = length(h);
ly = lx + lh - 1; % Length of the resulting sequence
y = zeros(1, ly); % Initialize result with zeros

% Perform linear convolution manually


for n = 1:ly
for k = 1:lx
if (n - k + 1 > 0) && (n - k + 1 <= lh)
y(n) = y(n) + x(k) * h(n - k + 1);
end
end
end

% Display the result from manual implementation


disp('Linear convolution without using built-in functions:');
disp(y);

% Plot the sequences and the results


figure;

% Plot x[n]
n = 0:lx-1;
subplot(3, 1, 1);
stem(n, x, 'filled');
title('Input Sequence x[n]');
xlabel('n');
ylabel('x[n]');
grid on;

% Plot h[n]
n = 0:lh-1;
subplot(3, 1, 2);
stem(n, h, 'filled');
title('Input Sequence h[n]');
xlabel('n');
ylabel('h[n]');
grid on;

% Plot linear convolution result


n = 0:ly-1;
subplot(3, 1, 3);
stem(n, y, 'filled');
title('Linear Convolution Result y[n]');
xlabel('n');
ylabel('y[n]');
grid on;

Linear Convolution(with buitin function)

clc;

clear;

% Input two sequences

x = input('Enter the first sequence x[n]: ');

h = input('Enter the second sequence h[n]: ');

% Calculate linear convolution using built-in function

y = conv(x, h);

lx = length(x);
lh = length(h);
ly = lx + lh - 1; % Length of the resulting sequence

% Plot the sequences and the results


figure;

% Plot x[n]
n=0:lx-1;
subplot(3, 1, 1);
stem(n, x, 'filled');
title('Input Sequence x[n]');
xlabel('n');
ylabel('x[n]');
grid on;

% Plot h[n]
n=0:lh-1;
subplot(3, 1, 2);
stem(n, h, 'filled');
title('Input Sequence h[n]');
xlabel('n');
ylabel('h[n]');
grid on;

% Plot linear convolution result


n=0:ly-1;
subplot(3, 1, 3);
stem(n, y, 'filled');
title('Linear Convolution Result y[n]');
xlabel('n');
ylabel('y[n]');
grid on;

You might also like