File size: 4,814 Bytes
3662621 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
import numpy
import cupy
from cupy import _core
_blackman_kernel = _core.ElementwiseKernel(
"float32 alpha",
"float64 out",
"""
out = 0.42 - 0.5 * cos(i * alpha) + 0.08 * cos(2 * alpha * i);
""", name="cupy_blackman")
_bartlett_kernel = _core.ElementwiseKernel(
"float32 alpha",
"T arr",
"""
if (i < alpha)
arr = i / alpha;
else
arr = 2.0 - i / alpha;
""", name="cupy_bartlett")
def bartlett(M):
"""Returns the Bartlett window.
The Bartlett window is defined as
.. math::
w(n) = \\frac{2}{M-1} \\left(
\\frac{M-1}{2} - \\left|n - \\frac{M-1}{2}\\right|
\\right)
Args:
M (int):
Number of points in the output window. If zero or less, an empty
array is returned.
Returns:
~cupy.ndarray: Output ndarray.
.. seealso:: :func:`numpy.bartlett`
"""
if M == 1:
return cupy.ones(1, dtype=cupy.float64)
if M <= 0:
return cupy.array([])
alpha = (M - 1) / 2.0
out = cupy.empty(M, dtype=cupy.float64)
return _bartlett_kernel(alpha, out)
def blackman(M):
"""Returns the Blackman window.
The Blackman window is defined as
.. math::
w(n) = 0.42 - 0.5 \\cos\\left(\\frac{2\\pi{n}}{M-1}\\right)
+ 0.08 \\cos\\left(\\frac{4\\pi{n}}{M-1}\\right)
\\qquad 0 \\leq n \\leq M-1
Args:
M (:class:`~int`):
Number of points in the output window. If zero or less, an empty
array is returned.
Returns:
~cupy.ndarray: Output ndarray.
.. seealso:: :func:`numpy.blackman`
"""
if M == 1:
return cupy.ones(1, dtype=cupy.float64)
if M <= 0:
return cupy.array([])
alpha = numpy.pi * 2 / (M - 1)
out = cupy.empty(M, dtype=cupy.float64)
return _blackman_kernel(alpha, out)
_hamming_kernel = _core.ElementwiseKernel(
"float32 alpha",
"float64 out",
"""
out = 0.54 - 0.46 * cos(i * alpha);
""", name="cupy_hamming")
def hamming(M):
"""Returns the Hamming window.
The Hamming window is defined as
.. math::
w(n) = 0.54 - 0.46\\cos\\left(\\frac{2\\pi{n}}{M-1}\\right)
\\qquad 0 \\leq n \\leq M-1
Args:
M (:class:`~int`):
Number of points in the output window. If zero or less, an empty
array is returned.
Returns:
~cupy.ndarray: Output ndarray.
.. seealso:: :func:`numpy.hamming`
"""
if M == 1:
return cupy.ones(1, dtype=cupy.float64)
if M <= 0:
return cupy.array([])
alpha = numpy.pi * 2 / (M - 1)
out = cupy.empty(M, dtype=cupy.float64)
return _hamming_kernel(alpha, out)
_hanning_kernel = _core.ElementwiseKernel(
"float32 alpha",
"float64 out",
"""
out = 0.5 - 0.5 * cos(i * alpha);
""", name="cupy_hanning")
def hanning(M):
"""Returns the Hanning window.
The Hanning window is defined as
.. math::
w(n) = 0.5 - 0.5\\cos\\left(\\frac{2\\pi{n}}{M-1}\\right)
\\qquad 0 \\leq n \\leq M-1
Args:
M (:class:`~int`):
Number of points in the output window. If zero or less, an empty
array is returned.
Returns:
~cupy.ndarray: Output ndarray.
.. seealso:: :func:`numpy.hanning`
"""
if M == 1:
return cupy.ones(1, dtype=cupy.float64)
if M <= 0:
return cupy.array([])
alpha = numpy.pi * 2 / (M - 1)
out = cupy.empty(M, dtype=cupy.float64)
return _hanning_kernel(alpha, out)
_kaiser_kernel = _core.ElementwiseKernel(
"float32 beta, float32 alpha",
"T arr",
"""
float temp = (i - alpha) / alpha;
arr = cyl_bessel_i0(beta * sqrt(1 - (temp * temp)));
arr /= cyl_bessel_i0(beta);
""", name="cupy_kaiser")
def kaiser(M, beta):
"""Return the Kaiser window.
The Kaiser window is a taper formed by using a Bessel function.
.. math:: w(n) = I_0\\left( \\beta \\sqrt{1-\\frac{4n^2}{(M-1)^2}}
\\right)/I_0(\\beta)
with
.. math:: \\quad -\\frac{M-1}{2} \\leq n \\leq \\frac{M-1}{2}
where :math:`I_0` is the modified zeroth-order Bessel function.
Args:
M (int):
Number of points in the output window. If zero or less, an empty
array is returned.
beta (float):
Shape parameter for window
Returns:
~cupy.ndarray: The window, with the maximum value normalized to one
(the value one appears only if the number of samples is odd).
.. seealso:: :func:`numpy.kaiser`
"""
if M == 1:
return cupy.array([1.])
if M <= 0:
return cupy.array([])
alpha = (M - 1) / 2.0
out = cupy.empty(M, dtype=cupy.float64)
return _kaiser_kernel(beta, alpha, out)
|