Skip to contents

Smooth hyperspectral data using a Savitzky-Golay filter via gsignal::sgolayfilt(). The filter fits successive subsets of adjacent data points with a low-degree polynomial by the method of linear least squares.

Usage

hsi_smooth_savgol(
  x,
  p = 3,
  n = p + 13 - p%%2,
  m = 0,
  ts = 1,
  filename = "",
  overwrite = FALSE,
  ...
)

Arguments

x

A SpatRaster with hyperspectral data.

p

Integer. Filter polynomial order. Typically 2–4. Default 3.

n

Positive odd integer. Filter window size. Must be odd and greater than p. Typically 5–15. Default computed from p.

m

Integer. Derivative order. 0 for smoothing, 1 for first derivative. Default 0.

ts

Numeric. Sampling interval for derivative calculations. Default 1.

filename

Character. Output filename. Default "" keeps result in memory.

overwrite

Logical. Overwrite existing file. Default FALSE.

...

Additional arguments passed to terra::writeRaster().

Value

A SpatRaster with Savitzky-Golay filtered values.

Details

The Savitzky-Golay filter preserves spectral features such as peak height and width that are typically flattened by other smoothing methods. The filter fits a polynomial of order p through a moving window of n points.

Setting m = 1 or m = 2 computes the first or second derivative of the smoothed spectrum respectively; higher-order derivatives are also supported by increasing m. Note that edge bands equal to roughly half the window size are unreliable for derivatives — always compute on the full spectrum before subsetting to a wavelength range of interest.

Pixels with NA values will cause the function to fail. For full-raster processing, hsi_tiled() can distribute the workload across parallel workers. Requires the gsignal package.

Examples

if (FALSE) { # \dontrun{
x <- terra::rast("REFLECTANCE_testdata.tif")

x_savgol <- hsi_smooth_savgol(x)

x_savgol <- hsi_smooth_savgol(
  x,
  filename = "output_savgol.tif",
  overwrite = TRUE
)
} # }