Savitzky-Golay filters in R

March 17, 2012

(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
Smooth data with a Savitzky-Golay smoothing filter.
sgolayfilt(x, p = 3, n = p + 3 – p%%2, m = 0, ts = 1)
## S3 method for class ‘sgolayFilter’
filter(filt, x, …)

signal to be filtered.
filter order.
filter length (must be odd).
return the m-th derivative of the filter coefficients.
time scaling factor.
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.
Chemometrics with R (Ron Wehrens)
R help page

To leave a comment for the author, please follow the link and comment on their blog: NIR-Quimiometría. 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...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...


Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)