lowpass_filter_iterative#

pypillometry.signal.lowpass_filter_iterative(signal, cutoff, fs, order=2, max_iter=5)[source]#

Lowpass filter that handles NaN values via iterative interpolation.

NaN values are initially bridged linearly between the neighbouring valid samples (see _bridge_gaps()), then iteratively replaced with filtered values.

Parameters:
  • signal (np.ndarray) – Input signal (can contain NaN values)

  • cutoff (float) – Lowpass filter cutoff frequency in Hz

  • fs (float) – Sampling rate in Hz

  • order (int, optional) – Butterworth filter order (default: 2)

  • max_iter (int, optional) – Maximum number of iterations (default: 5)

Returns:

filtered (np.ndarray) – Lowpass-filtered signal with NaN gaps smoothly filled

Notes

The iterative approach works by: 1. Fill NaN positions by a linear bridge between the valid samples 2. Apply lowpass filter to the filled signal 3. Replace NaN positions with the filtered values 4. Repeat steps 2-3 until max_iter reached

This converges to a smooth solution that respects the observed data while filling gaps with values consistent with the surrounding signal.

Examples

>>> signal = np.sin(np.arange(1000) / 100)
>>> signal[300:320] = np.nan  # a gap
>>> filtered = lowpass_filter_iterative(signal, cutoff=2.0, fs=100)
>>> bool(np.isnan(filtered).any())
False