# Savitzky-Golay filters in R

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**NIR-Quimiometría**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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**

To

**leave a comment**for the author, please follow the link and comment on their blog:**NIR-Quimiometría**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.