0% found this document useful (0 votes)
18 views5 pages

Moving Average and Savitzki-Golay Smoothing Filter

Uploaded by

ShashwathMurthy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

Moving Average and Savitzki-Golay Smoothing Filter

Uploaded by

ShashwathMurthy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/228407245

Moving average and Savitzki-Golay smoothing filters using Mathcad

Article · January 2007

CITATIONS READS

105 10,335

4 authors, including:

J. L. Guiñón Emma Ortega


Polytechnic University of Valencia Polytechnic University of Valencia
94 PUBLICATIONS 1,600 CITATIONS 79 PUBLICATIONS 2,163 CITATIONS

SEE PROFILE SEE PROFILE

V. Pérez-Herranz
Polytechnic University of Valencia
151 PUBLICATIONS 3,600 CITATIONS

SEE PROFILE

All content following this page was uploaded by J. L. Guiñón on 17 May 2014.

The user has requested enhancement of the downloaded file.


Moving Average and Savitzki-Golay Smoothing
Filters Using Mathcad
José Luis Guiñón, Emma Ortega, José García-Antón, Valentín Pérez-Herranz
Departamento de Ingeniería Química y Nuclear.
Universidad Politécnica de Valencia.
P.O. Box 22012. E-46071 Valencia (Spain)
[email protected]

Abstract - All measurement processes cause certain The Saviztky-Golay filter is based on the least squares
amount of random variations in the signal; this polynomial fitting across a moving window within the data
phenomenon is called noise. The procedure to reduce or in the time domain [5]. Typically, a long polynomial, and a
smooth the noise of a measured signal is commonly moderate order polynomial allows a high level of smoothing
known as filtering. In this paper we present the use of without attenuation of data features. By contrast, the
Mathcad software for the implementation and analysis of Saviztky-Golay filtering method is better than averaging
the moving average and Savitzky-Golay filters. The because it tends to preserve features of the data such as peak
moving average filter is the simplest digital filter to height and width, which are usually attenuated by the
understand and use. As its name suggests, this filter moving average filter.
operates by averaging a number of points in a recursive Different mathematical algorithms, generally based on
fashion. In spite of its simplicity, the moving average Excel macro and Matlab, have been developed to implement
filter is effective for time domain encoded signals. The the moving average and Saviztky-Golay filters [2, 6-8].
Saviztky-Golay filter is based on the least squares Among the different symbolic solvers, Mathcad is one of the
polynomial fitting across a moving window within the most widely used. The ease with which Mathcad lets the user
data in the time domain. Generally, a high order type equations and text, nearly exactly as we would write
polynomial (n=4) allows a high level of smoothing them down paper, and its capability of generating complex
without attenuation of data features. By contrast, the graphs, make Mathcad be a powerful and flexible tool to
Saviztky-Golay filtering method is better than averaging develop a variety of instructional designed programs to
because it tends to preserve data features such as peak promote knowledge.
height and width, which are usually attenuated by the The use of Mathcad in the curriculum of chemical
moving average filter. By means of the Mathcad engineering has been discussed in previous papers [9-12]. In
software, moving average and Savitzky-Golay filters this manuscript we report the use of the Mathcad software
were successfully applied to the smoothing of for the implementation and analysis of the moving average
photochemical and electrochemical reactor data. and Savitzky-Golay filters. Finally, Mathcad was
successfully implemented to the smoothing of photochemical
Index Terms - Mathcad, Computer Applications, Moving and electrochemical reactor data.
Average, Savitzki-Golay, Filters, Noise.
FUNDAMENTALS
INTRODUCTION
As its name suggests, the moving average filter
All measurement processes cause certain amount of random operates by averaging a number of points from the input
variations in the signal; this phenomenon is called noise. The signal to generate each point in the output signal. This
procedure to reduce or smooth the noise of a measured signal procedure is repeated so that a moving window of M points
is commonly known as filtering. There are many different is used to calculate the average of the data set. The algorithm
filter designs, but the most common ones are based on takes the mathematical expression:
averaging.
The moving average filter is the simplest digital filter to M−1

understand and use [1-4]. In spite of its simplicity, the ∑ ( si+ j)


j= 0
moving average filter is optimal for a common task: reducing yi := if i > 0 ∧ i < N − ( M − 1)
M
random noise while retaining a sharp step response. This
0 otherwise (1)
makes it the most suitable type of filter for time domain
encoded signals. However, the moving average filter can
produce significant distortions and reduce the signal Where (si+j) is the input signal, yi is the output signal,
intensity, with the subsequent loss of adjacent small signals. M is the number of points in the average and N the total
This effect increases with increasing filter bandwidth. number of data points.

Coimbra, Portugal September 3 – 7, 2007


International Conference on Engineering Education – ICEE 2007
As an alternative, the group of points of the input signal can plot of signal-to noise-ratio, S/N, vs the square-root of the
be chosen symmetrically around the output point. In that case number of points in the average, M1/2, where S is the
M has to be an odd number. In equation form, this is written: maximum value of the signal and N the standard deviation of
the baseline.
( M− 1)
2

∑ ( si+ j) 0.5
− ( M−1)
j=
2 ( M − 1) ( M + 1)
ySi := if i > ∧ i< N−
M 2 2
noisei 0
0 otherwise
(2)

The Saviztky-Golay filtering can be considered as a 0.5


0 0.1 0.2 0.3
generalized moving average. The underlying function within
the moving window is not a constant (whose estimate is the ti
average of M points), but coefficients resulting from an
4
unweighted linear least squares fit using a polynomial of a
given degree, n. The Saviztky-Golay filter of a waveform
can be calculated by:
2
nR si
gm := ∑ ck+n ⋅sm+k
L
k=−nL 0
(3)

where sm+k is the input signal, gm is the output signal. 0 0.1 0.2 0.3
Here nL is the number of points used to the left of a data ti
point m, while nR is the number used to the right. The
Saviztky-Golay coefficients, ck+nL , are given by: Figure 1.- Top: Random noise. Bottom: Simulation of a
signal with two Gaussian features and noisy baseline.
cM+n

( T
:=  ( A) ⋅A ) − 1 ⋅( (A)T ) 0 , M+n
L L (4)
where: 4

M := −n L .. n R
(5)
m := n L .. N − n R si
(6) 2
j := 0 .. n (7) ( ytS25 ) i
j
AM+n , j := M
L (8)
0
0 0.1 0.2 0.3
RESULTS AND DISCUSSION
ti
Figure 1 shows the random noise and the input signal formed 4
by two Gaussians, a major one centred at 0.20 s, while the
minor one is centred at 0.24 s. Figure 2 shows the output
signals obtained with 25-point moving average filters si
symmetrical and directional respectively. Both filters 2
produce a sharp noise reduction, however, the directional ( yt25 ) i
filter with the points on only one side, causes a relative shift
between the input and output signals. The effect of the
number of points on the average is shown in Figure 3. 0
Clearly, as the number of points in the filter increases, noise
signal becomes lower; however, there are significant 0 0.1 0.2 0.3
distortions of the signal by the smoothing filter, which ti
increases with increasing filter bandwidth. This phenomenon
is expected as it inevitably involves averaging part of the Figure 2.- Effect of the 25-point moving average filter. Top:
baseline with the rising portion of the minor peak, leading to Symmetrical filter. Bottom: Directional filter.
its disappearance. Figure 4 shows the linear variation in the

Coimbra, Portugal September 3 – 7, 2007


International Conference on Engineering Education – ICEE 2007
ti
the Saviztky-Golay filter much more than the moving
4
4 average (Figure 2). On the other hand, Figure 5 demonstrates
that there is little distortion in the signals, with possible
si observation of minor peaks.
2
( ytS7) i−0.8 4
( ytS15) i−1.4
0
( ytS25) i−2 si
2
g25m
− 2.2 2
0 0.1 0.2 0.3
0 ti 0.35 0
0 0.1 0.2 0.3
Figure 3.- Noisy Signal and the same signal filtered with 7, ti , tm
15 and 25 point symmetrical moving average filters,
respectively. Signals are offset for visibility. 4

yi
2
200 g25m
S/N

0
100
0 0.1 0.2 0.3
ti , tm
0 Observamos que el filtrado de SG, por ejemplo de 25 puntos conserva la amplitud
0 2 4 6 8 Figure 5.- Top: Effect of 25-point Saviztky-Golay filter on
0.5 the noisy signal (si). Bottom: Comparison of the signal
M
without noise (yi) and the filtered signal (g25).

Figure 4.- Plot of signal-to-noise ratio vs square-root of the Moving average and Savitzky-Golay filters were
number of points in the average. successfully applied to the smoothing of photochemical and
electrochemical reactor data. Figure 6 (top) shows the noisy
The coefficients of Saviztky-Golay filters for orders signal (energy vs wavelength, λ) obtained in a
n=2 and n=4, with several choices of nL and nR are shown in photochemical reactor equipped with a hydrogen or xenon
Table I. The central column is the coefficient applied to the lamp. The effect of the 25-point Savitzky-Golay (n=2) and
data sm for obtaining the smoothed gm. Coefficients to the 25-point moving average filters is shown in Figure 6
left are applied to earlier data and to the right to later data. (bottom). Savitzky-Golay filter produces very little distortion
The coefficient values always add the unity. Note that the in the signal in comparison to the moving average filter.
coefficients always have a central positive lobe and with
smaller outlying corrections of both positive and negative.
Figure 5 illustrates the effect of a 25 point Saviztky-
Golay filter (order 2) on the same input signal as shown in
Figure 1. Clearly, the baseline noise amplitude is reduced by

Table I.- Savitzky-Golay Smoothing Filters

n nL=nR
CM+nL: Savitzky-Golay Coefficients

2 3 -0.095 0.143 0.286 0.333 0.286 0.143 -0.095


2 4 -0.091 0.061 0.169 0.234 0.255 0.234 0.169 0.061 -0.091
2 5 -0.084 0.021 0.103 0.161 0.196 0.207 0.196 0.161 0.103 0.021 -0.084
4 3 0.022 -0.013 0.325 0.567 -0.13 0.022
4 4 0.035 -0.128 0.07 0.315 0.417 0.315 0.07 -0.128 0.035
4 5 0.042 -0.105 -0.023 0.14 0.28 0.333 0.28 0.14 -0.023 -0.105 0.042

Coimbra, Portugal September 3 – 7, 2007


International Conference on Engineering Education – ICEE 2007
The use of the Savitzky-Golay filter is consequently CONCLUSIONS
much easier to justify because the signal-to-noise ratio is
improved by a factor of around 20 relative to the non filtered This paper describes the use of the Mathcad software
input signal, whereas for the moving average filter this ratio for the implementation and analysis of the moving average
is approximately 10. and Savitzky-Golay filters. Mathcad was successfully
implemented to the smoothing of photochemical and
electrochemical reactor data.
1

ACKNOWLEDGMENT
Energy

0 We wish to express our gratitude to the Universidad


Politécnica de Valencia, and to Dr Asunción Jaime for her
650 652 654 656 658 660 translation assistance.
λ
1 REFERENCES

[1] Donnelly, D. “The fast Fourier transform for experimentalist, part V:


Energy81 Filters” Computing in Science&Engineering , 8 (1), p. 92,(2006).
0.5
EnergytS81 [2] De Levie, R. “Advanced Excel for Scientific Data Analysis” Oxford
University Press, Oxford, (2004).

0 [3] Tham, M.T. “Dealing with measurement noise. Moving average


filter” Chemical Engineering and Advanced Materials, University of
650 652 654 656 658 660
Newcastle upon Tyne. (1998).
λ www.lorien.ncl.ac.uk/ming/filter/filmav.htm
Figure 6.- Top: Noisy signal in a photochemical reactor.
[4] DATAQ Instruments. “A closer look at the advanced CODAS
Bottom: Relative effect of the 81-point Saviztky-Golay and moving average algorithm”.
moving average filters on the noisy signal. www.dataq.com/applicat/articles/an14.htm

Figure 7 shows the potential-time response to applying [5] Savitzky, A., Golay, M.J.E. “Smoothing and differentiation of data by
simplified least squares procedures” Analytical Chemistry, 36(2),
a pulse of current in an electrochemical reactor. In this case, p.1627, (1964).
the use of 201-point Savitzky-Golay (n=1) filter, is not so [6] Borchers, H.W. “Savitzky-Golay smoothing an R implementation” in
excellent as the above example; however, it suffices enough http://tostoy.newcastle.edu.au/R/help/04/02/0385.html
to retrieve the signal with little noise.
[7] Press, W.H., Flannery, B.P., Teukolsky, S.A., Vetterling, W.T.
“Numerical recipes in C: The Art of Scientific Computing”
Cambridge Univesity Press, Cambridge, (1992).

2 [8] Press, W.H., Teukolsky, S.A. “Savitzky-Golay smoothing filters”


Computers in Physics, Nov/Dec , p. 669, (1990).
1 [9] Guiñón, J.L., Igual, A., Pérez-Herranz, V., García-Antón. J.
Potential
“Simulación del análisis frecuencial de señales y del derrame
0 espectral mediante el programa Mathcad” Ingenieria Quimica, 39,
(445), p.144, (2007).
1 [10] Guiñón, J.L., Ortega, E., García-Antón, J., Pérez-Herranz, V.
0 50 100 150 200
“Regresión no lineal aplicada a la cinética de un reactor discontinuo:
Time Análisis comparativo mediante dos programas informáticos”
Ingenieria Quimica, 38, (441), p.170, (2006).
2
[11] Ortega, E., Guiñón, J.L., García-Anton, J., Pérez-Herranz, V.
“Nonlinear regression of a second-order kinetics using Mathcad:
Ethyl acetate saponification” Abstracts of Papers 230th ACS National
1 Meeting: Computer 45. Washington, DC, United States, Aug. 28,
Potential201
(2005).

[12] Guiñón, J.L., García-Antón, J., Ortega, E., Pérez-Herranz, V.


0 “Simulation of a lock-in amplifier by means of the Mathcad symbolic
software solver” Afinidad, 60(506), p. 396, (2003).
0 50 100 150 200
Time
Figure 7.- Top: Potential-time response in a electrochemical
reactor. Bottom: Effect of 201-point Saviztky-Golay filter.
Order n=1.

Coimbra, Portugal September 3 – 7, 2007


International Conference on Engineering Education – ICEE 2007

View publication stats

You might also like