function thm_lsp_remove_spintone, t, x, per, SpinPoly=SpinPoly, $
nsmooth=nsmooth, talk=talk, fail = fail
fail = 0
tt = t - t(0)
IF keyword_set(nsmooth) then BEGIN
thm_qfit, x, tt * !dpi * 2.0 / per, phsf=phsf, ec=ec, es=es, $
/do_sigma, sigma=sigma, fail = qfit_fail
if qfit_fail gt 0 then begin
dprint, 'THM_QFIT failed. NAN is returned.'
fail = 1
return, !values.d_nan
endif
tsc = phsf * per / (!dpi * 2.d)
IF keyword_set(talk) then BEGIN
ymax = max([es,ec,sigma])
ymin = min([es,ec,sigma])
plot, tsc, es, yran=[ymin, ymax], title='Spin Fits', xtit = 'Time (s)', $
ytit = 'Amplitude'
oplot, tsc, ec, col=2
ENDIF
es = thm_lsp_median_smooth(es, nsmooth)
ec = thm_lsp_median_smooth(ec, nsmooth)
esi = interpol(es, tsc, tt)
eci = interpol(ec, tsc, tt)
xx = x - esi*sin(tt*2d*!dpi/per) - eci*cos(tt*2d*!dpi/per)
IF keyword_set(talk) then BEGIN
oplot, tsc, es, col = 6
oplot, tsc, ec, col = 6
ENDIF
return, xx
ENDIF
IF NOT keyword_set(SpinPoly) then BEGIN
thm_efi_sin_fit, x, tt, es=es, ec=ec, per=per
xx = x - es*sin(tt*2d*!dpi/per) - ec*cos(tt*2d*!dpi/per)
return, xx
ENDIF
thm_qfit, x, tt * !dpi * 2.0 / per, phsf=phsf, ec=ec, es=es, $
/do_sigma, sigma=sigma, fail = qfit_fail
if qfit_fail gt 0 then begin
dprint, 'THM_QFIT failed. NAN is returned.'
fail = 1
return, !values.d_nan
endif
nfits = n_elements(es)
npoly = ((SpinPoly > 0) < 16)
npoly <= nfits-1
tsc = phsf * per / (!dpi * 2.d)
ind = findgen(nfits)
As = poly_fit(tsc, es, npoly, measure=sigma)
nfits = n_elements(ec)
npoly <= nfits-1
ind = findgen(nfits)
Ac = poly_fit(tsc, ec, npoly, measure=sigma)
esi = interpol(poly(tsc, As), tsc, tt)
eci = interpol(poly(tsc, Ac), tsc, tt)
xx = x - esi*sin(tt*2d*!dpi/per) - eci*cos(tt*2d*!dpi/per)
IF keyword_set(talk) then BEGIN
ymax = max([es,ec,sigma])
ymin = min([es,ec,sigma])
plot, tsc, es, yran=[ymin, ymax], title='Spin Fits', xtit = 'Time (s)', $
ytit = 'Amplitude'
oplot, tsc, ec, col=2
oplot, tsc, sigma, col=4
oplot, tsc, poly(tsc,As), col = 6
oplot, tsc, poly(tsc,Ac), col = 6
ENDIF
return, xx
end