How to fit multiple gaussian in a curve ?
203 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Abdelhakim Souidi
le 25 Fév 2023
Réponse apportée : Alex Sha
le 16 Avr 2023
Hello everyone,
I want to get individual gaussian functions to get the area under each peak
So, how I can fit a multiple gaussian in a curve with multiple peaks
0 commentaires
Réponse acceptée
John D'Errico
le 25 Fév 2023
Modifié(e) : John D'Errico
le 25 Fév 2023
Easy enough. You NEED good starting values though. Crappy starting values --> crappy results.
load dsp.mat
plot(f,pxx)
grid on
So there is one mode around x==100, a second mode near x==450, a third mode near x==600, a 4th mode near 750. What matters most is to have good starting values for the modes.
mdl = fittype('gauss4')
fittedmdl = fit(f,pxx,mdl,'start',[1e-5 110 20 1e-5 450 100 1e-5 600 50 1e-5 750 50])
plot(fittedmdl,f,pxx)
And, as you can see, despite my using decent starting values so it found 4 gaussian modes where I told it, they all fit poorly. This points out the next issue. YOUR CURVE IS NOT FIT WELL BY A SUM OF GAUSSIAN MODES! They look vaguely gaussian, but they are not in fact accurately fit by gaussians. I suppose I could have added another mode out around 900, and maybe split that mode near x==450 into two modes. Even then, look at the first pek. It is pretty much completely alone, yet a Gaussian does not fit well. Even so, I am sure you will want to try harder.
mdl = fittype('gauss6')
fittedmdl = fit(f,pxx,mdl,'start',[1e-5 110 20 1e-5 400 100 1e-5 450 100 1e-5 600 50 1e-5 750 50 1e-5 900 50])
plot(fittedmdl,f,pxx)
Still, total, complete, unambiguous crap. Note that between the first and second peak, there is a wide tail, far too wide for a Gaussian to fit. And the first peak itself has wider tails than would be suggested by a Gaussian term.
Using a sum of Gaussians is a poor solution to solve this problem. I'm sorry, but it is. (I'm not going to suggest a good solution, because that itself is not remotely trivial.)
Just because something has a bump does not mean it is well represented by a Gaussian curve shape. The problem is, you will not get a good estimate of the area under those peaks. And where there are several things happening at once, things are far more difficult to decide what area corresponds to each vaguely gaussian "bump".
4 commentaires
John D'Errico
le 26 Fév 2023
Was that not my example in my last comment? That is, I plotted the first Gaussian. I did this:
Gfun = @(x,a,b,c) a*exp(-((x-b)/c).^2);
Do you understand this is the Gaussian shaped function you are using? Next, what did I do?
fplot(@(x) Gfun(x,fmdl.a1,fmdl.b1,fmdl.c1),[0,200])
I plotted the FIRST gaussian. Why are you asking how to do exactly what I just showed you how to do?
Plus de réponses (2)
Image Analyst
le 26 Fév 2023
It looks like your data has about 6 peaks. See my attached demo, that just happens to use 6 peaks but that is a variable number you can change in the code to whatever you want. But you should really know what the theory says. Like are you expecting two peaks or 6? Use whatever the theory says you should be expecting. I have attached another demo that uses fitnlm to fit exactly two Gaussians.
In the above plot you can see the upper 2 curves match pretty well. One is the original data and the other (dashed one) is the sum of all 6 Gaussians.
Alex Sha
le 16 Avr 2023
For the summation of 6 Gaussians function:
Sum Squared Error (SSE): 2.61218021364296E-9
Root of Mean Square Error (RMSE): 4.49993989076388E-6
Correlation Coef. (R): 0.999145442992317
R-Square: 0.998291616252313
Parameter Best Estimate
--------- -------------
a1 0.000616781092742577
a2 0.000218333421684105
a3 -6.3828311170045E-6
a4 -0.000138418678090533
a5 0.000138852036452085
a6 0.000133702730236499
b1 105.976485712389
b2 106.829210687263
b3 7.3291143621259E-14
b4 309.94152870094
b5 379.758692434269
b6 425.217299664439
c1 -9.29831066559539
c2 25.1779734434923
c3 520.573845470695
c4 115.976664646099
c5 -119.557220394127
c6 295.98790157476
0 commentaires
Voir également
Catégories
En savoir plus sur Descriptive Statistics dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!