Code covered by the BSD License

### Highlights from Fit 2D Gaussian with Optimization Toolbox

5.0
5.0 | 3 ratings Rate this file 20 Downloads (last 30 days) File Size: 2.55 KB File ID: #41938 Version: 1.2

# Fit 2D Gaussian with Optimization Toolbox

### Nathan Orloff (view profile)

24 May 2013 (Updated )

Fit a 2D rotated gaussian. http://en.wikipedia.org/wiki/Gaussian_function

File Information
Description

FMGAUSSFIT performs a gaussian fit on 3D data (x,y,z).
[fitresult,..., rr] = fmgaussfit(xx,yy,zz) uses ZZ for the surface
height. XX and YY are vectors or matrices defining the x and y
components of a surface. If XX and YY are vectors, length(XX) = n and
length(YY) = m, where [m,n] = size(Z). In this case, the vertices of the
surface faces are (XX(j), YY(i), ZZ(i,j)) triples. To create XX and YY
matrices for arbitrary domains, use the meshgrid function. FMGAUSSFIT
uses the lsqcurvefit tool, and the OPTIMZATION TOOLBOX. The initial
guess for the gaussian is places at the maxima in the ZZ plane. The fit
is restricted to be in the span of XX and YY.
See:
http://en.wikipedia.org/wiki/Gaussian_function

Examples:
To fit a 2D gaussian:
[fitresult, zfit, fiterr, zerr, resnorm, rr] =
fmgaussfit(xx,yy,zz);

Required Products Optimization Toolbox
MATLAB release MATLAB 8.0 (R2012b)
22 Jul 2016 XYZ

### XYZ (view profile)

13 Jul 2016 Juan Garcia

### Juan Garcia (view profile)

Nice function. I was having a look at it and I have a question and a comment.

Question: What is the meaning of z0 or par(7) in the gaussian2D function.

Comment: I would say that both sigmas in the gaussian2D function lack the multiplication by 2, or to maintain the power of 2 to the whole fraction, by sqrt(2).

Then:
function z = gaussian2D(par,xy)
% compute 2D gaussian
z = par(7) + ...
par(1) * exp(-(((xy{1}-par(5)).*cosd(par(2))+(xy{2}-par(6)).*sind(par(2)))./(sqrt(2)*par(3))).^2-...
((-(xy{1}-par(5)).*sind(par(2))+(xy{2}-par(6)).*cosd(par(2)))./(sqrt(2)*par(4))).^2);
end

Comment only
12 Jan 2015 Eric T

### Eric T (view profile)

I get the following warning:
Warning: The Levenberg-Marquardt algorithm does not handle bound constraints; using the
> In lsqncommon at 83
In lsqcurvefit at 252
In fmgaussfit at 56

Comment only
07 Feb 2014 Nathan Orloff

### Nathan Orloff (view profile)

fitresult = fit parameters used to generate the fit.

zfit = the z values of the fit

fiterr = error in the fitparameters assuming a confidence interval.

zerr = error in the z values assuming the uncertainty in the fit paramteres

resnorm = residual

rr = reduced chi-squared.

Comment only

This bit of code is doing exactly what I want I think but I wondering if it might be possible for the Author to expand on the meaning of the values the function outputs or point me in the direction as to where I can find the answer. Thanks a Ton

24 Nov 2013 Christopher

### Christopher (view profile)

28 May 2013 Nathan Orloff

### Nathan Orloff (view profile)

I edited this a little. The fit is better with z bounds. I will post it soon... If you have already downloaded this just add the following.

%% Set up the startpoint
[amp, ind] = max(zData); % amp is the amplitude.
xo = xData(ind); % guess that it is at the maximum
yo = yData(ind); % guess that it is at the maximum
ang = 45; % angle in degrees.
sy = 1;
sx = 1;
zo = median(zData(:))-std(zData(:));
xmax = max(xData)+2;
ymax = max(yData)+2;
zmax = amp*2; % amp is the amplitude.
xmin = min(xData)-2;
ymin = min(yData)-2;
zmin = min(zData)/2; % amp is the amplitude.

%% Set up fittype and options.
Lower = [0, eps, 0, 0, xmin, ymin, zmin];
Upper = [Inf, 180, Inf, Inf, xmax, ymax, zmax]; % angles greater than 90 are redundant
StartPoint = [amp, ang, sx, sy, xo, yo, zo];%[amp, sx, sxy, sy, xo, yo, zo];

Comment only