问题描述
根据他们的 Matlab filter()和 SciPy lfilter(),就像它们应该是兼容的".但是我有一个问题,在Python中移植更大的Matlab代码,为此我得到了ValueError: object of too small depth for desired array
.由于我想不出如何不复杂地展示我的原始资料,我将使用Matlab文档中提供的示例:
According to their documentation for Matlab filter() and SciPy lfilter(), it seems like they should be "compatible". However I have a problem, porting larger Matlab code in Python, for which I get ValueError: object of too small depth for desired array
. As I can't think of how I can present my source without complicating it, I'll use the example provided in Matlab's documentation:
data = [1:0.2:4]';
windowSize = 5;
filter(ones(1,windowSize)/windowSize,1,data)
我在Python中将其翻译为:
which I translate in Python to:
import numpy as np
from scipy.signal import lfilter
data = np.arange(1, 4.1, 0.2)
windowSize = 5
lfilter(np.ones((1, windowSize)) / windowSize, 1, data)
在这种情况下,我得到:ValueError: object too deep for desired array
In this case I get:ValueError: object too deep for desired array
为什么会出现这些错误?
Why do I get these errors?
推荐答案
创建数组时是否有理由添加一个额外的维?这是您需要的吗?
Is there a reason you're adding a an extra dimension when creating your array of ones? Is this what you need:
lfilter(np.ones(windowSize) / windowSize, 1, data)
这篇关于具有SciPy lfilter()的Matlab filter()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!