Error in Linear Convolution code MATLAB

조회 수: 7 (최근 30일)
Achyuth S.S
Achyuth S.S 2023년 2월 4일
댓글: Sulaymon Eshkabilov 2023년 2월 8일
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
Dyuman Joshi
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
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);
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
  댓글 수: 2
Achyuth S.S
Achyuth S.S 2023년 2월 5일
Thank you so much, Will look into it
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 2월 8일
Most welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by