View source: R/prep_derivative.R
| prep_derivative | R Documentation |
Creates a preprocessing constructor for computing first or second order
derivatives of spectral data. The constructor is intended to be passed to
preprocess_recipe and executed via process.
Three algorithms are supported: Savitzky-Golay ("savitzky-golay"),
Norris-Gap/Gap-Segment ("gap-segment"), and the derivative
pre-treatment from BUCHI NIRWise PLUS software ("nwp").
prep_derivative(m, w, p, algorithm = c("savitzky-golay", "gap-segment", "nwp"))
m |
An integer indicating the derivative order. Must be |
w |
A positive odd integer indicating the filter window size.
For |
p |
An integer. For |
algorithm |
A character string specifying the algorithm. One of
|
Savitzky-Golay ("savitzky-golay"): fits a polynomial of
order p within a moving window of size w and differentiates
analytically. Implemented via savitzkyGolay.
Gap-Segment ("gap-segment"): computes the derivative over a
gap of w points, with optional averaging over a segment of p
points. When p = 1 this reduces to the standard Norris-Gap
derivative. Implemented via gapDer.
NWP ("nwp"): reproduces the "DG" derivative pre-treatment
from BUCHI NIRWise PLUS calibration software. A moving average of window
p is applied first (pre-smoothing), followed by differentiation.
For first order, a gap derivative with gap w is used. For second
order, a centered second difference with spacing half_w is computed:
d^2x_i = \frac2x_i - (x_i+h + x_i-h)2hd2x_i = (2*x_i - (x_i+h + x_i-h)) / (2h)
where \mjeqnh = half_wh = half_w. Edge columns affected by the window are removed from the output.
For the "nwp" algorithm, the NIRWise PLUS half-window conventions are:
\mjdeqnhalf_w = (w + 1) / 2half_w = (w + 1) / 2
\mjdeqnhalf_s = (p - 1) / 2half_s = (p - 1) / 2
These are stored internally for device file serialization and are not
user-facing parameters.
An object of class preprocessing to be used in
preprocess_recipe and executed by process.
The object is a list containing the method name, all parameters, and
(for algorithm = "nwp") the NIRWise PLUS half-window values
(half_w, half_s) required for device file serialization.
Leonardo Ramirez-Lopez and Claudio Orellano
preprocess_recipe, process
data("proximateCannabis")
X <- proximateCannabis$spc
# Savitzky-Golay first derivative, window 11, polynomial order 3
sg <- prep_derivative(m = 1, w = 11, p = 3, algorithm = "savitzky-golay")
# Gap-Segment second derivative, gap 9, segment 3
gs <- prep_derivative(m = 2, w = 9, p = 3, algorithm = "gap-segment")
# NWP first derivative, window 5, pre-smoothing 11
nwp <- prep_derivative(m = 1, w = 5, p = 11, algorithm = "nwp")
# Apply via preprocess_recipe
recipe <- preprocess_recipe(sg, device = "unspecified")
X_der <- process(X, recipe)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.