Given a discretely sampled sinusoidal signal (frequency $1/40$), and 2 Simple Moving Averages (SMA), better known as FIR low-pass filters of order e.g. $10$ and $21$, it can easily be calculated what these filter's gain will be, using this formula: with $n$ the window length of the SMA, and $p$ the period of the sine wave:

$sin (n * pi/p) / (n * sin(pi/p)) $

(thanks to this post): I refer to the dotted blue and green lines in the image below: difference of 2 simple moving averages

Now, is it possible to calculate this directly (without measuring) for a difference of 2 SMA's? Or more broadly: if the coefficients / weights of a filter (in this case the difference of 2 SMA's) are known, can you calculate the change in amplitude directly?

filter weights diff 2 SMA's


1 Answer 1


Of course.

The difference is linear operation so you simply get $$H(z) = H_1(z)-H_2(z)$$

The Z transform of a moving average filter of length N is simply $H_N(z) = \frac{1-z^{-N}}{1-z^{-1}}$ so in your case you get

$H(z) = \frac{1-z^{-10}}{1-z^{-1}}-\frac{1-z^{-21}}{1-z^{-1}}$

Pop in $z = e^{-j\omega}$ for your frequency of interest and solve for amplitude and phase shift.

Please note that this is dependent in how you time align the two moving average windows. The formula above assumes the both are aligned at the beginning. If you want to align them at the center you need apply a delay term to the shorter one as well. In your case you can't really do this since one window size is even and the other is odd.

Update based on Dan's comment

We can easily continue $H(z) = \frac{1-z^{-10}}{1-z^{-1}}-\frac{1-z^{-21}}{1-z^{-1}} = \frac{-z^{-10}+z^{-21}}{1-z^{-1}} = -z^{-10} \cdot \frac{1-z^{-11}}{1-z^{-1}}$

This is a moving average filter of length 11 cascaded with a 10 tap sample delay and multiplied with -1. That's also obvious if you subtract the impulse responses in the time domain: the first 10 samples cancel and you are left with the last 11 samples of the 21 tap filter.

  • $\begingroup$ Both SMA's are causal, non-centered filters; I picked 10 and 21 because -visually- the difference seems to be +/- in phase with the sinusoid: seemed an interesting property.. $\endgroup$
    – MisterH
    Jun 2, 2021 at 17:18
  • $\begingroup$ That really depends in the frequency: phase shift is simply $-j \omega \cdot N/2$ $\endgroup$
    – Hilmar
    Jun 2, 2021 at 20:45
  • $\begingroup$ @Hilmar it’s actually an interesting result if you combine your fractions and then pull out a $z^{-10}$ - if I did it right it says that it is equivalent to a 10 sample delay cascaded with an 11 sample moving average ... the math all makes sense but the fact that it comes out to that is interesting and wasn’t immediately apparent until I look at the block diagrams closer. The smaller moving average gets subtracted from the longer one making it the same as a shorter moving average with added delay. $\endgroup$ Jun 2, 2021 at 21:42
  • $\begingroup$ @DanBoschen: Nice. I didn't see that either but in hindsight it's obvious. Well, at least my hindsight is 20/20 :-) $\endgroup$
    – Hilmar
    Jun 2, 2021 at 23:34
  • $\begingroup$ Exactly! Nothing at all that came to mind when presented the idea of differencing two moving averages but obvious after the fact- the nice way you laid out the formula made that all clear. I find it quite interesting. $\endgroup$ Jun 2, 2021 at 23:35

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.