Removing cosmic spikes

Cosmic spikes are intense, spurious noise artifacts caused by high-energy cosmic ray particles impacting the CCD detector. They appear as random, single-pixel wide features, not related to the sample, and are especially common in long-duration measurements in Raman spectroscopy, where the main signal is weak and hence a very sensitive CCD detector is employed.

These spikes can be removed using various algorithms. In mdatools a simple algorithm based on median absolute deviation is implemented. The theory can be found in this paper. In short, it has two parameters, width — width of the filter, an odd number, and threshold, which defines how sensitive the method is (how big a z-score should be in order to consider it as a spike).

The method is implemented as function prep.spikes(), the code below shows how to use it.

# load spectra from the Simdata and add some attributes
data(simdata)
spectra0 <- simdata$spectra.c
attr(spectra0, "xaxis.values") <- simdata$wavelength
attr(spectra0, "xaxis.name") <- "Wavelength, nm"

# make a copy and add artificial "spike"
spectra1 <- spectra0
spectra1[100, 100] <- spectra1[100, 100] * 10

# apply spike removal algorithm
spectra2 <- prep.spikes(spectra1, width = 11, threshold = 8)


# define colors for plots
col = rep("gray", nrow(spectra1))
col[100] <- "red"

par(mfrow = c(2, 1))
mdaplot(spectra1, type = "l", col = col)
mdaplot(spectra2, type = "l", col = col)