# Why is the gain of my IIR filter positive?

Sorry, two questions in one day!

I'm struggling to understand what I'm doing wrong in this very simple filter design. I want to design a simple, single pole low pass filter and implement it as an IIR in an embedded system.

I was looking at the DSP book online at recursive filters, and using their single-pole example to get started: http://www.dspguide.com/ch19/1.htm

According to the next page the coefficients I should need for the IIR single pole low pass filter are: $a_0=1-x$ and $b_1=x$

In order to get $x$ they give the equation (19-5) of $x=\epsilon^{-2\pi f_c}$

Given an fc of 0.125, I calculate x = 0.455938. Is this correct?

An fc of 0.125 at a sampling rate of 8kHz is 1kHz.

I don't see why when I try to get the frequency response of this filter I end up with entirely positive gain, as shown below:

Here is the code I've been using:

%Start from nothing!
clear;

% Set the sampling frequency used by our digital filtering system:
fs=8000;

% Set the coefficients up (As already worked out!) for a single-pole low pass
% filter. This should give us -3dB @ 1kHz with -6dB/Octave roll off
a = [ 0.544062 ];
b = [ 1, 0.45594 ];

% Determine the frequency response of the filter design above. Get the output
% in frequency rather than rad/s. Use 64 plot points.
[H,f] = freqz(b, a, 64, fs);

% Show our cutoff frequency
cutoff = -3 * ones(64);

% Plot the result so that we can see if it is correct:
figure(1);
plot(f, 20*log10(abs(H)), f, cutoff);
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');


I'm again, just stumped as to what's going on, and don't know how to solve the issue.

• You have your a and b vectors reversed. For a first-order IIR filter, the a vector should have two elements (and you almost always want the first element to be unity). So, you're plotting the frequency response of the filter with difference equation $0.544062y[n] = x[n] + 0.45594 x[n-1]$, which is not what you want. – Jason R Jan 9 '13 at 16:11

You have your a and b vectors reversed. For a first-order IIR filter, the a vector should have two elements (and you almost always want the first element to be unity). So, you're plotting the frequency response of the filter with difference equation $0.544062y[n]=x[n]+0.45594x[n−1]$, which is not what you want.

Also, as a note on MATLAB syntax, you'll also need to negate the second element of a (multiply it by -1) in order to get what you want. Read the documentation on functions that take IIR filter coefficients to learn why.

• Hi Jason, thanks very much for the reply. Looks like I need to read a lot more! I figured a and b were a convention that would be used the same in MATLAB as in the DSP book that was guiding me. I guess not. I don't suppose you can link to any documentation can you? I'm using GNU Octave and I can't see any documentation that has any information regarding anything special about the coefficients used. Thank-you again. – Brian Sidebotham Jan 9 '13 at 21:26

Yep, a and b reversed, AND x has the wrong sign. What you want is a=[1 -x]; b=1-x; The way that x is calculated seems also a bit off. A "real" low pass typically 0 gain at the Nyquist frequency. Taking into a account the bilinear transform you would want something like this

x = tan(fc*pi); a=[1 -x]; b=[(1-x) (1-x)]/2;

• Hi Hilmar, thanks for the reponse. I was trying to get a better grasp of IIR filters and so tried to step through the book recreating the (what appeared to be anyway) simplest examples I could to start off to check that I was doing the right things with GNU Octave. It seems like it's more of a minefield than I thought. Time to select a different DSP book. Thank-you very much for your help. – Brian Sidebotham Jan 9 '13 at 21:30
• Hilmar, the filter given by the linked page seems to be designed by impulse invariance rather than bilinear transform. Is that inherently a wrong design method? – The Photon Jun 21 '13 at 16:11

Brian, I came across your post on the Valvers site when I was trying to learn about designing filters in Octave, so thanks for putting that up. The comment submission part of the page is broken though; thats not on purpose is it?

Anyway, the second filter (Hilmar's), is also what you get if you use Octave's Butterworth design routine (in package 'signal') to make a first order low pass with the values you had in for Fc and Fs:

octave-3.6.4:1467> [bb2 ba2] = butter(1,1000/4000)
bb2 =
0.29289   0.29289
ba2 =
1.00000  -0.41421


I've spent some time recently figuring out how to make different kind of digital filters in Octave, for implementation on an LPC cortex M3 microcontroller. I posted some notes here:

http://tooling-up.blogspot.com/2013/06/signal-acquisition-filtering-on-simple.html

• Hi Holly, Sorry for the late reply! That looks spot on. Thanks for putting up the link to the cortex M3 work. I'm using a few M0's and M3's at the moment so that's really useful. I'm not sure why the comment submission on valvers was broken. It seems okay at the moment. – Brian Sidebotham Aug 7 '13 at 15:35