Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
944 views
in Technique[技术] by (71.8m points)

signal processing - Create 2D hanning, hamming, blackman, gaussian window in NumPy

I am interested in creating 2D hanning, hamming, Blackman, etc windows in NumPy. I know that off-the-shelf functions exist in NumPy for 1D versions of it such as np.blackman(51), np.hamming(51), np.kaiser(51), np.hanning(51), etc.

How to create 2D versions of them? I am not sure if the following solution is the correct way.

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

---EDIT

The concern is that np.sqrt expects only positive values while np.outer(window1d,window1d) will definitely have some negative values. One solution is to relinquish np.sqrt

Any suggestions how to extend these 1d functions to 2d?

question from:https://stackoverflow.com/questions/65940166/create-2d-hanning-hamming-blackman-gaussian-window-in-numpy

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

That looks reasonable to me. If you want to verify what you are doing is sensible, you can try plotting out what you are creating.

import numpy as np
import matplotlib.pyplot as plt


x = np.linspace(0, 1.5, 51)
y = np.linspace(0, 1.5, 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()

This gives -

enter image description here

This looks like the 2d generalization of the 1d plot which looks like -

enter image description here

However, I had to do window1d = np.abs(np.blackman(51)) when creating the 1d version initially because otherwise, you would end up with small negative values in the final 2D array which you cannot take sqrt of.

Disclaimer: I am not familiar with the functions or their usual use-case. But the shapes of these plots seems to make sense. If the use-case of these functions is somewhere in which the actual values matter, this could be off.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...