## GAUSS2DFIT

The GAUSS2DFIT function fits a two-dimensional, elliptical Gaussian equation to rectilinearly gridded data.

Z = F ( x, y )

where:

And the elliptical function is:

The parameters of the ellipse U are:

• Axis lengths are 2 a and 2 b , in the unrotated X and Y axes, respectively.
• Center is at ( h, k ).
• Rotation of T radians from the X axis, in the clockwise direction.

The rotated coordinate system is defined as:

The rotation is optional, and can be forced to 0, making the major and minor axes of the ellipse parallel to the X and Y axes.

Coefficients of the computed fit are returned in argument A .

### Procedure Used and Other Notes

The peak/valley is found by first smoothing Z and then finding the maximum or minimum, respectively. GAUSSFIT is then applied to the row and column running through the peak/valley to estimate the parameters of the Gaussian in X and Y. Finally, CURVEFIT is used to fit the 2D Gaussian to the data.

Be sure that the 2D array to be fit contains the entire peak/valley out to at least 5 to 8 half-widths, or the curve-fitter may not converge.

This computationally-intensive routine takes approximately 4 seconds for a 128 by 128-element array on a Sun SPARC LX. The time required is roughly proportional to the number of elements in Z .

This routine is written in the IDL language. Its source code can be found in the file gauss2dfit.pro in the lib subdirectory of the IDL distribution.

### Calling Sequence

Result = GAUSS2DFIT( Z, A [,X, Y] )

### Arguments

#### Z

The dependent variable. Z should be a two-dimensional array with dimensions ( N x , N y ). Gridding in the array must be rectilinear.

#### A

A named variable in which the coefficients of the fit are returned. A is returned as a seven element vector the coefficients of the fitted function. The meanings of the seven elements in relation to the discussion above is:

• A [0] = A 0 = constant term
• A [1] = A 1 = scale factor
• A [2] = a = width of Gaussian in the X direction
• A [3] = b = width of Gaussian in the Y direction
• A [4] = h = center X location
• A [5] = k = center Y location.
• A [6] = T = Theta , the rotation of the ellipse from the X axis in radians, counter-clockwise .

#### X

An optional vector with N x elements that contains the X values of Z (i.e., X i is the X value for Z i,j . If this argument is omitted, a regular grid in X is assumed, and the X location of Z i,j = i .

#### Y

An optional vector with N y elements that contains the Y values of Z (i.e., Y j is the Y value for Z i,j . If this argument is omitted, a regular grid in Y is assumed, and the Y location of Z i,j = j .

### Keywords

#### NEGATIVE

Set this keyword to indicate that the Gaussian to be fitted is a valley (such as an absorption line). By default, a peak is fit.

#### TILT

Set this keyword to allow the orientation of the major and minor axes of the ellipse to be unrestricted. The default is that the axes of the ellipse must be parallel to the X and Y axes. Therefore, in the default case, A [6] is always returned as 0.

### Example

This example creates a 2D gaussian, adds random noise and then applies GAUSS2DFIT.

nx = 128 & ny = 100 ; Define array dimensions.

A = [ 5., 10., nx/6., ny/10., nx/2., .6*ny]
; Define input function parameters.

X = FINDGEN(nx) # REPLICATE(1.0, ny)

Y = REPLICATE(1.0, nx) # FINDGEN(ny)
; Create X and Y arrays.

U = ((X-A[4])/A[2])^2 + ((Y-A[5])/A[3])^2
; Create an ellipse.

Z = A[0] + A[1] * EXP(-U/2) ; Create gaussian Z.

Z = Z + RANDOMN(seed, nx, ny) ; Add random noise, SD = 1.

yfit = GAUSS2DFIT(Z, B) ; Fit the function, no rotation.

PRINT, 'Should be: ', STRING(A, FORMAT='(6f10.4)')

PRINT, 'Is: ', STRING(B(0:5), FORMAT='(6f10.4)')
; Report results.