LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Gaussian Peak Fit Algorithm Bug

I have been trying to fit a Gaussian to some 1D data shaped like a single Gaussian peak. The Gaussian Peak Fit VI should make short work of this, but there is usually at least one parameter which is completely off the mark. After looking up the VI I suspect this may be due to a minor bug in the algorithm. The 'min' in the decision node in the flow chart found here (https://zone.ni.com/reference/en-XX/help/371361R-01/gmath/gaussian_peak_fit/) should surely be a 'max' right? Is this how it is implemented in the dll? If there is indeed a problem would it be possible to get a replacement lvanlys.dll?

0 Kudos
Message 1 of 13
(2,244 Views)

Hi Peter,

 


@PeterMek wrote:

After looking up the VI I suspect this may be due to a minor bug in the algorithm. The 'min' in the decision node in the flow chart found here (https://zone.ni.com/reference/en-XX/help/371361R-01/gmath/gaussian_peak_fit/) should surely be a 'max' right?


Why don't you attach an example VI to demonstrate that suspected bug? That VI should also include some example data, either in constants or in controls with default values!

 

Btw. the help also explains the tolerance parameter like this:

For the Bisquare method, if any relative difference between amplitude, center, standard deviation, or offset in two successive iterations is less than tolerance, this VI returns the resulting amplitude, center, standard deviation, and offset.

So the flow chart is correct!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 13
(2,237 Views)

I read it that way too but that's my issue. If it's 'if any' then it might fit the amplitude, for example, to a great precision and then return the amplitude, offset, deviation, and center even if the other parameters have not converged. I usually found this when I tried using it. A few parameters were right and the others way off. I assume it should be 'if all' to ensure all have converged. I've replicated the part of my program which does the fitting as best I could and included some data as an example. In this case the number it doesn't hit is the standard deviation.

Download All
0 Kudos
Message 3 of 13
(2,212 Views)

Hi Peter,

 

please convert the VI for LV2017 and attach again…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 13
(2,199 Views)

Hi GerdW

This would hopefully do it. Also thanks for the help so far.
Peter

0 Kudos
Message 5 of 13
(2,185 Views)

Hi Peter,

 


@PeterMek wrote:

This would hopefully do it.


Nope.

You need to attach the downconverted version of the VI, but not the LV2019 version again…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 6 of 13
(2,182 Views)

Your data set is very strange!  You have five points that have meaningful data (points 26 through 30), padded with 45 zeros, and are fitting a Gaussian with four parameters.  If you look at your fit, there is only one point (the last non-zero one) that appears to be a little "high".

 

Too bad you are not fitting a 4th-degree polynomial -- you could have had a perfect fit to those 5 points, no error at all.

 

Time to learn more about fitting models to data.

 

Bob Schor

0 Kudos
Message 7 of 13
(2,176 Views)

Oops sorry. I checked this one with LV2017 so it definitely opens there.

Bob Schorf
I am aware I can fit the data with a polynomial with a low or no residual. I don't want to do this, I am specifcally interested in knowing which Gaussian function would best approximate the data here. Furthermore the issue I raise here is entirely surrounding the Gaussian Peak Fit algorithm's stopping criteria. The offset is constrained to 0 in the VI so I am looking for the best 3 parameters (amplitude, mean, standard deviation) which fit the 4 or 5 data points I have. The zeros were once noise but I soft thresholded them out because I reasoned it might interfere with the peak finding. I think it's a relatively straightforward task for other programming languages so I am surprised it does not work well here. Any clarification would be great.

 

Thanks for the insights
Peter

Download All
0 Kudos
Message 8 of 13
(2,172 Views)

Oops, saw that I couldn't even spell my name correctly (no points off to you -- I managed to "fix" my post after you pointed out the extra letter in my signature).

 

I suspect that what you are observing might be a "Feature" of NI's implementation of the Gaussian Peak Fit Algorithm rather than a "Bug".  The BiSquare method is designed to "de-emphasize" points that are far from the best-fit curve (which is the case with your data-set -- you have one outlier out of 5 "usable" points) -- the designers may have adopted the "Any" criteria as a better heuristic. Did you notice that Least Absolute Residual and BiSquare give essentially the same result (discarding the last non-zero point), while Least Squares "looks less good" (I'd say because there are insufficient data)?

 

Of course, you could always code your own Gaussian Peak Fit routine ...

 

Of course, you could alway

 

Bob Schor

0 Kudos
Message 9 of 13
(2,163 Views)

Ah I can't seem to edit the last post to correct the name, maybe I'll figure that out in a bit.

I've included a new data set which I generated by simply making a Gaussian and adding a bit of noise. The parameters are the same in both examples all that changed was the noise vector. The new data set should have more than 5 "usable" points, although I would argue that zeros are very usable as they tell you where the value of the Gaussian should be close to, well, zero. For least squares you do take the RMSE over the whole vector after all and it's clear from the residuals that one fit is significantly better than the other (if not immediately obvious fromt the plot).

In any case the issue I raise is somewhat independent of the data as these are just examples, I'll need to run the VI 10,000 odd times on similar looking data and I can't guarantee it will always give a reasonably close estimation for the Gaussian parameters even if I use absolute residual or bisquare.

I would also be surprised if the "if any" criterion were a feature since you can simply stop non-covergence problems by putting a cap on the number of iterations without jeopardising the accuracy of the other parameters. I am sure I am not the first to come up with that concept for iterative algorithms. The cap might give me the same result if the algorithm is simply very upset with my data but in that case I would know it's the data itself and I'd have to find another way.

Thanks for the help and the effort you have gone to so far I didn't expect responses so quickly.

0 Kudos
Message 10 of 13
(2,140 Views)