GreyMamba

Thinking Allowed … (under construction)

Thinking Allowed … (under construction)

Nothing is interesting if you're not interested
Helen Clark MacInnes
Python Stuff

I tend to spend too much time mucking around with Python code - as you can see from the bits of code scattered around this site. So, I've decided to add a page specifically for stuff related to just Python. It'll include snippets of code, interesting applications, algorithms or anything else Python related. Code that is purely there as a means to demonstrate or calculate something specific will remain as it is, in the most relevant post. Most of this stuff will be migrating to my sister pages -

- in the near future.

Sorry if you were hoping to see something on the Pythonidae family of snakes - interesting as they are.

Remove a spectrum's complex baseline.

The principle here is that if you have quite a spikey spectrum, you can simulate the baseline by:

  1. 'Smooth' the spectrum

  2. Using this, produce anew spectrum that is the original with all the spikes above the smoothed spectrum removed.

  3. 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]
Back
 
RapidWeaver Icon

Made in RapidWeaver