[SciPy-dev] reimplementation of lfilter

Sturla Molden sturla@molden...
Wed Sep 23 01:10:39 CDT 2009

Sturla Molden skrev:
> Well, I changed all the code to C++, to use the std::complex type, 
> std::vector instead of malloc, and templates for specializing the 
> filter functions to all dtypes supported by lfilter. I'm quite happy 
> with the way the C++ looks :-) 

I forgot to add support for the object dtype.

This also fixes a bug in the current signal.lfilter. This will crash the 

   import numpy as np
   import scipy
   from scipy.signal import butter, lfilter
   b,a = butter(4, .25)
   x = np.random.randn(1000000).astype(object).reshape((1000,1000))
   fx = lfilter(b,a,x, axis=1)

The C++ version here does not crash on this. :-)

I just ripped some code from current lfilter and cleaned it up slightly; 
I hope that is ok. (Cython was not happy about "cdef object *ptr", so I 
reluctantly had to put it all in C++.)  The reason my code does not 
crash like current lfilter, is copy-in copy-out for strided vectors. It 
seems lfilter messes up the strides and then segfaults.

<type 'object'>
axis=0, shape=(1000000,), speed: 102
axis=0, shape=(1000, 1000), speed: 105
axis=1, shape=(1000, 1000), scipy.signal.lfilter crashes the interpreter!!!

Building on Windows:

g++ -c -O3 -ffast-math -msse3 -march=core2 -Ic:/Python26/include 
-Ic:/Python26/Lib/site-packages/numpy/core/include _linear_filter.cpp
cython.py --cplus linear_filter.pyx
g++ -c -O2 -ffast-math -msse3 -march=core2 -Ic:/Python26/include 
-Ic:/Python26/Lib/site-packages/numpy/core/include linear_filter.cpp
g++ -shared -o linear_filter.pyd -Lc:/Python26/libs _linear_filter.o 
linear_filter.o -lpython26 -lmsvcr90

Sturla Molden

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: _linear_filter.cpp
Url: http://mail.scipy.org/pipermail/scipy-dev/attachments/20090923/1811e1ff/attachment-0002.pl 
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: linear_filter.pyx
Url: http://mail.scipy.org/pipermail/scipy-dev/attachments/20090923/1811e1ff/attachment-0003.pl 

More information about the Scipy-dev mailing list