Finding curvature from a noisy set of data points using 2d/3dsplines? C
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Computing curvature from noisy data points requires smoothing the data first, then computing derivatives of the smooth curve. Direct numerical differentiation amplifies noise, making results unreliable. The standard approach is to fit a smoothing spline to the data points and then compute curvature analytically from the spline's derivatives. In C++, libraries like Eigen, Boost, and ALGLIB provide spline fitting capabilities.
Curvature Formulas
2D Curvature
For a parametric curve (x(t), y(t)):
For y = f(x):
3D Curvature
For a parametric curve (x(t), y(t), z(t)):
where r' × r'' is the cross product of the first and second derivatives.
Step 1: Smoothing Spline Fitting
Using Eigen (Cubic Spline)
Step 2: Computing Curvature
2D Curvature from y = f(x)
2D Parametric Curvature
For curves that cannot be expressed as y=f(x) (e.g., loops):
3D Curvature
Handling Noise: Smoothing Splines
A smoothing spline balances fitting the data closely and maintaining smoothness. The smoothing parameter controls this tradeoff:
Smoothing Parameter Selection
| Parameter | Effect |
| Too low smoothing | Spline overfits noise, curvature is erratic |
| Too high smoothing | Spline over-smooths, real curvature features are lost |
| Optimal | Captures true curvature while suppressing noise |
Methods to choose the smoothing parameter:
- Cross-validation: Leave-one-out or k-fold
- GCV (Generalized Cross-Validation): Automated selection
- Visual inspection: Plot the spline against data points
Common Pitfalls
- Direct differentiation of noisy data: Numerical differentiation amplifies noise. First derivatives double the noise level; second derivatives (needed for curvature) quadruple it. Always smooth first.
- Parameterization choice: For parametric curves, using arc-length parameterization gives more uniform curvature estimates than using point index as the parameter. Compute cumulative chord length for better results.
- Boundary effects: Splines have reduced accuracy at endpoints due to fewer neighboring points. Consider using natural boundary conditions or discarding curvature estimates at the first and last few points.
- Non-uniform spacing: If data points are unevenly spaced, some spline implementations produce poor fits. Use libraries that explicitly handle non-uniform knot spacing.
- Smoothing parameter sensitivity: Curvature is very sensitive to the smoothing parameter. Too little smoothing gives noisy curvature; too much removes real curvature features. Always validate with cross-validation or domain knowledge.
Summary
- Fit a smoothing spline to noisy data before computing curvature — never differentiate noisy data directly
- Use the curvature formula κ = |f''| / (1 + f'²)^(3/2) for 2D curves and the cross-product formula for 3D
- For parametric curves, fit separate splines for each coordinate (x(t), y(t), z(t))
- The smoothing parameter critically affects curvature estimates — tune with cross-validation
- Use C++ libraries (ALGLIB, Eigen, Boost) for production-quality spline fitting
Related reading
- Finding daily patterns with machine learning
- Finding groups of similar strings in a large set of strings
- Finding K-nearest neighbors and its implementation
- Finding mean and median in constant time
- Finding if a string is an iterative substring Algorithm in C?
- function objects versus function pointers
- Finding median of list in Python
- Finding neighbourhoods cliques in street data a graph
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.