function thm_lsp_notch_spikes, t, xx, per, tpks, nfit=nfit, Amp=Amp, fit=fit, $
talk=talk, diagnose=diagnose, wt=wt
tt = t-t[0]
dt = t[1:*]-t[*]
mdt = median(dt)
x = xx
if not keyword_set(wt) then wt = 0.0
if not keyword_set(nfit) then nfit = round(1.0/mdt/32) > 16
nfit = long(nfit)
tpeaks = tpks - t[0]
tpeaks = tpeaks - floor(tpeaks/per)*per
xind = lindgen(nfit+1)
nind = where( (xind GE 3*nfit/8) AND (xind LE 5*nfit/8))
xind = where( (xind LT 3*nfit/8) OR (xind GT 5*nfit/8))
gfits = 0
ntchs = 0
FOR j = 0, n_elements(tpks)-1 DO BEGIN
tspike = tpeaks(j)
icent = round(tspike/mdt)
istart = (icent - nfit/2)
IF istart LT 0 then BEGIN
tspike = tspike + per
icent = round(tspike/mdt)
istart = (icent - nfit/2)
ENDIF
iend = (icent + nfit/2)
Ampmax = 25.0
Ampmin = -25.0
IF n_elements(Amp) EQ n_elements(tpks) then BEGIN
Ampmax = 0.0
Ampmin = 0.0
if Amp[j] GE 0.0 then Ampmax = Amp[j]*10.0 else Ampmin = Amp[j]*10.0
ENDIF
Centmax = nfit/16 + 1
Centmin = -Centmax
Widthmax = 8.00d-3
Widthmin = 0.25d-3
Chimax = 2.0
WHILE iend LT n_elements(x)-1 DO BEGIN
xwin = x[istart:iend]
twin = tt[istart:iend] - tt[istart]
notch = 1
if keyword_set(diagnose) then plot, twin, xwin
IF keyword_set(fit) then BEGIN
A = poly_fit(twin[xind], xwin[xind], 1)
xfit = xwin - poly(twin, A)
dum = gaussfit(twin, xfit, G, chisq=chisq, nterms=3)
fcent = round(G[1]/mdt) - nfit/2
IF (G[0] LT Ampmax) AND (G[0] GT Ampmin) AND (fcent LT Centmax) AND $
(G[1] GT Centmin) AND (G[2] LT Widthmax) AND (G[2] GT Widthmin) AND $
(chisq LT chimax) then BEGIN
gfits = gfits + 1
xwin = xwin - G[0]*exp(-(((twin-G[1])/G[2])^2)/2.0d )
IF keyword_set(diagnose) then BEGIN
oplot, twin, poly(twin, A), col = 2
oplot, twin, xfit, col = 4
oplot, twin, G[0]*exp(-(((twin-G[1])/G[2])^2)/2.0d ), col = 6
oplot, twin, xwin, col = 1
wait, wt
ENDIF
notch = 0
ENDIF
ENDIF
IF keyword_set(notch) then BEGIN
A = ladfit(twin, xwin)
xwin(nind) = poly(twin(nind), A)
IF keyword_set(diagnose) then BEGIN
oplot, twin, poly(twin, A), col = 2
oplot, twin, xwin, col = 1
wait, wt
ENDIF
ntchs = ntchs + 1
ENDIF
x[istart:iend] = xwin
tspike = tspike + per
icent = round(tspike/mdt)
istart = (icent - nfit/2)
iend = (icent + nfit/2)
ENDWHILE
ENDFOR
IF keyword_Set(talk) then BEGIN
if keyword_set(fit) then $
print, gfits, ' SPIKES REMOVED (GOOD FITS)', ntchs, ' SPIKES NOTCHED (FAILED FITS)' else $
print, ntchs, ' SPIKES NOTCHED'
ENDIF
return, x
end