使用Matlab的conv()函数进行卷积

时间:2018-10-27 19:15:05

标签: matlab convolution

这是对this question的补充问题。

根据documentation

len(output) = len(input) + len(kernel) - 1

所以,我知道了

  • 对于conv(u,v,"full")

    len(pad) = len(kernel) - 1
    

例如,根据Matlab命令行:

    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

因此,根据计算:

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
  • 对于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

因此,根据计算:

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     

但是,在以下示例的情况下会出现问题:

    u = [1 2 1 3 1]
    v = [2 0 1 0]

以下一项可以:

  • 对于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
    

所以

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

但是,以下一个问题:

  • 对于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

因此,根据计算:

0 0 1 2 1 3 1 0 0
0 1 0 2
---------------
0 0 0 4 = 4

0 0 1 2 1 3 1 0 0
  0 1 0 2
---------------
  0 1 0 2 = 3

0 0 1 2 1 3 1 0 0
    0 1 0 2
---------------
    0 2 0 6 = 8

0 0 1 2 1 3 1 0 0
      0 1 0 2
-----------------
      0 1 0 2 = 3

0 0 1 2 1 3 1 0 0
        0 1 0 2
-------------------
        0 3 0 0 = 3

0 0 1 2 1 3 1 0 0
          0 1 0 2
-------------------
          0 1 0 0 = 1

即输出= [4 3 8 3 3 1]与Matlab输出不匹配。

这是怎么回事?

0 个答案:

没有答案