二维卷积没有给出所需的输出

2d convolution gives not the desired output

我想像在 1D 中那样使用 2D 卷积。不幸的是,前一种情况的输出没有所需的形状。让n = 5,然后

h_0 = (1 / 4) * np.array([1, 2, 1])
x = np.random.rand(n)
np.convolve(h_0, x, 'same')

>>> array([0.65498075, 0.72729356, 0.51417706, 0.34597679, 0.1793755])

但是

h_00 = np.kron(h_0, h_0)
h_00 = np.reshape(h_00, (3, 3))
x = np.random.rand(n, n)
scipy.signal.convolve2d(h_00, x, 'same', boundary='symm')

>>> array([[1.90147294, 1.6541233 , 1.82704077],
           [1.55228912, 1.3641027 , 1.55536069],
           [1.61190909, 1.45159935, 1.58266083]])

我本来期望 (5, 5) 输出数组。

scipy.signal.convolve2d 的文档关于 mode 参数清楚地说明

mode
...
same
    The output is the same size as in1, centered with respect to the ‘full’ output.

因此,假设您首先传递内核,您的输出将与内核大小相同,而不是您正在过滤的数组。要修复,请交换前两个输入:

scipy.signal.convolve2d(x, h_00, 'same', boundary='symm')

混淆可能是由 numpy.convolve 的行为引起的,它执行以下操作:

mode : {‘full’, ‘valid’, ‘same’}, optional
...
‘same’:
    Mode ‘same’ returns output of length max(M, N). Boundary effects are still visible.

Numpy 将较大的数组解释为内核,而不考虑参数顺序。这是可能的,因为只有一个维度,总有一个明确的赢家。