# Computing several Fourier terms to machine precision

For periodic not-necessarily smooth $$f$$ and a range of $$m$$, say $$0\ldots 31$$, I want to compute $$\int_{-\pi}^\pi f(t) \cos (mt) dt$$ (and maybe the same integral with $$\sin$$ instead of $$\cos$$) to machine precision (or close-ish to it).

The obvious thing to do is to evaluate $$f$$ at an equispaced grid and take a DFT, but this is only giving me linear convergence, and getting enough precision will take a very fine grid.

The other obvious thing (which I haven't yet tried) would be to pre-compute $$w_i \cos(m t_i)$$ for $$w_i$$ $$t_i$$ the Legendre weights and points (scaled to $$(-\pi,\pi)$$), then at evaluation time compute $$f(t_i)$$ and find all the integrals with a matvec. (All else being equal, I'd prefer not to have to have a bunch of pre-computed data to keep track of, but it's better than re-computing it every time if that's the only other option)

My only other idea (also not yet tried) was to figure out a closed-form solution for $$\int_{-\pi}^\pi T_n(t/\pi)\cos(mt)$$ for $$T_n$$ the Chebyshev basis, then at evaluation time do a fast Chebyshev transform to get $$f \approx \sum_i c_i T_i$$ and use that to compute the integrals.

None of these seem particularly satisfying.

So my question is: is there some neat technique I'm missing that will let me compute banks of these coefficients with spectral (or at least quadratic) accuracy and with good complexity?

• There are several numerical integration techniques and probably several for computing Fourier coefficients. What references have you looked at? Mar 28, 2019 at 2:34
• There's literature out there but they tend to assume smooth $f$. Numerical Recipes (3rd edition) 13.9 has discussion for smooth (of unclear order) non-periodic, TOMS649 has one for p-order smooth. Lyness has ams.org/journals/mcom/1971-25-113/S0025-5718-1971-0293846-4 but the points of non-smoothness are needed. I did find this from Patterson : link.springer.com/article/10.1007%2FBF01399083 that looks like the third idea above, which is promising.
– JCK
Mar 28, 2019 at 18:44
• Perhaps an obvious question, but why do you want these integrals evaluated? What will you do with their values? Mar 28, 2019 at 18:58
• I'm interested in decomposing functions on the unit disk in terms of Zernike polynomials, and am following Boyd&Yu sciencedirect.com/science/article/pii/… , in particular Eq (141) in which $p_n(r)$ for Zernike polynomials is a fourier integral.
– JCK
Mar 28, 2019 at 21:34