diff --git a/python/mxnet/ndarray/numpy/_op.py b/python/mxnet/ndarray/numpy/_op.py index bb63e94691f1..99e35f37844d 100644 --- a/python/mxnet/ndarray/numpy/_op.py +++ b/python/mxnet/ndarray/numpy/_op.py @@ -34,7 +34,7 @@ 'trunc', 'logical_not', 'arcsinh', 'arccosh', 'arctanh', 'tensordot', 'linspace', 'expand_dims', 'tile', 'arange', 'split', 'concatenate', 'stack', 'vstack', 'mean', 'maximum', 'minimum', 'swapaxes', 'clip', 'argmax', 'std', 'var', 'indices', 'copysign', - 'ravel'] + 'ravel', 'hanning', 'hamming', 'blackman'] @set_module('mxnet.ndarray.numpy') @@ -2588,3 +2588,243 @@ def ravel(x, order='C'): return _npi.reshape(x, -1) else: raise TypeError('type {} not supported'.format(str(type(x)))) + + +@set_module('mxnet.ndarray.numpy') +def hanning(M, dtype=_np.float32, ctx=None): + r"""Return the Hanning window. + + The Hanning window is a taper formed by using a weighted cosine. + + Parameters + ---------- + M : int + Number of points in the output window. If zero or less, an + empty array is returned. + dtype : str or numpy.dtype, optional + An optional value type. Default is `float32`. Note that you need + select numpy.float32 or float64 in this operator. + ctx : Context, optional + An optional device context (default is the current default context). + + Returns + ------- + out : ndarray, shape(M,) + The window, with the maximum value normalized to one (the value + one appears only if `M` is odd). + + See Also + -------- + blackman, hamming + + Notes + ----- + The Hanning window is defined as + + .. math:: w(n) = 0.5 - 0.5cos\left(\frac{2\pi{n}}{M-1}\right) + \qquad 0 \leq n \leq M-1 + + The Hanning was named for Julius von Hann, an Austrian meteorologist. + It is also known as the Cosine Bell. Some authors prefer that it be + called a Hann window, to help avoid confusion with the very similar + Hamming window. + + Most references to the Hanning window come from the signal processing + literature, where it is used as one of many windowing functions for + smoothing values. It is also known as an apodization (which means + "removing the foot", i.e. smoothing discontinuities at the beginning + and end of the sampled signal) or tapering function. + + References + ---------- + .. [1] Blackman, R.B. and Tukey, J.W., (1958) The measurement of power + spectra, Dover Publications, New York. + .. [2] E.R. Kanasewich, "Time Sequence Analysis in Geophysics", + The University of Alberta Press, 1975, pp. 106-108. + .. [3] Wikipedia, "Window function", + http://en.wikipedia.org/wiki/Window_function + .. [4] W.H. Press, B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, + "Numerical Recipes", Cambridge University Press, 1986, page 425. + + Examples + -------- + >>> np.hanning(12) + array([0. , 0.07937324, 0.29229254, 0.5711574 , 0.8274304 , + 0.9797465 , 0.97974646, 0.82743025, 0.5711573 , 0.29229245, + 0.07937312, 0. ]) + + Plot the window and its frequency response: + + >>> import matplotlib.pyplot as plt + >>> window = np.hanning(51) + >>> plt.plot(window.asnumpy()) + [] + >>> plt.title("Hann window") + Text(0.5, 1.0, 'Hann window') + >>> plt.ylabel("Amplitude") + Text(0, 0.5, 'Amplitude') + >>> plt.xlabel("Sample") + Text(0.5, 0, 'Sample') + >>> plt.show() + """ + if ctx is None: + ctx = current_context() + return _npi.hanning(M, dtype=dtype, ctx=ctx) + + +@set_module('mxnet.ndarray.numpy') +def hamming(M, dtype=_np.float32, ctx=None): + r"""Return the hamming window. + + The hamming window is a taper formed by using a weighted cosine. + + Parameters + ---------- + M : int + Number of points in the output window. If zero or less, an + empty array is returned. + dtype : str or numpy.dtype, optional + An optional value type. Default is `float32`. Note that you need + select numpy.float32 or float64 in this operator. + ctx : Context, optional + An optional device context (default is the current default context). + + Returns + ------- + out : ndarray, shape(M,) + The window, with the maximum value normalized to one (the value + one appears only if `M` is odd). + + See Also + -------- + blackman, hanning + + Notes + ----- + The Hamming window is defined as + + .. math:: w(n) = 0.54 - 0.46cos\left(\frac{2\pi{n}}{M-1}\right) + \qquad 0 \leq n \leq M-1 + + The Hamming was named for R. W. Hamming, an associate of J. W. Tukey + and is described in Blackman and Tukey. It was recommended for + smoothing the truncated autocovariance function in the time domain. + Most references to the Hamming window come from the signal processing + literature, where it is used as one of many windowing functions for + smoothing values. It is also known as an apodization (which means + "removing the foot", i.e. smoothing discontinuities at the beginning + and end of the sampled signal) or tapering function. + + References + ---------- + .. [1] Blackman, R.B. and Tukey, J.W., (1958) The measurement of power + spectra, Dover Publications, New York. + .. [2] E.R. Kanasewich, "Time Sequence Analysis in Geophysics", The + University of Alberta Press, 1975, pp. 109-110. + .. [3] Wikipedia, "Window function", + https://en.wikipedia.org/wiki/Window_function + .. [4] W.H. Press, B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, + "Numerical Recipes", Cambridge University Press, 1986, page 425. + + Examples + -------- + >>> np.hamming(12) + array([0.08000001, 0.15302339, 0.34890914, 0.6054648 , 0.841236 , + 0.9813669 , 0.9813668 , 0.8412359 , 0.6054647 , 0.34890908, + 0.15302327, 0.08000001]) + + Plot the window and its frequency response: + + >>> import matplotlib.pyplot as plt + >>> window = np.hamming(51) + >>> plt.plot(window.asnumpy()) + [] + >>> plt.title("hamming window") + Text(0.5, 1.0, 'hamming window') + >>> plt.ylabel("Amplitude") + Text(0, 0.5, 'Amplitude') + >>> plt.xlabel("Sample") + Text(0.5, 0, 'Sample') + >>> plt.show() + """ + if ctx is None: + ctx = current_context() + return _npi.hamming(M, dtype=dtype, ctx=ctx) + + +@set_module('mxnet.ndarray.numpy') +def blackman(M, dtype=_np.float32, ctx=None): + r"""Return the Blackman window. + + The Blackman window is a taper formed by using the first three + terms of a summation of cosines. It was designed to have close to the + minimal leakage possible. It is close to optimal, only slightly worse + than a Kaiser window. + + Parameters + ---------- + M : int + Number of points in the output window. If zero or less, an + empty array is returned. + dtype : str or numpy.dtype, optional + An optional value type. Default is `float32`. Note that you need + select numpy.float32 or float64 in this operator. + ctx : Context, optional + An optional device context (default is the current default context). + + Returns + ------- + out : ndarray + The window, with the maximum value normalized to one (the value one + appears only if the number of samples is odd). + + See Also + -------- + hamming, hanning + + Notes + ----- + The Blackman window is defined as + + .. math:: w(n) = 0.42 - 0.5 \cos(2\pi n/{M-1}) + 0.08 \cos(4\pi n/{M-1}) + + Most references to the Blackman window come from the signal processing + literature, where it is used as one of many windowing functions for + smoothing values. It is also known as an apodization (which means + "removing the foot", i.e. smoothing discontinuities at the beginning + and end of the sampled signal) or tapering function. It is known as a + "near optimal" tapering function, almost as good (by some measures) + as the kaiser window. + + References + ---------- + Blackman, R.B. and Tukey, J.W., (1958) The measurement of power spectra, + Dover Publications, New York. + + Oppenheim, A.V., and R.W. Schafer. Discrete-Time Signal Processing. + Upper Saddle River, NJ: Prentice-Hall, 1999, pp. 468-471. + + Examples + -------- + >>> np.blackman(12) + array([-1.4901161e-08, 3.2606423e-02, 1.5990365e-01, 4.1439798e-01, + 7.3604530e-01, 9.6704686e-01, 9.6704674e-01, 7.3604506e-01, + 4.1439781e-01, 1.5990359e-01, 3.2606363e-02, -1.4901161e-08]) + + Plot the window and its frequency response: + + >>> import matplotlib.pyplot as plt + >>> window = np.blackman(51) + >>> plt.plot(window.asnumpy()) + [] + >>> plt.title("blackman window") + Text(0.5, 1.0, 'blackman window') + >>> plt.ylabel("Amplitude") + Text(0, 0.5, 'Amplitude') + >>> plt.xlabel("Sample") + Text(0.5, 0, 'Sample') + >>> plt.show() + """ + if ctx is None: + ctx = current_context() + return _npi.blackman(M, dtype=dtype, ctx=ctx) diff --git a/python/mxnet/numpy/multiarray.py b/python/mxnet/numpy/multiarray.py index f738d630fe8a..801538557c5d 100644 --- a/python/mxnet/numpy/multiarray.py +++ b/python/mxnet/numpy/multiarray.py @@ -53,7 +53,7 @@ 'fix', 'ceil', 'floor', 'trunc', 'logical_not', 'arcsinh', 'arccosh', 'arctanh', 'tensordot', 'linspace', 'expand_dims', 'tile', 'arange', 'split', 'concatenate', 'stack', 'vstack', 'mean', 'maximum', 'minimum', 'swapaxes', 'clip', 'argmax', 'std', 'var', 'indices', - 'copysign', 'ravel'] + 'copysign', 'ravel', 'hanning', 'hamming', 'blackman'] # Return code for dispatching indexing function call _NDARRAY_UNSUPPORTED_INDEXING = -1 @@ -4133,3 +4133,237 @@ def ravel(x, order='C'): [1. 4. 2. 5. 3. 6.] """ return _mx_nd_np.ravel(x, order) + + +@set_module('mxnet.numpy') +def hanning(M, dtype=_np.float32, ctx=None): + r"""Return the Hanning window. + + The Hanning window is a taper formed by using a weighted cosine. + + Parameters + ---------- + M : int + Number of points in the output window. If zero or less, an + empty array is returned. + dtype : str or numpy.dtype, optional + An optional value type. Default is `float32`. Note that you need + select numpy.float32 or float64 in this operator. + ctx : Context, optional + An optional device context (default is the current default context). + + Returns + ------- + out : ndarray, shape(M,) + The window, with the maximum value normalized to one (the value + one appears only if `M` is odd). + + See Also + -------- + blackman, hamming + + Notes + ----- + The Hanning window is defined as + + .. math:: w(n) = 0.5 - 0.5cos\left(\frac{2\pi{n}}{M-1}\right) + \qquad 0 \leq n \leq M-1 + + The Hanning was named for Julius von Hann, an Austrian meteorologist. + It is also known as the Cosine Bell. Some authors prefer that it be + called a Hann window, to help avoid confusion with the very similar + Hamming window. + + Most references to the Hanning window come from the signal processing + literature, where it is used as one of many windowing functions for + smoothing values. It is also known as an apodization (which means + "removing the foot", i.e. smoothing discontinuities at the beginning + and end of the sampled signal) or tapering function. + + References + ---------- + .. [1] Blackman, R.B. and Tukey, J.W., (1958) The measurement of power + spectra, Dover Publications, New York. + .. [2] E.R. Kanasewich, "Time Sequence Analysis in Geophysics", + The University of Alberta Press, 1975, pp. 106-108. + .. [3] Wikipedia, "Window function", + http://en.wikipedia.org/wiki/Window_function + .. [4] W.H. Press, B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, + "Numerical Recipes", Cambridge University Press, 1986, page 425. + + Examples + -------- + >>> np.hanning(12) + array([0. , 0.07937324, 0.29229254, 0.5711574 , 0.8274304 , + 0.9797465 , 0.97974646, 0.82743025, 0.5711573 , 0.29229245, + 0.07937312, 0. ]) + + Plot the window and its frequency response: + + >>> import matplotlib.pyplot as plt + >>> window = np.hanning(51) + >>> plt.plot(window.asnumpy()) + [] + >>> plt.title("Hann window") + Text(0.5, 1.0, 'Hann window') + >>> plt.ylabel("Amplitude") + Text(0, 0.5, 'Amplitude') + >>> plt.xlabel("Sample") + Text(0.5, 0, 'Sample') + >>> plt.show() + """ + return _mx_nd_np.hanning(M, dtype=dtype, ctx=ctx) + + +@set_module('mxnet.numpy') +def hamming(M, dtype=_np.float32, ctx=None): + r"""Return the hamming window. + + The hamming window is a taper formed by using a weighted cosine. + + Parameters + ---------- + M : int + Number of points in the output window. If zero or less, an + empty array is returned. + dtype : str or numpy.dtype, optional + An optional value type. Default is `float32`. Note that you need + select numpy.float32 or float64 in this operator. + ctx : Context, optional + An optional device context (default is the current default context). + + Returns + ------- + out : ndarray, shape(M,) + The window, with the maximum value normalized to one (the value + one appears only if `M` is odd). + + See Also + -------- + blackman, hanning + + Notes + ----- + The Hamming window is defined as + + .. math:: w(n) = 0.54 - 0.46cos\left(\frac{2\pi{n}}{M-1}\right) + \qquad 0 \leq n \leq M-1 + + The Hamming was named for R. W. Hamming, an associate of J. W. Tukey + and is described in Blackman and Tukey. It was recommended for + smoothing the truncated autocovariance function in the time domain. + Most references to the Hamming window come from the signal processing + literature, where it is used as one of many windowing functions for + smoothing values. It is also known as an apodization (which means + "removing the foot", i.e. smoothing discontinuities at the beginning + and end of the sampled signal) or tapering function. + + References + ---------- + .. [1] Blackman, R.B. and Tukey, J.W., (1958) The measurement of power + spectra, Dover Publications, New York. + .. [2] E.R. Kanasewich, "Time Sequence Analysis in Geophysics", The + University of Alberta Press, 1975, pp. 109-110. + .. [3] Wikipedia, "Window function", + https://en.wikipedia.org/wiki/Window_function + .. [4] W.H. Press, B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, + "Numerical Recipes", Cambridge University Press, 1986, page 425. + + Examples + -------- + >>> np.hamming(12) + array([0.08000001, 0.15302339, 0.34890914, 0.6054648 , 0.841236 , + 0.9813669 , 0.9813668 , 0.8412359 , 0.6054647 , 0.34890908, + 0.15302327, 0.08000001]) + + Plot the window and its frequency response: + + >>> import matplotlib.pyplot as plt + >>> window = np.hamming(51) + >>> plt.plot(window.asnumpy()) + [] + >>> plt.title("hamming window") + Text(0.5, 1.0, 'hamming window') + >>> plt.ylabel("Amplitude") + Text(0, 0.5, 'Amplitude') + >>> plt.xlabel("Sample") + Text(0.5, 0, 'Sample') + >>> plt.show() + """ + return _mx_nd_np.hamming(M, dtype=dtype, ctx=ctx) + + +@set_module('mxnet.numpy') +def blackman(M, dtype=_np.float32, ctx=None): + r"""Return the Blackman window. + + The Blackman window is a taper formed by using the first three + terms of a summation of cosines. It was designed to have close to the + minimal leakage possible. It is close to optimal, only slightly worse + than a Kaiser window. + + Parameters + ---------- + M : int + Number of points in the output window. If zero or less, an + empty array is returned. + dtype : str or numpy.dtype, optional + An optional value type. Default is `float32`. Note that you need + select numpy.float32 or float64 in this operator. + ctx : Context, optional + An optional device context (default is the current default context). + + Returns + ------- + out : ndarray + The window, with the maximum value normalized to one (the value one + appears only if the number of samples is odd). + + See Also + -------- + hamming, hanning + + Notes + ----- + The Blackman window is defined as + + .. math:: w(n) = 0.42 - 0.5 \cos(2\pi n/{M-1}) + 0.08 \cos(4\pi n/{M-1}) + + Most references to the Blackman window come from the signal processing + literature, where it is used as one of many windowing functions for + smoothing values. It is also known as an apodization (which means + "removing the foot", i.e. smoothing discontinuities at the beginning + and end of the sampled signal) or tapering function. It is known as a + "near optimal" tapering function, almost as good (by some measures) + as the kaiser window. + + References + ---------- + Blackman, R.B. and Tukey, J.W., (1958) The measurement of power spectra, + Dover Publications, New York. + + Oppenheim, A.V., and R.W. Schafer. Discrete-Time Signal Processing. + Upper Saddle River, NJ: Prentice-Hall, 1999, pp. 468-471. + + Examples + -------- + >>> np.blackman(12) + array([-1.4901161e-08, 3.2606423e-02, 1.5990365e-01, 4.1439798e-01, + 7.3604530e-01, 9.6704686e-01, 9.6704674e-01, 7.3604506e-01, + 4.1439781e-01, 1.5990359e-01, 3.2606363e-02, -1.4901161e-08]) + + Plot the window and its frequency response: + + >>> import matplotlib.pyplot as plt + >>> window = np.blackman(51) + >>> plt.plot(window.asnumpy()) + [] + >>> plt.title("blackman window") + Text(0.5, 1.0, 'blackman window') + >>> plt.ylabel("Amplitude") + Text(0, 0.5, 'Amplitude') + >>> plt.xlabel("Sample") + Text(0.5, 0, 'Sample') + >>> plt.show() + """ + return _mx_nd_np.blackman(M, dtype=dtype, ctx=ctx) diff --git a/python/mxnet/symbol/numpy/_symbol.py b/python/mxnet/symbol/numpy/_symbol.py index 03d3d0df15eb..f39d201638cb 100644 --- a/python/mxnet/symbol/numpy/_symbol.py +++ b/python/mxnet/symbol/numpy/_symbol.py @@ -36,7 +36,7 @@ 'trunc', 'logical_not', 'arcsinh', 'arccosh', 'arctanh', 'tensordot', 'linspace', 'expand_dims', 'tile', 'arange', 'split', 'concatenate', 'stack', 'vstack', 'mean', 'maximum', 'minimum', 'swapaxes', 'clip', 'argmax', 'std', 'var', 'indices', 'copysign', - 'ravel'] + 'ravel', 'hanning', 'hamming', 'blackman'] def _num_outputs(sym): @@ -2851,4 +2851,244 @@ def ravel(x, order='C'): raise TypeError('type {} not supported'.format(str(type(x)))) +@set_module('mxnet.symbol.numpy') +def hanning(M, dtype=_np.float32, ctx=None): + r"""Return the Hanning window. + + The Hanning window is a taper formed by using a weighted cosine. + + Parameters + ---------- + M : int + Number of points in the output window. If zero or less, an + empty array is returned. + dtype : str or numpy.dtype, optional + An optional value type. Default is `float32`. Note that you need + select numpy.float32 or float64 in this operator. + ctx : Context, optional + An optional device context (default is the current default context). + + Returns + ------- + out : _Symbol, shape(M,) + The window, with the maximum value normalized to one (the value + one appears only if `M` is odd). + + See Also + -------- + blackman, hamming + + Notes + ----- + The Hanning window is defined as + + .. math:: w(n) = 0.5 - 0.5cos\left(\frac{2\pi{n}}{M-1}\right) + \qquad 0 \leq n \leq M-1 + + The Hanning was named for Julius von Hann, an Austrian meteorologist. + It is also known as the Cosine Bell. Some authors prefer that it be + called a Hann window, to help avoid confusion with the very similar + Hamming window. + + Most references to the Hanning window come from the signal processing + literature, where it is used as one of many windowing functions for + smoothing values. It is also known as an apodization (which means + "removing the foot", i.e. smoothing discontinuities at the beginning + and end of the sampled signal) or tapering function. + + References + ---------- + .. [1] Blackman, R.B. and Tukey, J.W., (1958) The measurement of power + spectra, Dover Publications, New York. + .. [2] E.R. Kanasewich, "Time Sequence Analysis in Geophysics", + The University of Alberta Press, 1975, pp. 106-108. + .. [3] Wikipedia, "Window function", + http://en.wikipedia.org/wiki/Window_function + .. [4] W.H. Press, B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, + "Numerical Recipes", Cambridge University Press, 1986, page 425. + + Examples + -------- + >>> np.hanning(12) + array([0. , 0.07937324, 0.29229254, 0.5711574 , 0.8274304 , + 0.9797465 , 0.97974646, 0.82743025, 0.5711573 , 0.29229245, + 0.07937312, 0. ]) + + Plot the window and its frequency response: + + >>> import matplotlib.pyplot as plt + >>> window = np.hanning(51) + >>> plt.plot(window.asnumpy()) + [] + >>> plt.title("Hann window") + Text(0.5, 1.0, 'Hann window') + >>> plt.ylabel("Amplitude") + Text(0, 0.5, 'Amplitude') + >>> plt.xlabel("Sample") + Text(0.5, 0, 'Sample') + >>> plt.show() + """ + if ctx is None: + ctx = current_context() + return _npi.hanning(M, dtype=dtype, ctx=ctx) + + +@set_module('mxnet.symbol.numpy') +def hamming(M, dtype=_np.float32, ctx=None): + r"""Return the hamming window. + + The hamming window is a taper formed by using a weighted cosine. + + Parameters + ---------- + M : int + Number of points in the output window. If zero or less, an + empty array is returned. + dtype : str or numpy.dtype, optional + An optional value type. Default is `float32`. Note that you need + select numpy.float32 or float64 in this operator. + ctx : Context, optional + An optional device context (default is the current default context). + + Returns + ------- + out : _Symbol, shape(M,) + The window, with the maximum value normalized to one (the value + one appears only if `M` is odd). + + See Also + -------- + blackman, hanning + + Notes + ----- + The Hamming window is defined as + + .. math:: w(n) = 0.54 - 0.46cos\left(\frac{2\pi{n}}{M-1}\right) + \qquad 0 \leq n \leq M-1 + + The Hamming was named for R. W. Hamming, an associate of J. W. Tukey + and is described in Blackman and Tukey. It was recommended for + smoothing the truncated autocovariance function in the time domain. + Most references to the Hamming window come from the signal processing + literature, where it is used as one of many windowing functions for + smoothing values. It is also known as an apodization (which means + "removing the foot", i.e. smoothing discontinuities at the beginning + and end of the sampled signal) or tapering function. + + References + ---------- + .. [1] Blackman, R.B. and Tukey, J.W., (1958) The measurement of power + spectra, Dover Publications, New York. + .. [2] E.R. Kanasewich, "Time Sequence Analysis in Geophysics", The + University of Alberta Press, 1975, pp. 109-110. + .. [3] Wikipedia, "Window function", + https://en.wikipedia.org/wiki/Window_function + .. [4] W.H. Press, B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, + "Numerical Recipes", Cambridge University Press, 1986, page 425. + + Examples + -------- + >>> np.hamming(12) + array([0.08000001, 0.15302339, 0.34890914, 0.6054648 , 0.841236 , + 0.9813669 , 0.9813668 , 0.8412359 , 0.6054647 , 0.34890908, + 0.15302327, 0.08000001]) + + Plot the window and its frequency response: + + >>> import matplotlib.pyplot as plt + >>> window = np.hamming(51) + >>> plt.plot(window.asnumpy()) + [] + >>> plt.title("hamming window") + Text(0.5, 1.0, 'hamming window') + >>> plt.ylabel("Amplitude") + Text(0, 0.5, 'Amplitude') + >>> plt.xlabel("Sample") + Text(0.5, 0, 'Sample') + >>> plt.show() + """ + if ctx is None: + ctx = current_context() + return _npi.hamming(M, dtype=dtype, ctx=ctx) + + +@set_module('mxnet.symbol.numpy') +def blackman(M, dtype=_np.float32, ctx=None): + r"""Return the Blackman window. + + The Blackman window is a taper formed by using the first three + terms of a summation of cosines. It was designed to have close to the + minimal leakage possible. It is close to optimal, only slightly worse + than a Kaiser window. + + Parameters + ---------- + M : int + Number of points in the output window. If zero or less, an + empty array is returned. + dtype : str or numpy.dtype, optional + An optional value type. Default is `float32`. Note that you need + select numpy.float32 or float64 in this operator. + ctx : Context, optional + An optional device context (default is the current default context). + + Returns + ------- + out : _Symbol + The window, with the maximum value normalized to one (the value one + appears only if the number of samples is odd). + + See Also + -------- + hamming, hanning + + Notes + ----- + The Blackman window is defined as + + .. math:: w(n) = 0.42 - 0.5 \cos(2\pi n/{M-1}) + 0.08 \cos(4\pi n/{M-1}) + + Most references to the Blackman window come from the signal processing + literature, where it is used as one of many windowing functions for + smoothing values. It is also known as an apodization (which means + "removing the foot", i.e. smoothing discontinuities at the beginning + and end of the sampled signal) or tapering function. It is known as a + "near optimal" tapering function, almost as good (by some measures) + as the kaiser window. + + References + ---------- + Blackman, R.B. and Tukey, J.W., (1958) The measurement of power spectra, + Dover Publications, New York. + + Oppenheim, A.V., and R.W. Schafer. Discrete-Time Signal Processing. + Upper Saddle River, NJ: Prentice-Hall, 1999, pp. 468-471. + + Examples + -------- + >>> np.blackman(12) + array([-1.4901161e-08, 3.2606423e-02, 1.5990365e-01, 4.1439798e-01, + 7.3604530e-01, 9.6704686e-01, 9.6704674e-01, 7.3604506e-01, + 4.1439781e-01, 1.5990359e-01, 3.2606363e-02, -1.4901161e-08]) + + Plot the window and its frequency response: + + >>> import matplotlib.pyplot as plt + >>> window = np.blackman(51) + >>> plt.plot(window.asnumpy()) + [] + >>> plt.title("blackman window") + Text(0.5, 1.0, 'blackman window') + >>> plt.ylabel("Amplitude") + Text(0, 0.5, 'Amplitude') + >>> plt.xlabel("Sample") + Text(0.5, 0, 'Sample') + >>> plt.show() + """ + if ctx is None: + ctx = current_context() + return _npi.blackman(M, dtype=dtype, ctx=ctx) + + _set_np_symbol_class(_Symbol) diff --git a/src/operator/numpy/np_window_op.cc b/src/operator/numpy/np_window_op.cc new file mode 100644 index 000000000000..91338a134edc --- /dev/null +++ b/src/operator/numpy/np_window_op.cc @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! + * Copyright (c) 2019 by Contributors + * \file np_window_op.cc + * \brief CPU Implementation of unary op hanning, hamming, blackman window. + */ + +#include "np_window_op.h" + +namespace mxnet { +namespace op { + +DMLC_REGISTER_PARAMETER(NumpyWindowsParam); + +inline bool NumpyWindowsShape(const nnvm::NodeAttrs& attrs, + mxnet::ShapeVector* in_shapes, + mxnet::ShapeVector* out_shapes) { + const NumpyWindowsParam& param = nnvm::get(attrs.parsed); + CHECK_EQ(in_shapes->size(), 0U); + CHECK_EQ(out_shapes->size(), 1U); + CHECK(param.M.has_value()) << "missing 1 required positional argument: 'M'"; + int64_t out_size = param.M.value() <= 0 ? 0 : param.M.value(); + SHAPE_ASSIGN_CHECK(*out_shapes, 0, mxnet::TShape({static_cast(out_size)})); + return true; +} + +NNVM_REGISTER_OP(_npi_hanning) +.describe("Return the Hanning window." + "The Hanning window is a taper formed by using a weighted cosine.") +.set_num_inputs(0) +.set_num_outputs(1) +.set_attr_parser(ParamParser) +.set_attr("FInferShape", NumpyWindowsShape) +.set_attr("FInferType", InitType) +.set_attr("FCompute", NumpyWindowCompute) +.add_arguments(NumpyWindowsParam::__FIELDS__()); + +NNVM_REGISTER_OP(_npi_hamming) +.describe("Return the Hamming window." + "The Hamming window is a taper formed by using a weighted cosine.") +.set_num_inputs(0) +.set_num_outputs(1) +.set_attr_parser(ParamParser) +.set_attr("FInferShape", NumpyWindowsShape) +.set_attr("FInferType", InitType) +.set_attr("FCompute", NumpyWindowCompute) +.add_arguments(NumpyWindowsParam::__FIELDS__()); + +NNVM_REGISTER_OP(_npi_blackman) +.describe("Return the Blackman window." + "The Blackman window is a taper formed by using a weighted cosine.") +.set_num_inputs(0) +.set_num_outputs(1) +.set_attr_parser(ParamParser) +.set_attr("FInferShape", NumpyWindowsShape) +.set_attr("FInferType", InitType) +.set_attr("FCompute", NumpyWindowCompute) +.add_arguments(NumpyWindowsParam::__FIELDS__()); + +} // namespace op +} // namespace mxnet diff --git a/src/operator/numpy/np_window_op.cu b/src/operator/numpy/np_window_op.cu new file mode 100644 index 000000000000..d56a96e9282f --- /dev/null +++ b/src/operator/numpy/np_window_op.cu @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! + * Copyright (c) 2019 by Contributors + * \file np_window_op.cu + * \brief CPU Implementation of unary op hanning, hamming, blackman window. + */ + +#include "np_window_op.h" + +namespace mxnet { +namespace op { + +NNVM_REGISTER_OP(_npi_hanning) +.set_attr("FCompute", NumpyWindowCompute); + +NNVM_REGISTER_OP(_npi_hamming) +.set_attr("FCompute", NumpyWindowCompute); + +NNVM_REGISTER_OP(_npi_blackman) +.set_attr("FCompute", NumpyWindowCompute); + +} // namespace op +} // namespace mxnet diff --git a/src/operator/numpy/np_window_op.h b/src/operator/numpy/np_window_op.h new file mode 100644 index 000000000000..7501c7643b8e --- /dev/null +++ b/src/operator/numpy/np_window_op.h @@ -0,0 +1,127 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! + * Copyright (c) 2019 by Contributors + * \file np_window_op.h + * \brief CPU Implementation of unary op hanning, hamming, blackman window. + */ + +#ifndef MXNET_OPERATOR_NUMPY_NP_WINDOW_OP_H_ +#define MXNET_OPERATOR_NUMPY_NP_WINDOW_OP_H_ + +#include +#include +#include "../tensor/init_op.h" + +namespace mxnet { +namespace op { + +#ifdef __CUDA_ARCH__ +__constant__ const float PI = 3.14159265358979323846; +#else +const float PI = 3.14159265358979323846; +using std::isnan; +#endif + +struct NumpyWindowsParam : public dmlc::Parameter { + dmlc::optional M; + std::string ctx; + int dtype; + DMLC_DECLARE_PARAMETER(NumpyWindowsParam) { + DMLC_DECLARE_FIELD(M) + .set_default(dmlc::optional()) + .describe("Number of points in the output window. " + "If zero or less, an empty array is returned."); + DMLC_DECLARE_FIELD(ctx) + .set_default("") + .describe("Context of output, in format [cpu|gpu|cpu_pinned](n)." + "Only used for imperative calls."); + DMLC_DECLARE_FIELD(dtype) + .set_default(mshadow::kFloat32) + MXNET_ADD_ALL_TYPES + .describe("Data-type of the returned array."); + } +}; + +struct hanning_fwd { + template + MSHADOW_XINLINE static void Map(index_t i, index_t M, int req, DType* out) { + if (M == 1) { + KERNEL_ASSIGN(out[i], req, static_cast(1)); + } else { + KERNEL_ASSIGN(out[i], req, DType(0.5) - DType(0.5) * math::cos(DType(2 * PI * i / (M - 1)))); + } + } +}; + +struct hamming_fwd { + template + MSHADOW_XINLINE static void Map(index_t i, index_t M, int req, DType* out) { + if (M == 1) { + KERNEL_ASSIGN(out[i], req, static_cast(1)); + } else { + KERNEL_ASSIGN(out[i], req, + DType(0.54) - DType(0.46) * math::cos(DType(2 * PI * i / (M - 1)))); + } + } +}; + +struct blackman_fwd { + template + MSHADOW_XINLINE static void Map(index_t i, index_t M, int req, DType* out) { + if (M == 1) { + KERNEL_ASSIGN(out[i], req, static_cast(1)); + } else { + KERNEL_ASSIGN(out[i], req, DType(0.42) - DType(0.5) * math::cos(DType(2 * PI * i /(M - 1))) + + DType(0.08) * math::cos(DType(4 * PI * i /(M - 1)))); + } + } +}; + +template +void NumpyWindowCompute(const nnvm::NodeAttrs& attrs, + const OpContext& ctx, + const std::vector& inputs, + const std::vector& req, + const std::vector& outputs) { + using namespace mxnet_op; + mshadow::Stream *s = ctx.get_stream(); + const NumpyWindowsParam& param = nnvm::get(attrs.parsed); + if (param.M.has_value() && param.M.value() <= 0) return; + MSHADOW_TYPE_SWITCH(outputs[0].type_flag_, DType, { + if (window_select == 0) { + Kernel::Launch(s, outputs[0].Size(), static_cast(param.M.value()), + req[0], outputs[0].dptr()); + } else if (window_select == 1) { + Kernel::Launch(s, outputs[0].Size(), static_cast(param.M.value()), + req[0], outputs[0].dptr()); + } else if (window_select == 2) { + Kernel::Launch(s, outputs[0].Size(), static_cast(param.M.value()), + req[0], outputs[0].dptr()); + } else { + LOG(FATAL) << "window_select must be (0, 1, 2)"; + } + }); +} + +} // namespace op +} // namespace mxnet + +#endif // MXNET_OPERATOR_NUMPY_NP_WINDOW_OP_H_ diff --git a/tests/python/unittest/test_numpy_op.py b/tests/python/unittest/test_numpy_op.py index 8846d57dbe32..ad877bab5b7d 100644 --- a/tests/python/unittest/test_numpy_op.py +++ b/tests/python/unittest/test_numpy_op.py @@ -1899,10 +1899,10 @@ def get_grad_left(a1, a2): sign = 2 * sign.astype(int) - 1 sign = sign.reshape(a1.shape) return sign - + def get_grad_right(a1, a2): return _np.zeros_like(a2) - + shapes = [ (), (1), @@ -1943,7 +1943,7 @@ def get_grad_right(a1, a2): mx_out = np.copysign(a1, a2) expected_np = _np.copysign(a1_np, a2_np) assert_almost_equal(mx_out.asnumpy(), expected_np, rtol=rtol, atol=atol) - + types = ['float16', 'float32', 'float64'] for x_shape in shapes: for dtype in types: @@ -1957,12 +1957,12 @@ def get_grad_right(a1, a2): mx_out = np.copysign(x, scalar) assert mx_out.shape == expected_np.shape assert_almost_equal(mx_out.asnumpy(), expected_np, rtol=rtol, atol=atol) - + # Test gradient mx_out.backward() x_grad = get_grad_left(x_np, scalar) assert_almost_equal(x.grad.asnumpy(), x_grad, rtol=rtol, atol=atol) - + # Test right x_np = _np.array(_np.random.uniform(-2.0, 2.0, x_shape), dtype=dtype) scalar = _np.random.uniform(-2.0, 2.0) @@ -1973,7 +1973,7 @@ def get_grad_right(a1, a2): mx_out = np.copysign(scalar, x) assert mx_out.shape == expected_np.shape assert_almost_equal(mx_out.asnumpy(), expected_np, rtol=rtol, atol=atol) - + # Test gradient mx_out.backward() x_grad = get_grad_right(scalar, x_np) @@ -2271,6 +2271,42 @@ def g(data, axis1, axis2, offset): assert False +@with_seed() +@use_np +def test_np_windows(): + class TestWindows(HybridBlock): + def __init__(self, func, M, dtype): + super(TestWindows, self).__init__() + self._func = func + self._M = M + self._dtype = dtype + + def hybrid_forward(self, F, x, *args, **kwargs): + op = getattr(F.np, self._func) + assert op is not None + return x + op(M=self._M, dtype=self._dtype) + + configs = [-10, -3, -1, 0, 1, 6, 10, 20] + dtypes = ['float32', 'float64'] + funcs = ['hanning', 'hamming', 'blackman'] + for config in configs: + for dtype in dtypes: + for func in funcs: + x = np.zeros(shape=(), dtype=dtype) + for hybridize in [False, True]: + np_func = getattr(_np, func) + mx_func = TestWindows(func, M=config, dtype=dtype) + np_out = np_func(M=config).astype(dtype) + if hybridize: + mx_func.hybridize() + mx_out = mx_func(x) + assert_almost_equal(mx_out.asnumpy(), np_out, rtol=1e-3, atol=1e-5) + # test imperative + mx_out = getattr(np, func)(M=config, dtype=dtype) + np_out = np_func(M=config).astype(dtype) + assert_almost_equal(mx_out.asnumpy(), np_out, rtol=1e-3, atol=1e-5) + + if __name__ == '__main__': import nose nose.runmodule()