Fitting a Gaussian to multiple data sets

24 visualizaciones (últimos 30 días)
mabinj
mabinj el 5 de Feb. de 2019
Comentada: mabinj el 6 de Feb. de 2019
I am trying to analyse a spectral feature over time, attached is an image of the feature and the colour grading is with respect to time, black is T=0, red is T=end. I have tried to find a solution to this from other answered questions but am drawing a blank. What I'm trying to do is loop through each data set and fit a gaussian model to each, I can achieve this with the following code:
clear all
%Importing file
sixthpks = csvread('Peaks_1570-1640(secondrun).csv');
x_data = sixthpks(:,1);
y_data = sixthpks(:,2:end);
%Plotting
figure(1)
hold on
for pltcount = 1:size(y_data,2);
plot(x_data, y_data(:,pltcount),'Color',[pltcount/size(y_data,2),0,0]);
end
xlabel('Wavenumber (cm^{-1})');
ylabel('Absorbance');
hold off
%Fitting
for j = 1:size(y_data,2);
f = fit(x_data,y_data(:,j),'gauss2');
plot(f,x_data,y_data);
end
For context, the file being read in has wavenumbers in the first column and spectral data in the subsequent columns. The plotting section of code just produces the plot(attached) and the fitting section of code is where I am struggling.
The feature can be fitted by a two-term gaussian model, however there is no way to store the information about the fit which I need. I can't use the curve fitting toolbox (to the best of my knowledge) as it only deals with single data sets, I essentially need the fit to write it's variables into a seperate variable matrix, i.e the amplitude a1 and a2 used in the fit, should be written to variables a2 and a2 so I can plot them outside of the loop and the peak central location b1 and b2 be written to seperate variables for plotting.
Am I missing an easier way to analyse this in matlab?
Shoulder_peak@1603.png

Respuestas (1)

Jeff Miller
Jeff Miller el 6 de Feb. de 2019
If you are just trying to save the fit outputs, it seems like this should work:
saveme = cell(size(y_data,2),1) % before the loop where you do the fitting
and
saveme{j} = f; % inside the loop where you do the fitting, after each fit
Then after the fitting loop is finished saveme will hold the fitting object produced by each fit.
HTH--not sure I really understood the question.
  1 comentario
mabinj
mabinj el 6 de Feb. de 2019
Hi Jeff,
I am trying to save the fit outputs yes from the cfit function, but have found an alternative way which can do this within the loop:
%Fitting
for j = 1:size(y_data,2);
f= fit(x_data,y_data(:,j),'gauss2');
MyCoeffs(:,j) = coeffvalues(f);
plot(f,x_data,y_data);
end
Here I can store all my coefficients of the fit and plot them as a function of time.
I still have an underlying issue with using gauss2 on multiple datasets using the curvefit toolbox, but I'll figure that out.
Thanks again!

Iniciar sesión para comentar.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by