フィルターのクリア

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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by