Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Ehanced scipy.signal.filtfilt #29

wants to merge 4 commits into


None yet
2 participants

WarrenWeckesser commented Jun 4, 2011

  • Rewrote much of lfilter_zi, and added a docstring that explains what it does.
  • Enhanced filtfilt so that it can filter a given axis in an n-dimensional array, and also allow more flexibility in how the data is extended before filtering.
  • Added the module array_tools.py, which provides the convenient functions axis_slice and axis_reverse, and the axis extension functions odd_ext, even_ext and const_ext.
  • Changes to filtfilt also fix bug #1410.

WarrenWeckesser added some commits Jun 3, 2011

ENH+BUG: Rewrote lfilter_zi and filtfilt; enhanced filtfilt to accept…
… an axis keyword, and allow more flexibility in the choice of extensions of the data; added array_tools module; fixed ticket #1410

rgommers commented Jun 4, 2011

This look good to me. Here are a few comments.


  • why remove the reference?
  • docstring needs Returns, and Notes/Examples are in the wrong order


  • to allow the string 'none' as alternative to None seems unnecessary

  • the docstring still needs a Returns section, and the unit test in the last
    commit would also make a good example

  • to check this works for x.ndim > 1 along an axis I tried the following (could
    be good to add as a test, without the plot):

    x2 = np.vstack([x, x + np.cos(25_np.pi_t)])
    y2 = filtfilt(b, a, x2, axis=1, padlen=n, padtype='odd')
    plt.plot(y2.flatten(), color='r', lw=2)

and to show what the different padtype options do (maybe for tutorial?):

y_odd = filtfilt(b, a, x, padlen=n, padtype='odd')
y_even = filtfilt(b, a, x, padlen=n, padtype='even')
y_const = filtfilt(b, a, x, padlen=n, padtype='constant')
y_none = filtfilt(b, a, x, padlen=n, padtype=None)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(t, y_odd, 'b-', label='odd')
ax.plot(t, y_even, 'r-', label='even')
ax.plot(t, y_const, 'k-', label='constant')
ax.plot(t, y_none, 'g-', label='None')
ax.set_title('Responses for padtypes')
ax.set_ylabel('filtered response')

ax2 = fig.add_axes([0.55, 0.6, 0.3, 0.24])
tnum = 60
ax2.plot(t[-tnum:], y_odd[-tnum:], 'b-', label='odd')
ax2.plot(t[-tnum:], y_even[-tnum:], 'r-', label='even')
ax2.plot(t[-tnum:], y_const[-tnum:], 'k-', label='constant')
ax2.plot(t[-tnum:], y_none[-tnum:], 'g-', label='None')
tmin = t[-tnum]
tmax = t[-1]
ax2.set_xticks([tmin, tmax])
ax2.set_xticklabels(['%1.2f' % num for num in [tmin, tmax]])
ax2.set_xlim((t[-tnum]-5e-3, t[-1]+5e-3))



  • would be good to rename to _arraytools.py because these are certainly private

minor style points:

  • 1-d should be 1-D
  • there shouldn't be blank lines below section headers (Notes, Examples, ...)
* Renamed array_tools.py to _arraytools.py.
* lfilter_zi: Improved the docstring.
* filtfilt: Don't bother allowing padtype='none'; padtype=None is sufficient.
* filtfilt: Improved the docstring.
* filtfilt: Tweaked the existing filtfilt test, and added a test that uses the axis keyword.

WarrenWeckesser commented Jun 4, 2011

Thanks, Ralf. I committed a change to address most of the items you mentioned.

I removed the reference in lfilter_zi because it was not correct. lfilter_zi does not implement Gustafsson's method. In a footnote, Gustafsson points out that there was a bug in an old version of matlab's equivalent of lfilter_zi, and he gives a correction. That one line in the footnote appears to be what the original author of lfilter_zi used, but it really has nothing to do with Gustafsson's work.

(I have an implementation of Gustafsson's method under development, but that will be a future addition.)

I haven't touched the tutorial. Expanding the signal tutorial to include filtfilt (along with the FIR and IIR design tools) is on the "to do" list, but I'd like to get the current changes committed without waiting for the tutorial work to be done.


rgommers commented Jun 4, 2011

Looks good to go. I didn't mean to imply you should write the tutorial now - actually I didn't even check if filtfilt is in there now, just wrote the plot code to see what the padtype options do and it looked useful.

You probably want to squash this before committing.


WarrenWeckesser commented Jun 4, 2011

Committed: 8d4665f

Thanks again, Ralf!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment