# 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:** 1\
**Showing post:** 2

<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).

---

_[View the full topic](https://discourse.julialang.org/t/integration-packages-to-calculate-the-first-n-fourier-coefficients/114787)._
