矢量化`信号滤波器`

2024-09-28 21:26:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将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)

我得到了以下错误:

^{pr2}$

有人能引导我吗?在


Tags: ofthefromimportsizesignalnprandom
1条回答
网友
1楼 · 发布于 2024-09-28 21:26:17

所以,在x=...行得到错误

参数的形状是分子:(100,2)、分母:(100,3)和数据:(10010000)。你遇到的问题是lfilter期望对它处理的所有项目使用相同的过滤器,也就是说,它只接受1-d向量作为命名器和分母。在

看来你真的需要把它变成一个沿着行的循环。像这样:

# 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以了解有关提升者期望的更多信息。在

(但别担心,性能损失很小,大部分时间都花在过滤上了。)

相关问题 更多 >