https://github.com/jaantollander/oneeurofilter
Simple Python and Julia implementations of the 1€ Filter. The codes can be used as a pseudocode for implementing the algorithm in other languages.
https://github.com/jaantollander/oneeurofilter
noise-filter noise-filtering one-euro-filter python
Last synced: about 1 year ago
JSON representation
Simple Python and Julia implementations of the 1€ Filter. The codes can be used as a pseudocode for implementing the algorithm in other languages.
- Host: GitHub
- URL: https://github.com/jaantollander/oneeurofilter
- Owner: jaantollander
- License: mit
- Created: 2018-12-21T08:53:01.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-03-28T10:28:51.000Z (about 3 years ago)
- Last Synced: 2025-03-31T09:08:15.275Z (about 1 year ago)
- Topics: noise-filter, noise-filtering, one-euro-filter, python
- Language: Python
- Homepage: https://jaantollander.com/post/noise-filtering-using-one-euro-filter/
- Size: 1.35 MB
- Stars: 293
- Watchers: 6
- Forks: 38
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# 1€ Filter (One Euro Filter)
A simple Python implementation of the 1€ Filter (1e filter, 1 euro filter, One Euro Filter). The code in [`one_euro_filter.py`](./python/one_euro_filter.py) can be used as pseudocode for implementing the algorithm in other languages. Detailed pseudocode about the underlying mathematics of the algorithm and sources can be found in a blog post that I wrote about it, [Noise Filtering Using 1€ Filter](https://jaantollander.com/post/noise-filtering-using-one-euro-filter/).
```python
import math
def smoothing_factor(t_e, cutoff):
r = 2 * math.pi * cutoff * t_e
return r / (r + 1)
def exponential_smoothing(a, x, x_prev):
return a * x + (1 - a) * x_prev
class OneEuroFilter:
def __init__(self, t0, x0, dx0=0.0, min_cutoff=1.0, beta=0.0,
d_cutoff=1.0):
"""Initialize the one euro filter."""
# The parameters.
self.min_cutoff = float(min_cutoff)
self.beta = float(beta)
self.d_cutoff = float(d_cutoff)
# Previous values.
self.x_prev = float(x0)
self.dx_prev = float(dx0)
self.t_prev = float(t0)
def __call__(self, t, x):
"""Compute the filtered signal."""
t_e = t - self.t_prev
# The filtered derivative of the signal.
a_d = smoothing_factor(t_e, self.d_cutoff)
dx = (x - self.x_prev) / t_e
dx_hat = exponential_smoothing(a_d, dx, self.dx_prev)
# The filtered signal.
cutoff = self.min_cutoff + self.beta * abs(dx_hat)
a = smoothing_factor(t_e, cutoff)
x_hat = exponential_smoothing(a, x, self.x_prev)
# Memorize the previous values.
self.x_prev = x_hat
self.dx_prev = dx_hat
self.t_prev = t
return x_hat
```