Interpolation of more then 160 points triggers exception
See original GitHub issueBug Report
Issue: When interpolating data arrays with more then 160 elements using PeriodicSpline or EndSlopeSpline a System.InvalidOperationException is triggered. System.InvalidOperationException HResult=0x80131509 Message=Operation is not valid due to the current state of the object. Source=ScottPlot StackTrace: at ScottPlot.Statistics.Interpolation.PeriodicSpline.CalcParameters() at ScottPlot.Statistics.Interpolation.PeriodicSpline…ctor(Double[] xs, Double[] ys, Int32 resolution) at Chart_Test.MainWindow…ctor() in C:\Users\hoot\source\repos\Chart_Test\Chart_Test\MainWindow.xaml.cs:line 35
Reproducing:
Generate an array of 161 data points and interpolate using var esi = new PeriodicSpline(dataX, dataY, resolution: 10);
or var esi = new EndSlopeSpline(dataX, dataY, resolution: 10);
.
This issue can also reproduced with NaturalSpline but the maximum length of the array is higher. I have tested 2048 data points and it triggers the same exception.
double[] dataX = new double[161];
double[] dataY = new double[161];
Random random = new Random();
for (int i = 0; i < 161; i++) {
dataX[i] = -85 + i * 11.43;
dataY[i] = random.Next(-30, -20);
}
var esi = new PeriodicSpline(dataX, dataY, resolution: 10);
ScatterPlot scatterPlot = new ScatterPlot(dataX, dataY);
ScatterPlot scatterPlotInterpolated = new ScatterPlot(esi.interpolatedXs, esi.interpolatedYs);
scatterPlot.MarkerSize = 0;
scatterPlotInterpolated.MarkerSize = 0;
scatterPlot.Color = System.Drawing.Color.Blue;
scatterPlotInterpolated.Color = System.Drawing.Color.Red;
WpfPlot1.Plot.Add(scatterPlot);
WpfPlot1.Plot.Add(scatterPlotInterpolated);
WpfPlot1.Plot.SetAxisLimitsX(-85, -85 + 11.43 * 4);
WpfPlot1.Plot.SetAxisLimitsY(-110, -20);
WpfPlot1.Refresh();
System Details
- ScottPlot Version: 4.1.127
- Operating System: Windows 11
- Application Type: WPF
- .NET Version: NET Framework 4.6.1
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
An improved interpolation module will be available in the next release! 👍 🚀
In the mean time you can use the code from this web page to help you smoothly interpolate X/Y data
https://swharden.com/blog/2022-01-22-spline-interpolation/
Restating the problem, it’s not the number of points, but rather something about your original dataset. Extremely sharp changes in slope really confuse this interpolation engine.
No worries - I have a better cubic interpolator I’ll upload in a few minutes!
Test Setup
Sharp Data
This throws an exception
Smoother Data
This doesn’t crash… but the interpolated spline is obviously wrong ☠️
Conclusion
I think it’s not the number of points, it’s something about the interpolation calculation that’s crashing on this particular dataset. Also the math doesn’t seem to be right in the existing module.