# Deriving the difference equation of the filter

Here I have a diagram (at the bottom) and I am trying to create a difference equation for the filter.

I've been using practice examples to try figure this one out however I am unable to produce a complete difference equation.

For example:

      x[n] ---| A |---+---| B |--- y[n]
z^-1  |   z^-1
+---| C |
z^-1


where $$x \left[ n \right]$$ is the input signal, $$y \left[ n \right]$$ is the output signal, and $$A$$, $$B$$, and $$C$$ are blocks with transfer functions:

$$A$$: $$H_{1} \left( z \right) = \frac{1 + 2 z^{-1} + z^{-2}}{1 + 0.5 z^{-1}}$$
$$B$$: $$H_{2} \left( z \right) = \frac{1 - z^{-1}}{1 - 0.8 z^{-1}}$$
$$C$$: $$H_{3} \left( z \right) = \frac{1 + z^{-1}}{1 + 0.9 z^{-1} + 0.2 z^{-2}}$$

Block $$A$$: input = $$x \left[ n \right]$$, output = $$a \left[ n \right]$$
Block $$B$$: input = $$a \left[ n - 1 \right]$$, output = $$b \left[ n \right]$$
Block $$C$$: input = $$b \left[ n - 1 \right]$$, output = $$y \left[ n \right]$$\

Step 3: Write down the equations for each block.\

Block $$A$$: $$a \left[ n \right] = H_{1} \left( z \right) x \left[ n \right] + 0.5 a \left[ n - 1 \right] - 0.5 a \left[ n - 2 \right] - H_{1} \left( z \right) z^{-1} x \left[ n \right]$$
Block $$B$$: $$b \left[ n \right] = H_{2} \left( z \right) a \left[ n - 1 \right] + 0.8 b\left[ n - 1 \right] - H_{2} \left( z \right) z^{-1} a \left[ n - 1 \right]$$
Block $$C$$: $$y \left[ n \right] = H_{3} \left( z \right) b \left[ n - 1 \right] - 0.9 y \left[ n - 1 \right] - 0.2 y \left[ n - 2 \right] + H_{3} \left( z \right) z^{-1} b \left[ n - 1 \right]$$

Step 4: Combine the equations for all blocks to obtain the overall system equation.

Substituting the equation for $$a \left[ n \right]$$ into the equation for $$b \left[ n \right]$$, and the equation for $$b \left[ n \right]$$ into the equation for $$y \left[ n \right]$$, we get:

$$y \left[ n \right] = H_{3} \left( z \right) H_{2} \left( z \right) \left( H_{1} \left( z \right) x \left[ n \right] + 0.5 H_{1} \left( z \right) z^{-1} x \left[ n - 1 \right] - 0.5 H_{1} \left( z \right) z^{-2} x \left[ n - 2 \right] - 0.8 a \left[ n - 1 \right] + H_{2} \left( z \right) z^{-1} a \left[ n - 1 \right] \right) - 0.9 y \left[ n - 1 \right] - 0.2 y \left[ n - 2 \right]$$

Step 5: Use the $$\mathcal{Z}$$-transform to convert the system equation into a difference equation.

etc.. This is how I would usually approach this type of question; However I can't quite figure this one out. Would someone be able to give me the answer so I can reverse engineer this or at least put me along the right track!

This is a classic biquad difference equation. No need to go to or from the $$\mathcal{Z}$$-domain:
$$y[n] = a_0x[n] + a_1x[n-1] + a_2x[n-2] + b_1y[n-1] + b_2y[n-2]$$ usually written with input on one side and output on the other: $$y[n] - b_1y[n-1] - b_2y[n-2] = a_0x[n] + a_1x[n-1] + a_2x[n-2]$$ The associated transfer function is $$H(z) = \frac{a_0 + a_1z^{-1} + a_2z^{-2}}{1 + b_1z^{-1} + b_2z^{-2}}$$