Low pass filter

Contents

Low pass filter#

class c4dynamics.filters.lowpass.lowpass(alpha: float | None = None, dt: float | None = None, tau: float | None = None, y0: float = 0)[source]#

A first-order low-pass filter for smoothing signals, supporting both discrete and continuous systems.

Parameters:
  • alpha (float, optional) – Smoothing factor for a discrete system. Must be in the range (0, 1). Defaults to None.

  • dt (float, optional) – Time step for a continuous system. Must be positive. Defaults to None.

  • tau (float, optional) – Time constant for a continuous system. Must be positive. Defaults to None.

  • y0 (float, optional) – Initial value of the state. Defaults to 0.

Raises:

ValueError – If neither alpha nor both dt and tau are provided. If alpha is out of the range (0, 1) for a discrete system. If dt or tau is non-positive for a continuous system.

Notes

  • For a continuous system, dt and tau are required, and alpha is calculated as dt / tau.

  • For a discrete system, alpha alone is required and directly specifies the smoothing factor.

Example

>>> filter_continuous = lowpass(dt=0.01, tau=0.1, y0=0)
>>> filter_discrete = lowpass(alpha=0.5, y0=1)
>>> filter_continuous.sample(1.0) 
0.09...
>>> filter_discrete.sample(2.0)
1.5

Methods

lowpass.sample(x)

Applies the low-pass filter to the input value and returns the filtered output.