Error in Linear Convolution code MATLAB

7 views (last 30 days)
I am trying to run this code :
Question : Linear convolution and circular convolution of two sequences.
%%Linear convolution of two sequences.
clc; %clears the console window
clear all; %deletes the user defined variable in variable browser
close all; %close the figure window
x=input('Enter the 1st sequence : ');
nx=input('Enter the time index sequence : ');
h=input('Enter the 2nd sequence : ');
nh=input('Enter the time index sequence : ');
[y,ny]=findconv(x,nx,h,nh);
figure; subplot(3,1,1);
stem(nx,x);
xlabel('Time');
ylabel('Amplitude');
title('1st sequence');
subplot(3,1,2);
stem(nh,h);
xlabel('Time');
ylabel('Amplitude');
title('2nd sequence');
subplot(3,1,3);
stem(ny,y);
xlabel('Time');
ylabel('Amplitude');
title('Linear convolution');
disp(y);
disp(ny);
function [y,ny]=findconv(x,nx,h,nh)
nybegin=nx(1)+nh(1);
nyend=nx(length(nx))+nh(length(nh));
ny=nybegin:nyend;
%y=conv(x,h); %calling inbuilt function
y=calcconv(x,h)
end
function [y] = calcconv(x,h)
l1=length(x);
l2=length(h);
N = l1+l2-1;
%length of linear convolution
%y=linear convolution of x[n] and h[n]
%note: in matlab index starts with 1 and not 0
for n=1:1:N
y(n)=0;
for k=1:1:l1
if(n-k+1>=1 & n-k+1<=l2) % to avoid negative index
y(n)=y(n)+x(k)*h(n-k+1);
end
end
end
end
Everythings seemed runnning flawlessly until I got this error -
P.S : I am running this file on MATLAB live script
  1 Comment
Dyuman Joshi
Dyuman Joshi on 4 Feb 2023
Edited: Dyuman Joshi on 4 Feb 2023
From the error, the lengths of y and ny must not be not equal.
What is a sample input to x, nx, h and nh? And please copy paste the full error message.
Also, what is the logic behind calculating the values y and ny?
Additionally, instead of
y(n)=0;
do this
N = l1+l2-1;
y=zeros(1,N);
for
...
end

Sign in to comment.

Accepted Answer

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 4 Feb 2023
Edited: Sulaymon Eshkabilov on 4 Feb 2023
Hi,
Here is the corrected code. Note also two variable names are changed to avoid confusion l1 to L1 and l2 to L2.
%%Linear convolution of two sequences.
clc; %clears the console window
clearvars; %deletes the user defined variable in variable browser
close all; %close the figure window
x=input('Enter the 1st sequence : ');
nx=input('Enter the time index sequence : ');
h=input('Enter the 2nd sequence : ');
nh=input('Enter the time index sequence : ');
%% This Example simulated here
% x = randi([-2, 4], 1, 7);
% nx = 1:length(x);
% h = randi([-2, 0], 1,7);
% nh=1:length(h);
%%
[y,ny]=findconv(x,nx,h,nh);
y = 1×13
0 -2 -3 0 -1 2 -5 -1 2 -1 4 -6 4
figure; subplot(3,1,1);
stem(nx,x);
xlabel('Time');
ylabel('Amplitude');
title('1st sequence');
subplot(3,1,2);
stem(nh,h);
xlabel('Time');
ylabel('Amplitude');
title('2nd sequence');
subplot(3,1,3);
stem(ny,y);
xlabel('Time');
ylabel('Amplitude');
title('Linear convolution');
disp(y);
0 -2 -3 0 -1 2 -5 -1 2 -1 4 -6 4
% compare the computed y with conv(x, h)
disp(conv(x,h))
0 -2 -3 0 -1 2 -5 -1 2 -1 4 -6 4
disp(ny);
2 3 4 5 6 7 8 9 10 11 12 13 14
function [y,ny]=findconv(x,nx,h,nh)
nybegin=nx(1)+nh(1);
nyend=nx(length(nx))+nh(length(nh));
ny=nybegin:nyend;
%y=conv(x,h); %calling inbuilt function
y=calcconv(x,h)
end
function y = calcconv(x,h)
L1=length(x);
L2=length(h);
N = (L1+L2)-1;
%length of linear convolution
%y=linear convolution of x[n] and h[n]
%note: in matlab index starts with 1 and not 0
%y = zeros(1, N);
for n=1:N
y(n)=0;
for k=1:L1
if(n-k+1>=1 && n-k+1<=L2) % to avoid negative index
y(n)=y(n)+x(k)*h(n-k+1);
end
end
end
end

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!