python - Matlab 过滤器与 Python lfilter ValueError 对象深度太小

标签 python matlab scipy filtering

受 Matlab 代码的启发,我想在 Python 中过滤信号。 Matlab有函数filter ,应该类似于 scipy.signal.lfilter (来自问题:Matlab filter() with SciPy lfilter())。但是,我仍然收到一个ValueError:所需数组深度太小的对象

Matlab代码(在Octave中执行):

% Matlab
x = [1.0485e-04  -2.4193e-04  -3.0078e-04  1.5750e-03  -1.9698e-03  1.3902e-04  2.7568e-03  -3.8059e-03  2.0123e-03  3.3257e-03]
xfilt = filter(1, [1 -0.992217938], x);
disp(xfilt);

% output
1.0485e-04  -1.3790e-04  -4.3760e-04   1.1408e-03  -8.3788e-04  -6.9233e-04   2.0699e-03  -1.7522e-03   2.7378e-04   3.5974e-03

Python:

# Python
from scipy.signal import lfilter

x = np.array([1.0485e-04, -2.4193e-04, -3.0078e-04, 1.5750e-03, -1.9698e-03, 1.3902e-04, 2.7568e-03, -3.8059e-03, 2.0123e-03, 3.3257e-03])
lfilter(1, np.array([1, -0.992217938]), x, axis=0)

这会导致错误:

ValueError                                Traceback (most recent call last)
<ipython-input-87-d5c23d362b45> in <module>
      1 x = np.array([1.0485e-04, -2.4193e-04, -3.0078e-04, 1.5750e-03, -1.9698e-03, 1.3902e-04, 2.7568e-03, -3.8059e-03, 2.0123e-03, 3.3257e-03])
----> 2 print(lfilter(1, np.array([1, -0.992217938]), x, axis=0))

~/anaconda3/envs/*env*/lib/python3.6/site-packages/scipy/signal/signaltools.py in lfilter(b, a, x, axis, zi)
   1378     else:
   1379         if zi is None:
-> 1380             return sigtools._linear_filter(b, a, x, axis)
   1381         else:
   1382             return sigtools._linear_filter(b, a, x, axis, zi)

ValueError: object of too small depth for desired array

系统

  • Python:3.6.8
  • Scipy:1.2.0

尝试过

基于问题“Matlab filter not compatible with Python lfilter ”,我尝试将 axis=0 添加到 lfilter,但仍然收到 ValueError。

问题

如何用Python编写Matlab代码?

最佳答案

SciPy 的 lfilter 期望 b 参数是一维数组(或“类数组”,例如列表),而不是标量。例如,

lfilter([1], np.array([1, -0.992217938]), x, axis=0)

关于python - Matlab 过滤器与 Python lfilter ValueError 对象深度太小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54860107/

相关文章:

python - 如何使用 Python 输出仅证书 PKCS#7

python - 在迭代过程中删除行范围

python - 用红色或黑色像素替换2D矩阵值

python - 从向量中减去 scipy.sparse 矩阵的列

python - 函数内部多处理 scipy 优化的奇怪行为

python - 将交互式 Jupyter Notebook 导出到 html

python - Matplotlib 中 x 轴上的时间格式

matlab - 将 gabor 过滤器应用于图像

c - 如何在 linux KNN CUDA 上编译?

matlab - 为什么 Matlab 只接受脚本文件名中的一小部分字符?