Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 974
theta Avatar asked Jan 19 '12 07:01

theta


People also ask

What is Lfilter in Scipy?

lfilter does apply given filter and in Fourier space this is like applying filter transfer function ONCE. filtfilt apply the same filter twice and effect is like applying filter transfer function SQUARED. In case of Butterworth filter ( scipy. signal. butter ) with the transfer function.

How do you filter a signal in Python?

Well for starters, to filter a signal, we can simply take the impulse response of that filter and convolve it with the signal. FIR filtering is simply a convolution operation. It might be confusing because earlier we mentioned that convolution takes in two signals and outputs one.

What is Scipy signal?

signal) The signal processing toolbox currently contains some filtering functions, a limited set of filter design tools, and a few B-spline interpolation algorithms for one- and two-dimensional data.


1 Answers

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)
like image 75
Bi Rico Avatar answered Sep 25 '22 08:09

Bi Rico