# Sinc function - and sinc neural networks - and function approximation for the former

**URL:** <https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483>\
**Category:** General Usage\
**Created:** [September 11, 2020, 10:17pm UTC](https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483 "2020-09-11T22:17:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 11, 2020, 10:17pm UTC](https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483/1 "2020-09-11T22:17:21Z")

</div>

First, has anyone reimplemented Sinc neural networks in Julia? Those are for speaker recognition, state-of-the-art I believe, and should similarly help for speech recognition, and other audio work.

So far, I’m just looking at the sinc function itself, and I’m not sure the Julia implementation is as fast as it could be.

```julia
julia> function my_sinc(x) # this works (EDIT: for small domain), but now naive version
         sum([((-1)^n*x^(2n))/factorial(2n + 1) for n=0:3])
       end

julia> function my_sinc2(x) # I' looking into evalpoly, can I do something like this
         evalpoly(x, (((-1)^n*x^(2n))/factorial(2n + 1) for n=0:3))
       end

```

Anyone know if Padé approximant or some other way is best? I tried to google a bit, and didn’t see, but found:

[https://www.researchgate.net/publication/229965145\_Signal\_sinc-interpolation\_A\_fast\_computer\_algorithm/link/5a4a7739458515f6b05b32a5/download](https://www.researchgate.net/publication/229965145_Signal_sinc-interpolation_A_fast_computer_algorithm/link/5a4a7739458515f6b05b32a5/download)

What’s currently done in Julia:

```julia
sinc(x::Real) = x==0 ? one(x) : isinf(x) ? zero(x) : sinpi(x)/(pi*x)

sinc(x::Number) = x==0 ? one(x) : oftype(x,sinpi(x)/(pi*x))
sinc(x::Integer) = x==0 ? one(x) : zero(x)
sinc(x::Complex{<:AbstractFloat}) = x==0 ? one(x) : oftype(x, sinpi(x)/(pi*x))
sinc(x::Complex) = sinc(float(x))

```

EDIT (the network I have in mind; more details, in my own reply to this top post):

Mirco Ravanelli (he also has a youtube video on it), Yoshua Bengio,  
“Speaker Recognition from raw waveform with SincNet”.  
[[1808.00158] Speaker Recognition from Raw Waveform with SincNet](https://arxiv.org/abs/1808.00158)

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 11, 2020, 10:26pm UTC](https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483/2 "2020-09-11T22:26:42Z")

</div>

You’re actually 1 week late. Julia very recently got much better functions for sinc and cosc (the derivative). If you check out master, you should see much better performance (and accuracy).

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 11, 2020, 10:44pm UTC](https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483/3 "2020-09-11T22:44:18Z")

</div>

Great, I see it here:  
[https://github.com/JuliaLang/julia/blob/7ecd1f9e0da5d07e7dd1733093b246a37bc118ff/base/special/trig.jl#L1080](https://github.com/JuliaLang/julia/blob/7ecd1f9e0da5d07e7dd1733093b246a37bc118ff/base/special/trig.jl#L1080)

I should have know, even with my 11 days old master outdated…

Makes this a bit outdated, while just an example, so not wrong:  
[https://github.com/JuliaLang/julia/blob/2fc3dcdaecc0f77fee56400ee80752cc81c8dfb0/doc/src/manual/types.md](https://github.com/JuliaLang/julia/blob/2fc3dcdaecc0f77fee56400ee80752cc81c8dfb0/doc/src/manual/types.md)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 12, 2020, 3:08pm UTC](https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483/4 "2020-09-12T15:08:13Z")

</div>

> [@Palli](#):
>
> I’m not sure the Julia implementation is as fast as it could be.

Explicitly calling `factorial` and computing powers of `x` _separately_ for each term in a Taylor series is not a good approach. Not only is this very slow (you have a lot of redundant computations!), but it can also overflow to `0*Inf` if you compute enough terms.

See, for example, the [new implementation of the `cosc` function in Base](https://github.com/JuliaLang/julia/blob/eee48c58c0601858c3497d6982f7dea2d37ed88e/base/special/trig.jl#L1096-L1125) for an example of how to compute a Taylor series iteratively (to arbitrary precision), with specialized cases (`evalpoly` with hardcoded coefficients to a fixed order) for single and double precision.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 12, 2020, 3:31pm UTC](https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483/5 "2020-09-12T15:31:08Z")

</div>

About “the Julia implementation” (why I wrote “naive version” for mine), I was referring to the one in the standard library (and even your also now after seeing your new one).

Your new version ~~seems 2x slower~~ (except for the tiny region around 0, where you’re papering over the 2nd derivative issue, where yours is over 3x faster).

I was actually thinking of doing similar, with abs (or fastabs… as you do), but with a much wider region, avoiding division (and sin[pi] with range reduction, etc.).

got curious about the `fastabs`, and couldn’t not think of the classic “7 minutes abs”:

[![](https://global.discourse-cdn.com/julialang/original/3X/b/3/b3f39ee117f63e901591cecce61ac18b1e534a06.jpeg "There's Something About Mary - 7 minute abs") ](https://www.youtube.com/watch?v=JB2di69FmhE)

It was actually nothing (in my real case):

```julia
fastabs(x::Number) = abs(x)

fastabs(z::Complex) = abs(real(z)) + abs(imag(z))

```

More off-topic (I had never heard of this Lush Lisp-variant language, or QMNIST):

> **[Lush: Lisp Universal SHell](https://lush.sourceforge.net/)**
>
> Lush: the programming language for researchers

> Many software projects, particularly research projects, require **two languages** […]  
> The **vector and matrix manipulation engine** is quite powerful and efficient, making Lush ideal for **heavy numerical applications** and applications such as **signal and image processing**. […]  
> Lush offers an extensive library for **gradient-based machine learning**

where I found it:

> **[GitHub - facebookresearch/qmnist: The QMNIST dataset](https://github.com/facebookresearch/qmnist)**
>
> The QMNIST dataset. Contribute to facebookresearch/qmnist development by creating an account on GitHub.

> **[Cold Case: The Lost MNIST Digits](https://arxiv.org/abs/1905.10498)**
>
> Although the popular MNIST dataset \[LeCun et al., 1994\] is derived from the NIST database \[Grother and Hanaoka, 1995\], the precise processing steps for this derivation have been lost to time. We propose a reconstruction that is accurate enough to...

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 12, 2020, 3:44pm UTC](https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483/6 "2020-09-12T15:44:15Z")

</div>

The key difference between your version and the Base version is that your version only works for small `x` values. For example, `my_sinc(10)` is about `-130` instead of the correct answer of `0`. Making `sinc` fast and correct for the whole range pretty much requires calling `sinpi` which is going to be slow.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 12, 2020, 4:05pm UTC](https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483/7 "2020-09-12T16:05:52Z")

</div>

Yes, I know that. My question was really about exactly getting evalpoly to work (and if anyone actually working on Sinc [networks] already), and if Padé approximant appropriate. With the limit going to zero, a polynominal will never work without division, but it might for a large enough region, given a longer polynominal.

What are the options actually? I’m up to speed on the math of Taylor series (and sinc has one without division, if you’re ok with infinite…), missing some details on implementing with evalpoly. I’m reading up on minimax, and Padé etc. but what are the options I should be looking into? I think this (function approximation in general) will involve only polynominals, i.e. one or two (then division), of sufficient length.

Is this helpful to find the coefficients:

> **[GitHub - pjabardo/CurveFit.jl: Simple least squares and curve fitting functions](https://github.com/pjabardo/CurveFit.jl)**
>
> Simple least squares and curve fitting functions . Contribute to pjabardo/CurveFit.jl development by creating an account on GitHub.

There I see e.g.:

- `linear_king_fit(E, U)` , find coefficients `a` and `b` for `E[i]^2 = a + b*U^0.5`
- `linear_rational_fit(x, y, p, q)` finds the coefficients for rational polynomials: `y[i] = (a[1] + a[2]*x[i] + ... + a[p+1]*x[i]^p) / (1 + a[p+1+1]*x[i] + ... + a[p+1+q]*x[i]^q)`

I.e. the latter seems helpful, not sure with the former. You do have sqrt in processors, not sure e.g. it used for function approximations, I think that and other options in the package are for other applications.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 12, 2020, 4:09pm UTC](https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483/8 "2020-09-12T16:09:28Z")

</div>

The best library for this is called `Remez.jl` it finds minimax polynomials or rational functions. That said, I’m not sure you’ll be able to find something accurate over the entire range that isn’t really slow.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 12, 2020, 4:11pm UTC](https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483/9 "2020-09-12T16:11:59Z")

</div>

I should have been more clear, e.g. with more coefficients, more accurate (it’s still WIP), I’m looking for the best approach, before converting to get fast, e.g. with evalpoly:

```julia
julia> function my_sinc(x) # this works for larger domain as more coefficients, still naive WIP version
                sum([((-1)^n*x^(2n))/factorial(2n + 1) for n=0:7])
              end

```

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 12, 2020, 4:14pm UTC](https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483/10 "2020-09-12T16:14:29Z")

</div>

You mean the from -Inf to Inf as the “entire range”? I was thinking maybe -5 to 5 (just a guess), then fallback. But I’m actually not sure what the range needs to be, what’s needed for signal processing.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 12, 2020, 4:22pm UTC](https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483/11 "2020-09-12T16:22:34Z")

</div>

I do see at:

> **[Remez algorithm](https://en.wikipedia.org/wiki/Remez_algorithm)**
>
> The Remez algorithm or Remez exchange algorithm, published by Evgeny Yakovlevich Remez in 1934, is an iterative algorithm used to find simple approximations to functions, specifically, approximations by functions in a Chebyshev space that are the best in the uniform norm L∞ sense. It is sometimes referred to as Remes algorithm or Reme algorithm.\[citation needed\]
> A typical example of a Chebyshev space is the subspace of Chebyshev polynomials of order n in the space of real continuous functions o...

> Sometimes zero-error point constraints are included in a Modified Remez Exchange Algorithm.[[10]](https://en.wikipedia.org/wiki/Remez_algorithm#cite_note-toobs-10)

I’m not sure this should be used, for few [closest to zero] extremes and/or zero-crossings. Nor how inaccurate the approximation is tolerable. For signal processing, I guess you want the frequency preserved, i.e. zero crossings, but e.g. the extremes could be off, assuming it would only be in the vertical, not horizontal direction.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 12, 2020, 4:54pm UTC](https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483/12 "2020-09-12T16:54:11Z")

</div>

In case people are interested, I’m thinking in context of:

> **[GitHub - mravanelli/SincNet: SincNet is a neural architecture for efficiently...](https://github.com/mravanelli/SincNet)**
>
> SincNet is a neural architecture for efficiently processing raw audio samples. - GitHub - mravanelli/SincNet: SincNet is a neural architecture for efficiently processing raw audio samples.

> Feb, 16 2019:
> 
> - We replaced the old “sinc\_conv” with “SincConv\_fast”. The latter is 50% faster.

The code is not large, a few files (in top directory, each one smaller than this 304 sloc, I guess in total under 800-1000 lines):

> <https://github.com/mravanelli/SincNet/blob/788fc3cd42e5c3172c5a8525b3a872eb7205c8f9/dnn_models.py#L19>

I found there an .npy (raw) file, wasn’t sure what it is (seems to apply only to Python/Numpy, while there is NPZ.jl):

> **[What is .npy files and why you should use them…](https://towardsdatascience.com/what-is-npy-files-and-why-you-should-use-them-603373c78883)**
>
> Why you should always save your data as .npy instead of .csv

> ## Why you should always save your data as .npy instead of .csv

There are some other applications (where sinc is the activation function, unlike above, where ReLU is used):

> **[Function approximation using a sinc neural network](https://ui.adsabs.harvard.edu/abs/1996SPIE.2760..690E/abstract)**
>
> Neural networks for function approximation are the basis of many applications. Such networks often use a sigmoidal activation function (e.g. tanh) or a radial basis function (e.g. gaussian). Networks have also been developed using wavelets. In this...

> **[Sinc as a Neural Networks Activation Function - Sefik Ilkin Serengil](https://sefiks.com/2018/01/07/sinc-as-a-neural-networks-activation-function/)**
>
> Sinc function is a sinusoidal activation function in neural networks. In contrast to other common activation functions, it has rises and falls. However, the function saturated and its output converges to zero for large positive and negative...

> Funnily, name of the function comes from cardinal sine. The widest area of the graph resembles the cardinal with a hat.  
> [Picure: Assistant du Sinus Pape, membre du …]

> **[Function approximation using a sinc neural network](https://www.spiedigitallibrary.org/conference-proceedings-of-spie/2760/0000/Function-approximation-using-a-sinc-neural-network/10.1117/12.235959.short?SSO=1)**
>
> Neural networks for function approximation are the basis of many applications. Such networks often use a sigmoidal activation function (e.g. tanh) or a radial basis function (e.g. gaussian). Networks have also been developed using wavelets. In this...

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 12, 2020, 5:41pm UTC](https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483/13 "2020-09-12T17:41:35Z")

</div>

> [@Palli](#):
>
> We replaced the old “sinc\_conv” with “SincConv\_fast”. The latter is 50% faster.

_Convolution_ with a `sinc` function is an entirely different beast — the limiting factor for convolution seems unlikely to be evaluation of `sinc` itself (if it is evaluated at all — e.g. an FFT-based method won’t evaluate `sinc`, whereas an approximate overlap-add method using a truncated `sinc` would precompute the filter coefficients anyway).

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 12, 2020, 6:02pm UTC](https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483/14 "2020-09-12T18:02:27Z")

</div>

I see it used in the code, I’m not sure, are you saying there’s a better way, or this ok, just sinc will not be the speed critical part in the network (I know sinc is just used in the first layer):

```julia
        for i in range(self.N_filt):
                        
            low_pass1 = 2*filt_beg_freq[i].float()*sinc(filt_beg_freq[i].float()*self.freq_scale,t_right)
            low_pass2 = 2*filt_end_freq[i].float()*sinc(filt_end_freq[i].float()*self.freq_scale,t_right)
            band_pass=(low_pass2-low_pass1)

            band_pass=band_pass/torch.max(band_pass)

            filters[i,:]=band_pass.cuda()*window

        out=F.conv1d(x, filters.view(self.N_filt,1,self.Filt_dim))

```

From the paper (see top post):

> The first layer of a standard CNN performs a set of time-  
> domain convolutions between the input waveform and some  
> Finite Impulse Response (FIR) filters […]  
> In standard CNNs,  
> all the L elements (taps) of each filter are learned from data.  
> Conversely, the proposed SincNet (depicted in Fig. 1) per-  
> forms the convolution with a predefined function g that de-  
> pends on few learnable parameters θ only

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 12, 2020, 6:34pm UTC](https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483/15 "2020-09-12T18:34:12Z")

</div>

In general I do trust Julia to have the best math approximations, but it seems sinc may be an exception (I saw talk of it shouldn’t be in Base), maybe not considered too important; or at least original sinc implementation implemented before this paper I found from last October: [[1812.10884] A rational approximation of the sinc function based on sampling and the Fourier transforms](https://arxiv.org/abs/1812.10884)

# A rational approximation of the sinc function based on sampling and the Fourier transforms

> Figure 6 shows the absolute difference between the original sinc function sinc (πν) and its rational approximation. Despite that the sinc function  
> is not easy to approximate [12, 16], only 2^(6−1) = 32 terms in the proposed  
> formula (16) provide absolute difference smaller than 3.2 × 10^−3 within the  
> range ν ∈ [−2π, 2π]. A MATLAB code validating the results based on the  
> rational approximation (16) of the sinc function sinc (πν) is provided in Ap-  
> pendix A.

Maybe I shouldn’t look at the code, with it tainted (so I couldn’t contribute to Julia)? But ok to look at formulas?

> As we can see now, the truncated form of this limit can be implemented for  
> sampling in accordance with equation (1). Specifically, the sinc function can  
> be approximated as a cosine series expansion such that

FYI: the paper also seems to be on different things:

> Fig. 2. The rectangular function approximations f(t) = 1/[(2t)^70 + 1] and

[I’ve never seen raised to 70th power, this high before, not sure if fast, a better way seems for a rectangular function…]

> Voigt function [3], can be calculated by residues in a similar  
> way that we performed in our earlier work [2] to approximate the integral  
> (18). Due to rapid performance and high accuracy our algorithm [2] has  
> been implemented in current version of the atmospheric model bytran [6, 15]

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 12, 2020, 8:01pm UTC](https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483/17 "2020-09-12T20:01:22Z")

</div>

> [@Palli](#):
>
> provide absolute difference smaller than 3.2 × 10^−3

Three digits of accuracy is fine for specialized applications, but the Base version needs to be accurate to close to machine precision. If your application only needs a crude sinc over a limited range, and the sinc is performance-critical, then by all means use a simple approximation.

> [@Palli](#):
>
> I see it used in the code, I’m not sure, are you saying there’s a better way,

In the code you quoted almost certainly the time is mostly spent in the conv1d function. The time spent calling sinc while precomputing filter coefficients should be negligible, especially in Julia.

---

<div class="post-metadata">

**Author:** ![Nicolas\_Robidoux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nicolas_robidoux/32/212127_2.png) [@Nicolas\_Robidoux](https://discourse.julialang.org/u/Nicolas_Robidoux)\
**Post date:** [September 22, 2024, 6:40pm UTC](https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483/18 "2024-09-22T18:40:15Z")

</div>

Maybe you will find the branchless (normalized: zeros at integers) Sinc function approximations which ImageMagick has been using “forever” useful, e.g.,

```
const double xx = x*x;
/*
  Maximum absolute relative error 6.3e-6 < 1/2^17.
*/
const double c0 = 0.173610016489197553621906385078711564924e-2L;
const double c1 = -0.384186115075660162081071290162149315834e-3L;
const double c2 = 0.393684603287860108352720146121813443561e-4L;
const double c3 = -0.248947210682259168029030370205389323899e-5L;
const double c4 = 0.107791837839662283066379987646635416692e-6L;
const double c5 = -0.324874073895735800961260474028013982211e-8L;
const double c6 = 0.628155216606695311524920882748052490116e-10L;
const double c7 = -0.586110644039348333520104379959307242711e-12L;
const double p =
  c0+xx*(c1+xx*(c2+xx*(c3+xx*(c4+xx*(c5+xx*(c6+xx*c7))))));
return((xx-1.0)*(xx-4.0)*(xx-9.0)*(xx-16.0)*p);

```

Source: [ImageMagick resize.c source code](https://github.com/ImageMagick/ImageMagick/blob/main/MagickCore/resize.c)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 22, 2024, 7:29pm UTC](https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483/20 "2024-09-22T19:29:05Z")

</div>

> [@Nicolas\_Robidoux](#):
>
> Maybe you will find the branchless Sinc function approximations which ImageMagick has been using “forever” useful, e.g.,

Note that this approximation is [only accurate (to ≈ 6 digits) for x \in [-4,+4]](https://github.com/ImageMagick/ImageMagick/blob/c429de8343ec5ed7efc0252a1b73901531bcdb56/MagickCore/resize.c#L455-L456), so it’s only “branchless” if you restrict your inputs to that interval.

---

<div class="post-metadata">

**Author:** ![Nicolas\_Robidoux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nicolas_robidoux/32/212127_2.png) [@Nicolas\_Robidoux](https://discourse.julialang.org/u/Nicolas_Robidoux)\
**Post date:** [September 22, 2024, 8:34pm UTC](https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483/22 "2024-09-22T20:34:13Z")

</div>

Indeed. I should have pointed this out. (In that context we rarely need to step out of [-4, 4].) The branchlessness is w.r.t. division by 0 at the origin.  
RE: the accuracy of the approximation: The \>\>max\<\< error bound is \>\>relative\<\<, not absolute, which means that it’s the number of \>\>significant\<\< (instead of total) digits. Also note that the linked source code includes more accurate approximations, up to max. abs. rel. error 1.2e-12 \< 1/2^39. (All on [-4,4] for the normalized Sinc (zeros at integers). If you rarely call Sinc for values outside this range, you could branch and simply compute sin(pi \* x)/(pi \* x) outside of it. All the ImageMagick approximations are exactly 0 at x = ±4, so you know you’d be continuous.)  
P.S. An maximum \>\>absolute\<\< error of, say, 1.e-3 is likely to lead to a much larger \>\>relative\<\< error near roots, and Sinc has many. Also, for a function that stays in the range [-1,1], the absolute error is generally smaller than the absolute error.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [September 22, 2024, 9:06pm UTC](https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483/23 "2024-09-22T21:06:19Z")

</div>

> [@Palli](#):
>
> What are the options actually? I’m up to speed on the math of Taylor series (and sinc has one without division, if you’re ok with infinite…), missing some details on implementing with evalpoly. I’m reading up on minimax, and Padé etc. but what are the options I should be looking into?

My package FindMinimaxPolynomial.jl can find minimax polynomials, too. It doesn’t know about Padé approximants. I used it to implement DebyeFunctions.jl.

[Next page](https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483.md?page=2)
