Search code examples
pythondata-fittingastropyspectrumspecutils

Continuum Fitting with Python using specutils


it's my first time processing spectra from a black body radiation experiment, I'm using Python and having some troubles... I have this spectra with 2 peaks and uneven background noise which I want to normalize, I tried using specutils 1.1 "continuum-fitting" (documentation here: https://specutils.readthedocs.io/en/stable/fitting.html#continuum-fitting )

My implementation is this:

import matplotlib.pyplot as plt import numpy as np 
from astropy.modeling import models
from astropy import units as u
from specutils.spectra import Spectrum1D, SpectralRegion
from specutils.fitting import fit_generic_continuum

...

x = data[:, 0]
y = data[:, 1]
plt.plot(x, y, label = 'My Data', c='C0')

spectrum = Spectrum1D(flux=y*u.Jy, spectral_axis=x*u.um)
g1_fit = fit_generic_continuum(spectrum)
y_fit = g1_fit(x*u.um)

plt.plot(x, y_fit, label = ' Specutils Continuum Fit', c='C1')

plt.legend()

But the result is somewhat disappointing, it doesn't reflect the background signal.

https://i.sstatic.net/El0pc.png

Am I doing something wrong? Do you have any other way to normalize the background noise in python? Any fit that can cover only the background and ignore the peaks? any suggestion at all? Thanks in advance!


Solution

  • At the end I settled on using a median filter from scipy (medfilt) and with a high enough kernel it removed the spikes completely.

    The problem on my specutils implementation was that I needed to exclude the peaks in the spectrum window by doing this, althought I found this too hard to implement on 100-120 data measurement I had:

    ...
    from specutils import SpectralRegion
    
    spectrum = Spectrum1D(flux=y*u.Jy, spectral_axis=x*u.um)
    g1_fit = fit_generic_continuum(spectrum, exclude_regions=[SpectralRegion(2 * u.um, 6 * u.um), SpectralRegion(58 * u.um, 65 * u.um)])
    y_fit = g1_fit(x*u.um)