-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: Add sosfiltfilt #6274
ENH: Add sosfiltfilt #6274
Conversation
@WarrenWeckesser @endolith you might be interested in this since you were involved in |
@@ -2912,6 +2908,78 @@ def sosfilt(sos, x, axis=-1, zi=None): | |||
return out | |||
|
|||
|
|||
def sosfiltfilt(sos, x, axis=-1, padtype='odd', padlen=None): | |||
""" | |||
A forward-backward filter using cascoded second-order sections. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"cascaded"
return y | ||
|
||
|
||
def _validate_sos(sos): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function could also check that sos[:,3] == np.ones(sos.shape[0])
Thanks for the reviews, comments addressed. I think (hope?) this is actually a pretty straightforward change so it seems like merging for 0.18 is doable. @e-q do you agree? |
Looks like the current failures are a missing entry in |
@rgommers do you think it's reasonable to try for 0.18 for this?
|
Great. Tests are passing now, @e-q are you happy with it now? |
Returns | ||
------- | ||
y : ndarray | ||
The filtered output, an array of type numpy.float64 with the same |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lfilter
, filtfilt
and sosfilt
can filter and return complex arrays. I haven't tried it, but I don't see why this function shouldn't do the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied this from filtfilt
, maybe there they are restricted by the Gustafsson implementation or something. I'll add some simple tests. Maybe both this and filtfilt
need to be updated...
This will be great to get into scipy.signal. I've been trying it out, and ran into an issue with odd order filters. I'm not sure this is actually a problem, and if it is a problem, I'm not sure if it is a problem with Here's a script. When
Here's the plot of Because the order is odd, the SOS representation has an extra zero and pole at the origin. I suspect that is the source of the problem. This might be unavoidable. I'll continue experimenting and try to get a better handle on the issue this evening. |
Do they match for sosfilt and lfilter? If so, what if there are initial
conditions?
|
Good question. When I replaced |
I bet it's the pad length
|
Yes, it looks like it is something to do with padding. The outputs of |
I also noticed that the description of the default value of |
The filtered outputs
In general, (By the way, I have no idea where the original default of |
And now that I've taken another look at the code, I see that that is exactly what the default |
So now my only suggestions are for some tweaks to the documentation. In the description of the return value, just drop the reference to the data type of the result. The function does, in fact, return a complex array when the input is complex. And in the description of |
Sorry @WarrenWeckesser I should have saved you some time by telling you what the current behavior was, but was on mobile :( Without looking at the math, I suspect that we get the actual order by looking at whether the third coefficient is zero. I'll have to check. Then we can get exact equivalence for odd-order systems. |
(in terms of padlen I mean) |
@WarrenWeckesser added an equivalence test for odd order (it passes, hooray!) and updated the computation and docstrings. If you have time, see if you agree. |
I think this comes from MATLAB, since I know they do something similar. Octave has a |
Although thinking about it more, having that many values ensures that your system at least has all of its delay taps depend some aspect of the input signal, beyond the single edge value (which we otherwise use to set the initial conditions), which might also be part of the motivation. |
Your updated default |
Thanks for another short-notice look @WarrenWeckesser, docstring fixed. @e-q feel free to have a look in the next few hours if you have time, otherwise since people seem happy with it I'll go ahead and merge so it makes it to 0.18. |
How about squashing the commits? Then I'll merge. (In the "squash commits to keep the history relative simple and clean" vs "keep every little committed tweak" debate, I'm in the "squash" camp.) |
Wait, there is one more thing. Add a note about |
Hah! I was just doing that when you posted it. Not sure if great minds think alike or fools seldom differ here :) I'll add it and squash and ping you when the CIs are done. Thanks for the quick reviews. |
Merged. Thanks, Eric! |
ENH: signal: Add sosfiltfilt
ENH: signal: Add sosfiltfilt
This one is something I meant to add from back when I worked on #3717. Since
#3717(EDIT: #6137) is some ways off presumably it's probably better to get this in than wait. It would be great to have for 0.18 if possible, but not strictly necessary.