|
6 | 6 | if TYPE_CHECKING:
|
7 | 7 | from ._typing import Array, ModuleType
|
8 | 8 |
|
9 |
| -__all__ = ["atleast_nd", "cov", "expand_dims", "kron"] |
| 9 | +__all__ = ["atleast_nd", "cov", "expand_dims", "kron", "sinc"] |
10 | 10 |
|
11 | 11 |
|
12 | 12 | def atleast_nd(x: Array, /, *, ndim: int, xp: ModuleType) -> Array:
|
@@ -348,3 +348,88 @@ def kron(a: Array, b: Array, /, *, xp: ModuleType) -> Array:
|
348 | 348 | a_shape = xp.asarray(a_shape)
|
349 | 349 | b_shape = xp.asarray(b_shape)
|
350 | 350 | return xp.reshape(result, tuple(xp.multiply(a_shape, b_shape)))
|
| 351 | + |
| 352 | + |
| 353 | +def sinc(x: Array, /, *, xp: ModuleType) -> Array: |
| 354 | + r""" |
| 355 | + Return the normalized sinc function. |
| 356 | +
|
| 357 | + The sinc function is equal to :math:`\sin(\pi x)/(\pi x)` for any argument |
| 358 | + :math:`x\ne 0`. ``sinc(0)`` takes the limit value 1, making ``sinc`` not |
| 359 | + only everywhere continuous but also infinitely differentiable. |
| 360 | +
|
| 361 | + .. note:: |
| 362 | +
|
| 363 | + Note the normalization factor of ``pi`` used in the definition. |
| 364 | + This is the most commonly used definition in signal processing. |
| 365 | + Use ``sinc(x / xp.pi)`` to obtain the unnormalized sinc function |
| 366 | + :math:`\sin(x)/x` that is more common in mathematics. |
| 367 | +
|
| 368 | + Parameters |
| 369 | + ---------- |
| 370 | + x : array |
| 371 | + Array (possibly multi-dimensional) of values for which to calculate |
| 372 | + ``sinc(x)``. Must have a real floating point dtype. |
| 373 | + xp : array_namespace |
| 374 | + The standard-compatible namespace for `x`. |
| 375 | +
|
| 376 | + Returns |
| 377 | + ------- |
| 378 | + res : array |
| 379 | + ``sinc(x)`` calculated elementwise, which has the same shape as the input. |
| 380 | +
|
| 381 | + Notes |
| 382 | + ----- |
| 383 | + The name sinc is short for "sine cardinal" or "sinus cardinalis". |
| 384 | +
|
| 385 | + The sinc function is used in various signal processing applications, |
| 386 | + including in anti-aliasing, in the construction of a Lanczos resampling |
| 387 | + filter, and in interpolation. |
| 388 | +
|
| 389 | + For bandlimited interpolation of discrete-time signals, the ideal |
| 390 | + interpolation kernel is proportional to the sinc function. |
| 391 | +
|
| 392 | + References |
| 393 | + ---------- |
| 394 | + .. [1] Weisstein, Eric W. "Sinc Function." From MathWorld--A Wolfram Web |
| 395 | + Resource. https://mathworld.wolfram.com/SincFunction.html |
| 396 | + .. [2] Wikipedia, "Sinc function", |
| 397 | + https://en.wikipedia.org/wiki/Sinc_function |
| 398 | +
|
| 399 | + Examples |
| 400 | + -------- |
| 401 | + >>> import array_api_strict as xp |
| 402 | + >>> import array_api_extra as xpx |
| 403 | + >>> x = xp.linspace(-4, 4, 41) |
| 404 | + >>> xpx.sinc(x, xp=xp) |
| 405 | + Array([-3.89817183e-17, -4.92362781e-02, |
| 406 | + -8.40918587e-02, -8.90384387e-02, |
| 407 | + -5.84680802e-02, 3.89817183e-17, |
| 408 | + 6.68206631e-02, 1.16434881e-01, |
| 409 | + 1.26137788e-01, 8.50444803e-02, |
| 410 | + -3.89817183e-17, -1.03943254e-01, |
| 411 | + -1.89206682e-01, -2.16236208e-01, |
| 412 | + -1.55914881e-01, 3.89817183e-17, |
| 413 | + 2.33872321e-01, 5.04551152e-01, |
| 414 | + 7.56826729e-01, 9.35489284e-01, |
| 415 | + 1.00000000e+00, 9.35489284e-01, |
| 416 | + 7.56826729e-01, 5.04551152e-01, |
| 417 | + 2.33872321e-01, 3.89817183e-17, |
| 418 | + -1.55914881e-01, -2.16236208e-01, |
| 419 | + -1.89206682e-01, -1.03943254e-01, |
| 420 | + -3.89817183e-17, 8.50444803e-02, |
| 421 | + 1.26137788e-01, 1.16434881e-01, |
| 422 | + 6.68206631e-02, 3.89817183e-17, |
| 423 | + -5.84680802e-02, -8.90384387e-02, |
| 424 | + -8.40918587e-02, -4.92362781e-02, |
| 425 | + -3.89817183e-17], dtype=array_api_strict.float64) |
| 426 | +
|
| 427 | + """ |
| 428 | + if not xp.isdtype(x.dtype, "real floating"): |
| 429 | + err_msg = "`x` must have a real floating data type." |
| 430 | + raise ValueError(err_msg) |
| 431 | + # no scalars in `where` - array-api#807 |
| 432 | + y = xp.pi * xp.where( |
| 433 | + x, x, xp.asarray(xp.finfo(x.dtype).smallest_normal, dtype=x.dtype) |
| 434 | + ) |
| 435 | + return xp.sin(y) / y |
0 commit comments