MATLAB: Extracting data from a gaussian

extractinggaussian

Hello everyone,
I am facing troubles trying to extract data from a gaussian curve. I can extract the information I need from my raw data (max y, corresponding x value, and FWHM) but I am not sure how to get a gaussian fit and extract the same data from it.
I am able to create a gaussian using fit
f1=fit(Mwavelength(:,t),MPL(:,t),'gauss2');
but cannot get any information out of my gaussian curve, only plot it since the variable f1 comes out to be a 1 x 1 cfit.
Any help would be greatly appreciated!
N=(196x7 string) % Each entry corresponds to the title of a text file, which contains two columns of data.
for t = 1:7
for k = 1:196
spectra = importdata(N(k,t)); % Import the data from the .txt file
%I want to get a gaussian fit of the spectra here, and then gather the wavelength (x axis values) and PL (yaxis values) from that.
wavelength(:,k) = spectra(:,1); % my x-axis
PL(:,k) = spectra(:,2); % my y-axis
[M,I] = max(PL(:,k));
X = wavelength(I,k);
Peaks(k,t) = M; % Creates matrix 'Peaks' with the max peak for all pixels in the 14x14 data collection area
Position(k,t) = X; % Creates matrix 'Position' with the corresponding x value at the peak.
end
Mwavelength(:,t) = mean(wavelength,2);
MPL(:,t) = mean(PL,2);
end

Best Answer

Hi,
As you saw,
methods(f1)
returns the plausible functions that can be used with the cfit class object f1. You can check the documentation to know more about them.
coeffvalues(f1)
returns the coefficient values for the function
fun(x) = a1*exp(-((x-b1)/c1)^2) + a2*exp(-((x-b2)/c2)^2)
as a row vector. You can use these coefficients to recreate the estimated curve and extracting the required information from it.
For example:
function estimated_y = gaussian(x,coeff_vector)
estimated_y = coeff_vector (1)*exp(-((x- coeff_vector (2))/ coeff_vector (3)).^2) + coeff_vector (4)*exp(-((x- coeff_vector (5))/ coeff_vector (6)).^2);
end
The above function takes as input the x values and the coefficient values of the curve and returns the vector of estimated Y values from it. You can use findpeaks and max functions to know the maximum x and y values from the curve. Similarly, after knowing the curve parameters you can estimate other required information. For FWHM, you can refer here.