# Scilab's 'optim' and 'splin'

Started by June 5, 2006
```I've got this really cool application that makes a smoothed line using
splines, then fits it to a source line using a nonlinear weighting.  I'm
currently using SciLab with it's 'optim' and 'splin' function to do
this, but now I need to write some embeddable C++ code.

The 'splin' function is obvious -- it generates a spline from a number
of inflection points.  The 'optim' function as I am using it uses a
quasi-Newton method, which is working just fine for me.

Before I go and write this stuff up by hand, does anyone have any
suggestions on where I can find example code to do this?  I'm mostly
concerned with the optimization -- I'd like to get my hands on something
where some one else has already fallen into the pitfalls.

TIA.

--

Tim Wescott
Wescott Design Services
http://www.wescottdesign.com

"Applied Control Theory for Embedded Systems" came out in April.
See details at http://www.wescottdesign.com/actfes/actfes.html
```
```  Just a remark : there is a lsq_splin function which try to fit
a splin to discrete points (with various weight if needed).
May be it can help you at least to find a good starting point

hth
Bruno

```
```Tim Wescott wrote:
> I've got this really cool application that makes a smoothed line using
> splines, then fits it to a source line using a nonlinear weighting.

Is it important that you use non-liniear weighting?  There are many
types of splines.

I'm
> currently using SciLab with it's 'optim' and 'splin' function to do
> this, but now I need to write some embeddable C++ code.
>
If this is a motion application then how are you controlling the
velocity and acceleration at the end points?

> The 'splin' function is obvious -- it generates a spline from a number
> of inflection points.  The 'optim' function as I am using it uses a
> quasi-Newton method, which is working just fine for me.
>

It must not be necessary for the curve to go through the points.

> Before I go and write this stuff up by hand, does anyone have any
> suggestions on where I can find example code to do this?  I'm mostly
> concerned with the optimization -- I'd like to get my hands on something
> where some one else has already fallen into the pitfalls.
>
Not do exactly what you stated.  We use the cubic spline algorithm from
Numerical Recipes in C.  If you understand what the book says you can
enhance the algorithm to make the end points have first and second
derivatives of your choosing within limits.
On a motion application that usually means they are set to 0.

What are you really trying to do?

Peter Nachtwey

```
```Peter Nachtwey wrote:

> Tim Wescott wrote:
>
>>I've got this really cool application that makes a smoothed line using
>>splines, then fits it to a source line using a nonlinear weighting.
>
>
> Is it important that you use non-liniear weighting?

Yes.  It's a sort of pattern matching, where I'm trying to fit to a
slow-moving signal while ignoring the occasional fast-moving one.

> There are many types of splines.

Cubic splines seem to work well; I'll be experimenting with flavors of
endpoints to figure out what's best.
>
>   I'm
>
>>currently using SciLab with it's 'optim' and 'splin' function to do
>>this, but now I need to write some embeddable C++ code.
>>
>
> If this is a motion application then how are you controlling the
> velocity and acceleration at the end points?
>
Nope, it's data processing.
>
>>The 'splin' function is obvious -- it generates a spline from a number
>>of inflection points.  The 'optim' function as I am using it uses a
>>quasi-Newton method, which is working just fine for me.
>>
>
>
> It must not be necessary for the curve to go through the points.
>
The smoothed line doesn't have to go through the 'real' line, it just
needs to be a good fit per the weighting function.
>
>>Before I go and write this stuff up by hand, does anyone have any
>>suggestions on where I can find example code to do this?  I'm mostly
>>concerned with the optimization -- I'd like to get my hands on something
>>where some one else has already fallen into the pitfalls.
>>
>
> Not do exactly what you stated.  We use the cubic spline algorithm from
> Numerical Recipes in C.  If you understand what the book says you can
> enhance the algorithm to make the end points have first and second
> derivatives of your choosing within limits.

I need to hie me to a book store and get a copy -- but I have heard that
they have a rather draconian licensing policy for their code, and that
it's sometimes buggy.  I'd rather find something that's known to be
freely available for use, is suspected to work correctly on the first
try, and is already in electronic form.

Particularly for the spline, which is pretty easy to solve from first
principals.

> On a motion application that usually means they are set to 0.

One would hope.
>
> What are you really trying to do?
>
> Peter Nachtwey
>

--

Tim Wescott
Wescott Design Services
http://www.wescottdesign.com

"Applied Control Theory for Embedded Systems" came out in April.
See details at http://www.wescottdesign.com/actfes/actfes.html
```
```Tim Wescott wrote:

> Peter Nachtwey wrote:
>
>> Tim Wescott wrote:

...

>> Is it important that you use non-liniear weighting?
>
>
> Yes.  It's a sort of pattern matching, where I'm trying to fit to a
> slow-moving signal while ignoring the occasional fast-moving one.

Can you filter out the highs before connecting the dots?

...

Jerry
--
Engineering is the art of making what you want from things you can get.
&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
```
```Jerry Avins wrote:

> Tim Wescott wrote:
>
>
>>Peter Nachtwey wrote:
>>
>>
>>>Tim Wescott wrote:
>
>
>   ...
>
>
>>>Is it important that you use non-liniear weighting?
>>
>>
>>Yes.  It's a sort of pattern matching, where I'm trying to fit to a
>>slow-moving signal while ignoring the occasional fast-moving one.
>
>
> Can you filter out the highs before connecting the dots?
>
>   ...
>
> Jerry

Well, the customer's problem is segmenting what's a 'high' from what's a
'signal'.  This nonlinear fit to spline method seems to work very well
in SciLab -- it took an amazingly small amount of fiddling to get it
going.  So I really don't want to change the algorithm, I just want to
make it execute on my client's machine (which won't support SciLab,
more's the pity).

--

Tim Wescott
Wescott Design Services
http://www.wescottdesign.com

"Applied Control Theory for Embedded Systems" came out in April.
See details at http://www.wescottdesign.com/actfes/actfes.html
```
```Tim Wescott wrote:

> Nope, it's data processing.
>
> I need to hie me to a book store and get a copy -- but I have heard that
> they have a rather draconian licensing policy for their code, and that
> it's sometimes buggy.  I'd rather find something that's known to be
> freely available for use, is suspected to work correctly on the first
> try, and is already in electronic form.

Another thing you may try is the Savotzky-Golay data smoothing.  Of
course this means that you are batch processing the data.  It is also
in the NR in C.
The code and the book are cheap for the information they have.  The
licensing is strict and I don't always like the way they organize their
code.

Most of the bugs you hear about were from the original version.

Peter Nachtwey

```