Exponential Background Source

EPMA Probe

Exponential Background Source

Source code for the exponential background fit is shown here. This first section is the background calculation itself.

' Use exponential interpolation

If sample(1).OffPeakCorrectionTypes%(j%) = 4 Then

txdata!(1) = sample(1).LoPeaks!(j%)

txdata!(2) = sample(1).HiPeaks!(j%)

tydata!(1) = sample(1).LoPeakCounts!(i%, j%)

tydata!(2) = sample(1).HiPeakCounts!(i%, j%)

Call LeastExponential(txdata!(), tydata!(), sample(1).BackgroundExponentialBase!(j%), acoeff!())

If ierror Then Exit Sub

sample(1).BgdData!(i%, j%) = (acoeff!(1) * Exp(-acoeff!(2) * sample(1).OnPeaks!(j%))) / sample(1).OnPeaks!(j%) ^ acoeff!(3)

End If

 

This next section is the subroutine used to calculate the exponential coeficients:

 

Sub LeastExponential(txdata() As Single, tydata() As Single, texp As Single, acoeff() As Single)

' Calculates the coefficients for a 2 point exponential fit of the form:

'   y = (c* e^(-ax))/x^n,   where n is user specified

 

ierror = False

On Error GoTo LeastExponentialError

 

Dim c As Single, a As Single, n As Single

Dim temp1 As Double, temp2 As Double

 

' Init "acoeff()"

acoeff(1) = 0#

acoeff(2) = 0#

acoeff(3) = 0#

 

' Check for valid input data

If txdata!(1) <= 0# Then Exit Sub

If txdata!(2) <= 0# Then Exit Sub

If tydata!(1) <= 0# Then Exit Sub

If tydata!(2) <= 0# Then Exit Sub

 

' Load base

If texp! < -6# Or texp! > 6 Then GoTo LeastExponentialBadExponent

n! = texp!

 

' Calculate c

temp1# = (txdata!(1) * Log(tydata!(2)) - txdata!(2) * Log(tydata!(1)))

temp1# = temp1# + n! * txdata!(1) * Log(txdata!(2)) - n! * txdata!(2) * Log(txdata!(1))

temp2# = txdata!(1) - txdata!(2)

If temp2# = 0# Then Exit Sub

c! = Exp(temp1# / temp2#)

 

' Calculate a

If c! < 0# Then Exit Sub

temp1# = (Log(tydata!(1)) + Log(tydata!(2)) + n! * (Log(txdata!(2)) + Log(txdata!(1))) - 2 * Log(c!))

temp2# = txdata!(1) + txdata!(2)

If temp2# = 0# Then Exit Sub

a! = -(temp1# / temp2#)

 

' Load coefficients (1=c, 2=a, 3=n)

acoeff!(1) = c!

acoeff!(2) = a!

acoeff!(3) = n!

 

Exit Sub

 

' Errors

LeastExponentialError:

MsgBox Error$, vbOKOnly + vbCritical, "LeastExponential"

ierror = True

Exit Sub

 

LeastExponentialBadExponent:

msg$ = "Exponent must be greater than -6 and less than 6"

MsgBox msg$, vbOKOnly + vbExclamation, "LeastExponential"

ierror = True

Exit Sub

 

End Sub