# Sum of two gaussian fitting

Hi all,
Is there a plugin to fit a line scan to a sum of 2 gaussian ?
Thank you

Could you clarify what youâ€™re looking for? Do you want to fit a mixture of two Gaussians to a univariate vector? If so what for? There may be other ways of reaching your goal.

1 Like

Hi jkh1,
I draw line scans on images of microtubule (filaments in cells) after expansion microscopy (see enclosed). I obtain list of values (see below) from which I want to fit a sum of two Gaussian.
I want to get the value of the mean of each gaussian. I can do it in GraphPad (see fitting in red in the image of the curve ) but I want to do a macro in ImageJ to get the results from several line scans on each image of microtubules.

The distance between the two mean of gaussian will give me an idea of the width of the microtubule after staining and expansion.

X Y
0, 0,00000
1, 0,00000
2, 0,00000
3, 0,00000
4, 0,00000
5, 0,00000
6, 0,00000
7, 0,00000
8, 0,00000
9, 0,00000
10, 0,13946
11, 2,86704
12, 13,63093
13, 33,85059
14, 55,94421
15, 63,07479
16, 53,80614
17, 38,09314
18, 30,12526
19, 28,00000
20, 27,08540
21, 28,22264
22, 29,45161
23, 35,39682
24, 46,72342
25, 59,76957
26, 57,14603
27, 36,40720
28, 13,28155
29, 1,15738
30, 0,03142
31, 0,00000
32, 0,00000
33, 0,00000

If all you care about is the distance between the means, you donâ€™t need the fitting procedure, you could just detect the peaks and measure the distance between them. Given that your data seems well behaved, the find_peaks plugin could be all you need (links to other potentially useful resources can also be found under this link).
However, I normally do this kind of analysis in R.

Hello Eric -

Fiji / ImageJâ€™s built-in curve fitter,
`Analyze > Tools > Curve Fitting...`, will let you enter a
custom function to fit as a macro string.

(If for some reason this doesnâ€™t work for you, you can run its
underlying curve-fitting class, CurveFitter, programmatically
and pass it a custom function written in java.)

Thanks, mm

3 Likes

Hi Mountain Man,

``````  Thanks for your suggestion. I did not realize that I can wrote
``````

the equation myself with the curve fitter !!!

Bear in mind that you can use up to six parameters (at least, thatâ€™s the limit in IJ macros), so you cannot get two full-fledged gaussians (baseline, max-height, mean and SD). I usually get around that by ensuring a zero-baseline curve to start with. Also, to ensure convergence, you usually have to provide some ballpark estimates for the initial values (guesses), as in this example.

Cheers,
Nico

Hello Nico (and Eric) -

It looks like you can barely sneak in under the wire with just six
(IJM-formula) parameters.

If you are fitting a truly normalized two-gaussian probability
distribution (that is, the probability distribution integrates to one),
you only need five parameters: the two means, the two sigmas,
and the fraction of probability in the first gaussian vs. the second.

But you probably arenâ€™t fitting a normalized double-gaussian.
(You might well be fitting a histogram or something. In a case
like this, your data could be understood to represent a probability
distribution, so you could reasonably normalize your data before
fitting, but thatâ€™s a pain.)

parameter to be fit takes you up to six.

In many cases these six parameters will be enough. You often
wonâ€™t have (or want) a non-zero baseline â€“ your probability
distribution normally should go to zero in the tails. (As will the
data you fit if it comes from a typical histogram where the counts
in the bins towards the edges represent rare events.)

If you need the baseline (and you have to fit the normalization
constant), you will, indeed, need seven (i.e., more than six)
parameters, and will need to use the `UserFunction` argument
to the CurveFitter.doCustomFit() method by writing some java
code. (No limit on the number of fit parameters is imposed in this
case.)

Thanks, mm

1 Like

Indeed, thatâ€™s the case. As you describe, you can barely fit two gaussians if you assume a zero background. Then you can use the 6 parameters as two sets of {amplitude, mean, SD} for each.

Thatâ€™s a neat trick I didnâ€™t know! Iâ€™ll keep that in mind for the future.

Thanks!
Nico

Thank you all for your suggestions.
I was also interested in the SD of the two gaussian. Thus I will do a background substraction so that I only need 6 parameters. I will also use your suggestion for Array.findMaxima.