|
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 | 49 |
|
50 | 50 |
|
51 | 51 | def hamming(M, device=None, usm_type=None, sycl_queue=None): |
@@ -154,3 +154,111 @@ def hamming(M, device=None, usm_type=None, sycl_queue=None): |
154 | 154 | _manager.add_event_pair(ht_ev, win_ev) |
155 | 155 |
|
156 | 156 | return result |
| 157 | + |
| 158 | + |
| 159 | +def hanning(M, device=None, usm_type=None, sycl_queue=None): |
| 160 | + r""" |
| 161 | + Return the Hanning window. |
| 162 | +
|
| 163 | + The Hanning window is a taper formed by using a weighted cosine. |
| 164 | +
|
| 165 | + For full documentation refer to :obj:`numpy.hanning`. |
| 166 | +
|
| 167 | + Parameters |
| 168 | + ---------- |
| 169 | + M : int |
| 170 | + Number of points in the output window. If zero or less, an empty array |
| 171 | + is returned. |
| 172 | + device : {None, string, SyclDevice, SyclQueue, Device}, optional |
| 173 | + An array API concept of device where the output array is created. |
| 174 | + `device` can be ``None``, a oneAPI filter selector string, an instance |
| 175 | + of :class:`dpctl.SyclDevice` corresponding to a non-partitioned SYCL |
| 176 | + device, an instance of :class:`dpctl.SyclQueue`, or a |
| 177 | + :class:`dpctl.tensor.Device` object returned by |
| 178 | + :attr:`dpnp.ndarray.device`. |
| 179 | +
|
| 180 | + Default: ``None``. |
| 181 | + usm_type : {None, "device", "shared", "host"}, optional |
| 182 | + The type of SYCL USM allocation for the output array. |
| 183 | +
|
| 184 | + Default: ``None``. |
| 185 | + sycl_queue : {None, SyclQueue}, optional |
| 186 | + A SYCL queue to use for output array allocation and copying. The |
| 187 | + `sycl_queue` can be passed as ``None`` (the default), which means |
| 188 | + to get the SYCL queue from `device` keyword if present or to use |
| 189 | + a default queue. |
| 190 | +
|
| 191 | + Default: ``None``. |
| 192 | +
|
| 193 | + Returns |
| 194 | + ------- |
| 195 | + out : dpnp.ndarray of shape (M,) |
| 196 | + The window, with the maximum value normalized to one (the value one |
| 197 | + appears only if the number of samples is odd). |
| 198 | +
|
| 199 | + See Also |
| 200 | + -------- |
| 201 | + :obj:`dpnp.bartlett` : Return the Bartlett window. |
| 202 | + :obj:`dpnp.blackman` : Return the Blackman window. |
| 203 | + :obj:`dpnp.hamming` : Return the Hamming window. |
| 204 | + :obj:`dpnp.kaiser` : Return the Kaiser window. |
| 205 | +
|
| 206 | + Notes |
| 207 | + ----- |
| 208 | + The Hanning window is defined as |
| 209 | +
|
| 210 | + .. math:: w(n) = 0.5 - 0.5\cos\left(\frac{2\pi{n}}{M-1}\right) |
| 211 | + \qquad 0 \leq n \leq M-1 |
| 212 | +
|
| 213 | + Examples |
| 214 | + -------- |
| 215 | + >>> import dpnp as np |
| 216 | + >>> np.hanning(12) |
| 217 | + array([0. , 0.07937323, 0.29229249, 0.57115742, 0.82743037, |
| 218 | + 0.97974649, 0.97974649, 0.82743037, 0.57115742, 0.29229249, |
| 219 | + 0.07937323, 0. ]) |
| 220 | +
|
| 221 | + Creating the output array on a different device or with a |
| 222 | + specified usm_type: |
| 223 | +
|
| 224 | + >>> x = np.hanning(4) # default case |
| 225 | + >>> x, x.device, x.usm_type |
| 226 | + (array([0. , 0.75, 0.75, 0. ]), Device(level_zero:gpu:0), 'device') |
| 227 | +
|
| 228 | + >>> y = np.hanning(4, device="cpu") |
| 229 | + >>> y, y.device, y.usm_type |
| 230 | + (array([0. , 0.75, 0.75, 0. ]), Device(opencl:cpu:0), 'device') |
| 231 | +
|
| 232 | + >>> z = np.hanning(4, usm_type="host") |
| 233 | + >>> z, z.device, z.usm_type |
| 234 | + (array([0. , 0.75, 0.75, 0. ]), Device(level_zero:gpu:0), 'host') |
| 235 | +
|
| 236 | + """ |
| 237 | + |
| 238 | + try: |
| 239 | + M = int(M) |
| 240 | + except Exception as e: |
| 241 | + raise TypeError("M must be an integer") from e |
| 242 | + |
| 243 | + cfd_kwarg = { |
| 244 | + "device": device, |
| 245 | + "usm_type": usm_type, |
| 246 | + "sycl_queue": sycl_queue, |
| 247 | + } |
| 248 | + |
| 249 | + if M < 1: |
| 250 | + return dpnp.empty(0, **cfd_kwarg) |
| 251 | + if M == 1: |
| 252 | + return dpnp.ones(1, **cfd_kwarg) |
| 253 | + |
| 254 | + result = dpnp.empty(int(M), **cfd_kwarg) |
| 255 | + exec_q = result.sycl_queue |
| 256 | + _manager = dpu.SequentialOrderManager[exec_q] |
| 257 | + |
| 258 | + ht_ev, win_ev = wi._hanning( |
| 259 | + exec_q, dpnp.get_usm_ndarray(result), depends=_manager.submitted_events |
| 260 | + ) |
| 261 | + |
| 262 | + _manager.add_event_pair(ht_ev, win_ev) |
| 263 | + |
| 264 | + return result |
0 commit comments