Skip to content

Add sliding window operations to speasy.signal #282

@jeandet

Description

@jeandet

Description

Add a sliding window mechanism to speasy.signal that applies a function over a moving time window on a SpeasyVariable. This enables common time-series analysis operations like moving averages, sliding FFTs (spectrograms), running integrals, and arbitrary user-defined windowed computations.

Use cases

  • Moving average — smooth noisy time series over a configurable window
  • Sliding FFT / spectrogram — compute power spectral density in a moving window, producing a time-frequency representation
  • Running integration — fluence, accumulated flux over a sliding interval
  • Custom operations — any user-provided function applied per window (e.g. variance, percentiles)

Sketch of possible API

from speasy.signal.windowing import sliding_window

# Moving average
smoothed = sliding_window(var, window_size=np.timedelta64(30, 's'), func=np.mean)

# Sliding FFT (returns a different shape — time x frequency)
spectrogram = sliding_window(var, window_size=np.timedelta64(10, 's'), func='fft', step=np.timedelta64(5, 's'))

# Custom callable
result = sliding_window(var, window_size=np.timedelta64(1, 'm'), func=my_function)

Design considerations

  • window_size as np.timedelta64 or float (seconds)
  • Optional step parameter for non-overlapping or partially overlapping windows (defaults to one sample = fully overlapping)
  • Built-in fast paths for mean (cumsum trick, O(n)) and fft (stride tricks + vectorized FFT)
  • Arbitrary callable fallback for custom functions
  • Output shape depends on the function: same shape for mean/integrate, time x frequency for FFT
  • Should handle lists of variables like the rest of speasy.signal

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions