1d Convolution using Matlab's conv() function
显示 更早的评论
According to the documentation(https://www.mathworks.com/help/matlab/ref/conv.html),
len(output) = len(input) + len(kernel) - 1
So, I figured out
- In case of conv(u,v,"full"):
len(pad) = len(kernel) - 1
For instance, according to Matlab commandline:
u = [1 2 1 3]
v = [2 0 1]
w = [2 4 3 8 1 3]
Coz,
len(w) = len(u) + len(v) - 1
= 4 + 3 - 1
= 6
len(pad) = len(v) - 1
= 3 - 1
= 2
So, according to calculation:
0 0 1 2 1 3 0 0
1 0 2
---------------
0 0 2 = 2
. . . . . .
. . . . . .
0 0 1 2 1 3 0 0
1 0 2
-------------------
3 0 0 = 3
- In case of conv(u,v,"same"):
u = [1 2 1 3]
v = [2 0 1]
w = [4 3 8 1]
Coz,
len(w) = len(u)
= 4
len(pad) = floor(len(v) / 2)
= floor(3 / 2)
= 1
So, according to calculation:
0 1 2 1 3 0
1 0 2
-----------
0 0 4 = 4
. . . . . .
. . . . . .
0 1 2 1 3 0
1 0 2
---------------
1 0 0 = 1
But, the problem arises in case of the following example:
u = [1 2 1 3 1]
v = [2 0 1 0]
The following one is okay:
- In case of conv(u,v,"full"):
w = [2 4 3 8 3 3 1 0]
len(w) = len(u) + len(v) - 1
= 5 + 4 - 1
= 8
len(pad) = len(v) - 1
= 4 - 1
= 3
So,
0 0 0 1 2 1 3 1 0 0 0
0 1 0 2
---------------------
0 0 0 2 = 2
. . . . .
. . . . .
0 0 0 1 2 1 3 1 0 0 0
0 1 0 2
---------------------------
0 0 0 0 = 0
But, the following one has issues:
- In case of conv(u,v,"same"):
w = [3 8 3 3 1]
Coz,
len(w) = len(u)
= 5
len(pad) = floor(len(v) / 2)
= floor(4 / 2)
= 2
So, according to calculation:
0 0 1 2 1 3 0 0
0 1 0 2
---------------
0 0 0 4 = 4
0 0 1 2 1 3 0 0
0 1 0 2
---------------
0 1 0 2 = 3
0 0 1 2 1 3 0 0
0 1 0 2
---------------
0 2 0 6 = 8
0 0 1 2 1 3 0 0
0 1 0 2
-----------------
0 1 0 0 = 1
0 0 1 2 1 3 0 0
0 1 0 2
-------------------
0 3 0 0 = 3
I.e. output = [4 3 8 1 3] which doesn't match the Matlab output.
What is going on here?
采纳的回答
更多回答(1 个)
David Goodmanson
2018-10-28
Hi B^3S,
Your last example, the one you are having problems with, is incorrect. You should be using [1 2 1 3 1] but you are using [1 2 1 3] instead. With the right u,
conv(u,v)
ans = 2 4 3 8 3 3 1 0
For the 'same' option, conv picks the centermost 5 (in this case) elements. It's not documented very well if at all, but when there are an odd number of extra elements on the ends, conv seems to cut out one more unused element on the left hand side than the right hand side. Hence
conv(u,v,'same')
ans = 3 8 3 3 1
类别
在 帮助中心 和 File Exchange 中查找有关 Mathematics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!