# Integral of $f(x) \exp(ikx)$ with finite bounds calculated using Fourier transform, and its derivative

I have an integral which I need to calculate numerically along the lines of

$$I(k)=\int_0^{L} \exp(i k x)f(x) dx$$

where $x$ and $L$ are real. $f(x)$ is not necessarily periodic and differentiable but not easy to differentiate.

It looks remarkably like the Fourier transform of $f(x)$, but with finite bounds, so I'd like to be able to calculate this using a Fast Fourier Transform (FFT), though I suspect that FFT [$f(x)$] will give me $\int^{\infty}_{-\infty} \neq \int^L_0$ . Is there a way around this?

I'd also like to be able to calculate $dI/dk$. $f(x)$ is not easy to differentiate. Were $I(k)$ a simple FT, I would say that $dI/dk =$ FT[$i x f(x)$]. Is this still valid?

-
The FFT works on a finite array of equally spaced samples, so it cannot possibly give you $\int_{-\infty}^\infty$. – Rahul Jan 27 '13 at 16:07

Let $\chi_L(x)$ be the characteristic function of the interval $[0,L]$. Then $I(k)$ is the Fourier transform of $\chi_L\,f$.
In principle, you can certainly use the FFT to compute your integral. We may replace $f(x)$ with $\chi_L(x)f(x)$ over any interval $[0,\hat L]$ with $\hat L\ge L$, as in @Julián's answer, and then approximate the integral as $$\int_0^{\hat L}\exp(ikx)\chi_L(x)f(x)\,\mathrm dx\approx\sum_{n=0}^{N-1}\exp(ikx_n)\chi_L(x_n)f(x_n)\frac{\hat L}N$$ where $N$ is sufficiently large and $x_n=\hat Ln/N$. Let's choose $\hat L=2\pi m/k$ as the smallest multiple of $2\pi/k$ greater than $L$. Then the sum becomes $$\sum_{n=0}^N\exp(i2\pi m\cdot n/N)\chi_L(x_n)f(x_n)\frac{\hat L}N,\tag{\ast}$$ which is equal to $\hat L/N$ times the $m$th entry of the discrete Fourier transform of the discrete signal $\big[\chi_L(x_n)f(x_n)\big]_{n=0}^{N-1}$.
However! There's no point doing this. You can compute the sum $(\ast)$ directly in $O(N)$ time, while the FFT takes $O(N\log N)$ time. This is because the FFT simultaneously computes the other $N-1$ cofficients of the discrete Fourier transform for all other values of $m$ in $0,\ldots,N-1$, while you only care about the value of $m$ for which $\hat L=2\pi m/k$.