What are good/best practices to take the Fourier transform of an InterpolatingFunction? - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-18T14:40:36Z https://mathematica.stackexchange.com/feeds/question/47773 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/47773 4 What are good/best practices to take the Fourier transform of an InterpolatingFunction? Emilio Pisanty https://mathematica.stackexchange.com/users/1000 2014-05-13T13:42:01Z 2014-05-13T13:42:01Z <p>I have a function which I have obtained from numerical integration of a differential equation, and I would like to take its Fourier transform. What are good practices for doing this?</p> <p>To make things more precise, suppose I have a function like the following:</p> <pre><code>solution = u /. First@NDSolve[{ u'[t] == -E^(-(t^2/2)) t Cos[5 t] - 5 E^(-(t^2/2)) Sin[5 t] t, u == 1 }, u, {t, 0, 50}] </code></pre> <p>(Note that, while this example has an analytical solution, my full problem does not.) I am using <a href="http://reference.wolfram.com/mathematica/ref/NDSolve.html" rel="nofollow noreferrer"><code>NDSolve</code></a> since it appears to be the best way to obtain functions $F$ of the form $F(t)=\int_{t_0}^t f(\tau) \,\text d\tau$ where $f$ is known. I am interested in the Fourier transform of this solution. </p> <p>I know of course that I can sample this function over a discrete set of points and then use the discrete Fourier transform <a href="http://reference.wolfram.com/mathematica/ref/Fourier.html" rel="nofollow noreferrer"><code>Fourier</code></a> to obtain a fair approximation:</p> <pre><code>ListPlot[ Abs[ Fourier[solution /@ Range[0, 50, 0.01]] ] , PlotRange -&gt; {{0, 150}, {0, 1}}] </code></pre> <p><img src="https://i.stack.imgur.com/6xKbc.png" alt="enter image description here"></p> <p>However, this completely ignores the fact that the <a href="http://reference.wolfram.com/mathematica/ref/InterpolatingFunction.html" rel="nofollow noreferrer"><code>InterpolatingFunction</code></a> itself is already a discrete representation of the desired function. As it stands, the code above includes interpolation from a discrete data set into a new discrete data set, and this can only </p> <ul> <li>lose information, in the case that the sample grid is too sparse, or alternatively</li> <li>give the impression that it contains more information than was already in the original data, if the sample grid is too closely spaced.</li> </ul> <p>Either way, this extra sampling step has no communication with the internals of the <code>InterpolatingFunction</code> and as such it cannot know how much information it contains or how good it is. <strong>I am looking for a method which uses this information as optimally as possible.</strong> The accuracy of the ODE solution, and therefore of its transform, are best controlled from the options of <code>NDSolve</code>, and the subsequent transformations of the data should take heed of this.</p> <p>I am aware of <a href="http://reference.wolfram.com/mathematica/ref/NFourierTransform.html" rel="nofollow noreferrer"><code>NFourierTransform</code></a>, which, <a href="https://mathematica.stackexchange.com/a/1715">as I understand</a>, uses <a href="http://reference.wolfram.com/mathematica/ref/NIntegrate.html" rel="nofollow noreferrer"><code>NIntegrate</code></a> internally; I'm also aware that <a href="http://reference.wolfram.com/mathematica/FunctionApproximations/ref/NIntegrateInterpolatingFunction.html" rel="nofollow noreferrer"><code>NIntegrate</code> natively supports <code>InterpolatingFunction</code> objects</a>, which is presumably still the case if they are multiplied by a further factor of $e^{i\omega t}$. (On the other hand, the behaviour of this factor can rather radically change the overall behaviour of the whole integrand from 'slow' into highly oscillatory, so that some attention to <code>Method</code> would be warranted.) </p> <p>It could be, though, that the Fast nature of <a href="http://reference.wolfram.com/mathematica/ref/Fourier.html" rel="nofollow noreferrer"><code>Fourier</code></a> might be enough to outstrip such advantages, and still be faster than <a href="http://reference.wolfram.com/mathematica/ref/NFourierTransform.html" rel="nofollow noreferrer"><code>NFourierTransform</code></a> even when over-blasted on the accuracy. Speed is definitely an issue, as I will be performing automated scans over many such spectra, so I want them to be accurate enough but not waste time overcalculating.</p> <p>So: what are good ways of performing this transform?</p>