Eigen Spline interpolation - How to get spline y value at arbitray point x?
Asked Answered
H

2

6

I am try to use the Eigen library to create splines. However once I create a spline, I don't know how to get what the value would be at a given point x.

See example below with explanations of my intentions:

#include <Eigen/Core>
#include <unsupported/Eigen/Splines>

int main(int argc, char const* argv[])
{
    // points at (0,0) (15,12) and (30,17)
    Eigen::MatrixXd points(2, 3);
    points << 0, 15, 30,
              0, 12, 17;

    typedef Eigen::Spline<double, 2> spline2d;
    spline2d s = Eigen::SplineFitting<spline2d>::Interpolate(points, 2);

    // I now have a spline called s.
    // I want to do something like:
    double x = 12.34;
    double new_y = s(x)[1];  // However this s() function uses a chord value. What is a chord value?

    // Is there a:
    double new_y2 = s.eval(x)
}
Honeymoon answered 23/4, 2015 at 11:25 Comment(0)
C
17

I see how that could be confusing. The Eigen Spline fitting module, as you use it, does not model a function R -> R. You could, for example, build a spiral with it. This means that you cannot expect to get an Y value from an X value, and instead you pick out points on the spline by how far along the spline they are (hence the chord lengths).

It is possible to use the module to model a function, albeit not terribly intuitive: consider your Y values points in R1, and instead of letting Eigen calculate the chord lengths, supply your own set of knot parameters that are spaced like your X values (scaled down to [0,1] so the algorithm can cope). It could be packaged something like this:

#include <Eigen/Core>
#include <unsupported/Eigen/Splines>

#include <iostream>

class SplineFunction {
public:
  SplineFunction(Eigen::VectorXd const &x_vec,
                 Eigen::VectorXd const &y_vec)
    : x_min(x_vec.minCoeff()),
      x_max(x_vec.maxCoeff()),
      // Spline fitting here. X values are scaled down to [0, 1] for this.
      spline_(Eigen::SplineFitting<Eigen::Spline<double, 1>>::Interpolate(
                y_vec.transpose(),
                 // No more than cubic spline, but accept short vectors.

                std::min<int>(x_vec.rows() - 1, 3),
                scaled_values(x_vec)))
  { }

  double operator()(double x) const {
    // x values need to be scaled down in extraction as well.
    return spline_(scaled_value(x))(0);
  }

private:
  // Helpers to scale X values down to [0, 1]
  double scaled_value(double x) const {
    return (x - x_min) / (x_max - x_min);
  }

  Eigen::RowVectorXd scaled_values(Eigen::VectorXd const &x_vec) const {
    return x_vec.unaryExpr([this](double x) { return scaled_value(x); }).transpose();
  }

  double x_min;
  double x_max;

  // Spline of one-dimensional "points."
  Eigen::Spline<double, 1> spline_;
};

int main(int argc, char const* argv[])
{
  Eigen::VectorXd xvals(3);
  Eigen::VectorXd yvals(xvals.rows());

  xvals << 0, 15, 30;
  yvals << 0, 12, 17;

  SplineFunction s(xvals, yvals);

  std::cout << s(12.34) << std::endl;
}
Chassepot answered 23/4, 2015 at 13:39 Comment(3)
@Chassepot Thanks for this answer that I found while googling for spline support of Eigen. However, I do not understand why Eigen's spline interpolation does not give the same results as the plain Vanilla natural spline code in C. Additionnally, I do not see why the value scaling is necessary: I tried with unscaled values and the interpolation seems to work, while still different from plain vanilla numerical receipes C codeLasley
@Lasley The Eigen Splines module uses B-Splines (link to documentation). The required value scaling is likely an artifact of this; I'd have to go digging for details. In any case, without it bogus values are interpolated. You will find, for example, that if you replace scaled_value in the code above with return x;, s(30) becomes -496.714 rather than 17.Chassepot
How can I use this in 3-D Space?Shurlock
J
2

@Wintermute solution works well for small vectors. But if the vector size is large it is very slow and consumes massive amounts of RAM.

 Eigen::VectorXd xvals = Eigen::VectorXd::LinSpaced(10000,0.,1);
 Eigen::VectorXd yvals = xvals.array().sin(); 
        
 SplineFunction s(xvals, yvals);

 std::cout << s(0.50000001) << std::endl;

requires ~2 GB RAM and needs 17 seconds on my system (16 Threads were used)

For comparison I have used gsl

gsl_interp_accel* accel_ptr = gsl_interp_accel_alloc();
gsl_spline* spline_ptr;

spline_ptr = gsl_spline_alloc(  gsl_interp_cspline, 10000 );
gsl_spline_init( spline_ptr, &xvals[0], &yvals[0], 10000 );

std::cout << gsl_spline_eval( spline_ptr, 0.50000001, accel_ptr ) << std::endl;

gsl_spline_free( spline_ptr );
gsl_interp_accel_free( accel_ptr );

This needs some micro seconds and needs a very small amount of RAM

Jocularity answered 23/10, 2020 at 16:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.