Warning

You are reading the documentation related to the development version. Go here if you are looking for the documentation of the stable release.

Removing cosmic ray spikes from a Raman spectrum

In this example, we use the despike method to remove the noise from a Raman spectrum.

import spectrochempy as scp

Load the data

dataset = scp.read("ramandata/labspec/serie190214-1.txt")

Keep only one spectrum in this series and select the useful region

X = dataset[0, 70.0:]

Baseline correction the data using the fast ~spectrochempy.snip` algorithm

X1 = X.snip()

Plot the data

plot despike

Now let’s use the despike method. Only two parameters needs to be tuned: the size of the filter (actually a Savitsky-Golay filter of order 2), and delta, the threshold for the detection of spikes (outliers). A spike is detected if its value is greater than delta times the standard deviation of the difference between the original and the smoothed data.

X2 = scp.despike(X1, size=11, delta=5)
_ = X1.plot()
_ = X2.plot(clear=False, ls="-", c="r")
plot despike

Getting the desired results require the tuning of size and delta parameters. And sometimes may need to repeat the procedure on a previously filtered spectra.

For example, if size or delta are badly chosen, valid peaks could be removed. So careful inspection of the results is crucial.

X3 = scp.despike(X1, size=21, delta=2)
_ = X1.plot()
_ = X3.plot(clear=False, ls="-", c="r")
plot despike

This ends the example ! The following line can be uncommented if no plot shows when running the .py script with python

# scp.show()

Total running time of the script: ( 0 minutes 0.649 seconds)

Gallery generated by Sphinx-Gallery