1
$\begingroup$

I have implemented a moving average filter (in Python) where I fill a list with values and average them. When new values arrive the oldest will be deleted.

Now I am wondering how much delay I get with this filtering.

I am sampling a sensor signal (voltage) with 1000 Hz and I have read often that the group delay (for my understanding that is the delay?) for a moving average filter for M values (window size) is (M-1)/2.

I can't really understand it because:

Lets say I have the a window size of 5 and following measured values arriving in my empty moving average filter:

  • 6x 1 Volt
  • 6x 3 Volt

    |1|1|1|1|1|1|3|3|3|3|3|3|

First I am waiting until my moving average filter is filled (until the fifth measured value).

Then I get these average values

|1|1|1|1|1| 1 |3|3|3|3|3|3|   1   [1,1,1,1,1]
|1|1|1|1|1|1| 3 |3|3|3|3|3|  7/5  [1,1,1,1,3] <- 3 Volt first time measured
|1|1|1|1|1|1|3| 3 |3|3|3|3|  9/5  [1,1,1,3,3]
|1|1|1|1|1|1|3|3| 3 |3|3|3| 11/5  [1,1,3,3,3]
|1|1|1|1|1|1|3|3|3| 3 |3|3| 13/5  [1,3,3,3,3]
|1|1|1|1|1|1|3|3|3|3| 3 |3| 15/5  [3,3,3,3,3] <- 3 Volt average

I would expect that my delay would be (M-1)/2 = (5-1)/2 = 2 measurements but I get my 3 volts in my moving average filter just after 4 more measurements (so delay M-1).

I would be very happy if someone could explain how I can determine my delay.

$\endgroup$

4 Answers 4

3
$\begingroup$

"Group delay" isn't the delay between the change on the input and the first effect; it's the delay that a packet of oscillations of different frequencies experience.

In the case of a linear phase filter (and your moving average, its impulse response being symmetric, is linear phase) that is the "center" of the effects of a single impulse. So, it's the center between you seeing the first effect of your input change, and the last effect.

$\endgroup$
0
$\begingroup$

If your window is realizable (causal, and it has to be as we cannot measure and use future values) you have to wait for whole window not the half of it.

$\endgroup$
3
  • 4
    $\begingroup$ but the group delay is half the window width. $\endgroup$ Aug 2, 2017 at 19:14
  • $\begingroup$ Is my delay the full window size M or M-1? I think it's M-1. $\endgroup$
    – ce_guy
    Aug 10, 2017 at 14:27
  • $\begingroup$ It is M-1, thinking the extremes may help time to time. Consider your filter being a dirac delta function (A window of size 1), it causes no delay. $\endgroup$
    – keoxkeox
    Aug 15, 2017 at 21:01
0
$\begingroup$

Here's another way to look at it:

group delay (what non-dsp people call "lag") is also the center of gravity (COG) of the weights of the average. For a Simple Moving Average (SMA), those weights look like a rectangle (length M, height 1/M), so the COG of the SMA is in the middle: that's why a 10-period SMA will have a 4.5 period lag (n-1)/2. Another possible moving average would be the Weighted Moving Average: in that case the weights look like a right triangle (length M, height M/sum(1:M) ). The horizontal coordinate of the COG of a triangle is at 2/3rd the base, so the lag (group delay) of a WMA is (n-1)/3 the length of the window it is calculated on.

$\endgroup$
0
$\begingroup$

I know this is an old question but after reading dozens of answers, it was still not crystal clear. So here is my attempt to make it so.

Let's say your moving average filter depth is 10, it means that the equivalent FIR filter will have 10 coefficients and be order 9.

There is a frequent confusion (and I must admit it was also my case) between the filter output delay and the group delay:

  • Output delay is the amount of time it takes for a change on the input to fully reflect on the output. It can also be expressed in samples and is equal to filter order (so 9 samples for my example).
  • Group delay is the time lag of the amplitude envelopes of the various sinusoidal components of the input signal through the filter. It can also be expressed in samples and is equal to half the filter order (so 4.5 samples for my example).

And because a good sketch is better than a long speech: Outputdelay vs. Group delay

Also please note that in the case of simple moving average, all of the coefficients of the impulse response are equal (to $\frac{1}{10}$ for my example) so the impulse response is symmetrical. This means that the filter have a linear phase and group delay will also be equal to phase delay.

$\endgroup$
1
  • $\begingroup$ Your last sentence is important, because your second statement about group delay (time-lag of sinusoidal components) is inaccurate, and only true for linear phase systems (where group delay equals phase delay). In general, the delay of a sinusoid is determined by the system's phase delay at the sinusoid's frequency, not by the group delay. $\endgroup$
    – Matt L.
    Dec 15, 2021 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.