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