Skip to content

Commit 28bff59

Browse files
committed
ENH: add sinc
1 parent b83ba61 commit 28bff59

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

docs/api-reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
cov
1111
expand_dims
1212
kron
13+
sinc
1314
```

src/array_api_extra/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

3-
from ._funcs import atleast_nd, cov, expand_dims, kron
3+
from ._funcs import atleast_nd, cov, expand_dims, kron, sinc
44

55
__version__ = "0.1.2.dev0"
66

7-
__all__ = ["__version__", "atleast_nd", "cov", "expand_dims", "kron"]
7+
__all__ = ["__version__", "atleast_nd", "cov", "expand_dims", "kron", "sinc"]

src/array_api_extra/_funcs.py

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
if TYPE_CHECKING:
77
from ._typing import Array, ModuleType
88

9-
__all__ = ["atleast_nd", "cov", "expand_dims", "kron"]
9+
__all__ = ["atleast_nd", "cov", "expand_dims", "kron", "sinc"]
1010

1111

1212
def atleast_nd(x: Array, /, *, ndim: int, xp: ModuleType) -> Array:
@@ -348,3 +348,80 @@ def kron(a: Array, b: Array, /, *, xp: ModuleType) -> Array:
348348
a_shape = xp.asarray(a_shape)
349349
b_shape = xp.asarray(b_shape)
350350
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

Comments
 (0)