我正在尝试将lfilter应用于一维数组的集合,即应用于其行对应于不同信号的2D数组。这是代码:

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)


我收到以下错误:

ValueError: object too deep for desired array


有人可以指导我吗?

最佳答案

因此,您在x=...行上收到错误

参数的形状为分子:(100,2),分母:(100,3)和数据:(100,10000)。您遇到的问题是lfilter期望对它处理的所有项目都使用相同的过滤器,即它仅接受一维向量作为分母和分母。

看来您确实需要将其变成沿行的循环。像这样:

# 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])] )


有关提升器期望的更多信息,请参见http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.lfilter.html

(但请放心,性能影响很小,无论如何,大多数时间都花在了过滤上。)

10-07 18:15