# Integration packages to calculate the first N Fourier coefficients

**URL:** <https://discourse.julialang.org/t/integration-packages-to-calculate-the-first-n-fourier-coefficients/114787>\
**Category:** Numerics\
**Tags:** integral, quadrature\
**Created:** [May 27, 2024, 2:26pm UTC](https://discourse.julialang.org/t/integration-packages-to-calculate-the-first-n-fourier-coefficients/114787 "2024-05-27T14:26:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ccmejia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccmejia/32/18680_2.png) [@ccmejia](https://discourse.julialang.org/u/ccmejia)\
**Post date:** [May 27, 2024, 2:26pm UTC](https://discourse.julialang.org/t/integration-packages-to-calculate-the-first-n-fourier-coefficients/114787/1 "2024-05-27T14:26:57Z")

</div>

I am trying to calculate the first N Fourier coefficients for sampled data (harmonic + gaussian like). The Riemann sum was my first approach, however I think the results are not enough accurate. Therefore, I am trying to choose a more suitable method. The QuadGK package seems to be extremely accurate, but it only works (I think…) for analytical functions. Among the others integration packages, which would be your recommendation?

---

<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:** [May 27, 2024, 3:11pm UTC](https://discourse.julialang.org/t/integration-packages-to-calculate-the-first-n-fourier-coefficients/114787/2 "2024-05-27T15:11:46Z")

</div>

> [@ccmejia](#):
>
> I am trying to calculate the first N Fourier coefficients for sampled data (harmonic + gaussian like).

More infomation is needed.

Sampled data = experimental data? From a simulation? Noisy? Smooth function? If it is from a computer program, then can you evaluate it on demand for any desired x?

When you say “N Fourier coefficients” do you mean that the data is periodic and you want a Fourier series? If it’s not periodic, then what do you mean?

> [@ccmejia](#):
>
> The Riemann sum was my first approach, however I think the results are not enough accurate.

If you have equally-spaced samples of a periodic function, then a Riemann sum actually converges exponentially fast (with the number of samples) for smooth functions.

(However, adaptive methods like QuadGK [can still be advantageous for functions with localized features](https://discourse.julialang.org/t/insanely-higher-order-derivatives/104455/17) like sharp peaks.)

And, of course, a Riemann sum of equally-spaced samples is exactly equivalent (with suitable scaling) to a [discrete Fourier transform (DFT)](https://en.wikipedia.org/wiki/Discrete_Fourier_transform), so you can compute it very efficiently with FFT algorithms.

> [@ccmejia](#):
>
> The QuadGK package seems to be extremely accurate, but it only works (I think…) for analytical functions.

QuadGK and similar methods assume that you can evaluate f(x) at any requested x in the integration domain. It doesn’t require you to have an analytical formula for f(x), but you need a way to compute it on demand (e.g. via a computer program).

---

<div class="post-metadata">

**Author:** ![IlianPihlajamaa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ilianpihlajamaa/32/28766_2.png) [@IlianPihlajamaa](https://discourse.julialang.org/u/IlianPihlajamaa)\
**Post date:** [May 27, 2024, 4:42pm UTC](https://discourse.julialang.org/t/integration-packages-to-calculate-the-first-n-fourier-coefficients/114787/3 "2024-05-27T16:42:50Z")

</div>

In addition to the above answer, if your integrant is not periodic and you have a discrete set of fixed (possibly irregular) sampling points, you can try [Filon’s method](https://en.m.wikipedia.org/wiki/Filon_quadrature) or related methods. I implemented it once in Julia, but I’m not sure it is available in a package.

Edit: a quick search gives

> **[GitHub - dkm2/FCCQuad.jl: Filon-Clenshaw-Curtis Quadrature](https://github.com/dkm2/FCCQuad.jl)**
>
> Filon-Clenshaw-Curtis Quadrature. Contribute to dkm2/FCCQuad.jl development by creating an account on GitHub.

And

> [@\[RFC/ANN\] OscillatoryIntegralsODE.jl: Levin method + OrdinaryDiffEq](https://discourse.julialang.org/t/rfc-ann-oscillatoryintegralsode-jl-levin-method-ordinarydiffeq/55601):
>
> I’m requesting feedback on [OscillatoryIntegralsODE.jl](https://github.com/xzackli/OscillatoryIntegralsODE.jl), which numerically evaluates integrals I = \int\_a^b f(x) S(rx) \, dx where f(x) is smooth and not oscillatory, but S(x) is highly oscillatory for large frequencies r. Examples of oscillatory functions include Bessel functions J\_{\nu}(rx), spherical Bessel j\_{\nu}(rx), \cos(\omega x), and the harmonic transform e^{i \omega x}. Gaussian quadrature is inefficient for these integrals, since the number of nodes required for accurate integration …

---

<div class="post-metadata">

**Author:** ![ccmejia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccmejia/32/18680_2.png) [@ccmejia](https://discourse.julialang.org/u/ccmejia)\
**Post date:** [May 27, 2024, 6:54pm UTC](https://discourse.julialang.org/t/integration-packages-to-calculate-the-first-n-fourier-coefficients/114787/4 "2024-05-27T18:54:58Z")

</div>

Sampled data comes from a function _H(V, u)_ that depends (sum) of transversal harmonic complex potential _V(x)_ and nonlinear terms that depends of a localized complex function _u(x)_, where _u(x)_ is a mode for a nonlinear wave equation in periodical media.

Here is the Riemann approach:

```julia
N # Number of modes
M # Grid size
L # Transversal length
k0 = 2*pi/L
dx = L/M 
x = -L/2:dx=L/2-dx
c = zeros(N)
for n in 1:N
    c[n]=dx*sum(H.*exp.(-im*k0*n*x))/L
end

```
