-
Notifications
You must be signed in to change notification settings - Fork 14
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
Live digital filters as a separate package / library #15
Comments
Hi thank you for the feedback. zi = scipy.signal.lfilter_zi(b, a)
ys = []
for y in yraw:
yout, zi = scipy.signal.lfilter(b, a, [y], zi=zi)
ys.append(yout) I intend to make an update to the post once I get around to it. |
Thanks @SamProell for this update. |
Maybe some classes for stateful version of digital filters should be implemented |
sure, this was just a quick demonstration of how to use the filter state. Writing a class around this would be necessary. |
I tried class StateFulLiveLFilter(LiveFilter):
def __init__(self, b, a):
"""Initialize live filter based on difference equation.
Args:
b (array-like): numerator coefficients obtained from scipy.
a (array-like): denominator coefficients obtained from scipy.
"""
self.b = b
self.a = a
self.zi = scipy.signal.lfilter_zi(self.b, self.a)
def _process(self, y):
"""Filter incoming data with standard difference equations.
"""
yout, self.zi = scipy.signal.lfilter(b, a, [y], zi=self.zi)
return yout but MAE is a bit higher lfilter error: 1.8117e-15 (with previous version) |
Same problem also occurs with class StatefulLiveSosFilter(LiveFilter):
"""Live implementation of digital filter with second-order sections.
"""
def __init__(self, sos):
"""Initialize live second-order sections filter.
Args:
sos (array-like): second-order sections obtained from scipy
filter design (with output="sos").
"""
self.sos = sos
self.zi = scipy.signal.sosfilt_zi(sos)
def _process(self, y):
"""Filter incoming data with cascaded second-order sections.
"""
yout, self.zi = scipy.signal.sosfilt(self.sos, [y], zi=self.zi)
return yout sosfilter error was 0 and is now 0.039018 |
In principal, your implementation(s) are good. I played around with it myself and the "problem" lies with setting the initial state.
After some playing around, I found that |
Ok so class StateFulLiveLFilter(LiveFilter):
def __init__(self, b, a, yi=None, xi=None):
"""Initialize live filter based on difference equation.
Args:
b (array-like): numerator coefficients obtained from scipy.
a (array-like): denominator coefficients obtained from scipy.
"""
self.b = b
self.a = a
if yi is None:
yi = [0.0]
self.zi = scipy.signal.lfiltic(b, a, yi, xi)
def _process(self, y):
"""Filter incoming data with standard difference equations.
"""
yout, self.zi = scipy.signal.lfilter(b, a, [y], zi=self.zi)
return yout no idea about how to overcome this sosfilt error. |
looks good. I started working on a "minor" rework of the internal structure, including formatting. If it's ok with you, I might just replace the current implementation of I'd say that having some deviation in the beginning is not that big of a deal for most applications. Focus is on online filtering, where some startup differences should be no problem. And also, you cannot get the same behavior with the original sosfilt anyway, i guess. |
Yes we can live with this initialization problem... but maybe that's a lack on Scipy side |
also many thanks to you! |
@s-celles, just FYI. I have finally come around to rework the code and improve code quality drastically. I am now also working on a documentation for it here: https://samproell.github.io/yarppg/ |
Hello,
I read with interest your blog post https://www.samproell.io/posts/yarppg/yarppg-live-digital-filter/
I wonder if a separate package just for implementing live digital filters in Python won't make sense.
It could be based on list as you did in
yarppg/yarppg/rppg/filters.py
Line 15 in 613155c
or on deque as mentioned in your blog post.
A last idea could be to rely on circular buffer / ring buffer on top of Numpy such as https://github.com/eric-wieser/numpy_ringbuffer or https://github.com/Dennis-van-Gils/python-dvg-ringbuffer
Any opinion?
The text was updated successfully, but these errors were encountered: