Tjaz Brelih Tjaz Brelih - 4 months ago 14
Python Question

Most accurate way to interpolate data and find the peak?

The data I have is always on a second degree polynomial (quadratic function). I want to find the peak of the interpolated function as accurately as possible.

Data sample points

So far I've been using

interp1d
and then extract the peak value using
linspace
and a simple
for
loop. Although you can use a large number of newly generated samples in
linspace
you can still be more precise using the derivative of the fitted polynomial.
I haven't found a way to do that using
interp1d
.

Now the only function I've found that returns the fitted polynomial coefficients is
polyfit
, but this fitted function is quite inaccurate (most of the time the function doesn't even go through the data points).

<code>interp1d</code> and <code>polyfit</code> functions

I've tried using
UnivariateSpline
and the fitted function seems to be quite accurate and it's very simple to get the derivative spline and its root.

Other polynomial fitting functions (
BarycentricInterpolator
,
KroghInterpolator
, ...) state that they are not computing polynomial coefficients for reasons of numerical stability.

How accurate is
UnivariateSpline
and its derivatives, or are there any better options out there?

Answer

In the end I went with polyfit. Although the fitted function didn't go exactly through the data points the end result was still good. From the returned coefficients I got the desired x and y coordinates of the peak.