Spectral raster smooth with a Savitzky-Golay filter
Source:R/hsi_smooth_savgol.R
hsi_smooth_savgol.RdSmooth 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
SpatRasterwith 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 fromp.- m
Integer. Derivative order.
0for smoothing,1for first derivative. Default0.- 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.
See also
Other HSI Transformations:
hsi_apply_mnf(),
hsi_calc_difference(),
hsi_calc_mnf(),
hsi_calc_ndi(),
hsi_calc_raba(),
hsi_calc_rabd(),
hsi_calc_ratio(),
hsi_calc_rcv(),
hsi_calc_reflectance(),
hsi_calc_remp(),
hsi_calc_rmean(),
hsi_calc_rmedian(),
hsi_calc_rsd(),
hsi_calc_stretch(),
hsi_destripe(),
hsi_remove_continuum(),
hsi_smooth_median(),
hsi_tiled(),
hsi_write_scaled()
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
)
} # }