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

braingateway braingateway@gmail....
Thu Nov 18 19:12:18 CST 2010


braingateway :
> gwenael.guillaume@aliceadsl.fr :
>> 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?
>>   
> No, there is only naive implementation of filter(). As I know, there 
> is no SOS convertion exists. The Scipy.signal is only for very very 
> simple signal processing. In this state, you either implement your own 
> SOS system convertion code and then use concatenated Scipy.lfilter() 
> call to achieve your go, or you stick to matlab.
To make it more clear, if you have 3 SO block, then you need to call 3 
times of lfilter() *gain
>
> LittleBigBrain
>> Thanks
>>
>> Gwenaël
>>
>>
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>   
>



More information about the SciPy-User mailing list