Matlab filter() with SciPy lfilter()

13,282

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)
Share:
13,282
theta
Author by

theta

Updated on August 02, 2022

Comments

  • theta
    theta over 1 year

    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?

  • theta
    theta about 12 years
    There is no reason, just my ignorance :) I didn't spotted that while translating. Thanks
  • Bi Rico
    Bi Rico about 12 years
    ah yes I remember now, in matlab ones(10) returns a 10 by 10, oh those good old matlab days.