Cupitor
Cupitor

Reputation: 11637

Vectorised `signal.lfilter`

I am trying to apply lfilter on a collection of 1D arrays, i.e. on a 2D array which its rows correspond to different signals. This is the code:

import numpy as np
from scipy import signal
from scipy import stats

sysdim=2 #dimension of filter, i.e. the amount that it depends on the past
ksim=100 #number of different singals to be filtered
x_size=10000
# A and C are 
A=np.random.randn(sysdim*ksim).reshape((ksim,sysdim))
B=np.random.randn(sysdim*ksim).reshape((ksim,sysdim))
C=2.0*np.random.randn(sysdim*ksim).reshape((ksim,sysdim))
D=2.0*np.random.randn(sysdim*ksim).reshape((ksim,sysdim))
print A.shape,np.random.randn(x_size*ksim).reshape((ksim,x_size)).shape
x=signal.lfilter(A,np.hstack((np.ones((ksim,1)),C)),np.random.randn(x_size*ksim).reshape((ksim,x_size)),axis=1)
y=signal.lfilter(B,np.hstack((np.ones((ksim,1)),D)),x,axis=1)

And I am getting the following error:

ValueError: object too deep for desired array 

Can somebody guide me please?

Upvotes: 2

Views: 678

Answers (1)

DrV
DrV

Reputation: 23510

So, you get the error on the line x=...

The shapes of your parameters are numerator: (100,2), denominator: (100,3) and data: (100,10000). The problem you are having is that lfilter expects to use the same filter for all items it processes, i.e. it only accepts 1-d vectors for the nominator and denominator.

It seems that you really need to turn that into a loop along the rows. Something like this:

# denom_array: R different denominators in an array with R rows
# numer_array: R different numerators in an array with R rows
# data: R data vectors in an array with R rows
# out_sig: output signal
out_sig = array([ scipy.signal.lfilter(denom_array[n], numer_array[n], data[n]) for n in range(data.shape[0])] ) 

See http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.lfilter.html for more information on what lifter expects.

(But don't worry, the performance hit is minimal, most time is spent filtering anyway.)

Upvotes: 1

Related Questions