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

Commit 6ee77e5

Browse files
committed
add numpy op hanning, hamming, blackman
1 parent bcbdc1c commit 6ee77e5

File tree

7 files changed

+1159
-3
lines changed

7 files changed

+1159
-3
lines changed

python/mxnet/ndarray/numpy/_op.py

Lines changed: 274 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
from ..ndarray import NDArray
2828

2929
__all__ = ['zeros', 'ones', 'add', 'subtract', 'multiply', 'divide', 'mod', 'power', 'tensordot',
30-
'linspace', 'expand_dims', 'tile', 'arange', 'split', 'concatenate', 'stack']
30+
'linspace', 'expand_dims', 'tile', 'arange', 'split', 'concatenate', 'stack', 'hanning',
31+
'hamming', 'blackman']
3132

3233

3334
@set_module('mxnet.ndarray.numpy')
@@ -732,3 +733,275 @@ def get_list(arrays):
732733

733734
arrays = get_list(arrays)
734735
return _npi.stack(*arrays, axis=axis, out=out)
736+
737+
738+
@set_module('mxnet.ndarray.numpy')
739+
def hanning(M, dtype=_np.float64, ctx=None):
740+
r"""Return the Hanning window.
741+
742+
The Hanning window is a taper formed by using a weighted cosine.
743+
744+
Parameters
745+
----------
746+
M : int
747+
Number of points in the output window. If zero or less, an
748+
empty array is returned.
749+
dtype : str or numpy.dtype, optional
750+
An optional value type. Default is `numpy.float64`. Note that you need
751+
select numpy.float32 or float64 in this operator.
752+
ctx : Context, optional
753+
An optional device context (default is the current default context).
754+
755+
Returns
756+
-------
757+
out : ndarray, shape(M,)
758+
The window, with the maximum value normalized to one (the value
759+
one appears only if `M` is odd).
760+
761+
See Also
762+
--------
763+
blackman, hamming
764+
765+
Notes
766+
-----
767+
The Hanning window is defined as
768+
769+
.. math:: w(n) = 0.5 - 0.5cos\left(\frac{2\pi{n}}{M-1}\right)
770+
\qquad 0 \leq n \leq M-1
771+
772+
The Hanning was named for Julius von Hann, an Austrian meteorologist.
773+
It is also known as the Cosine Bell. Some authors prefer that it be
774+
called a Hann window, to help avoid confusion with the very similar
775+
Hamming window.
776+
777+
Most references to the Hanning window come from the signal processing
778+
literature, where it is used as one of many windowing functions for
779+
smoothing values. It is also known as an apodization (which means
780+
"removing the foot", i.e. smoothing discontinuities at the beginning
781+
and end of the sampled signal) or tapering function.
782+
783+
References
784+
----------
785+
.. [1] Blackman, R.B. and Tukey, J.W., (1958) The measurement of power
786+
spectra, Dover Publications, New York.
787+
.. [2] E.R. Kanasewich, "Time Sequence Analysis in Geophysics",
788+
The University of Alberta Press, 1975, pp. 106-108.
789+
.. [3] Wikipedia, "Window function",
790+
http://en.wikipedia.org/wiki/Window_function
791+
.. [4] W.H. Press, B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling,
792+
"Numerical Recipes", Cambridge University Press, 1986, page 425.
793+
794+
Examples
795+
--------
796+
>>> np.hanning(12)
797+
array([0.00000000e+00, 7.93732437e-02, 2.92292528e-01, 5.71157416e-01,
798+
8.27430424e-01, 9.79746513e-01, 9.79746489e-01, 8.27430268e-01,
799+
5.71157270e-01, 2.92292448e-01, 7.93731320e-02, 1.06192832e-13], dtype=float64)
800+
801+
Plot the window and its frequency response:
802+
803+
>>> import matplotlib.pyplot as plt
804+
>>> window = np.hanning(51)
805+
>>> plt.plot(window.asnumpy())
806+
[<matplotlib.lines.Line2D object at 0x...>]
807+
>>> plt.title("Hann window")
808+
Text(0.5, 1.0, 'Hann window')
809+
>>> plt.ylabel("Amplitude")
810+
Text(0, 0.5, 'Amplitude')
811+
>>> plt.xlabel("Sample")
812+
Text(0.5, 0, 'Sample')
813+
>>> plt.show()
814+
"""
815+
if dtype is None:
816+
dtype = _np.float64
817+
if ctx is None:
818+
ctx = current_context()
819+
return _npi.hanning(M, dtype=dtype, ctx=ctx)
820+
821+
822+
@set_module('mxnet.ndarray.numpy')
823+
def hamming(M, dtype=_np.float64, ctx=None):
824+
r"""Return the hamming window.
825+
826+
The hamming window is a taper formed by using a weighted cosine.
827+
828+
Parameters
829+
----------
830+
M : int
831+
Number of points in the output window. If zero or less, an
832+
empty array is returned.
833+
dtype : str or numpy.dtype, optional
834+
An optional value type. Default is `numpy.float64`. Note that you need
835+
select numpy.float32 or float64 in this operator.
836+
ctx : Context, optional
837+
An optional device context (default is the current default context).
838+
839+
Returns
840+
-------
841+
out : ndarray, shape(M,)
842+
The window, with the maximum value normalized to one (the value
843+
one appears only if `M` is odd).
844+
845+
See Also
846+
--------
847+
blackman, hanning
848+
849+
Notes
850+
-----
851+
The Hamming window is defined as
852+
853+
.. math:: w(n) = 0.54 - 0.46cos\left(\frac{2\pi{n}}{M-1}\right)
854+
\qquad 0 \leq n \leq M-1
855+
856+
The Hamming was named for R. W. Hamming, an associate of J. W. Tukey
857+
and is described in Blackman and Tukey. It was recommended for
858+
smoothing the truncated autocovariance function in the time domain.
859+
Most references to the Hamming window come from the signal processing
860+
literature, where it is used as one of many windowing functions for
861+
smoothing values. It is also known as an apodization (which means
862+
"removing the foot", i.e. smoothing discontinuities at the beginning
863+
and end of the sampled signal) or tapering function.
864+
865+
References
866+
----------
867+
.. [1] Blackman, R.B. and Tukey, J.W., (1958) The measurement of power
868+
spectra, Dover Publications, New York.
869+
.. [2] E.R. Kanasewich, "Time Sequence Analysis in Geophysics", The
870+
University of Alberta Press, 1975, pp. 109-110.
871+
.. [3] Wikipedia, "Window function",
872+
https://en.wikipedia.org/wiki/Window_function
873+
.. [4] W.H. Press, B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling,
874+
"Numerical Recipes", Cambridge University Press, 1986, page 425.
875+
876+
Examples
877+
--------
878+
>>> np.hamming(12)
879+
array([0.08 , 0.15302338, 0.34890913, 0.60546482, 0.84123599,
880+
0.98136679, 0.98136677, 0.84123585, 0.60546469, 0.34890905,
881+
0.15302328, 0.08 ], dtype=float64)
882+
883+
Plot the window and its frequency response:
884+
885+
>>> import matplotlib.pyplot as plt
886+
>>> window = np.hamming(51)
887+
>>> plt.plot(window.asnumpy())
888+
[<matplotlib.lines.Line2D object at 0x...>]
889+
>>> plt.title("hamming window")
890+
Text(0.5, 1.0, 'hamming window')
891+
>>> plt.ylabel("Amplitude")
892+
Text(0, 0.5, 'Amplitude')
893+
>>> plt.xlabel("Sample")
894+
Text(0.5, 0, 'Sample')
895+
>>> plt.show()
896+
"""
897+
if dtype is None:
898+
dtype = _np.float64
899+
if ctx is None:
900+
ctx = current_context()
901+
return _npi.hamming(M, dtype=dtype, ctx=ctx)
902+
903+
904+
@set_module('mxnet.ndarray.numpy')
905+
def blackman(M, dtype=_np.float64, ctx=None):
906+
r"""Return the Blackman window.
907+
908+
The Blackman window is a taper formed by using the first three
909+
terms of a summation of cosines. It was designed to have close to the
910+
minimal leakage possible. It is close to optimal, only slightly worse
911+
than a Kaiser window.
912+
913+
Parameters
914+
----------
915+
M : int
916+
Number of points in the output window. If zero or less, an
917+
empty array is returned.
918+
dtype : str or numpy.dtype, optional
919+
An optional value type. Default is `numpy.float64`. Note that you need
920+
select numpy.float32 or float64 in this operator.
921+
ctx : Context, optional
922+
An optional device context (default is the current default context).
923+
924+
Returns
925+
-------
926+
out : ndarray
927+
The window, with the maximum value normalized to one (the value one
928+
appears only if the number of samples is odd).
929+
930+
See Also
931+
--------
932+
bartlett, hamming, hanning, kaiser
933+
934+
Notes
935+
-----
936+
The Blackman window is defined as
937+
938+
.. math:: w(n) = 0.42 - 0.5 \cos(2\pi n/{M-1}) + 0.08 \cos(4\pi n/{M-1})
939+
940+
Most references to the Blackman window come from the signal processing
941+
literature, where it is used as one of many windowing functions for
942+
smoothing values. It is also known as an apodization (which means
943+
"removing the foot", i.e. smoothing discontinuities at the beginning
944+
and end of the sampled signal) or tapering function. It is known as a
945+
"near optimal" tapering function, almost as good (by some measures)
946+
as the kaiser window.
947+
948+
References
949+
----------
950+
Blackman, R.B. and Tukey, J.W., (1958) The measurement of power spectra,
951+
Dover Publications, New York.
952+
953+
Oppenheim, A.V., and R.W. Schafer. Discrete-Time Signal Processing.
954+
Upper Saddle River, NJ: Prentice-Hall, 1999, pp. 468-471.
955+
956+
See Also
957+
--------
958+
hamming, hanning
959+
960+
Notes
961+
-----
962+
The Blackman window is defined as
963+
964+
.. math:: w(n) = 0.42 - 0.5 \cos(2\pi n/{M-1}) + 0.08 \cos(4\pi n/{M-1})
965+
966+
Most references to the Blackman window come from the signal processing
967+
literature, where it is used as one of many windowing functions for
968+
smoothing values. It is also known as an apodization (which means
969+
"removing the foot", i.e. smoothing discontinuities at the beginning
970+
and end of the sampled signal) or tapering function. It is known as a
971+
"near optimal" tapering function, almost as good (by some measures)
972+
as the kaiser window.
973+
974+
References
975+
----------
976+
Blackman, R.B. and Tukey, J.W., (1958) The measurement of power spectra,
977+
Dover Publications, New York.
978+
979+
Oppenheim, A.V., and R.W. Schafer. Discrete-Time Signal Processing.
980+
Upper Saddle River, NJ: Prentice-Hall, 1999, pp. 468-471.
981+
982+
Examples
983+
--------
984+
>>> np.blackman(12)
985+
array([-1.38777878e-17, 3.26064393e-02, 1.59903660e-01, 4.14397978e-01,
986+
7.36045260e-01, 9.67046812e-01, 9.67046772e-01, 7.36045039e-01,
987+
4.14397819e-01, 1.59903601e-01, 3.26063877e-02, 3.82194276e-14], dtype=float64)
988+
989+
Plot the window and its frequency response:
990+
991+
>>> import matplotlib.pyplot as plt
992+
>>> window = np.blackman(51)
993+
>>> plt.plot(window.asnumpy())
994+
[<matplotlib.lines.Line2D object at 0x...>]
995+
>>> plt.title("blackman window")
996+
Text(0.5, 1.0, 'blackman window')
997+
>>> plt.ylabel("Amplitude")
998+
Text(0, 0.5, 'Amplitude')
999+
>>> plt.xlabel("Sample")
1000+
Text(0.5, 0, 'Sample')
1001+
>>> plt.show()
1002+
"""
1003+
if dtype is None:
1004+
dtype = _np.float64
1005+
if ctx is None:
1006+
ctx = current_context()
1007+
return _npi.blackman(M, dtype=dtype, ctx=ctx)

0 commit comments

Comments
 (0)