SPLINEFIT

License

Copyright (c) 2017, Jonas Lundgren
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

Direct spline interpolation of noisy data may result in a curve with
unwanted oscillations. This is particularly bad if the slope of the
curve is important.
A better approach is to reduce the degrees of freedom for the spline
and use the method of least squares to fit the spline to the noisy data.
The deegres of freedom are connected to the number of breaks (knots),
so the smoothing effect is controlled by the selection of breaks.
SPLINEFIT:
- A curve fitting tool based on B-splines
- Splines on ppform (piecewise polynomial)
- Any spline order (cubic splines by default)
- Periodic boundary conditions
- Linear constraints on function values and derivatives
- Robust fitting scheme
- Operates on ND arrays in the same way as SPLINE
- Nonuniform distributions of breaks

Thanks so much for the code. For my data works fine. Some questions though :
1. How can I retrieve the standard error (a posteriori sigma zero ) for the overall result?
2. How can I retrieve the standard errors (or Covariance matrix) for each coefficient

thank you for your quick reply. It took me some time and help from people at my university (TU Delft), but I got it right now. I developed a script to do what I asked you for. It solves the polynomial in a different way, by using mldivide. It uses the fact that overdetermined matrices are solved by a least squares fit.

I am currently searching to minimize influence of different pieces (between the breaks) on one another. I want to have a high degree polynomial (10 and up) but only have the total piecewise polynomial differentiable up to 3rd or 4th order.

I think I only need to change a line or two in the splinebase function, but I wouldn't know which, since I do not understand the fine details of it, in spite the very crisp and clear code you wrote.

Michael, splinefit fits a picewise polynomial curve to your data set by least squares. In the cubic case the curve has continuous second derivative. In other cases the regularity will follow the order of the spline. This is achieved by a base of spline curves with minimal support (B-splines). The method is straightforward and I have no specific reference. You can study B-splines and the method of least squares in the textbooks.

The smoothing effect on the noisy data is controlled by the degrees of freedom of the curve, i.e. the number of pieces. A cubic spline with P pieces has P+3 degrees of freedom. If the number of data points is greater than P+3 smoothing will happen.

Splinefit has no support for a desired tolerance or standard deviation. You have to select the number of pieces, see what you get and try again OR write a clever code for the task.

Outstanding work with the splinefit function, this is exactely what i have been looking for. Very clean code and good documentation including the published examples.
I also like the ppdiff and the ppint function, how they work seamlessly with the standard piecewise functions.

Splinefit works great for 1D data, and I see ND support, but it looks like this simply facilitates batch processing several sets of 1D data. Is it possible to use this to generate 2D (an higher dimensional) splines? If so, some examples would be great!

Sami, the robust fitting scheme uses weigthed regression where the weights are computed from previous residuals. It is close to the scheme described by John D'Errico in Optimization Tips and Tricks, section 33 (File ID: #8553).

The periodic condition forces endpoint derivatives to be equal. Example: A cubic spline with endpoints x=1 and x=4 (period length 3) satisfies the conditions y(1) = y(4), y'(1) = y'(4) and y"(1) = y"(4). This is perhaps not evident in the code where the B-splines at the endpoints are matched (pairwise) to have the same shape (and the same derivatives).