flexlib: Robust Biomechanics With Hysteresis and Rust-Powered Speed

By Jonas Walkling August 21, 2025

How Schmitt-trigger style hysteresis enables reliable state detection in noisy wearable data, and why we pair Python with Rust via Maturin.

Our FlexTail Software Ecosystem: Advancing Biomechanical Analysis

Our FlexTail software ecosystem is growing with a clear goal: make biomechanical analysis powerful and accessible. flexlib is our Python library for processing and analyzing data from the wearable FlexTail sensor, designed for real-world robustness and scientific depth.

Flexlib hysteresis and Rust-powered speed title graphic

Title image: flexlib signal processing and performance overview

What flexlib Does

  • Data ingestion from common CSV files and our high‑efficiency RSF format.
  • Signal analysis with hysteresis‑based state detection to resist noise.
  • Biomechanical metrics such as sagittal flexion, lateral flexion, and rotation for detailed posture and movement insights.

Python is excellent for research and iteration. For the most computationally intensive parts, we reach for Rust and build the Python package via Maturin. This gives data scientists a familiar interface while delivering native performance for large‑scale processing.

The Power of a Hybrid Approach: Python meets Rust via Maturin

We implement performance‑critical components in Rust for its exceptional speed and memory safety, and expose them to Python with Maturin. This hybrid architecture keeps the user‑facing API simple and intuitive, while dramatically accelerating heavy computational kernels such as resampling, filtering, and feature extraction.

Maturin is a build tool that simplifies the creation and distribution of Python packages that include Rust code. It handles the complexities of compiling the Rust code and linking it to the Python interpreter, allowing developers to seamlessly call Rust functions from Python. This approach offers several key advantages:

  • Performance boost: Offload computationally intensive tasks to Rust to achieve near‑native performance for critical operations, significantly speeding up data processing and analysis.
  • Seamless integration: Maturin creates a single, easily installable Python package. Users install and use it like any other library, without worrying about the underlying Rust implementation.
  • Best of both worlds: Python provides a high‑level, user‑friendly interface for data scientists and researchers, while Rust ensures the core processing engine is fast, efficient, and reliable.

In practice, Rust handles the tight, performance‑critical loops, while Python orchestrates the overall workflow. It’s the right tool for each layer of the stack.

Why Hysteresis Matters in Noisy Signals: A Deeper Look at Schmitt Triggers

Wearable sensor data can be noisy. If you classify states (e.g., “bending”) with a single threshold, small fluctuations around that boundary can cause chattering—rapid on‑off flickering that corrupts metrics and provides unreliable user feedback. Hysteresis solves this by introducing two thresholds and a memory of the previous state.

This approach is often implemented with a Schmitt trigger, a circuit that exhibits hysteresis. A Schmitt trigger has two distinct threshold levels: an upper threshold for turning “on” and a lower threshold for turning “off”.

  • Rising threshold (upper): Transition to the active state occurs only when the signal crosses this higher boundary.
  • Falling threshold (lower): Return to the inactive state happens only after the signal drops below this lower boundary.

The gap between these thresholds is the hysteresis band. It acts as a buffer, filtering transient noise and preventing rapid toggling when the input hovers around a single limit.

Schmitt trigger characteristic with upper/lower thresholds and the
hysteresis band

Schmitt trigger hysteresis characteristic (source: electronics-tutorial.net)

Benefits in our context:

  • Noise rejection without heavy smoothing filters (and thus minimal lag).
  • Preservation of sharp, meaningful transitions.
  • Deterministic behavior under borderline conditions for consistent state detection.

A compact Schmitt‑trigger style detector in Python

# x: input signal (e.g., lumbar flexion angle in degrees)
# hi: upper threshold to enter the active state
# lo: lower threshold to exit the active state (lo < hi)
# state: previous boolean state (False = inactive, True = active)

def schmitt_update(x: float, state: bool, lo: float, hi: float) -> bool:
    if state:
        # Stay active until the signal is clearly below lo
        return x > lo
    else:
        # Stay inactive until the signal is clearly above hi
        return x >= hi

In flexlib, we apply this pattern to detect postures and transitions, such as forward bending, while effectively resisting sensor spikes and other noise. The result is cleaner state traces and more reliable downstream biomechanical metrics.

See it in Action

Explore the interactive demo and notebooks at https://minktec.com/notebook. We welcome feedback from researchers, engineers, and practitioners working with movement data.

For a deeper dive into packaging Rust for Python, see the Maturin docs: https://www.maturin.rs/.

More Articles