[SciPy-user] newbie trouble with lfilter

Joseph Anderson J.Anderson@hull.ac...
Thu Jan 17 06:30:11 CST 2008


Hi All,

I'm new to both python and scipy, so it could be that I'm just not seeing what I'm doing wrong here.

I'm wanting to use lfilter by setting the initial conditions (zi), and filter multi-dimensional arrays. The notion for this is so that I can repeatedly call a filter on blocks of samples pulled out of a large file--too big to load into a single array in memory. I can get it to work for the case of a one dimensional array, but not managing to put the zi array in the correct form for anything else.

Here's my code that works for a 1-d array:

# **************************************
# test filter: 1-d array
# 
# **************************************
from numpy import *
from numpy.random import *
from scipy.signal import *

# order and cutoff
N = 1
freq = 0.1

# signal, single channel
x = uniform(-1., 1., 8)

# design filter
b, a = butter(N, freq)

# set initial condition
zi = zeros(N)

# filter
y, zf = lfilter(b, a, x, 0, zi)

print "b, a:", b, a
print "zi, zf:", zi, zf
print "x:", x
print "y:", y



Here's code that doesn't work for a 2-d array:

# **************************************
# test filter: 2-d array
# zi = zeros(N * 2).reshape(2, N)
#
# **************************************
from numpy import *
from numpy.random import *
from scipy.signal import *

# order and cutoff
N = 1
freq = 0.1

# signal, two channels
x = array([uniform(-1., 1., 8), zeros(8)]).transpose()

# design filter
b, a = butter(N, freq)

# set initial condition, need N * channels zeros
zi = zeros(N * 2).reshape(2, N)

# filter
y, zf = lfilter(b, a, x, 0, zi)

print "b, a:", b, a
print "zi, zf:", zi, zf
print "x:", x
print "y:", y


The error I get is:

>>> Traceback (most recent call last):
  File "/var/folders/BZ/BZCZyDCzGYio72sPH9CkP++++TM/-Tmp-/py275R3I", line 58, in <module>
    y, zf = lfilter(b, a, x, 0, zi)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/scipy/signal/signaltools.py", line 495, in lfilter
    return sigtools._linear_filter(b, a, x, axis, zi)
ValueError: The number of initial conditions must be max([len(a),len(b)]) - 1


Trying something different and changing zi to:

zi = zeros(N * 2).reshape(N, 2)

Results in a crash with the message:

Process Python segmentation fault



Attempting a slightly different approach, I tried using lfiltic with the 2-dim case, to see what form zi is returned. Here's that code:


# **************************************
# test filter: 2-d array
# find zi with lfiltic
#
# **************************************
from numpy import *
from numpy.random import *
from scipy.signal import *

# order and cutoff
N = 1
freq = 0.1

# signal, two channels
x = array([uniform(-1., 1., 8), zeros(8)]).transpose()

# design filter
b, a = butter(N, freq)

# filter
y = lfilter(b, a, x, 0)

# # set initial condition, need N * channels
zi = empty(N * 2).reshape(2, N)

# find zi from initial conditions
zi = lfiltic(b, a, y, x)

print "b, a:", b, a
print "zi:", zi
print "x:", x
print "y:", y


Running results in:

>>> Traceback (most recent call last):
  File "/var/folders/BZ/BZCZyDCzGYio72sPH9CkP++++TM/-Tmp-/py275Uzp", line 133, in <module>
    zi = lfiltic(b, a, y, x)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/scipy/signal/signaltools.py", line 534, in lfiltic
    zi[m] = sum(b[m+1:]*x[:M-m],axis=0)
ValueError: setting an array element with a sequence.


Swapping the zi initialization for:

zi = empty(N * 2).reshape(N, 2)

results in the same error.


My system is a G5 running Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04) [GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin. Numpy version is 1.0.4.dev3884.Scipy version is 0.5.3.dev3165.


As you can likely imaging, I can take the approach to split all my 'interleaved' 2-dimensional arrays into 1-d arrays and successfully set zi and access the final (zf) state from lfilter. However, as lfilter is happy to filter 2-dim arrays, would like to keep my own code simple by using that feature. It's just that I'm not managing to initially set up zi correctly

Thanks in advance for your help.



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dr Joseph Anderson
Lecturer in Music

School of Arts and New Media
University of Hull, Scarborough Campus,
Scarborough, North Yorkshire, YO11 3AZ, UK

T: +44.(0)1723.357341 T: +44.(0)1723.357370 F: +44.(0)1723.350815
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: not available
Url: http://projects.scipy.org/pipermail/scipy-user/attachments/20080117/6c8c83c7/attachment.pl 


More information about the SciPy-user mailing list