LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I fit to a step function?

Hi,

I'm using the Levenberg-Marquard algorithm to optimize a fit to some data.  I would like to fit to a step function, but when I try to do this with simulated data, the algorithm is not at all responsive to optimizing the position of where the step occurs.  (It simply returns the initial guess position of the step location, rather than optimizing it.)  I have tried using both the step(x) and sign(x) functions but both have the same problem.  Does anyone have any experience with this? 
 
Cheers,
Nancy
0 Kudos
Message 1 of 9
(4,011 Views)
Nancy,

I have not used the Lev_Mar functions very much, but I think I recall that they use derivatives. Since the derivatives of the sign and step functions are undefined at the step, this may be part of your difficulty.

To fit to a step function try the Threshold Array function with the threshhold set to 0.5*(ArrayMax - ArrayMin). This should give you the index of the step. The Max and Min values would be first estimates of the before and after values, if the data is not too noisy.

Lynn
Message 2 of 9
(4,003 Views)
The problem is with the partial derivatives for the position parameter. If the position changes less than the point spacing, the partial derivative with respect to that parameter is always zero.
 
I have quickly hacked the ABX subVI to use something larger than the x spacing of the points as increment for the partial derivative (instead of 1e-6), and it converges just fine after that if the initial estimate is reasonable. (See image). Still, I recommend to not do that!
 
 
Follow Lynn's advice. 😉
 
 
 

Message Edited by altenbach on 05-10-2006 01:01 PM

Message 3 of 9
(4,000 Views)

Hi Lynn & altenbach,

Thanks for your suggestions.  Yours seems quite simple to implement, Lynn, but unfortunately, while for my test case the noise is less than the step size, the signal I would ideally like to fit to is not substantially larger than the noise, so thresholding the array won't work in general. 

As to the step size, I don't see how to change it in the fitting algorithm, but I did try making the increments of x orders of magnitude smaller, so that the step occurs over a length less than 1E-6.  While the Lev_Mar algorithm now gives a step location different than that input, it is still much closer to the input guess than to the new location of the step. 

Any other ideas?

Cheers,

Nancy

 

0 Kudos
Message 4 of 9
(3,981 Views)
Can you attach some sample data?
0 Kudos
Message 5 of 9
(3,978 Views)

Yes, the increment trick should not work that well in general, because even though we now have a direction for the partial derivative, the magnitude is still very quantized and only relies on one or a few data points.

Is your data equally spaced in X?

0 Kudos
Message 6 of 9
(3,974 Views)
If I use the following as my y-coordinates:  (0,0,0,0,0,2,2,2,2,2) and try to fit to the following function:
a*0.5*(sign(y-b)+1)+c
with guesses:
a=2
b=5
c=0
I get out as best-fit coefficients:
a=2.00
b=5.00
c=8.70E-5
 
If I now change the position of the step in the input data to position four (0,0,0,0,2,2,2,2,2,2) with the same input guesses, I get out, as best-fit coefficients:
a=2.08
b=5.00
c=0.136
 
I have just tried using a sigmoid function (continuous) to fit to this step data, with much better results (the step position is optimized).  Ideally, I'd like to use a step function, but there is at least one alternate solution if that fails.
 
Cheers,
Nancy
 
0 Kudos
Message 7 of 9
(3,972 Views)
Yes, the data is equally spaced in x.  For the sample data I just quoted, the results assume a spacing of one.  The results don't change (in any meaningful way) if I use a spacing of 1E-7.  (The numbers might change, but the problem is the same, where the step location, b, does not get optimized.)
 
0 Kudos
Message 8 of 9
(3,971 Views)
Hello,
 
You can easily do this with TOMVIEW using a derivative free solver, such as glbDirect or glcDirect.
 
These are global solvers so you will need to set conservative bounds on your decision variables.
 
Look at for example nllsQG.vi in the quickguide, then use glcDirect as the solver options.
 
Best wishes, Marcus
Marcus M. Edvall
Tomlab Optimization Inc.
855 Beech St #121
San Diego, CA 92101-2886
USA

web: http://tomopt.com
e-mail: medREMvall@tomREMopt.coREMm
Office1: (619) 203-2037
Office2: (619) 595-0472
Fax: (619) 245-2476
0 Kudos
Message 9 of 9
(3,968 Views)