필터 지우기
필터 지우기

Convolution without any Built-in Commands

조회 수: 4 (최근 30일)
SB
SB 2012년 11월 24일
댓글: SM60 2019년 3월 7일
Hey guys, I'm trying to learn how convolution works without any built in fft or
conv commands, and I'm not quite sure how to write it. I'm starting at index 1 in
this case, so y(n) =(x*h)(n)= sum from 1 to infinity of (x(k)h(n-k+1)), and
trying to use just arrays and no loops, while also creating a stem plot. The lack
of a for loop is giving me the most trouble, any suggestions?
  댓글 수: 3
SB
SB 2012년 11월 24일
편집: Image Analyst 2012년 11월 25일
It's actually not homework, but anyways (I'm trying to implement convolution in the MathScript Node, which is similar to Matlab, for LabView):
%
m = length(x);
n = length(h);
X = [x,zeros(1,n)];
H = [h,zeros(1,m)];
for i = 1 : n+m-1
Y(i)=0;
for j=1:m
if(i-j+1>0)
Y(i)=Y(i)+X(j)*H(i-j+1);
else
end
end
end
Y
stem(Y);
ylabel('Y[n]');
xlabel('----->n');
x and h are inputs on the node, so I haven't really provided them.
SM60
SM60 2019년 3월 7일
please do deconv() function using for loops

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

답변 (2개)

Matt J
Matt J 2012년 11월 24일
TOEPLITZ isn't a built-in command. You could use that to implement convolution, e.g.,
>> A=toeplitz([2 3, zeros(1,8)],[2 1 0 0 0 0 0 0 0 0 ]), b=rand(10,1);
A =
2 1 0 0 0 0 0 0 0 0
3 2 1 0 0 0 0 0 0 0
0 3 2 1 0 0 0 0 0 0
0 0 3 2 1 0 0 0 0 0
0 0 0 3 2 1 0 0 0 0
0 0 0 0 3 2 1 0 0 0
0 0 0 0 0 3 2 1 0 0
0 0 0 0 0 0 3 2 1 0
0 0 0 0 0 0 0 3 2 1
0 0 0 0 0 0 0 0 3 2
>> A*b-conv(b,1:3,'same')
ans =
1.0e-15 *
0
0.4441
0.8882
0
0
0.8882
-0.4441
0
0
0
  댓글 수: 3
Matt J
Matt J 2012년 11월 25일
Why don't you test it against the output of conv() to see if you get the same results?
Image Analyst
Image Analyst 2012년 11월 25일
I think you mean x(n) ** h(n), which is the usual textbook notation for convolution, rather than (x*h)(n). The code it's not exactly the way I'd do it (padding with zeros, etc.) but it's easy enough to test, like Matt suggested. I would use the double for loop though.

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


Jan
Jan 2012년 11월 25일
Even CONV is not a built-in function. But it calls conv2(), which is built-in now.
conv(a,b) can be calculated using:
c = filter(a, 1, b);
when the shorter of the inputs is padded by zeros. And filter() can by run as M-file also, see http://www.mathworks.com/matlabcentral/answers/9900#answer_13623. And considering the special inputs, this code can be improved.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by