Fitting data to a Gaussian when data is a little bit skewed (but not too much)
Hello, I have some data below that should approx to a Gaussian.
1.00 NaN
2.00 4.00
3.00 3.00
4.00 6.00
5.00 9.00
6.00 7.00
7.00 9.00
8.00 57.00
9.00 141.00
10.00 205.00
11.00 204.00
12.00 190.00
13.00 86.00
14.00 23.00
15.00 5.00
16.00 4.00
17.00 7.00
18.00 5.00
19.00 7.00
20.00 4.00
21.00 5.00
I understand its not perfect, but when I give my gaussian fitting the parameters represented by the orange curve, it fails to do any kind of fit.
This is my Routine
b0 = 11.00
c0 = 1.73 % This is actually sqrt (fwhm guess = 3)
function [fwhm,xdataFine,fitGaus,xpeak,ypeak,rsquared]=myGaussianFit2(xdata,ydata, b0,c0)
%——————–Gaussian Fit—————————————-
%Input is xdata, ydata and must be in column vector format
%only two initial guesses required as the following are determine by ydata
a0 = max(ydata(:)); %-min(ydata(:));
d0 = min(ydata(:));
%b0 is the guess at x-location of peak
%c0 is guess at width of peak
%Output gives the fitted parameters as well as xpeak location and its
%yvalue (i.e. xpeak & ypeak
%————————————————————————-
%Define Gauss Equation (remember the . notation
gaussEqn =’a*exp(-0.5*((x-b)/(c^2)).^2)+d’;
%Use startpoint to define guess values (otherwise fit doesn’t perform well)
try
%f = fit(xdata,ydata,gaussEqn,’Normalize’,’off’, ‘StartPoint’,[a0,b0,sqrt(c0),d0]); %use c^2 instred of c to enforce postive sigma/fwhm
[f,gof]=fit(xdata,ydata,gaussEqn,’Normalize’,’off’, ‘StartPoint’,[a0,b0,sqrt(c0),d0]); %use c^2 instead of c to enforce postive sigma/fwhm
coeffs=coeffvalues(f);
a=coeffs(1); b=coeffs(2);
c=coeffs(3); %need to square it as used c^2 in fitting equation to enforce +ve values
c=c^2;
d=coeffs(4);
rsquared=gof.rsquare;
fwhm=c*sqrt(log(256));
% %Increase resolution of x data (by 30)
xdataFine=(linspace(xdata(1),xdata(end),100))’;
% Create high res Gauss function
fitGaus = a*exp(-0.5*((xdataFine-b)/c).^2)+d;
%Find max value and its x location
ypeak=max(fitGaus);
xpeak=b;
xpeak=mean(xpeak(:)); %Take mean incase more than one peak found
catch
a=0;b=0;c=0;d=0;xpeak=0;ypeak=0;
end
Is there anyhting I can do here to make the fit work, although I understand its not great, but I still think it should be able to fit something!Hello, I have some data below that should approx to a Gaussian.
1.00 NaN
2.00 4.00
3.00 3.00
4.00 6.00
5.00 9.00
6.00 7.00
7.00 9.00
8.00 57.00
9.00 141.00
10.00 205.00
11.00 204.00
12.00 190.00
13.00 86.00
14.00 23.00
15.00 5.00
16.00 4.00
17.00 7.00
18.00 5.00
19.00 7.00
20.00 4.00
21.00 5.00
I understand its not perfect, but when I give my gaussian fitting the parameters represented by the orange curve, it fails to do any kind of fit.
This is my Routine
b0 = 11.00
c0 = 1.73 % This is actually sqrt (fwhm guess = 3)
function [fwhm,xdataFine,fitGaus,xpeak,ypeak,rsquared]=myGaussianFit2(xdata,ydata, b0,c0)
%——————–Gaussian Fit—————————————-
%Input is xdata, ydata and must be in column vector format
%only two initial guesses required as the following are determine by ydata
a0 = max(ydata(:)); %-min(ydata(:));
d0 = min(ydata(:));
%b0 is the guess at x-location of peak
%c0 is guess at width of peak
%Output gives the fitted parameters as well as xpeak location and its
%yvalue (i.e. xpeak & ypeak
%————————————————————————-
%Define Gauss Equation (remember the . notation
gaussEqn =’a*exp(-0.5*((x-b)/(c^2)).^2)+d’;
%Use startpoint to define guess values (otherwise fit doesn’t perform well)
try
%f = fit(xdata,ydata,gaussEqn,’Normalize’,’off’, ‘StartPoint’,[a0,b0,sqrt(c0),d0]); %use c^2 instred of c to enforce postive sigma/fwhm
[f,gof]=fit(xdata,ydata,gaussEqn,’Normalize’,’off’, ‘StartPoint’,[a0,b0,sqrt(c0),d0]); %use c^2 instead of c to enforce postive sigma/fwhm
coeffs=coeffvalues(f);
a=coeffs(1); b=coeffs(2);
c=coeffs(3); %need to square it as used c^2 in fitting equation to enforce +ve values
c=c^2;
d=coeffs(4);
rsquared=gof.rsquare;
fwhm=c*sqrt(log(256));
% %Increase resolution of x data (by 30)
xdataFine=(linspace(xdata(1),xdata(end),100))’;
% Create high res Gauss function
fitGaus = a*exp(-0.5*((xdataFine-b)/c).^2)+d;
%Find max value and its x location
ypeak=max(fitGaus);
xpeak=b;
xpeak=mean(xpeak(:)); %Take mean incase more than one peak found
catch
a=0;b=0;c=0;d=0;xpeak=0;ypeak=0;
end
Is there anyhting I can do here to make the fit work, although I understand its not great, but I still think it should be able to fit something! Hello, I have some data below that should approx to a Gaussian.
1.00 NaN
2.00 4.00
3.00 3.00
4.00 6.00
5.00 9.00
6.00 7.00
7.00 9.00
8.00 57.00
9.00 141.00
10.00 205.00
11.00 204.00
12.00 190.00
13.00 86.00
14.00 23.00
15.00 5.00
16.00 4.00
17.00 7.00
18.00 5.00
19.00 7.00
20.00 4.00
21.00 5.00
I understand its not perfect, but when I give my gaussian fitting the parameters represented by the orange curve, it fails to do any kind of fit.
This is my Routine
b0 = 11.00
c0 = 1.73 % This is actually sqrt (fwhm guess = 3)
function [fwhm,xdataFine,fitGaus,xpeak,ypeak,rsquared]=myGaussianFit2(xdata,ydata, b0,c0)
%——————–Gaussian Fit—————————————-
%Input is xdata, ydata and must be in column vector format
%only two initial guesses required as the following are determine by ydata
a0 = max(ydata(:)); %-min(ydata(:));
d0 = min(ydata(:));
%b0 is the guess at x-location of peak
%c0 is guess at width of peak
%Output gives the fitted parameters as well as xpeak location and its
%yvalue (i.e. xpeak & ypeak
%————————————————————————-
%Define Gauss Equation (remember the . notation
gaussEqn =’a*exp(-0.5*((x-b)/(c^2)).^2)+d’;
%Use startpoint to define guess values (otherwise fit doesn’t perform well)
try
%f = fit(xdata,ydata,gaussEqn,’Normalize’,’off’, ‘StartPoint’,[a0,b0,sqrt(c0),d0]); %use c^2 instred of c to enforce postive sigma/fwhm
[f,gof]=fit(xdata,ydata,gaussEqn,’Normalize’,’off’, ‘StartPoint’,[a0,b0,sqrt(c0),d0]); %use c^2 instead of c to enforce postive sigma/fwhm
coeffs=coeffvalues(f);
a=coeffs(1); b=coeffs(2);
c=coeffs(3); %need to square it as used c^2 in fitting equation to enforce +ve values
c=c^2;
d=coeffs(4);
rsquared=gof.rsquare;
fwhm=c*sqrt(log(256));
% %Increase resolution of x data (by 30)
xdataFine=(linspace(xdata(1),xdata(end),100))’;
% Create high res Gauss function
fitGaus = a*exp(-0.5*((xdataFine-b)/c).^2)+d;
%Find max value and its x location
ypeak=max(fitGaus);
xpeak=b;
xpeak=mean(xpeak(:)); %Take mean incase more than one peak found
catch
a=0;b=0;c=0;d=0;xpeak=0;ypeak=0;
end
Is there anyhting I can do here to make the fit work, although I understand its not great, but I still think it should be able to fit something! fit, gaussian MATLAB Answers — New Questions