[SciPy-User] scipy.signal.butter function different from Matlab

gwenael.guillaume@alicead... gwenael.guillaume@alicead...
Thu Nov 18 04:55:43 CST 2010


Hi,

I'm trying to rewrite a Matlab code in Python and I encounter some difficulties
using the butter function.

In Matlab, my code is:

% Parameters:
N=5;
F_inf=88.38834764831843;
F_sup=111.3623397675424;
Fs=32768.00013421773;

% Filter coefficients computing:
[z,p,k]=butter(N,[F_inf F_sup]/(Fs/2));

% Result:
z=[1;1;1;1;1;-1;-1;-1;-1;-1;]
p=[0.999020109086358+0.021203989980732i;...
   0.999020109086358-0.021203989980732i;...
   0.99789313059316+0.020239448648803i;...
   0.99789313059316-0.020239448648803i;...
   0.99762168426512+0.018853164086747i;...
   0.99762168426512-0.018853164086747i;...
   0.998184731408311+0.017659802911797i;...
   0.998184731408311-0.017659802911797i;...
   0.999249126282689+0.017020854458606i;...
   0.999249126282689-0.017020854458606i;]
k=5.147424357763888e-014

In Python, the code is quite similar:
import numpy as np
import scipy.signal as sig

# Parameters:
N=5
F_inf=88.38834764831843
F_sup=111.3623397675424
Fs=32768.00013421773

# Filter coefficients computing:
z,p,k=sig.butter(N,np.array([freqmin,freqmax])/(Fs/2),output='zpk')

# Result:
 Error message:
C:\Python26\lib\site-packages\scipy\signal\filter_design.py:221:
BadCoefficients: Badly conditionned filter coefficients (numerator): the results
may be meaningless
  "results may be meaningless", BadCoefficients)

z=array([], dtype=float64)
p=array([
0.99464737+0.01603399j,0.99464737-0.01603399j,0.98633302+0.00982676j,0.98633302-0.00982676j,0.98319371+0.j])
k=4.2522321460489923e-11

Does anyone could help me to understand why the results are not the same?

I have also another question. In Matlab, after having computed the filter
coefficients, I apply the filtering to my signal X using the following code:

% To avoid round-off errors, do not use the transfer function.  Instead
% get the zpk representation and convert it to second-order sections.
[sos_var,g]=zp2sos(z, p, k);
Hd=dfilt.df2sos(sos_var, g);

% Signal filtering
y = filter(Hd,X);

Is there any Python functions that could do the same?

Thanks

Gwenaël




More information about the SciPy-User mailing list