|
45 | 45 | import dpnp
|
46 | 46 | import dpnp.backend.extensions.window._window_impl as wi
|
47 | 47 |
|
48 |
| -__all__ = ["hamming"] |
| 48 | +__all__ = ["hamming", "hanning"] |
| 49 | + |
| 50 | + |
| 51 | +def _call_window_kernel( |
| 52 | + M, _window_kernel, device=None, usm_type=None, sycl_queue=None |
| 53 | +): |
| 54 | + |
| 55 | + try: |
| 56 | + M = int(M) |
| 57 | + except Exception as e: |
| 58 | + raise TypeError("M must be an integer") from e |
| 59 | + |
| 60 | + cfd_kwarg = { |
| 61 | + "device": device, |
| 62 | + "usm_type": usm_type, |
| 63 | + "sycl_queue": sycl_queue, |
| 64 | + } |
| 65 | + |
| 66 | + if M < 1: |
| 67 | + return dpnp.empty(0, **cfd_kwarg) |
| 68 | + if M == 1: |
| 69 | + return dpnp.ones(1, **cfd_kwarg) |
| 70 | + |
| 71 | + result = dpnp.empty(M, **cfd_kwarg) |
| 72 | + exec_q = result.sycl_queue |
| 73 | + _manager = dpu.SequentialOrderManager[exec_q] |
| 74 | + |
| 75 | + ht_ev, win_ev = _window_kernel( |
| 76 | + exec_q, dpnp.get_usm_ndarray(result), depends=_manager.submitted_events |
| 77 | + ) |
| 78 | + |
| 79 | + _manager.add_event_pair(ht_ev, win_ev) |
| 80 | + |
| 81 | + return result |
49 | 82 |
|
50 | 83 |
|
51 | 84 | def hamming(M, device=None, usm_type=None, sycl_queue=None):
|
@@ -127,30 +160,90 @@ def hamming(M, device=None, usm_type=None, sycl_queue=None):
|
127 | 160 |
|
128 | 161 | """
|
129 | 162 |
|
130 |
| - try: |
131 |
| - M = int(M) |
132 |
| - except Exception as e: |
133 |
| - raise TypeError("M must be an integer") from e |
| 163 | + return _call_window_kernel( |
| 164 | + M, wi._hamming, device=device, usm_type=usm_type, sycl_queue=sycl_queue |
| 165 | + ) |
134 | 166 |
|
135 |
| - cfd_kwarg = { |
136 |
| - "device": device, |
137 |
| - "usm_type": usm_type, |
138 |
| - "sycl_queue": sycl_queue, |
139 |
| - } |
140 | 167 |
|
141 |
| - if M < 1: |
142 |
| - return dpnp.empty(0, **cfd_kwarg) |
143 |
| - if M == 1: |
144 |
| - return dpnp.ones(1, **cfd_kwarg) |
| 168 | +def hanning(M, device=None, usm_type=None, sycl_queue=None): |
| 169 | + r""" |
| 170 | + Return the Hanning window. |
145 | 171 |
|
146 |
| - result = dpnp.empty(M, **cfd_kwarg) |
147 |
| - exec_q = result.sycl_queue |
148 |
| - _manager = dpu.SequentialOrderManager[exec_q] |
| 172 | + The Hanning window is a taper formed by using a weighted cosine. |
149 | 173 |
|
150 |
| - ht_ev, win_ev = wi._hamming( |
151 |
| - exec_q, dpnp.get_usm_ndarray(result), depends=_manager.submitted_events |
152 |
| - ) |
| 174 | + For full documentation refer to :obj:`numpy.hanning`. |
153 | 175 |
|
154 |
| - _manager.add_event_pair(ht_ev, win_ev) |
| 176 | + Parameters |
| 177 | + ---------- |
| 178 | + M : int |
| 179 | + Number of points in the output window. If zero or less, an empty array |
| 180 | + is returned. |
| 181 | + device : {None, string, SyclDevice, SyclQueue, Device}, optional |
| 182 | + An array API concept of device where the output array is created. |
| 183 | + `device` can be ``None``, a oneAPI filter selector string, an instance |
| 184 | + of :class:`dpctl.SyclDevice` corresponding to a non-partitioned SYCL |
| 185 | + device, an instance of :class:`dpctl.SyclQueue`, or a |
| 186 | + :class:`dpctl.tensor.Device` object returned by |
| 187 | + :attr:`dpnp.ndarray.device`. |
155 | 188 |
|
156 |
| - return result |
| 189 | + Default: ``None``. |
| 190 | + usm_type : {None, "device", "shared", "host"}, optional |
| 191 | + The type of SYCL USM allocation for the output array. |
| 192 | +
|
| 193 | + Default: ``None``. |
| 194 | + sycl_queue : {None, SyclQueue}, optional |
| 195 | + A SYCL queue to use for output array allocation and copying. The |
| 196 | + `sycl_queue` can be passed as ``None`` (the default), which means |
| 197 | + to get the SYCL queue from `device` keyword if present or to use |
| 198 | + a default queue. |
| 199 | +
|
| 200 | + Default: ``None``. |
| 201 | +
|
| 202 | + Returns |
| 203 | + ------- |
| 204 | + out : dpnp.ndarray of shape (M,) |
| 205 | + The window, with the maximum value normalized to one (the value one |
| 206 | + appears only if the number of samples is odd). |
| 207 | +
|
| 208 | + See Also |
| 209 | + -------- |
| 210 | + :obj:`dpnp.bartlett` : Return the Bartlett window. |
| 211 | + :obj:`dpnp.blackman` : Return the Blackman window. |
| 212 | + :obj:`dpnp.hamming` : Return the Hamming window. |
| 213 | + :obj:`dpnp.kaiser` : Return the Kaiser window. |
| 214 | +
|
| 215 | + Notes |
| 216 | + ----- |
| 217 | + The Hanning window is defined as |
| 218 | +
|
| 219 | + .. math:: w(n) = 0.5 - 0.5\cos\left(\frac{2\pi{n}}{M-1}\right) |
| 220 | + \qquad 0 \leq n \leq M-1 |
| 221 | +
|
| 222 | + Examples |
| 223 | + -------- |
| 224 | + >>> import dpnp as np |
| 225 | + >>> np.hanning(12) |
| 226 | + array([0. , 0.07937323, 0.29229249, 0.57115742, 0.82743037, |
| 227 | + 0.97974649, 0.97974649, 0.82743037, 0.57115742, 0.29229249, |
| 228 | + 0.07937323, 0. ]) |
| 229 | +
|
| 230 | + Creating the output array on a different device or with a |
| 231 | + specified usm_type: |
| 232 | +
|
| 233 | + >>> x = np.hanning(4) # default case |
| 234 | + >>> x, x.device, x.usm_type |
| 235 | + (array([0. , 0.75, 0.75, 0. ]), Device(level_zero:gpu:0), 'device') |
| 236 | +
|
| 237 | + >>> y = np.hanning(4, device="cpu") |
| 238 | + >>> y, y.device, y.usm_type |
| 239 | + (array([0. , 0.75, 0.75, 0. ]), Device(opencl:cpu:0), 'device') |
| 240 | +
|
| 241 | + >>> z = np.hanning(4, usm_type="host") |
| 242 | + >>> z, z.device, z.usm_type |
| 243 | + (array([0. , 0.75, 0.75, 0. ]), Device(level_zero:gpu:0), 'host') |
| 244 | +
|
| 245 | + """ |
| 246 | + |
| 247 | + return _call_window_kernel( |
| 248 | + M, wi._hanning, device=device, usm_type=usm_type, sycl_queue=sycl_queue |
| 249 | + ) |
0 commit comments