Remove a spectrum's complex baseline.
The principle here is that if you have quite a spikey spectrum, you can simulate the baseline by:
'Smooth' the spectrum
Using this, produce anew spectrum that is the original with all the spikes above the smoothed spectrum removed.
Repeat until you've got a reasonably smooth baseline.
here's the code:
# Simplistic iterative way to approximate a complex spectrum baseline.
# Works by repetatively ('iter' times - default is 50 times) getting a smoothed version of the
# spectrum, and removing any peaks above this spectrum. This will approximate
# any underlying baseline signal.
#
# It isn't guaranteed not to distort the original though - check it!
# The smoothing function is the Savitzky_Golay filter from Scipy.signal but even a moving average is OKish
# Fiddling with the SG filter parameters will also tune the fit
def getBaseline(spec, iters=50):
import scipy.signal as scs
base = np.copy(spec) # clone the input spectrum
for i in range(iters):
smoothed = scs.savgol_filter(base,7,1)
for j in range(len(base)):
if smoothed[j]