[英]Vectorised `signal.lfilter`
I am trying to apply lfilter
on a collection of 1D arrays, ie on a 2D array which its rows correspond to different signals. 我试图将
lfilter
应用于一维数组的集合,即应用于其行对应于不同信号的二维数组。 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? 有人可以指导我吗?
So, you get the error on the line x=...
因此,您在
x=...
行上得到了错误x=...
The shapes of your parameters are numerator: (100,2), denominator: (100,3) and data: (100,10000). 参数的形状为分子:(100,2),分母:(100,3)和数据:(100,10000)。 The problem you are having is that lfilter expects to use the same filter for all items it processes, ie it only accepts 1-d vectors for the nominator and denominator.
您遇到的问题是lfilter期望对它处理的所有项目都使用相同的过滤器,即,它仅接受一维向量作为分母和分母。
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. 有关提升器期望的更多信息,请参见http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.lfilter.html 。
(But don't worry, the performance hit is minimal, most time is spent filtering anyway.) (但请放心,性能影响很小,无论如何,大多数时间都花在了过滤上。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.