How to fit multiple gaussian in a curve ?
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (2)
Image Analyst
le 26 Fév 2023
2 votes
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.
1 commentaire
Abdelhakim Souidi
le 26 Fév 2023
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

3 commentaires
James
le 30 Avr 2025
Can you link your code please?
Walter Roberson
le 30 Avr 2025
@Alex Sha used the third party toolbox named "1stOpt" http://www.7d-soft.com/en/ . Alex has a hobby of running requested fits through that software and posting the results. I have always been amazed at how well that toolbox fits data, and how quickly it operates, I have considered buying a license myself... unfortunately I just cannot justify the price.
Hi @James
You should zoom in on the fitted curve to evaluate the quality of the fit from a human perspective. Even with a very high R-squared value, there can be deviations between the curve and the data points.
More importantly, you should investigate why the built-in fit() function did not produce a similar result when the 1stOpt results were assigned as initial fitting values to the coefficients of the gauss6 model.
As @John D'Errico stated, 'Crappy starting values, crappy results.' Does this not conversely imply that 'Quality in, quality out' (QIQO)?
format long g
load('dsp.mat');
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 ;
%% Fit Alex's gauss6 model to the data
x = linspace(f(1), f(end), 100001);
y = a1*exp(-((x-b1)/c1).^2) + a2*exp(-((x-b2)/c2).^2) + a3*exp(-((x-b3)/c3).^2) + ...
a4*exp(-((x-b4)/c4).^2) + a5*exp(-((x-b5)/c5).^2) + a6*exp(-((x-b6)/c6).^2);
figure
plot(f, pxx, '.'), hold on
plot(x, y), grid on, hold off
legend('Data', 'Fitted Model', 'FontSize', 14)
title('Fitting result by 1stOpt')
xlabel('f'), ylabel('pxx')
figure
tl = tiledlayout(2, 2);
nexttile
plot(f, pxx, '.'), hold on
plot(x, y), grid on, hold off
xlim([0 250])
nexttile
plot(f, pxx, '.'), hold on
plot(x, y), grid on, hold off
xlim([250 500])
nexttile
plot(f, pxx, '.'), hold on
plot(x, y), grid on, hold off
xlim([500 750])
nexttile
plot(f, pxx, '.'), hold on
plot(x, y), grid on, hold off
xlim([750 1000])
xlabel(tl, 'f'), ylabel(tl, 'pxx')
%% Fit six Gaussian functions using Alex's best values as starting points
mdl = fittype('gauss6')
[Gauss6, gof] = fit(f, pxx, mdl, 'StartPoint', [a1 b1 c1 a2 b2 c2 a3 b3 c3 a4 b4 c4 a5 b5 c5 a6 b6 c6])
figure
plot(Gauss6, f, pxx), grid on
title('MATLAB Fit using 1stOpt results as initial values')
Catégories
En savoir plus sur Numerical Integration and Differentiation dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!







