velocity_savgol#
- pypillometry.signal.velocity_savgol(x, window_len=11, polyorder=2, direction='center')[source]#
Compute velocity (first derivative) using Savitzky-Golay filter.
This computes velocity by fitting a polynomial to a window of samples and evaluating its derivative, which is more robust than simple differencing. The direction parameter controls whether the velocity estimate uses past, future, or both samples relative to each point.
- Parameters:
x (np.ndarray) – The input signal
window_len (int) – The dimension of the smoothing window; should be an odd integer. Must be greater than polyorder.
polyorder (int) – Order of the polynomial to fit. Default is 2 (quadratic).
direction (str) – ‘center’ - symmetric, uses both past and future samples (default) ‘backward’ - only uses current and past samples (useful for onset detection) ‘forward’ - only uses current and future samples (useful for offset detection)
- Return type:
np.ndarray
- Returns:
The velocity (first derivative) in units per sample (same length as input)
Examples
>>> rng = np.random.default_rng(0) >>> signal = np.sin(np.linspace(0, 10, 200)) + rng.normal(0, 0.1, 200) >>> # Standard centered velocity >>> vel = velocity_savgol(signal, window_len=11) >>> vel.shape (200,) >>> # Backward-looking for onset detection >>> vel_back = velocity_savgol(signal, window_len=11, direction='backward') >>> # Forward-looking for offset detection >>> vel_fwd = velocity_savgol(signal, window_len=11, direction='forward')