Understanding behaviour of MATLAB's convn

后端 未结 2 1930
孤城傲影
孤城傲影 2020-12-19 14:44

I\'m doing convolution of some tensors.

Here is small test in MATLAB:

    ker= rand(3,4,2);
    a= rand(5,7,2);
    c=convn(a,ker,\'valid\');
    c11         


        
2条回答
  •  隐瞒了意图╮
    2020-12-19 14:54

    Yes, your understanding of convolution is wrong. Your formula for c11 is not convolution: you just multiplied matching indices and then summed. It's more of a dot-product operation (on tensors trimmed to the same size). I'll try to explain beginning with 1 dimension.

    1-dimensional arrays

    Entering conv([4 5 6], [2 3]) returns [8 22 27 18]. I find it easiest to think of this in terms of multiplication of polynomials:

    (4+5x+6x^2)*(2+3x) = 8+22x+27x^2+18x^3

    Use the entries of each array as coefficients of a polynomial, multiply the polynomials, collect like terms, and read off the result from coefficients. The powers of x are here to keep track of what gets multiplied and added. Note that the coefficient of x^n is found in the (n+1)th entry, because powers of x begin with 0 while the indices begin with 1.

    2-dimensional arrays

    Entering conv2([2 3; 3 1], [4 5 6; 0 -1 1]) returns the matrix

     8  22  27  18
    12  17  22   9
     0  -3   2   1
    

    Again, this can be interpreted as multiplication of polynomials, but now we need two variables: say x and y. The coefficient of x^n y^m is found in (m+1, n+1) entry. The above output means that

    (2+3x+3y+xy)*(4+5x+6x^2+0y-xy+x^2y) = 8+22x+27x^2+18x^3+12y+17xy+22x^2y+9x^3y-3xy^2+2x^2y^2+x^3y^2

    3-dimensional arrays

    Same story. You can think of the entries as coefficients of a polynomial in variables x,y,z. The polynomials get multiplied, and the coefficients of the product are the result of convolution.

    'valid' parameter

    This keeps only the central part of the convolution: those coefficients in which all terms of the second factor have participated. For this to be nonempty, the second array should have dimensions no greater than the first. (This is unlike the default setting, for which the order convolved arrays does not matter.) Example:

    conv([4 5 6], [2 3]) returns [22 27] (compare to the 1-dimensional example above). This corresponds to the fact that in

    (4+5x+6x^2)*(2+3x) = 8+22x+27x^2+18x^3

    the bolded terms got contributions from both 2 and 3x.

提交回复
热议问题