% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/spectra-preprocess.R
\name{apply_spectral_preprocessing}
\alias{apply_spectral_preprocessing}
\title{Apply a step-by-step Vis-NIR / MIR preprocessing pipeline}
\usage{
apply_spectral_preprocessing(
  X,
  wavelengths = NULL,
  absorbance = FALSE,
  sg_smooth = FALSE,
  sg_derivative = 0L,
  window = 11L,
  poly = 2L
)
}
\arguments{
\item{X}{Numeric matrix (rows = samples/horizons, columns =
wavelengths) or a numeric vector (treated as one sample).}

\item{wavelengths}{Optional numeric wavelength axis. Defaults to the
numeric part of \code{colnames(X)}, else \code{1:ncol(X)}.}

\item{absorbance}{Logical; apply \eqn{A = \log_{10}(1 / R)}.}

\item{sg_smooth}{Logical; apply Savitzky-Golay smoothing
(\code{m = 0}).}

\item{sg_derivative}{Integer \code{0}, \code{1} or \code{2};
Savitzky-Golay derivative order (\code{0} = none).}

\item{window}{Odd Savitzky-Golay window (default \code{11});
coerced to a valid odd value in \code{[3, ncol)}.}

\item{poly}{Savitzky-Golay polynomial order (default \code{2});
clamped to \code{[1, window - 1]}.}
}
\value{
A list with \code{X} (the treated numeric matrix, wavelength
        column names trimmed to match), \code{wavelengths} (numeric)
        and \code{steps} (an ordered character vector describing the
        transforms actually applied, starting with
        \code{"Reflectance"}).
}
\description{
Composes the canonical soil-spectroscopy sequence, each step optional
and applied in this fixed scientific order:
reflectance \eqn{\to} (absorbance) \eqn{\to} (Savitzky-Golay
smoothing) \eqn{\to} (Savitzky-Golay 1st or 2nd derivative). Each
Savitzky-Golay pass trims \code{(window - 1) / 2} columns from each
edge; the wavelength axis is trimmed to match and returned so callers
can plot the treated spectrum on the correct axis.
}
\details{
The transform is robust: reflectance that looks like a percentage
(maximum \code{> 1.5}) is rescaled to a 0--1 fraction before the
absorbance log, values are clamped away from zero to avoid
\code{log(0)}, and a Savitzky-Golay step that cannot fit the requested
window into the available wavelengths is skipped (recorded in
\code{steps}) rather than erroring.
}
\examples{
X <- matrix(seq(0.1, 0.5, length.out = 3 * 60), nrow = 3, byrow = TRUE)
colnames(X) <- seq(400, 2400, length.out = 60)
res <- apply_spectral_preprocessing(X, absorbance = TRUE,
                                    sg_smooth = TRUE, sg_derivative = 1L)
res$steps          # ordered treatment labels
dim(res$X)         # columns trimmed by the two SG passes
}
\seealso{
\code{\link{preprocess_spectra}}
}
