This function replaces data for bad pixels by a local estimate, by either simple interpolation or using the algorithm of Whitaker and Hayes (2018).
Arguments
- x
numeric vector containing spectral data.
- bad.pix.idx
logical vector or integer. Index into bad pixels in
x.- window.width
integer. The full width of the window used for the running mean.
- method
character The name of the method:
"run.mean"is running mean as described in Whitaker and Hayes (2018);"adj.mean"is mean of adjacent neighbors (isolated bad pixels only).- na.rm
logical Treat
NAvalues as additional bad pixels and replace them.
Value
A logical vector of the same length as x. Values that are TRUE
correspond to local spikes in the data.
Note
In the current implementation NA values are not removed, and
if they are in the neighborhood of bad pixels, they will result in the
generation of additional NAs during their replacement. On the other
hand if the NAs locations are listed in bad.pix.idx they
will be replaced as any other bad pixel.
Replacement values
Simple interpolation enabled by method = "adj.mean" replaces values of
isolated bad pixels by the mean of their two closest neighbours. The running
mean approach enabled by method = "run.mean" allows the replacement of
short runs of bad pixels by the running mean of neighboring pixels within a
window of user-specified width. The first approach works well for spectra
from array spectrometers to correct for hot and dead pixels in an instrument.
The second approach is most suitable for Raman spectra in which spikes
triggered by radiation are wider than a single pixel but usually not more
than five pixels wide.
Simple interpolation can replace spikes at any position in x, using
a single neighbour as replacement at the extremes of x instead of the
mean of two neighbours. The
running mean approach does not replace those pixels whose distance to the
first or last member of x is less than half the window used for the
running mean, issuing a warning.
When na.rm = TRUE, NA values are considered "bad pixels" and
replaced as such rather than discarded with no replacement. This is the
default behaviour.
References
Whitaker, D. A.; Hayes, K. (2018) A simple algorithm for despiking Raman spectra. Chemometrics and Intelligent Laboratory Systems, 179, 82-84.
See also
Other peaks and valleys functions:
find_peaks(),
find_spikes(),
get_peaks(),
peaks(),
spikes(),
valleys(),
wls_at_target()
Examples
# in a vector
replace_bad_pixs(c(1, 2, NA, 4, 5))
#> [1] 1 2 3 4 5
# in a vector
replace_bad_pixs(c(1, 2, 100, 4, 5),
method = "adj.mean",
bad.pix.idx = c(FALSE, FALSE, TRUE, FALSE, FALSE))
#> [1] 1 2 3 4 5
replace_bad_pixs(c(1, 2, 100, 4, 5),
method = "adj.mean",
bad.pix.idx = 3)
#> [1] 1 2 3 4 5
# in a vector
replace_bad_pixs(c(0, 1, 2, 100, 4, 5, 6),
method = "run.mean",
bad.pix.idx = 4)
#> [1] 0 1 2 3 4 5 6
# in a vector
replace_bad_pixs(c(1, 1, NA, 1, 1),
method = "run.mean",
bad.pix.idx = 3)
#> [1] 1 1 1 1 1
# in a vector
replace_bad_pixs(c(1, 1, NA, 1, 1),
method = "run.mean",
bad.pix.idx = 1, na.rm = FALSE)
#> [1] NA 1 NA 1 1
# In spectrum
# before replacement
white_led.raw_spct$counts_3[120:125]
#> [1] 8683.0 8058.0 8670.5 22247.5 7667.5 8261.0
# replacing bad pixels at index positions 123 and 1994
with(white_led.raw_spct,
replace_bad_pixs(counts_3, bad.pix.idx = c(123, 1994)))[120:125]
#> [1] 8683.00 8058.00 8670.50 8228.25 7667.50 8261.00
