Error in Linear Convolution code MATLAB
6 次查看(过去 30 天)
显示 更早的评论
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 -
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1284375/image.png)
P.S : I am running this file on MATLAB live script
1 个评论
Dyuman Joshi
2023-2-4
编辑:Dyuman Joshi
2023-2-4
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
采纳的回答
Sulaymon Eshkabilov
2023-2-4
编辑:Sulaymon Eshkabilov
2023-2-4
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!