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?
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With