LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Fitting 1D array to a double-peak gaussian function

Solved!
Go to solution

Hi LabVIEW experts!

 

I am rewriting a MATLAB code into LabVIEW code. I am stuck at this following part:

---------------------------------

(MATLAB code)

x=1:770

y = current_frame_1D

options.StartPoint=[amp1, pos1, Lim_mean1(3), amp2,pos2, Lim_mean1(3)];

options = fitoptions('gauss2','Lower',[Lower_Lim1,Lower_Lim2], 'Upper',[Upper_Lim1,Upper_Lim2],'TolFun',1e-2,'TolX',1e-1);

 

G_fit = fit(x.',y','gauss2',options);   

 

----------------------

The gauss2 parameter in the MATLAB fit function aims to fit the data following this equation: 

Y = a1*exp(-((x-b1)/c1)^2)+a2*exp(-((x-b2)/c2)^2)

 

Which is a sum of two Gaussian curves. 

 

Is there a way to do this in LabVIEW? (I can only see the Gaussian peak fit.vi, which detects 1 peak only...)

 

Thank you!

 

 

0 Kudos
Message 1 of 5
(4,995 Views)
Solution
Accepted by topic author bazingagirlsun

The Fitting Palette has a Nonlinear Curve Fit function that uses the Levenberg-Marquardt algorithm to fit an arbitrary (in your case, sum of two Gaussians) function.  Nonlinear curve fits are something of a black art -- if you haven't used LM before, I'd do a little reading about it (use the Web).

 

Another method to do such a fit is the Simplex method.  There is a Downhill Simplex nD VI on the Optimization Palette, but when I tried to understand how to use this for an optimization/curve-fitting problem of my own, I gave up and wrote my own Simplex algorithm in LabVIEW.  I don't know if it would work for your data and model -- if you can attach a data file, I might give it a try ...

 

Bob Schor

Message 2 of 5
(4,964 Views)
Solution
Accepted by topic author bazingagirlsun

Start with this and modify as needed (also read the rest of the tread).

 

 

 

Message 3 of 5
(4,954 Views)

@Bob_Schor wrote:

Another method to do such a fit is the Simplex method.  There is a Downhill Simplex nD VI on the Optimization Palette, but when I tried to understand how to use this for an optimization/curve-fitting problem of my own, I gave up and wrote my own Simplex algorithm in LabVIEW.


Yes, the stock downhill simplex is in the optimization palette, not under fitting and it also does not accept a VI model (The code is also a bit ugly ;)). Similar to you, I wrote a nelder-mead simplex fitting VI that is "pin compatible" with the nonlinear fit (VI-model). It converges definitely slower than lev-mar. The advantage of nelder-mead is that it does not require partial derivatives so it can deal better with functions that are not very smooth (e.g. based on an iterative procedure). That's not a problem with Gaussians.

0 Kudos
Message 4 of 5
(4,953 Views)

Thank you for the suggestions! The fitting is still not working, (but I have other bugs in the vi, so I am troubleshooting one-by-one right now).

 

I also found the Sum of 3 Gaussian Model in the LabVIEW Examples, and I am going to see if minor changes to that code will accelerate my application...

0 Kudos
Message 5 of 5
(4,903 Views)