in reply to How to smooth values of an x,y,z array using Math::Spline

I assume the rows of @accel are x,y,z values. Also, @accel=[,[],[]] creates a 3deep array: the outer array has a single element, which is an arrayref; inside that arrayref are four arrayrefs with three elements each. To simplify that structure, I changed the outer brackets [] to parentheses () so it's a 2deep array instead of 3deep array, so each element of @accel is an arrayref of three values, which is what I think you wanted.

When I tried running

... I found that $spline_xy=Math::Spline::->new(...) and $spline_xz both die when they are created; if I change the third row to having an x value of -0.6437 instead of -0.7437, it fixes it, so I am assuming that it's because those x-values are the same.

Were you intending the middle two of the four points to be points on the curve (so the interpolation would hit them exactly); or are they "control points", where the interpolation doesn't hit them directly, but instead only touches on the outer two, and the inner two just define directions (like this image from Bézier_curve)? Because, from what I can tell, Math::Spline makes sure that it hits the points from the instantiation-list rather than treats them as controls.

If what you really wanted was more along the lines of a Bézier curve, let me know, because that's pretty simple to code up without a module.

Replies are listed 'Best First'.
Re^2: How to smooth values of an x,y,z array using Math::Spline
by cormanaz (Deacon) on May 10, 2023 at 17:34 UTC
    Yes, I think a Bezier curve would be fine. Mainly just trying to smooth-out some noise. I'd appreciate you sharing that code.
      Given what you said Re^2: How to smooth values of an x,y,z array using Math::Spline, I think my second answer Re^2: How to smooth values of an x,y,z array using Math::Spline is your best bet. That answer allows for any number of observations to be smoothed. My original thought for the Bezier would have been to only pay attention to four points, only hitting the outer two; and if I expanded that idea to multiple "fixed points", it would require that the number of observations be 3N+1, and it would "waste" (go toward but not hit) roughly 2/3 of the points.

      But, if you really want the Bezier, and thus not hitting most of the points you list:

      Whether this or Re^2: How to smooth values of an x,y,z array using Math::Spline is better for "smoothing" the data that you have is really up to you.

      addenda: Please note that in all of my solutions, @interp_accel will contain the complete list of interpolated values, even if my manual editing or in-code logic doesn't print them all. I just skipped most of the printing to save space in the posts, so the reduced output was enough to show what was going on.