17.11.2012 Views

Numerical recipes

Numerical recipes

Numerical recipes

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

14.8 Savitzky-Golay Smoothing Filters 651<br />

M nL nR Sample Savitzky-Golay Coefficients<br />

2 2 2 −0.086 0.343 0.486 0.343 −0.086<br />

2 3 1 −0.143 0.171 0.343 0.371 0.257<br />

2 4 0 0.086 −0.143 −0.086 0.257 0.886<br />

2 5 5 −0.084 0.021 0.103 0.161 0.196 0.207 0.196 0.161 0.103 0.021 −0.084<br />

4 4 4 0.035 −0.128 0.070 0.315 0.417 0.315 0.070 −0.128 0.035<br />

4 5 5 0.042 −0.105 −0.023 0.140 0.280 0.333 0.280 0.140 −0.023 −0.105 0.042<br />

nL + nR +1points in the moving window, and then set gi to be the value of that polynomial<br />

at position i. (If you are not familiar with least-squares fitting, you might want to look ahead<br />

to Chapter 15.) We make no use of the value of the polynomial at any other point. When we<br />

move on to the next point fi+1, we do a whole new least-squares fit using a shifted window.<br />

All these least-squares fits would be laborious if done as described. Luckily, since the<br />

process of least-squares fitting involves only a linear matrix inversion, the coefficients of a<br />

fitted polynomial are themselves linear in the values of the data. That means that we can do<br />

all the fitting in advance, for fictitious data consisting of all zeros except for a single 1, and<br />

then do the fits on the real data just by taking linear combinations. This is the key point, then:<br />

There are particular sets of filter coefficients cn for which equation (14.8.1) “automatically”<br />

accomplishes the process of polynomial least-squares fitting inside a moving window.<br />

To derive such coefficients, consider how g0 might be obtained: We want to fit a<br />

polynomial of degree M in i, namely a0 + a1i + ···+ aMi M to the values f−nL ,...,fnR .<br />

Then g0 will be the value of that polynomial at i =0, namely a0. The design matrix for<br />

this problem (§15.4) is<br />

Aij = i j<br />

i = −nL,...,nR, j =0,...,M (14.8.2)<br />

and the normal equations for the vector of aj’s in terms of the vector of fi’s is in matrix notation<br />

(A T · A) · a = A T · f or a =(A T · A) −1 · (A T We also have the specific forms<br />

· f) (14.8.3)<br />

�<br />

A T �<br />

· A =<br />

ij<br />

nR� AkiAkj =<br />

nR� k i+j<br />

(14.8.4)<br />

and<br />

�<br />

A T �<br />

· f =<br />

j<br />

k=−n L<br />

n R�<br />

k=−n L<br />

Akjfk =<br />

k=−n L<br />

n R�<br />

k=−n L<br />

k j fk<br />

(14.8.5)<br />

Since the coefficient cn is the component a0 when f is replaced by the unit vector en,<br />

−nL ≤ n1,<br />

the array c must be multiplied by k! to give derivative coefficients. For derivatives, one<br />

usually wants m =4or larger.<br />

Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5)<br />

Copyright (C) 1988-1992 by Cambridge University Press. Programs Copyright (C) 1988-1992 by <strong>Numerical</strong> Recipes Software.<br />

Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable<br />

files (including this one) to any server computer, is strictly prohibited. To order <strong>Numerical</strong> Recipes books or CDROMs, visit website<br />

http://www.nr.com or call 1-800-872-7423 (North America only), or send email to directcustserv@cambridge.org (outside North America).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!