Convolution without any Built-in Commands

6 visualizzazioni (ultimi 30 giorni)
SB
SB il 24 Nov 2012
Commentato: SM60 il 7 Mar 2019
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 Commenti
SB
SB il 24 Nov 2012
Modificato: Image Analyst il 25 Nov 2012
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 il 7 Mar 2019
please do deconv() function using for loops

Accedi per commentare.

Risposte (2)

Matt J
Matt J il 24 Nov 2012
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 Commenti
Matt J
Matt J il 25 Nov 2012
Why don't you test it against the output of conv() to see if you get the same results?
Image Analyst
Image Analyst il 25 Nov 2012
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.

Accedi per commentare.


Jan
Jan il 25 Nov 2012
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.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by