# Errors when converting from digital filter object to transfer function

I am trying to use the designfilt tools in MATLAB to create a bandpass filter, but to be compatible with other code I need the resultant filter in transfer function coefficient form with B and A. To start with I took an example from the filter design gallery, and just changed the sampling rate to match that of my real data.

Fstop1 = 150;
Fpass1 = 200;
Fpass2 = 300;
Fstop2 = 350;
Astop1 = 65;
Apass  = 0.5;
Astop2 = 65;
Fs=16384;
% Fs=1000;

d = designfilt('bandpassiir', ...
'StopbandFrequency1',Fstop1,'PassbandFrequency1', Fpass1, ...
'PassbandFrequency2',Fpass2,'StopbandFrequency2', Fstop2, ...
'StopbandAttenuation1',Astop1,'PassbandRipple', Apass, ...
'StopbandAttenuation2',Astop2, ...
'DesignMethod','butter','SampleRate', Fs);

This filter seems to be behaving itself, isstable returns 1 and the $z$-plane seems acceptable:

However, when I try to get the coefficients using [B,A]=d.tf; the resultant filter is no longer stable and the $z$-plane looks totally different:

The values for B are incredibly small around e-26, so I am imaging that it might be to do with gains of the coefficients? If I set Fs back to the original $1\textrm{kHz}$, this two $z$-planes plots are the same.

Can someone help me with sorting this problem? Is the fact that the transfer function form is unstable speak to the stability of the filter when it is in the digitalfilter object form?

edit

The documentation for the butter function even describes these errors here

## 1 Answer

Generally you should never use the Transfer Function syntax. Look how your poles are closely placed next to the unit circle. With high order Butterworth filters you will see the effect of round-off numerical errors. What is the high order? Well 5 is already high and your filter has 30.

Instead you should use SOS or Zero-Pole-Gain representation. For example try to visualise the same filter but with SOS matrix:

fvtool(d.Coefficients) % or: zplane(sos2zp(d.Coefficients))

You will see that there are no visible numerical errors.

• Thanks very much, I guess its worth switching the other code to the other representations then! Its interesting that such differences can occur just by how you store the filter. Would they be exactly the same given enough precision in storing the coefficients? Jul 19 '16 at 10:09
• Given ridiculously good precision - yes, there would be a match.
– jojek
Jul 19 '16 at 15:51