From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Lev-Mar non-linear fit problem

Hello All,

 

I am trying to get a non-linear fit to my data using the Nonlinear Curve Fit.vi, the Lev-Mar: formula string option.  What I am doing in my VI is sending a pulse to a He-3 cell and displaying the response from a DAQ board.  This is done using the Pulse_and_Recieve subvi.  I need to fit a curve to the response and output the amplitude, frequency, and t2 from the formula a*cos(w*t)*exp(-t/t2) as you will see in the model description for the Lev-Mar fit.  For some reason every time I run the VI the fit line starts at the beginning of the response signal that I am trying to analyze but instead of the oscillating decay behavior that it should follow it immediately becomes a straight horizontal line.  I appreciate any help and/or suggestions.  I hope someone can figure out what I am doing wrong!

 

Thanks a lot,

Justin

 

P.S. The main VI is NMR.vi, while the subvi contained within is the Pulse_and_Recieve.vi.

Download All
0 Kudos
Message 1 of 25
(4,372 Views)

Justin,

 

Please attach some typical data.  We have a much better chance of evaluating where the problems might be with data.

 

Lev-Mar does not do well at fitting the frequency of the sinusoidal component.  The fitting process uses derivatives to force the parameter closer to the actual value.  But the derivative d(cos(wt))/dw = -(1/t)sin(wt) does not force w toward the actual value.  It helps a lot if the initial estimate is very close, but I have seen it move far away even when the exact value was provided as an estimate.

 

For some types of functions using a function VI which calculates the derivatives works better than the formula version.

 

Lynn

0 Kudos
Message 2 of 25
(4,349 Views)

Please reduce the clutter on your VIs (they are 99% DAQ and data processing related!) and let's focus on the fitting part.

 

Please add some indicators to all data input wires leading to the fitting VI, run your VI so they all contain typical data, then convert the new indicators to diagram constants (right-click...change to constant). Now delete all the DAQ part and all that extra stuff. Save and attach the VI again.

 

Periodic signals are sometimes hard to fit because there is an infinite number of alias frequencies that would fit equally well to your data, meaning the initial parameter estimates need to be in the ballpark. You could probably get great parameter estimates by FFT, or even "extract single tone".

 

If you have reasonable guesses, the fitting should converge nicely. 🙂

 

As a first step, graph both the data and the function calculated from the initial guesses on the same graph.  Do they look similar at all?

0 Kudos
Message 3 of 25
(4,340 Views)

Sorry for the dense version.  Here is just the essentials. I think it has the info you need but if it's lacking something let me know and I will update it again.

Download All
0 Kudos
Message 4 of 25
(4,322 Views)

Your data is not in phase with cosine, you either need to clip to data after the first maximum (or minimum, for a negative amplitude), or you need to fit for a phase shift, for example.

 

 

If you graph your function and the data (not fit), it is easy to come up with some reasonable starting values. Here's a manual attempt, but as you can see, the phase is way off.

 

Message 5 of 25
(4,315 Views)

Still a bit of work to fix the broken wires, but the needed info was buried in there.

 

Problems: 

No way you are going to fit that entire data set, it is almost entirely 0, you need to select a subset at the beginning. 

 

1e-10 is not going to be that useful as a termination, 1e-6 is usually fine, maybe 1e-8.  Change the constant to a control and use about 500-2000 as max iteration.

 

You need a phase term, your data does not match the phase of cos(wt)

 

Pick better starting values:  Set max iteration to 0 and run, the best fit will be the formula with the initial parameters.   Change max iteration back to 200 or so when you have decent values.  If you need to programmatically guess, there may be a few things you can do.  Just see how far some generic guesses will take you.

 

19545iA5C1563CD9B3F2D0

 

 

Message 6 of 25
(4,313 Views)

 


@altenbach wrote:

...the phase is way off.


Don't you have the full quadrature data? If you could include the real and imaginary parts, things would be easier. 😉

 

Message 7 of 25
(4,310 Views)

Here's a quick draft to get you started. Just run it. 😄

.

 

Download All
0 Kudos
Message 8 of 25
(4,294 Views)

Hi altenbach,

 

I was looking for somewhat similar curve fit for my project too....But my conditions are that

 

1) I have only 12 data points for each x and y axis which are unevenly positioned.

 

2) how can I modify the formula to a sine series fit of the type "a0*sin(2*pi*f*x+c0)+ a1*sin(2*pi*f*x+c1)+a2*sin(2*pi*f*x+c2)+a3*sin(2*pi*f*x+c4)"

 

Please provide me some assistance with this...I am so stuck try to do this curve fitting...If you ant I can attach my txt data points and my vi model sofar how much I have done...

 

Thanks very much,

 

Marky

0 Kudos
Message 9 of 25
(3,933 Views)

Marky,

 

Please post your attempts and your data.  Without that it is almost impossible to help you.

 

You have 12 data points and want to fit to 8 or 9 parameters?  It appears that all the signals are at the same frequency but have different amplitudes and phases.  I have not tried to work through the math but I suspect that this will be a very difficult fitting problem (if it is even possible) even if you had lots of points.

 

Consider the situation where f*x = 0. Then you have a0*sin(c0) + a1*sin(c1) + a2*sin(c2) + a3*sin(c4). For any non-zero value of f*x you get similar expressions which also include cos(c[i]).  It looks like these expressions are linear combinations and will not be easily separated.

 

Lynn

0 Kudos
Message 10 of 25
(3,919 Views)