When applying SG, we select a moving average window with an odd value “n” for the number of data points. SG fit a polynomial of “p” degree to this data points and give the value to the central point (this is the reason to have an odd value).
We apply also an smooth in the case of “m” = 0, or the first (m=1), second (m=2) or third (m=3) derivatives.
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). |
Playing with these three parameter we change the shape of the spectra.
Example:
The upper spectra was taken with p=2, n=7 and m=2
The lower one with p=2, n=31 and m=2
As we can see the size of the window has a big influence on the resolution of the peaks.
How to apply it in R:
(For the lower spectrum)
>sgolay_2_31_2<-apply(fattyac$NITm,1,sgolayfilt,p=2,n=31,m=2)
>matplot(wavelengths,sgolay_2_31_2,lty=1,pch=21,
+ xlab="nm",ylab="SG_2_31_2_abs")
Tweet
Perfect, but when I want to perform a pls with the resulting sgolay spectra, R informs me about the next problem:
ResponderEliminarError in data.frame(NIR = c(-9.50582756092354e-05, -9.83938884112877e-05, :
arguments imply differing number of rows: 3001, 294
But the structure of my data (similar to "yarn") is OK (supposedly)
str(lambda.sgolay_2_31_1)
List of 2
$ NIR : num [1:3001, 1:294] -9.17e-05 -9.51e-05 -9.84e-05 -1.02e-04 -1.05e-04 ...
$ gluc: num [1:294] 0.97 1.141 1.843 0.916 1.226 ...