; $Id: gaussfNB.pro,v 1.1 1993/04/02 19:43:31 idl Exp $ PRO GAUSS_FN,X,A,F,PDER ; NAME: ; GAUSS_FN ; ; PURPOSE: ; EVALUATE A GAUSSIAN ; AND OPTIONALLY RETURN THE VALUE OF IT'S PARTIAL DERIVATIVES. ; NORMALLY, THIS FUNCTION IS USED BY CURVEFIT TO FIT ; A LINE TO ACTUAL DATA. ; ; CATEGORY: ; E2 - CURVE AND SURFACE FITTING. ; CALLING SEQUENCE: ; GAUSS_FN,X,A,F,PDER ; INPUTS: ; X = VALUES OF INDEPENDENT VARIABLE. ; A = PARAMETERS OF EQUATION DESCRIBED BELOW. ; OUTPUTS: ; F = VALUE OF FUNCTION AT EACH X(I). ; ; OPTIONAL OUTPUT PARAMETERS: ; PDER = (N_ELEMENTS(X),3) ARRAY CONTAINING THE ; PARTIAL DERIVATIVES. P(I,J) = DERIVATIVE ; AT ITH POINT W/RESPECT TO JTH PARAMETER. ; COMMON BLOCKS: ; NONE. ; SIDE EFFECTS: ; NONE. ; RESTRICTIONS: ; NONE. ; PROCEDURE: ; F = A(0)*EXP(-Z^2/2) ; Z = (X-A(1))/A(2) ; MODIFICATION HISTORY: ; WRITTEN, DMS, RSI, SEPT, 1982. ; Modified, DMS, Oct 1990. Avoids divide by 0 if A(2) is 0. ; Added to Gauss_fit, when the variable function name to ; Curve_fit was implemented. DMS, Nov, 1990. ; Modified, DPS, Oct. 1994 from GAUSS_FUNCT. ; ON_ERROR,2 ;Return to caller if an error occurs if a(2) ne 0.0 then Z = (X-A(1))/A(2) $ ;GET Z else z= 10. EZ = EXP(-Z^2/2.)*(ABS(Z) LE 7.) ;GAUSSIAN PART IGNORE SMALL TERMS F = A(0)*EZ IF N_PARAMS(0) LE 3 THEN RETURN ;NEED PARTIAL? ; PDER = FLTARR(N_ELEMENTS(X),3) ;YES, MAKE ARRAY. PDER(0,0) = EZ ;COMPUTE PARTIALS if a(2) ne 0. then PDER(0,1) = A(0) * EZ * Z/A(2) PDER(0,2) = PDER(*,1) * Z RETURN END Function GaussfNB, x, y, a ;+ ; NAME: ; GAUSSFNB ; ; PURPOSE: ; Fit the equation y=f(x) where: ; ; F(x) = A0*EXP(-z^2/2) ; and ; z=(x-A1)/A2 ; ; A0 = height of exp, A1 = center of exp, A2 = sigma (the width). ; The parameters A0, A1, A2 are estimated and then CURVEFIT is ; called. ; ; CATEGORY: ; ?? - fitting ; ; CALLING SEQUENCE: ; Result = GAUSSFNB(X, Y [, A]) ; ; INPUTS: ; X: The independent variable. X must be a vector. ; Y: The dependent variable. Y must have the same number of points ; as X. ; ; OUTPUTS: ; The fitted function is returned. ; ; OPTIONAL OUTPUT PARAMETERS: ; A: The coefficients of the fit. A is a six-element vector as ; described under PURPOSE. ; ; COMMON BLOCKS: ; None. ; ; SIDE EFFECTS: ; None. ; ; RESTRICTIONS: ; The peak or minimum of the Gaussian must be the largest ; or smallest point in the Y vector. ; ; PROCEDURE: ; If the (MAX-AVG) of Y is larger than (AVG-MIN) then it is assumed ; that the line is an emission line, otherwise it is assumed there ; is an absorbtion line. The estimated center is the MAX or MIN ; element. The height is (MAX-AVG) or (AVG-MIN) respectively. ; The width is found by searching out from the extrema until ; a point is found less than the 1/e value. ; ; MODIFICATION HISTORY: ; DMS, RSI, Dec, 1983. ;- ; on_error,2 ;Return to caller if an error occurs n = n_elements(y) ;# of points. ; NB: The following section was changed by DPS, 960516 to subtract the ; mean of the Y array rather than a best-fit straight line. It is hoped ; that this will help the routine better distinguish an "emission" curve ; from an "absorption" curve. ; c = poly_fit(x,y,1,yf) ;fit a straight line. ; yd = y-yf ;difference. yd=y-mean(y) ymax=max(yd) & xmax=x(!c) & imax=!c ;x,y and subscript of extrema ymin=min(yd) & xmin=x(!c) & imin=!c a=fltarr(3) ;coefficient vector ; if abs(ymax) gt abs(ymin) then i0=imax else i0=imin ;emiss or absorp? i0=imax ; Curve MUST be an "emission" line in this context! i0 = i0 > 1 < (n-2) ;never take edges dy=yd(i0) ;diff between extreme and mean del = dy/exp(1.) ;1/e value i=0 while ((i0+i+1) lt n) and $ ;guess at 1/2 width. ((i0-i) gt 0) and $ (abs(yd(i0+i)) gt abs(del)) and $ (abs(yd(i0-i)) gt abs(del)) do i=i+1 a = DOUBLE([yd(i0), x(i0), abs(x(i0)-x(i0+i))]) ;estimates !c=0 ;reset cursor for plotting return,curvefit(x,y,replicate(1.,n),a,sigmaa, $ function_name = "GAUSS_FN") ;call curvefit end