# Savitzky-Golay filters in R

March 17, 2012
By

(This article was first published on NIR-Quimiometría, and kindly contributed to R-bloggers)

Derivatives are a good way to extract information to our spectra. As we know NIR contents overlapping bands, and spectra must be treated with math operations in order to extract as much information as possible and to correlate it with the constituent  of interest in the case of quantitative analysis or to discriminate two similar products.
Derivatives are one of those math treatments.
With “R”, we can apply a Savitzky Golay smoothing filter to apply derivatives to our spectra, and to select the order of the derivative.
Let´s have a look first to the R help:
Apply a Savitzky-Golay smoothing filter
Description
Smooth data with a Savitzky-Golay smoothing filter.
Usage
sgolayfilt(x, p = 3, n = p + 3 – p%%2, m = 0, ts = 1)
## S3 method for class ‘sgolayFilter’
filter(filt, x, …)
Arguments

 x signal to be filtered. p filter order. n filter length (must be odd). m return the m-th derivative of the filter coefficients. ts time scaling factor. filt filter characteristics (normally generated by sgolay). … additional arguments (ignored).

Let’s apply the default values for SG filter to the “fattyac” data (you can do the same for the “yarn” data or “gasoline” data).
But first remember how this data looks before any math treatment was applied:
Now let’s give to m, the values 1 (First Derivative) and 2 (Second Derivative)

Here we have a powerful to continue progressing doing Chemometrics with R.
Bibliography:
Chemometrics with R (Ron Wehrens)
R help page

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

Tags: