Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit 53ebe12

Browse files
gyshireminisce
authored andcommitted
add numpy op hanning, hamming, blackman (#15815)
address test file fix test file make default dtype is float32
1 parent da6e744 commit 53ebe12

File tree

7 files changed

+1006
-9
lines changed

7 files changed

+1006
-9
lines changed

python/mxnet/ndarray/numpy/_op.py

Lines changed: 241 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
'trunc', 'logical_not', 'arcsinh', 'arccosh', 'arctanh', 'tensordot',
3535
'linspace', 'expand_dims', 'tile', 'arange', 'split', 'concatenate', 'stack', 'vstack', 'mean',
3636
'maximum', 'minimum', 'swapaxes', 'clip', 'argmax', 'std', 'var', 'indices', 'copysign',
37-
'ravel']
37+
'ravel', 'hanning', 'hamming', 'blackman']
3838

3939

4040
@set_module('mxnet.ndarray.numpy')
@@ -2588,3 +2588,243 @@ def ravel(x, order='C'):
25882588
return _npi.reshape(x, -1)
25892589
else:
25902590
raise TypeError('type {} not supported'.format(str(type(x))))
2591+
2592+
2593+
@set_module('mxnet.ndarray.numpy')
2594+
def hanning(M, dtype=_np.float32, ctx=None):
2595+
r"""Return the Hanning window.
2596+
2597+
The Hanning window is a taper formed by using a weighted cosine.
2598+
2599+
Parameters
2600+
----------
2601+
M : int
2602+
Number of points in the output window. If zero or less, an
2603+
empty array is returned.
2604+
dtype : str or numpy.dtype, optional
2605+
An optional value type. Default is `float32`. Note that you need
2606+
select numpy.float32 or float64 in this operator.
2607+
ctx : Context, optional
2608+
An optional device context (default is the current default context).
2609+
2610+
Returns
2611+
-------
2612+
out : ndarray, shape(M,)
2613+
The window, with the maximum value normalized to one (the value
2614+
one appears only if `M` is odd).
2615+
2616+
See Also
2617+
--------
2618+
blackman, hamming
2619+
2620+
Notes
2621+
-----
2622+
The Hanning window is defined as
2623+
2624+
.. math:: w(n) = 0.5 - 0.5cos\left(\frac{2\pi{n}}{M-1}\right)
2625+
\qquad 0 \leq n \leq M-1
2626+
2627+
The Hanning was named for Julius von Hann, an Austrian meteorologist.
2628+
It is also known as the Cosine Bell. Some authors prefer that it be
2629+
called a Hann window, to help avoid confusion with the very similar
2630+
Hamming window.
2631+
2632+
Most references to the Hanning window come from the signal processing
2633+
literature, where it is used as one of many windowing functions for
2634+
smoothing values. It is also known as an apodization (which means
2635+
"removing the foot", i.e. smoothing discontinuities at the beginning
2636+
and end of the sampled signal) or tapering function.
2637+
2638+
References
2639+
----------
2640+
.. [1] Blackman, R.B. and Tukey, J.W., (1958) The measurement of power
2641+
spectra, Dover Publications, New York.
2642+
.. [2] E.R. Kanasewich, "Time Sequence Analysis in Geophysics",
2643+
The University of Alberta Press, 1975, pp. 106-108.
2644+
.. [3] Wikipedia, "Window function",
2645+
http://en.wikipedia.org/wiki/Window_function
2646+
.. [4] W.H. Press, B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling,
2647+
"Numerical Recipes", Cambridge University Press, 1986, page 425.
2648+
2649+
Examples
2650+
--------
2651+
>>> np.hanning(12)
2652+
array([0. , 0.07937324, 0.29229254, 0.5711574 , 0.8274304 ,
2653+
0.9797465 , 0.97974646, 0.82743025, 0.5711573 , 0.29229245,
2654+
0.07937312, 0. ])
2655+
2656+
Plot the window and its frequency response:
2657+
2658+
>>> import matplotlib.pyplot as plt
2659+
>>> window = np.hanning(51)
2660+
>>> plt.plot(window.asnumpy())
2661+
[<matplotlib.lines.Line2D object at 0x...>]
2662+
>>> plt.title("Hann window")
2663+
Text(0.5, 1.0, 'Hann window')
2664+
>>> plt.ylabel("Amplitude")
2665+
Text(0, 0.5, 'Amplitude')
2666+
>>> plt.xlabel("Sample")
2667+
Text(0.5, 0, 'Sample')
2668+
>>> plt.show()
2669+
"""
2670+
if ctx is None:
2671+
ctx = current_context()
2672+
return _npi.hanning(M, dtype=dtype, ctx=ctx)
2673+
2674+
2675+
@set_module('mxnet.ndarray.numpy')
2676+
def hamming(M, dtype=_np.float32, ctx=None):
2677+
r"""Return the hamming window.
2678+
2679+
The hamming window is a taper formed by using a weighted cosine.
2680+
2681+
Parameters
2682+
----------
2683+
M : int
2684+
Number of points in the output window. If zero or less, an
2685+
empty array is returned.
2686+
dtype : str or numpy.dtype, optional
2687+
An optional value type. Default is `float32`. Note that you need
2688+
select numpy.float32 or float64 in this operator.
2689+
ctx : Context, optional
2690+
An optional device context (default is the current default context).
2691+
2692+
Returns
2693+
-------
2694+
out : ndarray, shape(M,)
2695+
The window, with the maximum value normalized to one (the value
2696+
one appears only if `M` is odd).
2697+
2698+
See Also
2699+
--------
2700+
blackman, hanning
2701+
2702+
Notes
2703+
-----
2704+
The Hamming window is defined as
2705+
2706+
.. math:: w(n) = 0.54 - 0.46cos\left(\frac{2\pi{n}}{M-1}\right)
2707+
\qquad 0 \leq n \leq M-1
2708+
2709+
The Hamming was named for R. W. Hamming, an associate of J. W. Tukey
2710+
and is described in Blackman and Tukey. It was recommended for
2711+
smoothing the truncated autocovariance function in the time domain.
2712+
Most references to the Hamming window come from the signal processing
2713+
literature, where it is used as one of many windowing functions for
2714+
smoothing values. It is also known as an apodization (which means
2715+
"removing the foot", i.e. smoothing discontinuities at the beginning
2716+
and end of the sampled signal) or tapering function.
2717+
2718+
References
2719+
----------
2720+
.. [1] Blackman, R.B. and Tukey, J.W., (1958) The measurement of power
2721+
spectra, Dover Publications, New York.
2722+
.. [2] E.R. Kanasewich, "Time Sequence Analysis in Geophysics", The
2723+
University of Alberta Press, 1975, pp. 109-110.
2724+
.. [3] Wikipedia, "Window function",
2725+
https://en.wikipedia.org/wiki/Window_function
2726+
.. [4] W.H. Press, B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling,
2727+
"Numerical Recipes", Cambridge University Press, 1986, page 425.
2728+
2729+
Examples
2730+
--------
2731+
>>> np.hamming(12)
2732+
array([0.08000001, 0.15302339, 0.34890914, 0.6054648 , 0.841236 ,
2733+
0.9813669 , 0.9813668 , 0.8412359 , 0.6054647 , 0.34890908,
2734+
0.15302327, 0.08000001])
2735+
2736+
Plot the window and its frequency response:
2737+
2738+
>>> import matplotlib.pyplot as plt
2739+
>>> window = np.hamming(51)
2740+
>>> plt.plot(window.asnumpy())
2741+
[<matplotlib.lines.Line2D object at 0x...>]
2742+
>>> plt.title("hamming window")
2743+
Text(0.5, 1.0, 'hamming window')
2744+
>>> plt.ylabel("Amplitude")
2745+
Text(0, 0.5, 'Amplitude')
2746+
>>> plt.xlabel("Sample")
2747+
Text(0.5, 0, 'Sample')
2748+
>>> plt.show()
2749+
"""
2750+
if ctx is None:
2751+
ctx = current_context()
2752+
return _npi.hamming(M, dtype=dtype, ctx=ctx)
2753+
2754+
2755+
@set_module('mxnet.ndarray.numpy')
2756+
def blackman(M, dtype=_np.float32, ctx=None):
2757+
r"""Return the Blackman window.
2758+
2759+
The Blackman window is a taper formed by using the first three
2760+
terms of a summation of cosines. It was designed to have close to the
2761+
minimal leakage possible. It is close to optimal, only slightly worse
2762+
than a Kaiser window.
2763+
2764+
Parameters
2765+
----------
2766+
M : int
2767+
Number of points in the output window. If zero or less, an
2768+
empty array is returned.
2769+
dtype : str or numpy.dtype, optional
2770+
An optional value type. Default is `float32`. Note that you need
2771+
select numpy.float32 or float64 in this operator.
2772+
ctx : Context, optional
2773+
An optional device context (default is the current default context).
2774+
2775+
Returns
2776+
-------
2777+
out : ndarray
2778+
The window, with the maximum value normalized to one (the value one
2779+
appears only if the number of samples is odd).
2780+
2781+
See Also
2782+
--------
2783+
hamming, hanning
2784+
2785+
Notes
2786+
-----
2787+
The Blackman window is defined as
2788+
2789+
.. math:: w(n) = 0.42 - 0.5 \cos(2\pi n/{M-1}) + 0.08 \cos(4\pi n/{M-1})
2790+
2791+
Most references to the Blackman window come from the signal processing
2792+
literature, where it is used as one of many windowing functions for
2793+
smoothing values. It is also known as an apodization (which means
2794+
"removing the foot", i.e. smoothing discontinuities at the beginning
2795+
and end of the sampled signal) or tapering function. It is known as a
2796+
"near optimal" tapering function, almost as good (by some measures)
2797+
as the kaiser window.
2798+
2799+
References
2800+
----------
2801+
Blackman, R.B. and Tukey, J.W., (1958) The measurement of power spectra,
2802+
Dover Publications, New York.
2803+
2804+
Oppenheim, A.V., and R.W. Schafer. Discrete-Time Signal Processing.
2805+
Upper Saddle River, NJ: Prentice-Hall, 1999, pp. 468-471.
2806+
2807+
Examples
2808+
--------
2809+
>>> np.blackman(12)
2810+
array([-1.4901161e-08, 3.2606423e-02, 1.5990365e-01, 4.1439798e-01,
2811+
7.3604530e-01, 9.6704686e-01, 9.6704674e-01, 7.3604506e-01,
2812+
4.1439781e-01, 1.5990359e-01, 3.2606363e-02, -1.4901161e-08])
2813+
2814+
Plot the window and its frequency response:
2815+
2816+
>>> import matplotlib.pyplot as plt
2817+
>>> window = np.blackman(51)
2818+
>>> plt.plot(window.asnumpy())
2819+
[<matplotlib.lines.Line2D object at 0x...>]
2820+
>>> plt.title("blackman window")
2821+
Text(0.5, 1.0, 'blackman window')
2822+
>>> plt.ylabel("Amplitude")
2823+
Text(0, 0.5, 'Amplitude')
2824+
>>> plt.xlabel("Sample")
2825+
Text(0.5, 0, 'Sample')
2826+
>>> plt.show()
2827+
"""
2828+
if ctx is None:
2829+
ctx = current_context()
2830+
return _npi.blackman(M, dtype=dtype, ctx=ctx)

0 commit comments

Comments
 (0)