Savitzky–Golay smoothing and differentiation filter for even number

ARTICLE IN PRESS
Signal Processing 85 (2005) 1429–1434
www.elsevier.com/locate/sigpro
Savitzky–Golay smoothing and differentiation filter
for even number data
Jianwen Luo, Kui Ying, Jing Bai
Department of Biomedical Engineering, Tsinghua University, Beijing 100084, PR China
Received 19 April 2004; received in revised form 22 February 2005
Abstract
The Savitzky–Golay smoothing and differentiation filter optimally fits a set of data points to a polynomial in the
least-squares sense. The Savitzky–Golay filter has been developed and generalized well in the literatures. However, the
data subset is subject to an odd number ð2m þ 1Þ: In this communication, the Savitzky–Golay filter is extended for even
number data. Simulations are performed to validate the feasibility of such an approach. And some corresponding
properties are discussed.
r 2005 Elsevier B.V. All rights reserved.
Keywords: Savitzky–Golay; Smoothing; Differentiation; Least squares; Polynomial
1. Introduction
In various fields such as signal processing,
imaging processing, analytical chemistry and
spectroscopic analysis, smoothing and differentiation is important and necessary. In the classical
paper written by Savitzky and Golay [1], which has
been cited more than 3800 times according to ISI
Web of Science, one kind of digital filter for
smoothing and differentiation was developed. In
their approach, each successive subset of 2m þ 1
Corresponding author. Department of Biomedical Engineering, Tsinghua University, Beijing 100084, PR China. Tel.:
+86 10 6278 6480; fax: +86 10 6278 0650.
E-mail address: [email protected] (J. Bai).
points is fitted by a polynomial of degree p (pp2m)
in the least-squares sense. The dth ð0pdppÞ
differentiation (zeroth differentiation ¼ smoothing) of the original data at the midpoint is
obtained by performing the differentiation on the
fitted polynomial rather than on the original data.
Finally, the running least-squares polynomial
fitting can be simply and automatically performed
by convolving the entire input data with a digital
filter of length 2m þ 1: The convolution coefficients can be obtained for all data points,
all polynomial degrees and all differentiation
orders but with only an odd number of data
sets [2,3].
As pointed out by Mark and Workman [4], one
limitation is that the existing Savitzky–Golay filter
0165-1684/$ - see front matter r 2005 Elsevier B.V. All rights reserved.
doi:10.1016/j.sigpro.2005.02.002
ARTICLE IN PRESS
J. Luo et al. / Signal Processing 85 (2005) 1429–1434
1430
is subject to using an odd number of data subset.
However, the data subset can also have an even
number of data ð2mÞ; i.e., the SG filter can have an
even length. This extension will be of interest to
some readers, e.g., spectroscopist [4,5] and engineers [6,7]. In this communication, a generalized
SG smoothing and differentiation filter is presented to calculate the convolution coefficients for
even number data using a matrix form [3]. Several
closed-form solutions are given. The feasibility of
such an approach is validated by computer
simulations, and some properties are discussed.
where S is the 2m þ 1-by-p þ 1 basic matrix, as
S ¼ ½s0 ; s1 ; . . . ; sp ,
si ¼ ½ðmÞi ; ðm þ 1Þi ; . . . ; mi T
(3)
ð0pippÞ.
(4)
The convolution coefficients for the dth differentiation are given by [3]
hp;d ¼ d!gd ¼ d!½gd ðmÞ; gd ðm þ 1Þ; . . . ; gd ðmÞT ,
(5)
Referring to Ref. [3], assume that 2m þ 1 data
points are positioned symmetrically about the
origin (i.e. the midpoint)
where gd is the dth column vector of the matrix G
while gd ðnÞ is the nth element of the vector gd :
Now we consider an even number (2m;
2m 14 ¼ p) of data sets. The index of the data
samples ranges from m þ 1 to m: The data
samples can be equivalently regarded as being
symmetrical about the origin, by shifting the origin
one half. Therefore, the input signals are represented by
x ¼ ½xm ; xmþ1 ; . . . ; xm T .
x ¼ ½xmþ1=2 ; . . . ; x1=2 ; x1=2 ; . . . ; xm1=2 T .
2. Theory
(1)
The matrix Gð2mþ1Þðpþ1Þ contains the convolution coefficients of the SG filter for different
differentiation orders at the midpoint, given by [3]
T
1
G ¼ SðS SÞ
¼ ½g0 ; g1 ; . . . ; gp ,
(2)
(6)
Consequently, the column vectors of the basic
matrix Sð2mÞðpþ1Þ are represented by
si ¼ ½ðm þ 1=2Þi ; ðm þ 3=2Þi ; . . . ; ðm 1=2Þi T
ð0pippÞ.
ð7Þ
Table 1
Convolution coefficients of Savitzky–Golay filter for even number data
Differentiation
order (d)
Polynomial
degree (p)
Convolution coefficients
hp;d ðkÞ (k ¼ m þ 1; m þ 2; . . . ; m)
0
0 or 1
1
2m
0
2 or 3
3½12m2 7 20ðk 1=2Þ2 32ðm þ 1Þmðm 1Þ
0
4 or 5
15½240m4 920m2 þ 407 1120m2 ðk 1=2Þ2 þ 1960ðk 1=2Þ2 þ 1008ðk 1=2Þ4 2048ðm þ 2Þðm þ 1Þmðm 1Þðm 2Þ
1
1 or 2
6ðk 1=2Þ
ð2m þ 1Þmð2m 1Þ
1
3 or 4
5ðk 1=2Þ½240m4 360m2 þ 155 336m2 ðk 1=2Þ2 þ 196ðk 1=2Þ2 8ð2m þ 3Þð2m þ 1Þðm þ 1Þmðm 1Þð2m 1Þð2m 3Þ
2
2 or 3
15½4m2 þ 1 þ 12ðk 1=2Þ2 4ð2m þ 1Þðm þ 1Þmðm 1Þð2m 1Þ
ARTICLE IN PRESS
J. Luo et al. / Signal Processing 85 (2005) 1429–1434
The convolution coefficients corresponding to
Eq. (5) are given by
hp;d ¼ d!gd ¼ d!½gd ðm þ 1Þ,
gd ðm þ 2Þ; . . . ; gd ðmÞT .
ð8Þ
The coefficients of the SG filter are then
obtained by directly substituting Eq. (7) into Eq.
(2) (where the matrix G is 2m-by-p þ 1 now) and
Eq. (8). Several closed-form solutions are given in
Table 1. Here only the SG filter at the midpoint or
the center of symmetry (1/2) is considered. In
practice, the SG filter at any position can be
obtained. However, the SG filter at the midpoint
has the advantage of symmetry (or anti-symmetry)
and linear phase property.
1431
The smoothed data and the smoothed differentiation are given by the convolution of the noisy
data and the coefficients of the SG smoothed and
differentiation filter, respectively. The parameters
of the SG filter are taken as m ¼ 10; p ¼ 3 and
d ¼ 0; 1; 2; respectively. The filter coefficients are
calculated from the closed-form solutions and
listed in Table 2. The corresponding frequencies
(transfer functions) are shown in Figs. 1–3.
The results are presented in Figs. 4–6, respectively. As can be seen, the SG filter presented in
this communication is valid to calculate the
3. Simulations
Simulations in MATLAB 6.5 (The MathWorks
Inc., Natick, MA) are used to validate the
feasibility of the approach presented previously.
The underlying function of the data is assumed to
be a Gaussian function. Adding uncorrelated
Gaussian white noise, we obtained the noisy data.
The maximum and the full-width at half maximum
(FWHM) are set at 1.0 and 5.0, respectively. The
additional noise is zero-mean and has a standard
deviation of 0.05. The sampling interval is set to be
1
20 FWHM, i.e. 20 data points per FWHM.
Fig. 1. The frequency response of the Savitzky–Golay smoothing filter (m ¼ 10; p ¼ 3; d ¼ 0).
Table 2
Convolution coefficients used in the simulations
Parameters
Convolution coefficients ðhp;d ðkÞÞ
p ¼ 3; d ¼ 0;
1=2640½153; 63; 17; 87; 147; 197; 237; 267; 287; 297;
m ¼ 10
297; 287; 267; 237; 197; 147; 87; 17; 63; 153
p ¼ 3; d ¼ 1;
1=41186376½1030047; 69819; 606475; 1032239; 1240877; 1265793;
m ¼ 10
1140391; 898075; 572249; 196317; 196317; 572249; 898075; 1140391;
1265793; 1240877; 1032239; 606475; 69819; 1030047
p ¼ 3; d ¼ 2;
1=8778½57; 39; 23; 9; 3; 13; 21; 27; 31; 33;
m ¼ 10
33; 31; 27; 21; 13; 3; 9; 23; 39; 57
ARTICLE IN PRESS
1432
J. Luo et al. / Signal Processing 85 (2005) 1429–1434
Fig. 2. The frequency response (—) of the Savitzky–Golay
differentiation filter (m ¼ 10; p ¼ 3; d ¼ 1), as well as that of an
ideal 1st differentiation filter (- -).
Fig. 3. The frequency response (—) of the Savitzky–Golay
differentiation filter (m ¼ 10; p ¼ 3; d ¼ 2), as well as that of an
ideal 2nd differentiation filter (- -).
Fig. 4. The ideal data (—), the noisy data ( ) and the
smoothed data (- -) using the Savitzky–Golay smoothing filter
(m ¼ 10; p ¼ 3; d ¼ 0).
Fig. 5. The ideal 1st differentiation (—) and the smoothed 1st
differentiation (- -) using the Savitzky–Golay differentiation
filter (m ¼ 10; p ¼ 3; d ¼ 1).
smoothed value and smoothed differentiation for
even number of noisy data.
Expand the column-wise of the left-hand side
and the right-hand side, respectively, and substitute Eq. (5). We can get
4. Discussions
m
X
According to Eq. (2), we have
T
S G ¼ I,
where I is the p þ 1-by-p þ 1 identity matrix.
(9)
ki hp;d ðkÞ ¼ d!dði dÞ;
i ¼ 0; 1; . . . ; d,
(10)
k¼m
where dðnÞ represents the Dirac delta function.
ARTICLE IN PRESS
J. Luo et al. / Signal Processing 85 (2005) 1429–1434
Fig. 6. The ideal 2nd differentiation (—) and the smoothed 2nd
differentiation (- -) using the Savitzky–Golay differentiation
filter (m ¼ 10; p ¼ 3; d ¼ 2).
Similarly, the coefficient restriction for even
number data is given by
m
X
1 i
k
hp;d ðkÞ ¼ d!dði dÞ,
2
k¼mþ1
i ¼ 0; 1; . . . ; d.
ð11Þ
The coefficient restriction given by Eqs. (10)
and (11) is related to the flatness of the frequency response at o ¼ 0; or to the moment
conservation of the input signal or signal differentiation [3,8].
Assuming that the input signal is contaminated
by independent Gaussian white noise (or a weaker
condition of uncorrelated and homoscedastic
noise), the noise amplification factor of a digital
filter is given by the sum of the squares of the
filter impulse response [3,9]. In statistics, the
Gauss–Markov theorem [10] states that the best
linear unbiased estimators of the coefficients for a
linear model with independent Gaussian white
noise or errors (or a weaker condition mentioned
previously) are the least-squares estimators, which
are equivalent to the SG filter. Therefore, for an
even filter length, the SG filter presented in this
work is an optimal filter in the sense of minimizing
the noise amplification factor, but its coefficients
are subject to additional constrains given by
1433
Eq. (11) or to the aforementioned flatness constrain of the frequency response at o ¼ 0:
Since the origin has been shifted to the
imaginary halfway position in the implementation,
the SG filter for even number data will result in a
time delay of half a sampling interval. With the
compensation of a noninteger time delay, the SG
filter can have better results. For example, the twopoint backward differentiation [11] that can be
regarded as the SG filter for even number data in
the case where m ¼ 1; p ¼ 1; d ¼ 1 is more precise
to calculate the differentiation at a point halfway
between the data points, rather than at either one
of the two points.
An ideal full-pass differentiation filter has a gain
that increases with frequency (Hðejo Þ ¼ ðjoÞd );
therefore it greatly amplifies high-frequency
noises. In practice, one would be readily choose
a low-pass differentiation filter rather than a fullpass one. The SG differentiation filter can be
regarded as a kind of low-pass filters. According to
the symmetry or antisymmetry of the coefficients
[1,3], the SG filters of odd length for even and odd
order differentiations belong to type I and type III
filter [12], respectively. Similarly, the SG filters of
even length for even and odd order differentiations
belong to type II and type IV filter [12],
respectively. It is known that the frequency
responses at o ¼ p of the type II and type III
filter are zero [12]. Therefore, for the even order
differentiation, the SG filter of even length has a
better characteristic of attenuation at the high
frequency of o ¼ p than that of odd length (see
Figs. 1 and 3). It may be helpful to reduce some
high-frequency noises when they are present. For
the odd length SG smoothing filter, Hamming
presented a modified least-squares method (i.e., a
modified SG filter) to restrict the frequency
response at o ¼ p to be zero [9]. The use of the
even length SG smoothing filter would be an
alternative method. For the odd order differentiation, the SG filter of even length has a worse
characteristic of attenuation at the high frequency
than that of even length (see Fig. 2), and hence
should be used with caution. The performance
evaluation and the comparison of the SG filter of
even length and that of odd length, both in theory
and in applications, remains to be investigated.
ARTICLE IN PRESS
1434
J. Luo et al. / Signal Processing 85 (2005) 1429–1434
Acknowledgements
This work is supported in part by the National
Natural Science Foundation of China (60171039,
30470466).
References
[1] A. Savitzky, M.J.E. Golay, Smoothing and differentiation
of data by simplified least-squares procedures, Anal.
Chem. 36 (8) (1964) 1627–1639.
[2] P.A. Gorry, General least-squares smoothing and differentiation by the convolution (Savitzky–Golay) method,
Anal. Chem. 62 (6) (1990) 570–573.
[3] S.J. Orfanidis, Introduction to Signal Processing, PrenticeHall, Englewood Cliffs, NJ, 1996 (Chapter 8).
[4] H. Mark, J. Workman, Derivatives in spectroscopy—Part
III—computing the derivative, Spectroscopy 18 (12) (2003)
106–111.
[5] H. Mark, personal communication, 2005.
[6] F. Kallel, J. Ophir, A least-squares strain estimator
for elastography, Ultrason. Imaging 19 (3) (1997)
195–208.
[7] J.W. Luo, J. Bai, P. He, K. Ying, Axial strain calculation
using a low-pass digital differentiator in ultrasound
elastography, IEEE Trans. Ultrason. Ferroelectr. Freq.
Control. 51 (9) (2004) 1119–1127.
[8] J.W. Luo, K. Ying, P. He, J. Bai, Properties of
Savitzky–Golay digital differentiators, Digit. Signal Prog.
15 (2) (2005) 122–136.
[9] R.W. Hamming, Digital Filters, third ed., Prentice-Hall,
Englewood Cliffs, NJ, 1989 (Chapter 3).
[10] X.D. Zhang, Modern Signal Processing, second ed.,
Tsinghua University, Beijing, China, 2002 (Chapter 2)
(in Chinese).
[11] R.L. Burden, J.D. Faires, Numerical Analysis, fourth ed.,
PWS-KENT Pub. Co., Boston, 1989 (Chapter 4).
[12] A.V. Oppenheim, R.W. Schafer, Discrete-time Signal
Processing, Prentice-Hall, Englewood Cliffs, NJ, 1989
(Chapter 5).