theta
theta

Reputation: 25601

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?

Upvotes: 11

Views: 17792

Answers (1)

Bi Rico
Bi Rico

Reputation: 25813

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)

Upvotes: 8

Related Questions