How to interpolate onto a (1D) array of points?


(Allen Nugent) #1

I want to use CubicSpline.InterpolateHermiteSorted or CubicSpline.InterpolateAkimaSorted for real-time resampling of a sequence of points acquired from an IMU. In the interest of speed, it would be logical to compute (i.e. train) the interpolation function in one step, then apply it to the sequence in the next step.

However, it seems to me that because these methods are static and take only a scalar double parameter, I would be forced to invoke the fitting computation once for every target point – even though the training data have not changed between points in the target sequence.

Am I missing something here? (BTW: I haven’t managed to find any documentation with examples – only the API spec.)


(Christoph Rüegg) #2

Interpolation in Math.NET Numerics indeed follows a two step scheme:

  1. First you call a static routine, provide your data samples (to interpolate over) and get back a class instance, which implements IInterpolation.
  2. Use the returned class to interpolate/extrapolate for arbitrary data points.

For example:

var x = new[] { 1.0, 2.5, 3.3, 4.0, 5.0 };
var y = new[] { 10.0, 20.0, 15.0, 12.0, 8.0 };

CubicSpline spline = CubicSpline.InterpolateAkimaSorted(x, y);
spline.Interpolate(1.2); // 12.79
spline.Interpolate(4.2); // 11.17

(Allen Nugent) #3

Thanks, Christoph.

That’s exactly how I thought it should work. I was misunderstanding the cause of an error (“Cannot declare instance members in a static class”), but it turned out to be a mistake in the declaration of my wrapper function.

Cheers!

  • Allen

(Mike S) #4

Was wondering about Polynomial class. It doesn’t seem to operate the same way, has no interpolate method. What am I missing? There is not static method to return a class with implements IInterpolation.


(Christoph Rüegg) #5

The polynomial class is different in that it is not intended as interpolation algorithm class (like CubicSpline), but actually represents a polynomial. That’s why it is also in the root namespace, not in the .Interpolation namespace like the latter.

Instead, you can use Interpolate.Polynomial()

That being said, for discoverability it could indeed make sense to add a static interpolation function there as well, similar to Polynomial.Fit.


(Peter Vanderwaart) #6

If you use a function like Polynomial.Fit to find the coefficients of a polynomial, then you can evaluate the function with Polynomial.Evaluate(). This does exactly what you would expect a function named Polynomial.Interpolate() would do if it existed (which is probably why it doesn’t).