streamsim.src.features.local_maxima module
Local Maxima Peak Detector for Streaming Signals
This module provides a streaming feature deriver that detects local maxima (peaks) in time-series signals. It implements the StreamingFeatureDeriver interface to identify peaks where the current sample is strictly greater than its neighboring samples.
It uses a configurable sliding window to compare each sample against its left and right neighbors, with an optional threshold to filter out minor fluctuations. This makes it suitable for detecting significant peaks in various signal types (sinusoidal, triangular, ECG, etc.).
- Features:
Configurable window size for neighbor comparison
Optional threshold filtering relative to local mean
Returns peak timestamps for downstream processing
Memory-efficient buffer using deque
Explicit reset capability for state management
- Important Dependencies:
streamsim.src.core.interfaces.StreamingFeatureDeriver: Base interface
collections.deque: Efficient circular buffer
Author: F.Feenstra
- class streamsim.src.features.local_maxima.LocalMaximaDeriver(window_size: int = 3, threshold: float = 0.0)[source]
Bases:
StreamingFeatureDeriverGeneral-purpose peak detector for any signal (Sinus, Sine, Triangle, etc.).
Detects a peak when the current sample is strictly greater than its immediate neighbors (left and right).
- Returns:
Returns peak timestamp when a peak is detected, None otherwise.
- Return type:
get_feature()
- add_sample(sample: float, timestamp: float = None) None[source]
Add a new sample and evaluate for peak detection.
Stores the sample with its timestamp in the internal buffer, then checks if the previous sample (now at center position) qualifies as a local maximum based on neighbor comparison and threshold criteria.
- Parameters:
sample (float) – The raw signal sample value.
timestamp (float, optional) – Timestamp of the sample in seconds. If None, uses current system time. Default: None.
Note
Minimum Requirement: You need at least window_size + 1 samples before any peak can be detected. Early samples are skipped. To confirm a peak, you need to see both sides of it: - Ascending slope: The sample before must be lower - Descending slope: The sample after must be lower The Catch: When a new sample arrives, you can’t tell if it’s a peak yet—you need the next sample to confirm the signal drops. So the code checks the previous sample (index -2) once the current sample (index -1) arrives.