Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Fitting using spline basis function

Subject: Fitting using spline basis function

From: Aino

Date: 17 Jul, 2008 07:27:02

Message: 1 of 7

Hello all!

I have been trying to fit my (noisy) signal with suitable
basis function. I noticed that polynomial basis function was
not flexible enough so I changed to splines. I've started
with cubic spline interpolation and noticed that the created
curve goes through every point of my data (not good since I
estimate the goodness of the fit using residuals). Second, I
tried using functions such as 'spline' but this just
connected the dots (say every tenth dot) and gave no smooth
fit.

Basically, what I need is a smooth fitted curve created by
flexible basis functions (perhaps cubic splines) so that
residuals are not zero. I also have A LOT of data, so
efficiency is important.

Could someone tip me off with the Matlab functions that I
need to use.

Thank you.

Subject: Fitting using spline basis function

From: Bruno Luong

Date: 17 Jul, 2008 08:12:02

Message: 2 of 7

"Aino " <aino.tietavainen@removeThis.helsinki.fi> wrote in
message <g5ms86$kui$1@fred.mathworks.com>...
> Hello all!
>
> I have been trying to fit my (noisy) signal with suitable
> basis function. I noticed that polynomial basis function was
> not flexible enough so I changed to splines. I've started
> with cubic spline interpolation and noticed that the created
> curve goes through every point of my data (not good since I
> estimate the goodness of the fit using residuals). Second, I
> tried using functions such as 'spline' but this just
> connected the dots (say every tenth dot) and gave no smooth
> fit.
>
> Basically, what I need is a smooth fitted curve created by
> flexible basis functions (perhaps cubic splines) so that
> residuals are not zero. I also have A LOT of data, so
> efficiency is important.
>
> Could someone tip me off with the Matlab functions that I
> need to use.
>

1. You must create a basis of spline functions {Sj} on a
coarse knots.

2. For each basis spline function Sj, compute the spline
values at each abscissa xi of the data. Consider each of
those set of values as a vectors (same dimension as your data).

3. Form a matrix 'A' where each column is the above vector.
In other word Aij = Sj(xi)

4. Form a rhs 'b' with the ordinates yi of your data

5. Compute least-square solution c = A \ b.

The interpolate function is
   f(x) = sum_j c(j).Si(x)

Bruno

Subject: Fitting using spline basis function

From: rych

Date: 17 Jul, 2008 18:36:34

Message: 3 of 7

On Jul 17, 9:12=A0am, "Bruno Luong" <b.lu...@fogale.fr> wrote:
>
> 1. You must create a basis of spline functions {Sj} on a
> coarse knots.
>
Sorry, what is the preferable way of choosing the knots? Some
clustering and choosing a mean in each cluster? Or choosing a mean on
in regularly spaced bins dx? What should be the value of dx?
Another ignorant question, how do I create a basis of spline functions
in Matlab?

Subject: Fitting using spline basis function

From: Bruno Luong

Date: 17 Jul, 2008 19:04:02

Message: 4 of 7

rych <rychphd@gmail.com> wrote in message
<5227196a-d2ca-4c28-ba1d-93f358aefd81@b1g2000hsg.googlegroups.com>...
> On Jul 17, 9:12=A0am, "Bruno Luong" <b.lu...@fogale.fr> wrote:
> >
> > 1. You must create a basis of spline functions {Sj} on a
> > coarse knots.
> >
> Sorry, what is the preferable way of choosing the knots?

The set of knots must cover entirely the abscissa interval
of your data.

Enlarge the interval slightly to avoid end effect (I do not
like MATLAB default not-a-knot-end conditions for spline,
which tends to oscillate a lot, but that's just personal).

The knots should be finer where the model get stiffer, if
you know this information in advance. But start with a
uniform sample is OK. I recommennd to try it first with 10
knots, and experiment it with different number to get your
own feeling.

More number of knots -> Better approximation to data, but
model oscillate more, and vice-versa. You need to find a
compromise from what you expect from the model (similar to
selecting degree for polynomial interpolation).

Later, one might introduce regularization on spline defined
with finer grid, but let's this issue on the side for the
moment.

> Another ignorant question, how do I create a basis of
spline functions
> in Matlab?

A basis function could be a spline function that interpolate
0 for all the knots, and 1 for a selected knot, for example.

Bruno

Subject: Fitting using spline basis function

From: Aino

Date: 30 Jul, 2008 08:34:04

Message: 5 of 7

"Bruno Luong" <b.luong@fogale.fr> wrote in message
<g5o532$720$1@fred.mathworks.com>...
> rych <rychphd@gmail.com> wrote in message
>
<5227196a-d2ca-4c28-ba1d-93f358aefd81@b1g2000hsg.googlegroups.com>...
> > On Jul 17, 9:12=A0am, "Bruno Luong" <b.lu...@fogale.fr>
wrote:
> > >
> > > 1. You must create a basis of spline functions {Sj} on a
> > > coarse knots.
> > >
> > Sorry, what is the preferable way of choosing the knots?
>
> The set of knots must cover entirely the abscissa interval
> of your data.
>
> Enlarge the interval slightly to avoid end effect (I do not
> like MATLAB default not-a-knot-end conditions for spline,
> which tends to oscillate a lot, but that's just personal).
>
> The knots should be finer where the model get stiffer, if
> you know this information in advance. But start with a
> uniform sample is OK. I recommennd to try it first with 10
> knots, and experiment it with different number to get your
> own feeling.
>
> More number of knots -> Better approximation to data, but
> model oscillate more, and vice-versa. You need to find a
> compromise from what you expect from the model (similar to
> selecting degree for polynomial interpolation).
>
> Later, one might introduce regularization on spline defined
> with finer grid, but let's this issue on the side for the
> moment.
>
> > Another ignorant question, how do I create a basis of
> spline functions
> > in Matlab?
>
> A basis function could be a spline function that interpolate
> 0 for all the knots, and 1 for a selected knot, for example.
>
> Bruno
>

Hello again. Thank you for your help. I have been thinking
this, though it seemed a bit overwhelming to me. However,
fortunately I stumbled across something while trying to find
some answers:

y=TR;%TR is my signal
x=1:30000;% time axis
b=1:10:30000;% knots
%It was nice to stumble on this:
a=spline(b,y(:)'/spline(b,eye(length(b)),x(:)'));
v=ppval(x,a);
plot(x,y,'*',x,v,'.')

With this I had a nice approximation of my signal. I was
happily surprised to see that the fit did not exactly pass
every tenth point (knots) as I had suspected, and was indeed
a continuous fit.


 

Subject: Fitting using spline basis function

From: Bruno Luong

Date: 30 Jul, 2008 08:49:02

Message: 6 of 7

"Aino " <aino.tietavainen@removeThis.helsinki.fi> wrote in
message <g6p91s$g07$1@fred.mathworks.com>...

> >
>
> Hello again. Thank you for your help. I have been thinking
> this, though it seemed a bit overwhelming to me. However,
> fortunately I stumbled across something while trying to find
> some answers:
>
> y=TR;%TR is my signal
> x=1:30000;% time axis
> b=1:10:30000;% knots
> %It was nice to stumble on this:
> a=spline(b,y(:)'/spline(b,eye(length(b)),x(:)'));
> v=ppval(x,a);
> plot(x,y,'*',x,v,'.')
>

This code accomplishes exactly what we were just discussing.
We can appreciate how nice MATLAB syntax can be.

Bruno

Subject: Fitting using spline basis function

From: Aino

Date: 25 Aug, 2008 13:17:01

Message: 7 of 7

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in
message <g6p9tu$nmv$1@fred.mathworks.com>...
> "Aino " <aino.tietavainen@removeThis.helsinki.fi> wrote in
> message <g6p91s$g07$1@fred.mathworks.com>...
>
> > >
> >
> > Hello again. Thank you for your help. I have been thinking
> > this, though it seemed a bit overwhelming to me. However,
> > fortunately I stumbled across something while trying to find
> > some answers:
> >
> > y=TR;%TR is my signal
> > x=1:30000;% time axis
> > b=1:10:30000;% knots
> > %It was nice to stumble on this:
> > a=spline(b,y(:)'/spline(b,eye(length(b)),x(:)'));
> > v=ppval(x,a);
> > plot(x,y,'*',x,v,'.')
> >
>
> This code accomplishes exactly what we were just discussing.
> We can appreciate how nice MATLAB syntax can be.
>
> Bruno

I'll continue this one..

It would seem that the splines provide a better fit when the
length of the signal is divisible with the knot interval.
Can this be true and can it be helped?

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us