# How do I fit multiple Gaussians to my data?

28 views (last 30 days)
Marin Vojkovic on 27 May 2020
Commented: Jeff Miller on 28 May 2020
I have 1D data that I want to fit as 3 (possibly more) Gaussian functions. So, I do a simple:
Fit=fitgmdist(AbsY(242:654),3);
z = AbsX(340:580);
pdfz = pdf(Fit,z);
plot(z,pdfz);
figure
plot(AbsX,AbsY)
hold on
plot(z,pdfz);
But the output I get is not what I'm looking for. The script runs and it's ok, but hte output looks like this:
The fit functions overlap, so it seems it's only one line, and their magnitude is way too low.
The examples I've seen on the web and here on Answers use this function, and it's recommended everywhere as a solution to this kind of problems, and it makes sense.
So, what am I doing wrong here?
Any help with this would be greatly appreciated.
I managed to do it in Origin, so the result should be something like this:
Ok, here's 4 Gaussians, but that's besides the point for the moment. How do I get this in Matlab please?

Jeff Miller on 28 May 2020
From the new graph you've posted, it looks like the values in AbsY are not the type of data expected by fitgmdist, and I suspect it isn't actually the right tool for getting you the fit you want. Just to be sure: fitgmdist expects data x that is just one big long list of values for a dependent variable. Think of, for example, the individual heights of a large random sample of people. So, fitgmdist is trying to model the frequency distribution of those x's, i.e., "how often does each of the different possible values appear within the x array?"
It looks like your numbers in AbsY are something else entirely--I'm guessing they are the heights of an intensity function at a certain wavelength. Whatever they are, it looks like you are trying to model a function relating AbsX to AbsY, which is a different kind of problem entirely, because you don't want to describe the counts of AbsY, you want to describe their relation to AbsX.

Marin Vojkovic on 28 May 2020
Yea, that's what I was suspecting.
Yes, as you assume, the blue line is not a histogram, but an intensity function. It's an absorption spectrum that arises from various atomic and molecular excitation states. That's why I want to fit it with the Gaussians to see if we can reproduce the bands that cause the spectrum.
In Origin I used Fit multiple peaks toolbox. Is there something similar in Matlab? I thought the fitgmdist does that, but apparently not.
What would you reccomend method-wise?
Jeff Miller on 28 May 2020
The curve fitting toolbox.