|
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,80 @@ 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 / np.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)``. |
| 373 | +
|
| 374 | + Returns |
| 375 | + ------- |
| 376 | + out : ndarray |
| 377 | + ``sinc(x)`` calculated elementwise, which has the same shape as the input. |
| 378 | +
|
| 379 | + Notes |
| 380 | + ----- |
| 381 | + The name sinc is short for "sine cardinal" or "sinus cardinalis". |
| 382 | +
|
| 383 | + The sinc function is used in various signal processing applications, |
| 384 | + including in anti-aliasing, in the construction of a Lanczos resampling |
| 385 | + filter, and in interpolation. |
| 386 | +
|
| 387 | + For bandlimited interpolation of discrete-time signals, the ideal |
| 388 | + interpolation kernel is proportional to the sinc function. |
| 389 | +
|
| 390 | + References |
| 391 | + ---------- |
| 392 | + .. [1] Weisstein, Eric W. "Sinc Function." From MathWorld--A Wolfram Web |
| 393 | + Resource. https://mathworld.wolfram.com/SincFunction.html |
| 394 | + .. [2] Wikipedia, "Sinc function", |
| 395 | + https://en.wikipedia.org/wiki/Sinc_function |
| 396 | +
|
| 397 | + Examples |
| 398 | + -------- |
| 399 | + >>> import array_api_strict as xp |
| 400 | + >>> import array_api_extra as xpx |
| 401 | + >>> x = xp.linspace(-4, 4, 41) |
| 402 | + >>> xpx.sinc(x, xp=xp) |
| 403 | + Array([-3.89817183e-17, -4.92362781e-02, |
| 404 | + -8.40918587e-02, -8.90384387e-02, |
| 405 | + -5.84680802e-02, 3.89817183e-17, |
| 406 | + 6.68206631e-02, 1.16434881e-01, |
| 407 | + 1.26137788e-01, 8.50444803e-02, |
| 408 | + -3.89817183e-17, -1.03943254e-01, |
| 409 | + -1.89206682e-01, -2.16236208e-01, |
| 410 | + -1.55914881e-01, 3.89817183e-17, |
| 411 | + 2.33872321e-01, 5.04551152e-01, |
| 412 | + 7.56826729e-01, 9.35489284e-01, |
| 413 | + 1.00000000e+00, 9.35489284e-01, |
| 414 | + 7.56826729e-01, 5.04551152e-01, |
| 415 | + 2.33872321e-01, 3.89817183e-17, |
| 416 | + -1.55914881e-01, -2.16236208e-01, |
| 417 | + -1.89206682e-01, -1.03943254e-01, |
| 418 | + -3.89817183e-17, 8.50444803e-02, |
| 419 | + 1.26137788e-01, 1.16434881e-01, |
| 420 | + 6.68206631e-02, 3.89817183e-17, |
| 421 | + -5.84680802e-02, -8.90384387e-02, |
| 422 | + -8.40918587e-02, -4.92362781e-02, |
| 423 | + -3.89817183e-17], dtype=array_api_strict.float64) |
| 424 | +
|
| 425 | + """ |
| 426 | + y = xp.pi * xp.where(x == 0, xp.asarray(1.0e-20), x) |
| 427 | + return xp.sin(y) / y |
0 commit comments