简体   繁体   中英

Matlab filter() with SciPy lfilter()

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)

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM