Convolution without any Built-in Commands
조회 수: 4 (최근 30일)
이전 댓글 표시
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
답변 (2개)
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
2012년 11월 25일
Why don't you test it against the output of conv() to see if you get the same results?
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
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!