Error in Linear Convolution code MATLAB
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Achyuth S.S
le 4 Fév 2023
Commenté : Sulaymon Eshkabilov
le 8 Fév 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 commentaire
Dyuman Joshi
le 4 Fév 2023
Modifié(e) : Dyuman Joshi
le 4 Fév 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
Réponse acceptée
Sulaymon Eshkabilov
le 4 Fév 2023
Modifié(e) : Sulaymon Eshkabilov
le 4 Fév 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
