One Euro Filter (1€ Filter)

repository·main·Indexed 42 days ago

https://github.com/casiez/oneeurofilter

A resource-efficient algorithm designed to reduce jitter in noisy signals while minimizing lag in interactive systems. It uses a first-order low-pass filter with an adaptive cutoff frequency that stabilizes signals at low speeds and reduces latency at high speeds. Implementations are available for Python (v0.2.1), TypeScript, Rust, C++, and Arduino.

Tokens
4.2K
Snippets
19
Records
23
Agent score
54%

What's inside One Euro Filter

  1. What is the 1€ filter?

    main

    The 1€ filter (one Euro filter) is a simple algorithm designed to filter noisy signals while maintaining high precision and responsiveness. It works by using a first-order low-pass filter with an adaptive cutoff frequency:

    • At low speeds: It uses a low cutoff frequency to stabilize the signal and reduce jitter.
    • At high speeds: It increases the cutoff frequency to reduce lag.

    It is characterized by being easy to implement, resource-efficient, and easy to tune using two primary parameters. Compared to other filters, it provides less lag for a given amount of jitter reduction.

  2. Install the 1€ filter implementations

    main

    The 1€ filter is available for multiple platforms and languages via the following package managers:

    • Python: Install via PyPI using pip.
    • JavaScript/TypeScript: Install via NPM using npm.
    • Arduino: Available as an Arduino library.
    # For Python
    pip install OneEuroFilter
    
    # For JavaScript/TypeScript
    npm install 1eurofilter
  3. Run the OneEuroFilter Docker container

    main

    To run the container in detached mode with a bind mount that maps the current directory to /mnt/1euro inside the container, execute the following command from the parent folder of the docker directory:

    docker run -i -t -d --name=oneeurofilter_con --mount type=bind,source=.,target=/mnt/1euro oneeurofilter
  4. Access and build the filter inside the Docker container

    main

    To interact with the running container, use docker exec to open a bash shell. Once inside, navigate to the mounted directory and use make to build the implementations.

    docker exec -it oneeurofilter_con /bin/bash
    cd /mnt/1euro
    make
  5. Minimal example of using OneEuroFilter

    main

    This example demonstrates how to initialize the filter with specific parameters and process a noisy value with a timestamp.

    import {OneEuroFilter} from '1eurofilter'
    
    let frequency = 120; // Hz
    let mincutoff = 1.0; // Hz
    let beta = 0.1;      
    let dcutoff = 1.0; 
    
    let f = new OneEuroFilter(frequency, mincutoff, beta, dcutoff);
    
    let noisyvalue = 2.1;
    let timestamp = 0.0; // in seconds
    
    let filtered = f.filter(noisyvalue, timestamp);
  6. Use OneEuroFilter in Python

    main

    To use the filter, instantiate the OneEuroFilter class with a configuration dictionary or specific parameters, then call the instance as a function with the noisy value and the current timestamp.

    from OneEuroFilter import OneEuroFilter
    
    config = {
        'freq': 120,       # Hz
        'mincutoff': 1.0,  # Hz
        'beta': 0.1,       
        'dcutoff': 1.0    
    }
    
    f = OneEuroFilter(**config)
    
    # First parameter is the value to filter
    # the second parameter is the current timestamp in seconds
    filtered = f(2.1, 0)
  7. OneEuroFilter Class API Reference

    main

    The OneEuroFilter class provides methods to filter noisy signals and dynamically adjust filter parameters.

    Constructor

    OneEuroFilter(freq: float, mincutoff: float = 1.0, beta: float = 0.0, dcutoff: float = 1.0)

    Methods

    • filter(x: float, timestamp: float = None) -> float: Filters a noisy value x. If timestamp is not provided, it uses the internal frequency to estimate time.
    • reset(): Resets the internal state of the filter.
    • setBeta(beta: float): Sets the beta parameter to reduce latency (value should be > 0).
    • setDerivateCutoff(dcutoff: float): Sets the dcutoff parameter used to filter derivatives. Defaults to 1 Hz. Use with caution.
    • setFrequency(freq: float): Sets the signal frequency in Hz. Requires freq > 0.
    • setMinCutoff(mincutoff: float): Sets the minimum cutoff frequency in Hz. Lower values remove more jitter. Requires mincutoff > 0.
    • setParameters(freq: float, mincutoff: float = 1.0, beta: float = 0.0, dcutoff=1.0): Updates all filter parameters at once.
  8. Configure OneEuroFilter parameters

    main

    You can update the filter's parameters after instantiation using the following methods:

    • setBeta(beta: number): Sets the Beta parameter to reduce latency (> 0).
    • setDerivateCutoff(dcutoff: number): Sets the dcutoff parameter used to filter derivates.
    • setFrequency(freq: number): Sets the estimated frequency of the signal in Hz (> 0).
    • setMinCutoff(mincutoff: number): Sets the min cutoff frequency in Hz (> 0). Lower values remove more jitter.
    • setParameters(freq: number, mincutoff: number, beta: number): Sets the frequency, min cutoff, and beta parameters simultaneously.