# IIR Filter Difference Equation Implementation

Started by July 10, 2003
```Hi All,
I have a beginner-esqe question: all of my DSP literature shows the
IIR Difference Equation as:
y(n) = b(0)x(n) + b(1)x(n-1) + b(2)x(n-2) + ... + a(1)y(n-1) +
a(2)y(n-2) + ...

But then the C code implementations I see are something along the
lines:
FeedForwardVariable = (b[0] * x[n]) + (b[1] * x[n-1]) + (b[2] *
x[n-2]);
FeedBackwardVariable = (a[1] * y[n-1]) + (a[2] * y[n-2]) + (a[3] *
y[n-3]);
ResultVariable = FeedForwardVariable - FeedBackwardVariable;

The implementation uses a subtraction operation on the feed back
calculations that I don't see in the Difference Equation. Why is that?

I realize I'm setting myself up for a "Duh!!", but I'm really confused
why this is the case.

Thanks much,

Ben
```
```"Benjamin M. Stocks" <stocksb@ieee.org> wrote in message
> Hi All,
> I have a beginner-esqe question: all of my DSP literature shows the
> IIR Difference Equation as:
> y(n) = b(0)x(n) + b(1)x(n-1) + b(2)x(n-2) + ... + a(1)y(n-1) +
> a(2)y(n-2) + ...
>
> But then the C code implementations I see are something along the
> lines:
> FeedForwardVariable = (b[0] * x[n]) + (b[1] * x[n-1]) + (b[2] *
> x[n-2]);
> FeedBackwardVariable = (a[1] * y[n-1]) + (a[2] * y[n-2]) + (a[3] *
> y[n-3]);
> ResultVariable = FeedForwardVariable - FeedBackwardVariable;
>
> The implementation uses a subtraction operation on the feed back
> calculations that I don't see in the Difference Equation. Why is that?
>
> I realize I'm setting myself up for a "Duh!!", but I'm really confused
> why this is the case.
>
> Thanks much,
>
> Ben

y(n) = b(0)x(n) + b(1)x(n-1) + b(2)x(n-2) + ... - [a(1)y(n-1) + a(2)y(n-2) +
...]
n.b^
or

y(n) = b(0)x(n) + b(1)x(n-1) + b(2)x(n-2) + ... -a(1)y(n-1) -
a(2)y(n-2) - ...

is the normal expression for the difference equation.

Fred

```
```On Thu, 10 Jul 2003 13:25:13 -0700, Benjamin M. Stocks wrote:
> I have a beginner-esqe question: all of my DSP literature shows the
> IIR Difference Equation as:
> y(n) = b(0)x(n) + b(1)x(n-1) + b(2)x(n-2) + ... + a(1)y(n-1) +
> a(2)y(n-2) + ...
>
> But then the C code implementations I see are something along the
> lines:
> FeedForwardVariable = (b[0] * x[n]) + (b[1] * x[n-1]) + (b[2] *
> x[n-2]);
> FeedBackwardVariable = (a[1] * y[n-1]) + (a[2] * y[n-2]) + (a[3] *
> y[n-3]);
> ResultVariable = FeedForwardVariable - FeedBackwardVariable;
>
> The implementation uses a subtraction operation on the feed back
> calculations that I don't see in the Difference Equation. Why is that?
>
> I realize I'm setting myself up for a "Duh!!", but I'm really confused
> why this is the case.

That is not a dumb question at all; it causes a lot of confusion.

It has to do with a few different conventions.

Oppenheim and Schafer, and Lyons use the following:

y[n] = sum(k=0..M)(b[k]*x[n-k]) + sum(k=1..N)(a[k]*y[n-k])

sum(k=0..M)(b[k]*z^-k)
H(z) = --------------------------
1 - sum(k=1..N)(a[k]*z^-k)

Rabiner and Gold, Parkus and Burrus (*), and Proakis and Manolakis use the
following:

y[n] = sum(k=0..M)(b[k]*x[n-k]) - sum(k=1..N)(a[k]*y[n-k])

sum(k=0..M)(b[k]*z^-k)
H(z) = --------------------------
1 + sum(k=1..N)(a[k]*z^-k)

In addition, some sources use a[k] for the numerator and b[k] for the
denominator coefficients.  Also be warned, that some texts (like (*)
above), don't always normalize a[0] to 1.

They are all equivalent, except for the sign of the a[k].  With positive a[k]
you subtract, and with negative a[k] you add.  The moral of the story is
to be triple sure of what you have when designing and implementing IIR
filters, especially if you are using someone else's code.

--

```
```Matthew Donadio <m.p.donadio@ieee.org> wrote in message news:<pan.2003.07.10.20.38.48.728260@ieee.org>...
> On Thu, 10 Jul 2003 13:25:13 -0700, Benjamin M. Stocks wrote:
> > I have a beginner-esqe question: all of my DSP literature shows the
> > IIR Difference Equation as:
> > y(n) = b(0)x(n) + b(1)x(n-1) + b(2)x(n-2) + ... + a(1)y(n-1) +
> > a(2)y(n-2) + ...
> >
> > But then the C code implementations I see are something along the
> > lines:
> > FeedForwardVariable = (b[0] * x[n]) + (b[1] * x[n-1]) + (b[2] *
> > x[n-2]);
> > FeedBackwardVariable = (a[1] * y[n-1]) + (a[2] * y[n-2]) + (a[3] *
> > y[n-3]);
> > ResultVariable = FeedForwardVariable - FeedBackwardVariable;
> >
> > The implementation uses a subtraction operation on the feed back
> > calculations that I don't see in the Difference Equation. Why is that?
> >
> > I realize I'm setting myself up for a "Duh!!", but I'm really confused
> > why this is the case.
>
> That is not a dumb question at all; it causes a lot of confusion.
>
> It has to do with a few different conventions.
>
> Oppenheim and Schafer, and Lyons use the following:
>
> 	y[n] = sum(k=0..M)(b[k]*x[n-k]) + sum(k=1..N)(a[k]*y[n-k])
>
> 	         sum(k=0..M)(b[k]*z^-k)
> 	H(z) = --------------------------
> 	       1 - sum(k=1..N)(a[k]*z^-k)
>
> Rabiner and Gold, Parkus and Burrus (*), and Proakis and Manolakis use the
> following:
>
> 	y[n] = sum(k=0..M)(b[k]*x[n-k]) - sum(k=1..N)(a[k]*y[n-k])
>
> 	         sum(k=0..M)(b[k]*z^-k)
> 	H(z) = --------------------------
> 	       1 + sum(k=1..N)(a[k]*z^-k)
>
> In addition, some sources use a[k] for the numerator and b[k] for the
> denominator coefficients.  Also be warned, that some texts (like (*)
> above), don't always normalize a[0] to 1.
>
> They are all equivalent, except for the sign of the a[k].  With positive a[k]
> you subtract, and with negative a[k] you add.  The moral of the story is
> to be triple sure of what you have when designing and implementing IIR
> filters, especially if you are using someone else's code.

Thank you, I understand it much better now.

Regards,

Ben
```