Error in Linear Convolution code MATLAB
15 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Achyuth S.S
am 4 Feb. 2023
Kommentiert: Sulaymon Eshkabilov
am 8 Feb. 2023
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 Kommentar
Dyuman Joshi
am 4 Feb. 2023
Bearbeitet: Dyuman Joshi
am 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
Akzeptierte Antwort
Sulaymon Eshkabilov
am 4 Feb. 2023
Bearbeitet: Sulaymon Eshkabilov
am 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);
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);
% compare the computed y with conv(x, h)
disp(conv(x,h))
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
%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
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
