How to fix my my convolution function?

조회 수: 11 (최근 30일)
Andrew
Andrew 2014년 3월 19일
댓글: Image Analyst 2014년 3월 19일
Hi, my teacher wants us to make a convolution function instead of using matlabs and this is what i have
x=[1 2 3 4] y=[1 2 3 4]
function P = MyConv1(x,y)
p = length(x); q = length(y);
for n = 1:p+q-1
for k = 1:n
P(n) = [sum(x(k)*y(n-k))];
end
end
however i get this error:
Attempted to access y(0); index must be a positive integer or logical.
Error in MyConv1 (line 11) P(n) = [sum(x(k)*y(n-k))];
someone please help me figure out what the issue is

답변 (1개)

Image Analyst
Image Analyst 2014년 3월 19일
Then add 1. n-k is 0 and MATLAB doesn't allow a zero index. Try n-k+1.
  댓글 수: 2
Andrew
Andrew 2014년 3월 19일
that did not work. And the formula for convolution states n-k so that is why I kept that.
this is the error with that
Attempted to access y(5); index out of bounds because numel(y)=4.
Error in MyConv1 (line 11) P(n) = [sum(x(k)*y(n-k+1))];
Image Analyst
Image Analyst 2014년 3월 19일
Just write the arrays down on paper. Slide them across and see what gets multiplied. It's not that hard to see what to do if you do that. If you really can't figure it out, come back.

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

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by