LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Lev-Mar fit: linear baseline + 4 gauss

Solved!
Go to solution

Hi,

 

I've been working on a fit with LV. I have a set of data presenting a baseline and 4 superposed gaussian.

 

Using general functions: 

 

a1 * exp (-0.5*(( x- b1 )/ c1 )^2)+

a2 * exp (-0.5*(( x- b2 )/ c2 )^2)+

a3 * exp (-0.5*(( x- b3 )/ c3 )^2)+

a4 * exp (-0.5*(( x- b4 )/ c4 )^2)+

d*x+e

 

where a,b,c are the parameters of the gaussian curves; d, e those of my baseline and x my variable.

 

If you try the VI, you'll find a solution but I'd like to improve it.

 

We want the parameter c1, c2, c3, c4 to be the same, lets call it "c". That way my gaussians all have the same "width" (that's what we want).

 

When I try to simplify those parameters to only one, the VI returs an error -20041: The system of equations cannot be solved because the input matrix is singular

 

I don't know how to obtain that. I guess it is quite simple but I am not use to LV.

 

Can you please give me a hand on this problem.

 

attached: VI and data (changed the extension as it seems I can't send .lvm files)

 

Cheers

Félix

 

Download All
0 Kudos
Message 1 of 10
(4,395 Views)
Solution
Accepted by topic author felixd

Felix,

 

Generally the Lev_Mar VI which uses a Curve Fit Model subVI is more robust than the formual version, although not quite as easy to set up.  Read the detailed Help carefully! When you have an equation which can be differntiated analytically, calculating the partial derivatives in the model subVI often avoids numerical approximation problems also.

 

When I put together the model VI attached below it seems to work OK with the data file you attached. Note: I used the Constrained L-M Curve Fit VI because I could not get at the non-string version of the regular L-M VI for some reason.

 

Lynn

Download All
Message 2 of 10
(4,364 Views)

Sounds terrific.

 

Thank you for your help and advice. I'll learn how to prepare subVi like this.

 

Cheers

Félix

0 Kudos
Message 3 of 10
(4,356 Views)

Hi,

 

I've been working around my set-up and the solution you gave me is working fine.

 

My problem is about the fit parameters. I've seen that the fit is strongly dependant of it. Here is what I have. 

 

A set of spectrum, taken every hour. I'd like to fit them and see the variation of the amplitude and FWHM for a selected gaussian peak. I used a feedback node and the replace array subset to "reset" my initial parameters with the best fit parameters.

 

But then the fit doesn't match the new spectrum. The error is even bigger and grows up.

 

Do you have any idea for this?

 

Thanks

Félix

 

P.S. Attached a series of spectrum

0 Kudos
Message 4 of 10
(4,325 Views)

I do not seem to see the kind of effect that you mention.  Running my program with the data you posted shows the fit varying somewhat accross the dataset, but I do not observe a growing error.  When I put the Initial parameters into a shift register and connect the Best Fit Coefficients to the right terminal I see almost no difference.

 

I tried putting a delay in the loop so I could watch the progression, but that did not give me any clues either. It does not seem to "track" the position of the peaks.

 

There is an error in the notes for the quad gauss 1 VI.  The code is OK, I think.  The df/dbi terms should be df/db1 = a1*(x-b1)/(c*c)*exp(-0.5*(((x-b1)/c)^2)).  The (c*c) needs to be in the denominator.  I omitted the parentheses.

 

Sorry I do not have any better suggestions.

 

Lynn

0 Kudos
Message 5 of 10
(4,302 Views)

Thank you for your help. This really helped me into my work.

Cheers

Félix

0 Kudos
Message 6 of 10
(4,293 Views)

Hi Lynn,

 

I finally found the solution. We had this ideas of reducing the number of parameters, thus I had to change the model.

 

I found a connection errors and some partial derivative errors, all corrected it workd just fine.

 

Thank you for you help as I finish my programming work here.

 

Cheers,

Félix

 

0 Kudos
Message 7 of 10
(4,282 Views)

Felix,

 

My apologies for the errors.

 

Glad you got it working.

 

Lynn

0 Kudos
Message 8 of 10
(4,277 Views)

@felixd wrote:

I found a connection errors and some partial derivative errors, all corrected it workd just fine.



One of the main reasons for errors is an overly complicated diagram.

 

  • I would arrange the parameters in a better order (you need to change the calling VI too!)
  • You don't need the partial derivatives. To fit experimental data, numeric derivatives are sufficient and the fitting routines will calculate them automatically. Just don't connect the partial derivatives output.
  • There is way too much duplicate code, one gaussian should be enough, simply loop over all components. (Imagine you want 2 or 20 gaussians at a later point? Imagine you want a slightly different formula: you would need to make the same change in n places.)

Here's a quick draft that should get the job done with significantly less code. Try it. 😉

 

 

(For further speedup, you can parallelize the FOR loop above if you have multiple CPU cores)

Download All
Message 9 of 10
(4,256 Views)

I take note of the comments, thank you.

 

I tried with you proposition, works fine and less complicated.

 

Cheers

0 Kudos
Message 10 of 10
(4,237 Views)