# Moving average filter using DF-I and DF-II

I understand that the moving average filter is simply the average of a number of points from the input signal to produce the output signal. If x[] is the input and y[] is the output and M is the number of points, then the filter is just $$y[i]=\frac{1}{M}\sum_{j=0}^{M-1}x\left[ i+j \right]$$

This can be done through many ways, but I want to ask about using Direct Form I and Direct Form II. How do I follow the DF-I and DF-II structures to create a moving average filter? Don't I need a two separate multipliers like $$b_{M}$$ and $$a_{N}$$ to use the DF-I and DF-II, which moving average filters don't necessarily have? How about if I want to use DF-I and DF-II to create an autoregressive filter? Isn't it even more incompatible with DF-I and DF-II?

• Do you need to implement it using the recursive formula, or like a basic FIR? Becauase there's a bit of mismatch between the formula you're showing (+ text) and the pictures. Commented May 20, 2021 at 8:31
• @aconcernedcitizen sorry for the confusion. The image is just to illustrate the multipliers $$a$$ and $$b$$, which I cannot seem to connect with a moving average filter, or even the autoregressive filter. The images doesn't need to be necessarily used. To be clear, I am asking about implementing a moving average filter or any other filter using DF-I and DF-II. How exactly do I use DF-I and DF-II to create such filters? And why would they be different if I'm just adding averages for the filters? Thanks for the clarification. Commented May 20, 2021 at 8:54

To start, notice that the diagrams are in the $$\mathcal{Z}$$ domain, multiplying by $$z^{-1}$$ is the delay of one sample.

## DF-I

Writing the equation for $$y_n$$ of the DF-I

$$y_n = \sum_{k=0}^{M} b_k x_{n-k} - \sum_{l=1}^{N} a_{l} y_{n-l}$$

Comparing with the equation of the moving average

$$y_n = \sum_{k=0}^{M} \frac{1}{M+1} x_{n-k}$$

we see that we could realise the moving average by settings $$b_k=1/(M+1)$$, and $$N$$ set to zero.

## DF-II

For the DF-II realization you the sequences $$a$$ and $$b$$ must have the same size, so what we can do is to set $$a_l=0$$, and $$b_k = 1/(M+1)$$.

If you choose to compute a moving sum then $$b_k=1$$ and you can save the multiplications, the moving average still can be computed by a gain, thus, performing one multiplication per sample instead of $$M+1$$ multiplications per sample.

Don't I need a two separate multipliers ?

No. The moving average is an FIR filter hence you have $$a_k = 0, k > 0$$

DF1 and DF2 are for generic IIR filters. An FIR filter is a subset of an IIR filter, so technically you can implement it as DF1 or DF2, but there is really no reason to do that way since it's inefficient as compared to the alternatives.

One of the more efficient ways to do moving average is

$$y[n] = y[n-1]+\frac{1}{M}(x[n]-x[n-M))$$

That is now looks like IIR filter but still has a finite response since it includes pole cancellation.

• This! You can even "draw" your $y[n]$ as both forms, and it will be rather boring. Boring is good! Commented May 21, 2021 at 9:19