View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

4.7 | 42 ratings Rate this file 152 Downloads (last 30 days) File Size: 188 KB File ID: #13812 Version: 1.12
image thumbnail



Jonas Lundgren (view profile)


31 Jan 2007 (Updated )

Fit a spline to noisy data.

| Watch this File

File Information

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.

- 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

examples - Examples for splinefit
ppdiff - Differentiate piecewise polynomial
ppint - Integrate piecewise polynomial


This file inspired Clickfit Oh For Curve Fitting By Eye/Hand and Ppfit(Varargin).

Required Products Control System Toolbox
MATLAB release MATLAB 7.10 (R2010a)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (71)
05 Nov 2016 Marc Compere

Example 6, robust fitting with outlier data is really nice

03 Nov 2016 Kribashnee Dorasamy

Can this be applied to 3D points x, y ,z and how? I noticed the examples are 2D.

Comment only
27 Apr 2016 Ramoflaple

11 Apr 2016 Vania Valerie

Can you perhaps include a documentation (or reply to this comment) about how the robust weighting works? I would really appreciate that! Thank you so much in advance.

Comment only
07 Apr 2016 ogre99

ogre99 (view profile)

Thanks for Sharing!

05 Apr 2016 Vania Valerie

It is amazing! It works perfectly and contains very clear examples. Thanks for sharing, you're awesome.

09 Feb 2016 Anna Weeks

10 Dec 2015 loonger

Nice! Thanks for sharing!

29 Sep 2015 Wenming Wei

08 Jun 2015 Tianhan

The usage is easy to understand, also the examples help a lot. Thanks!

Comment only
07 Jun 2015 Tianhan

31 May 2015 majid

majid (view profile)

Comment only
23 Jan 2015 majid

majid (view profile)

here you can find lots of useful matlab examples and projects in electrical engineering:

Comment only
02 Dec 2014 Marc

Marc (view profile)

Works excellent. Thanks!

11 Nov 2014 Kahye

Kahye (view profile)

16 Oct 2014 Greg

Greg (view profile)

Excellent. Huge time save. Thank you.

23 Sep 2014 Liwei

Liwei (view profile)

I have one question. The curve fitting use b spline. In the code, mkpp is used. Why not use spmak? the function will represent splines in b-form.

Comment only
22 Sep 2014 Panty

Panty (view profile)

Very good fit of the smoothing line on my data. Good job

22 Sep 2014 Liwei

Liwei (view profile)

28 Aug 2014 samfort

07 Aug 2014 Dennis A. Dean, II, Ph.D.

Exactly what I needed to generate prototypical data examples for presentation from noisy data.

05 Aug 2014 zaki ahmed

how can i convert polynomial fitting into cubic splines

Comment only
20 Feb 2014 francesco

great tool

07 Jan 2014 maryam

maryam (view profile)

23 Aug 2013 Maria

Maria (view profile)

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

I need these std errors for evaluating the results.

Comment only
21 Aug 2013 Greg Fichter

09 May 2013 Dang

Dang (view profile)

It works well on my data. Thanks

26 Sep 2012 venky BS

Thanks for this useful code. It works really nice.

05 Jun 2012 Bradley Treeby


25 May 2012 Marc

Marc (view profile)

How I should write the constrains to have an slope = 0 or a given value. With that you think that i can avoid the overshoot?

Comment only
25 May 2012 Marc

Marc (view profile)

well slope = 0 or a given value

27 Apr 2012 Maas

Maas (view profile)


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.

If you are interested, I could mail my script to you.

Comment only
25 Apr 2012 Jonas Lundgren

Jonas Lundgren (view profile)

Maas, I admit that the splinebase function is a bit cryptic (also for the author). Unfortunatelly I can't see any quick fix to achieve the spline base you ask for.

Comment only
23 Apr 2012 Maas

Maas (view profile)

Jonas, thank you for this wonderful script!

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.

I'd really appreciate your help, thank you in advance.

10 Apr 2012 Daniel Forsberg

Really nice piece of code.

Have a question though. Trying to use this to fit a curve to some 3D points and I have used the suggestion in However, I would like to find the appropriate x- and y-values for curve for given certain z-values. Any suggestions for how to do this?

The only thing that I can think of is to use 1D linear interpolation for the x- and y-values respectively based upon the z-values.

Comment only
28 Mar 2012 Jonas Lundgren

Jonas Lundgren (view profile)

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.

You can find the theory behind csaps/spaps in the documentation.

Comment only
26 Mar 2012 Michael Lindholm Nielsen

I was wondering if you have any references to the method you use? How does it differ to the smoothing spline approach (csaps/spaps)?

Comment only
13 Mar 2012 SHEIKH

SHEIKH (view profile)

Thanks to the author for such a beautiful piece of work.

I was wondering, how to achieve a monotonic spline? Any suggestions?

01 Feb 2012 Elena

Elena (view profile)

Works very well and includes excellent documentation and examples

19 Jan 2012 Moncef

Moncef (view profile)

good guess

Comment only
03 Dec 2011 Sander Aerts

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.

This one certainly deserves a five star rating, congrats on the job

22 Nov 2011 Fer

Fer (view profile)

02 Aug 2011 K B

K B (view profile)

27 Jul 2011 Naveen

Naveen (view profile)

23 May 2011 Jonas Lundgren

Jonas Lundgren (view profile)

Dani, I have no plans for more extensions of splinefit. If you have the Optimization Toolbox you should try SLM.

Comment only
20 May 2011 Dani

Dani (view profile)

Will it be possible in future versions to add monotonicity constrains to the spline?

17 Mar 2011 Jonas Lundgren

Jonas Lundgren (view profile)

Stefan, only equality constraints so far.

Comment only
14 Mar 2011 Stefan

Stefan (view profile)

Vey nice tool! Is there also a possibility to enter non-equalities in the constraints?
eg= slope at certain points should be greater then 2

Comment only
18 Feb 2011 Jonas Lundgren

Jonas Lundgren (view profile)

Gulcan, the smoothing spline is not intended to go through the knots/breaks. The curve fits to noisy data in the least square sense, also for knots. Use the constraint argument if you have exact data.

Example: If the curve must go through the points (0,5) and (2,3) use the constraint
con = struct('xc',[0 2],'yc',[5 3])

Comment only
17 Feb 2011 Gulcan

Gulcan (view profile)

Really appreciate the work, thank you. I have a question though. Cubic spline does not go through all knots that I've selected. Is it related to some constraints that I forget to indicate?

Comment only
12 Nov 2010 Jonas Lundgren

Jonas Lundgren (view profile)

Jonathan, SPLINEFIT is a curve fitting tool and deals with mappings from R to R^N. SPLINEFIT mimics SPLINE and the ND support can of course be replaced by a for-loop, but that will be less efficient.

If you are looking for a surface fitting tool i can recommend GRIDFIT by John D'Errico or SMOOTHN by Damien Garcia.

Comment only
11 Nov 2010 Jonathan

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!

02 Sep 2010 Jonas Lundgren

Jonas Lundgren (view profile)

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).

Comment only
01 Sep 2010 Sami Aldalahmeh

That's a very useful code. Can you please provide some references for the robust spline fit and the periodic condition options?

Comment only
09 Jul 2010 Vinesh Rajpaul

Vinesh Rajpaul (view profile)

Useful...however, support for data containing NaNs would be helpful (spline interpolation using built-in MATLAB functions works fine for data containing NaNs)

Comment only
09 Jul 2010 Vinesh Rajpaul

Vinesh Rajpaul (view profile)

25 Sep 2009 Jonas Lundgren

Jonas Lundgren (view profile)

Non-uniform distributions of breaks/knots is ok. No problem.

Comment only
12 Sep 2009 Ambrogio Manzino

If point distribution is not homogeneous, probably the software have some problem in L.S. parametres solution.
How cai I use the software in a multiresolution field data?

Comment only
02 Sep 2009 Arthur Hebert

Arthur Hebert (view profile)

Easy to use, does what it says, saves me a ton of work. Thanks.

29 Jul 2009 Steffen Huber

Thank you very much - this code saved me a lot of time. Can you please send me references to the implemented method?

28 Apr 2009 David

David (view profile)

Works very good to fit noisy 1D data, saved me a lot of effort, thank you!

22 Apr 2009 Maryam Ra

A very useful code in Matlab. I am wondering if anyone knows how to call this function from VC++?

01 Apr 2009 Amit Ailiani

A very useful code for cubic spline interpolation. Can you please give me the details of the method or any reference to read?

17 Mar 2009 Jonas Lundgren

Jonas Lundgren (view profile)

That's true Chaos. This code was not intended for image processing and the figure shouldn't contain any image noise. How did you do to apply the code to a noisy image?

Comment only
13 Mar 2009 Chaos

Chaos (view profile)

for images, it's too slow, use better steepest descent. this figure isn't 'noisy' at all. try to pluck an image out of the 'dirt' where the SN is about 12dB

06 Feb 2009 Husam Aldahiyat

Good I guess.

23 Jan 2009 Jonas Lundgren

Jonas Lundgren (view profile)

Sorry, SPFIT don't support periodicity.

Comment only
22 Jan 2009 P.S. Ramesh

I am trying to generate a smooth curve through noisy data that is periodic at the ends. Can I use this code or is there a version that enforces periodicty

Comment only
03 Dec 2008 David Villeneuve

Thanks, this provides a simple method to put a smooth line through noisy data. You control the tightness by the number of break points.

05 Aug 2008 M C

10 Mar 2007 Trevor Rayment

this is a useful piece of code. It does exactly what it says it does. Much appreciated.

07 Feb 2007

Code cleanup.

21 Feb 2007

Generalization to piecewise polynomial splines of arbitrary order.

11 Dec 2007

Exact conditions added.

17 Dec 2008 1.1

New polynomial base eliminating half the unknowns.
Short description of the numerical method added.

18 Dec 2008 1.3

Description update.

24 Feb 2009 1.4

Update of examples in help.

06 May 2009 1.5

A faster routine for cubic splines added.

15 May 2009 1.6

Bug fix for SPLINEFIT. Two utilities added.

28 Aug 2009 1.7

Description update

23 Jun 2010 1.8

New version of SPLINEFIT based on B-splines.

01 Sep 2010 1.9

Robust fitting scheme added. Support for data containing NaNs.

01 Jul 2011 1.10

Robust fitting parameter added.

22 Nov 2011 1.11

New contact info

19 Dec 2014 1.12

Illustration of the recursive B-spline generation added (splinebase.png)

Contact us