Polynomial Background Source
' Use polynomial
pxdata!(1) = sample(1).LoPeaks!(j%) ' load three known positions
pxdata!(2) = sample(1).OnPeaks!(j%)
pxdata!(3) = sample(1).HiPeaks!(j%)
' Get three y values for these positions
pydata!(1) = sample(1).BackgroundPolynomialCoefficients!(1, j%) + sample(1).BackgroundPolynomialCoefficients!(2, j%) * sample(1).LoPeaks!(j%) + sample(1).BackgroundPolynomialCoefficients!(3, j%) * sample(1).LoPeaks!(j%) ^ 2
pydata!(2) = sample(1).BackgroundPolynomialCoefficients!(1, j%) + sample(1).BackgroundPolynomialCoefficients!(2, j%) * sample(1).OnPeaks!(j%) + sample(1).BackgroundPolynomialCoefficients!(3, j%) * sample(1).OnPeaks!(j%) ^ 2
pydata!(3) = sample(1).BackgroundPolynomialCoefficients!(1, j%) + sample(1).BackgroundPolynomialCoefficients!(2, j%) * sample(1).HiPeaks!(j%) + sample(1).BackgroundPolynomialCoefficients!(3, j%) * sample(1).HiPeaks!(j%) ^ 2
' Scale y values to current nominal beam
If UseBeamDriftCorrectionFlag Then
If sample(1).BackgroundPolynomialNominalBeam!(j%) <> 0# And NominalBeam! <> 0 Then
temp! = NominalBeam! / sample(1).BackgroundPolynomialNominalBeam!(j%)
pydata!(1) = pydata!(1) * temp!
pydata!(2) = pydata!(2) * temp!
pydata!(3) = pydata!(3) * temp!
End If
End If
' Re-fit the data using new scaled y values
order% = 2 ' 2nd order polynomial
npts% = 3
Call LeastSquares(order%, npts%, pxdata!(), pydata!(), acoeff!())
If ierror Then Exit Sub
If DebugMode Then
msg$ = "Polynomial re-fit coefficients for " & sample(1).Elsyms$(j%) & " (beam correction = " & Format$(temp!) & ") " & Str$(acoeff!(1)) & Str$(acoeff!(2)) & Str$(acoeff!(3))
Call IOWriteLog(msg$)
End If
' Calculate off-peak values based on new fit coefficients
temp1! = acoeff!(1) + acoeff!(2) * sample(1).HiPeaks!(j%) + acoeff!(3) * sample(1).HiPeaks!(j%) ^ 2
temp2! = acoeff!(1) + acoeff!(2) * sample(1).LoPeaks!(j%) + acoeff!(3) * sample(1).LoPeaks!(j%) ^ 2
' Calculate offsets from current off-peak measurements
temp3! = sample(1).HiPeakCounts!(i%, j%) - temp1!
temp4! = sample(1).LoPeakCounts!(i%, j%) - temp2!
' Average current offsets
temp! = (temp3! + temp4!) / 2#
' Calculate new y intercept for this sample based on offset
tintercept! = temp! + acoeff(1)
' Calculate actual background using new rescaled coefficients at on-peak position
bgdcts! = tintercept! + acoeff!(2) * sample(1).OnPeaks!(j%) + acoeff!(3) * sample(1).OnPeaks!(j%) ^ 2
sample(1).BgdData!(i%, j%) = bgdcts!