Tweet
  • Home
  • Full Article Index
  • Who Are We?
  • Contact Us
  • Prosig.com

Data Smoothing : RC Filtering And Exponential Averaging

Apr28th
2003
13 Comments Written by Dr Colin Mercer

[Updated 12th March 2013]

What are RC Filtering and Exponential Averaging and how do they differ? The answer to the second part of the question is that they are the same process! If one comes from an electronics background then RC Filtering (or RC Smoothing) is the usual expression. On the other hand an approach based on time series statistics has the name Exponential Averaging, or to use the full name Exponential Weighted Moving Average. This is also variously known as EWMA or EMA.

A key advantage of the method is the simplicity of the formula for computing the next output. It takes a fraction of the previous output and one minus this fraction times the current input. Algebraically at time k the smoothed output yk is given by

y_k = {\alpha}y_{k-1} + (1 - \alpha)x_k\,\,where\,0<\alpha <1 (1)

As shown later this simple formula emphasises recent events, smooths out high frequency variations and reveals long term trends. Note there are two forms of the exponential averaging equation, the one above and a variant

y_k = {\alpha}x_k + (1 - \alpha)y_{k-1} (2)

Both are correct. See the notes at end of the article for more details. In this discussion we will only use equation (1).

The above formula is sometimes written in the more limited fashion.

y_k = \left(\frac{N-1}{N}\right)y_{k-1} + \left(\frac{1}{N}\right)x_k

How is this formula derived and what is its interpretation? A key point is how do we select \alpha. To look into this one simple way is to consider an RC low pass filter.

Now an RC low pass filter is simply a series resistor R and a parallel capacitor C as illustrated below.

The time series equation for this circuit is

RC\left(\frac{dy}{dt}\right) + y = x

The product RC has units of time and is known as the time constant ,T , for the circuit. Suppose we represent the above equation in its digital form for a time series which has data taken every h seconds. We have

T\left(\frac{y_k - y_{k-1}}{h}\right) + y_k = x_k

Rearranging gives

y_k = \left(\frac{T}{T+h}\right)y_{k-1} + \left(\frac{h}{T+h}\right)x_k

or

y_k = {\alpha}y_{k-1} + (1-\alpha)x_k

This is exactly the same form as the previous equation. Comparing the two relationships for a we have

\frac{T}{T+h} = \frac{N-1}{N}

which reduces to the very simple relationship

T = (N - 1)h

Hence the choice of N is guided by what time constant we chose. Now equation (1) may be recognised as a low pass filter and the time constant typifies the behaviour of the filter. To see the significance of the Time Constant we need to look at the frequency characteristic of this low pass RC filter. In its general form this is

H(\omega) = \frac{1}{1+i{\omega}RC} = \frac{1}{1+i{\omega}T}

Expressing in modulus and phase form we have

H(\omega) = {\frac{1}{\sqrt{1+\omega^2T^2}}e^{i\phi}

where the phase angle \phi$ is $tan^{-1}(-{\omega}T ).

The frequency \omega = 1/T is called the nominal cut off frequency \omega_c. Physically it may be shown that at this frequency the power in the signal has been reduced by one half and the amplitude is reduced by the factor (1/\sqrt{2}) = 0.7071. In dB terms this frequency is where the amplitude has been reduced by 3dB.

Clearly as the time constant T increases so then the cut off frequency {\omega}_c reduces and we apply more smoothing to the data, that is we eliminate the higher frequencies.

It is important to note that the frequency response is expressed in radians/second. That is there is a factor of 2\pi involved. For example choosing a time constant of 5 seconds gives an effective cut off frequency of 1/(2\pi * 5) = 0.032Hz. One popular use of RC smoothing is to simulate the action of a meter such as used in a Sound Level Meter. These are generally typified by their time constant such as 1 second for S types and 0.125 seconds for F types. For these 2 cases the effective cut off frequencies are 0.16Hz and 1.27Hz respectively.

Actually it is not the time constant we usually wish to select but those periods we wish to include. Suppose we have a signal where we wish to include features with a P second period. Now a period P is a frequency f_p = 1/P. We could then choose a time constant T given by T=P/(2*\pi) . However we know that we have lost about 30% of the output (-3dB) at f_p. Thus choosing a time constant which exactly corresponds to the periodicities we wish to keep is not the best scheme. It is usually better to choose a slightly higher cut off frequency, say 1.5f_p . The time constant is then T=P/(3*\pi) which in practical terms is similar to T = P/10. This reduces the loss to around 15% at this periodicity. Hence in practical terms to retain events with a periodicity of P or greater then choose a time constant of (P/10). This will include the effects of periodicities of down to about 0.6P. For example if we wish to include the effects of events happening with say an 8 second period (= 0.125Hz) then choose a time constant of 0.8 seconds. This gives a cut off frequency of approximately 0.2Hz so that our 8 second period is well in the main pass band of the filter. If we were sampling the data at 20 times/second (h = 0.05) then the value of N is (0.8/0.05) = 16 and \alpha = (16/17) = 0.9412.

This gives some insight into how to set \alpha. Basically for a known sample rate it typifies the averaging period and selects which high frequency fluctuations will be ignored.

By looking at the expansion of the algorithm we can see that it favours the most recent values, and also why it is referred to as exponential weighting. We have

y_k = (1-\alpha)x_k + {\alpha}y_{k-1}

Substituting for yk-1 gives

y_k = (1 - \alpha)x_k + \alpha\left[(1-\alpha)x_{k-1} + {\alpha}y_{k-2}\right] =(1 - \alpha)(x_k + {\alpha}x_{k-1}) + \alpha_2y_{k-2}

Repeating this process several times leads to

y_k = (1-\alpha)(x_k + {\alpha}x_{k-1} + {\alpha}^2x_{k-2}+ \dots + {\alpha}^Nx_{k-N})+{\alpha}^{N+1}y_{k-N-1}

Because \alpha is in the range 0<\alpha<1 then clearly the terms to the right become smaller and behave like a decaying exponential. That is the current output is biased towards the more recent events but the larger we choose T then the less bias.

In summary we see that the simple formula y_k= {\alpha}y_{k-1} + (1-\alpha)x_k

  • emphasises recent events
  • smoothes out high frequency (short period) events
  • reveals long term trends

Appendix 1 – Alternate forms of the equation

Caution There are two forms of the exponential averaging equation that appear in the literature. Both are correct and equivalent.

The first form as shown above is y_k = {\alpha}_1y_{k-1} + (1 - {\alpha}_1)x_k…(A1)

The alternate form is y_k = {\alpha}_{2}x_k + (1 - {\alpha}_2)y_{k-1} …(A2)

Note the use of {\alpha}_1 in the first equation and {\alpha}_2 in the second equation. In both equations {\alpha}_1 and {\alpha}_2 are values between zero and unity.

Earlier {\alpha}_1 was defined as

{\alpha}_1 = \big(\frac{T}{T+h}\big)

which gives

(1-{\alpha}_1)=\big(\frac{h}{T+h}\big).

Now choosing to define

{\alpha}_2 = \big(\frac{h}{T+h}\big)

gives

(1 - {\alpha}_2)=\big(\frac{T}{T+h}\big).

Hence the alternate form of the exponential averaging equation is

y_k={\alpha}_2x_k+(1-{\alpha}_2)y_{k-1}

where

{\alpha}_2 = (1-{\alpha}_1)= \big(\frac{h}{h+T}\big).

In physical terms it means that the choice of form one uses depends on how one wants to think of either taking {\alpha} as the feed back fraction [equation (A1)] or as the fraction of the input [equation (A2)].

The first form is slightly less cumbersome in showing the RC filter relationship, and leads to a simpler understanding in filter terms.

Print or send:

  • Print
  • Email
signal processing    DATS, ewma, filter, high frequency, low pass filter, moving average, Prosig, series resistor, smoothing, spikes
Similar posts
  • A Different Way Of Averaging – ... — Assuming one wants a frequency spectrum from an acquired ...
  • Quantifying Signals – Peak, Pea... — Anytime you measure something which is changing with time...
  • What Can I Do About Spikes In My Data... — Whether you call them spikes, glitches, anomalies or data...
  • What Are AC and DC Coupling? — This is an important subject to understand when working i...
  • Understanding & Measuring Noise — There are many reasons to measure and analyze noise. It m...
← Smoothing Spectral Data
Notes On Fourier Analysis →

12 Comments

  1. Brandon's Gravatar Brandon
    February 21, 2008 at 5:28 pm | Permalink

    Excellent! Exactly what I was looking for.

    Reply
  2. marco clark's Gravatar marco clark
    December 9, 2010 at 5:48 pm | Permalink

    I think you want to change the ‘p’ to the symbol for pi.

    Reply
  3. Chris Mason's Gravatar Chris Mason
    December 9, 2010 at 6:37 pm | Permalink

    Marco, thank you for pointing that out. I think this is one of our older articles that has been transferred from an old word processing document. Obviously, the editor (me!) failed to spot that the pi had not been transcribed correctly. It will be corrected shortly.

    Reply
  4. zheng's Gravatar zheng
    March 7, 2011 at 3:57 am | Permalink

    it’s a very good article explanation about the exponential averaging!

    Reply
  5. Mike Z's Gravatar Mike Z
    December 6, 2011 at 6:14 pm | Permalink

    I believe there is an error in the formula for T. It should be T = h*(N-1), not T = (N-1)/h.

    Reply
  6. Chris Mason's Gravatar Chris Mason
    December 7, 2011 at 9:24 am | Permalink

    Mike, thanks for spotting that. I have just checked back to Dr Mercer’s original technical note in our archive and it seems that there was error made when transferring the equations to the blog. We will correct the post. Thank you for letting us know

    Reply
  7. Julia Truchsess's Gravatar Julia Truchsess
    December 12, 2011 at 4:52 pm | Permalink

    Thank you thank you thank you. You could read 100 DSP texts without finding anything saying that an exponential averaging filter is the equivalent of an R-C filter.

    Reply
  8. Alan's Gravatar Alan
    March 7, 2013 at 11:39 am | Permalink

    hmm, do you have the equation for an EMA filter correct? is it not Yk = aXk + (1-a)Yk-1; rather than Yk = aYk-1 + (1-a)Xk

    Reply
    • Dr Colin Mercer's Gravatar Dr Colin Mercer
      March 12, 2013 at 2:08 pm | Permalink

      Alan,
      Both forms of the equation appear in the literature, and both forms are correct as I will show below. The point you make is important one because using the alternate form means that the physical relationship with an RC filter is less apparent, moreover the interpretation of the meaning of a shown in the article is not appropriate for the alternate form.

      First let us show both forms are correct. The form of the equation that I have used is

      y_k = {\alpha}_1y_{k-1} + (1 - {\alpha}_1)x_k …(1)

      and the alternate form which does appear in many texts is

      y_k = {{\alpha}_2}x_k + (1 - {\alpha}_2)y_{k-1} …(2)

      Note in the above I have used {\alpha}_1 in the first equation and {\alpha}_2 in the second equation. The equality of both forms of the equation is shown mathematically below taking simple steps at a time. What is not the same is the value used for {\alpha} in each equation.

      In both forms {\alpha} is a value between zero and unity. First rewrite equation (1) replacing {\alpha}_1 by {\beta}. This gives

      y_k = {\beta}y_{k-1} + (1 - \beta)x_k …(1A)

      Now define \beta = (1 - {\alpha}_2) and so we also have {\alpha}_2 = (1 - \beta). Substituting these into equation (1A) gives

      y_k = (1 - {\alpha}_2)y_{k-1} + {\alpha}_2x_k …(1B)

      And finally re-arranging gives

      y_k = {\alpha}_{2}x_k + (1 - {\alpha}_2)y_{k-1} …(1C)

      This equation is identical to the alternate form given in equation (2).

      Put more simply {\alpha}_2 = (1 - {\alpha}_1).

      In physical terms it means that the choice of form one uses depends on how one wants to think of either taking \alpha as the feed back fraction [equation (1)] or as the fraction of the input [equation (2)].

      As mentioned above I have used the first form as it is slightly less cumbersome in showing the RC filter relationship, and leads to simpler understanding in filter terms.

      However omitting the above is, in my view, a deficiency in the article as other people could make an incorrect inference so a revised version will appear soon.

      Colin

      Reply
  9. Colin King's Gravatar Colin King
    March 12, 2013 at 11:10 pm | Permalink

    I’ve always wondered about this, thanks for describing it so clearly.

    Reply
  10. Michael's Gravatar Michael
    May 3, 2013 at 5:10 am | Permalink

    I think another reason the first formulation is nice is alpha maps to ‘smoothness’: a higher choice of alpha means a ‘more smooth’ output.

    Reply
    • Dr Colin Mercer's Gravatar Dr Colin Mercer
      May 8, 2013 at 1:46 pm | Permalink

      Michael
      Thanks for observation – I will add to the article something on those lines as it is always better in my view to relate to physical aspects.

      Reply
  1. » Filtering Noise from Sensor Readings: A Simple Low Pass Filter The Aspiring Roboticist on January 6, 2013 at 9:38 pm

Leave a Reply Cancel reply

Video Gallery



Who Are Prosig?

Hammer Impact Testing

Analysing Rotating Machines

Calibrating an Accelerometer

Follow Us!

Follow Us on FacebookFollow Us on Google+Follow Us on TwitterFollow Us on LinkedInFollow Us on FlickrFollow Us on YouTubeFollow Us on StumbleUponFollow Us on Tumblr

EvoLve theme by Theme4Press  •  Powered by WordPress Prosig Noise & Vibration Blog
Telling you what you need to know about noise & vibration

Performance Optimization WordPress Plugins by W3 EDGE
loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.