在 NumPy 中创建 2D 汉宁、汉明、布莱克​​曼、高斯窗口

问题描述

我对在 NumPy 中创建 2D hanning、hamming、Blackman 等窗口感兴趣。我知道 NumPy 中存在一维版本的现成函数,例如 np.blackman(51)np.hamming(51)np.kaiser(51)np.hanning(51) 等。

如何创建它们的 2D 版本?我不确定以下解决方案是否正确。

window1d = np.blackman(51)
window2d = np.sqrt(np.outer(window1d,window1d)) 

---编辑

令人担忧的是,np.sqrt 只期望正值,而 np.outer(window1d,window1d) 肯定会有一些负值。一种解决方案是放弃 np.sqrt

有什么建议可以将这些一维函数扩展到二维吗?

解决方法

我觉得这很合理。如果您想验证您所做的是否合理,您可以尝试绘制出您正在创建的内容。

import numpy as np
import matplotlib.pyplot as plt


x = np.linspace(0,1.5,51)
y = np.linspace(0,51)

window1d = np.abs(np.blackman(51))
window2d = np.sqrt(np.outer(window1d,window1d))

X,Y = np.meshgrid(x,y)
Z = window2d

fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(X,Y,Z,50,cmap='viridis')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z');

plt.show()

这给 -

enter image description here

这看起来像 1d 绘图的 2d 概括,看起来像 -

enter image description here

但是,我在最初创建 1d 版本时必须执行 window1d = np.abs(np.blackman(51)),否则,您最终会在最终的 2D 数组中得到小的负值,而这些负值无法取 sqrt

免责声明:我不熟悉这些功能或其通常的用例。但这些地块的形状似乎是有道理的。如果这些函数的用例是实际值很重要的地方,这可能是关闭的。