Overview
Temporal filtering functions apply bandpass filters along the temporal dimension of video tensors to isolate specific frequency bands corresponding to vital signs. The implementation uses Butterworth filters for smooth frequency response.create_bandpass_filter
Creates Butterworth bandpass filter coefficients for a specified frequency range.Parameters
Low cutoff frequency in Hz. Frequencies below this are attenuated.
High cutoff frequency in Hz. Frequencies above this are attenuated.
Nyquist frequency (sampling_rate / 2). For 30 FPS video, nyquist = 15 Hz.
Filter order. Higher order = steeper roll-off but more computational cost.
- Order 2: Good balance for real-time processing
- Order 4: Better frequency separation
Returns
Numerator coefficients of the IIR filter.
Denominator coefficients of the IIR filter.
Returns
None if filter creation fails (e.g., invalid frequency range).Implementation Details
The function normalizes frequencies and applies safety bounds:apply_temporal_bandpass
Applies temporal bandpass filter to a video tensor along the time axis.Parameters
Video tensor with shape
(T, H, W, C) where T is the temporal dimension.Low cutoff frequency in Hz.
High cutoff frequency in Hz.
Frames per second of the video (sampling rate).
Axis along which to apply the filter (0 = temporal axis).
Returns
Temporally filtered tensor with same shape as input. Returns original tensor if filtering fails.
Zero-Phase Filtering
Usesscipy.signal.filtfilt for zero-phase filtering:
- Zero phase distortion
- No time delay introduced
- Forward-backward filtering for stability
temporal_dual_bandpass_filter
Applies two bandpass filters simultaneously to extract both HR and RR signals efficiently.Parameters
Video tensor with shape
(T, H, W, C).Frames per second of the video.
Heart rate low frequency bound (Hz). Default: 0.83 Hz (50 BPM).
Heart rate high frequency bound (Hz). Default: 3.0 Hz (180 BPM).
Respiratory rate low frequency bound (Hz). Default: 0.18 Hz (11 RPM).
Respiratory rate high frequency bound (Hz). Default: 0.5 Hz (30 RPM).
Temporal axis for filtering.
Returns
Tensor filtered for heart rate frequency band (0.8-3 Hz).
Tensor filtered for respiratory rate frequency band (0.2-0.8 Hz).
Frequency Band Configuration
Heart Rate Band
Optimized for detecting cardiac pulse:The HR band (0.83-3.0 Hz) corresponds to 50-180 beats per minute, covering resting to exercise heart rates.
Respiratory Rate Band
Optimized for detecting breathing:The RR band (0.18-0.5 Hz) corresponds to 11-30 breaths per minute, covering normal breathing rates.
Usage Example: EVM Processing
Complete example showing temporal filtering in the EVM pipeline:Filter Response Characteristics
Butterworth Filter Properties
Frequency Response
- Maximally flat passband
- Smooth roll-off
- No ripple in passband
Phase Response
- Zero phase (using filtfilt)
- No time delay
- Preserves waveform shape
Order 2 vs Order 4
| Property | Order 2 | Order 4 |
|---|---|---|
| Roll-off | -40 dB/decade | -80 dB/decade |
| Computation | Faster | Slower |
| Frequency separation | Good | Excellent |
| Use case | Real-time | Offline analysis |
Frequency Response Visualization
Performance Considerations
Computational Cost
For a typical tensor(200, 40, 30, 3) on Raspberry Pi 4:
- Filter creation: < 1ms
- filtfilt application: ~50-100ms per tensor
- Total for dual-band: ~100-200ms
Memory Usage
Filtering is done in-place where possible:- Input tensor: 200×40×30×3×4 bytes = 2.88 MB
- Output tensor: 2.88 MB (same)
- Temporary arrays: ~3-5 MB during filtfilt
- Peak usage: ~8-10 MB
Edge Effects
To minimize edge artifacts:- Minimum frames: Requires at least 10 frames (enforced in code)
- filtfilt padding: Automatically pads signal to reduce edge effects
- Buffer size: Use 200+ frames for best results
Error Handling
Robust error handling prevents crashes:- Invalid frequency range: Returns
Nonefilter coefficients - Filtering failure: Returns original tensor unmodified
- Short sequences: Skips filtering for tensors with < 10 frames
- Exception logging: Prints diagnostic messages
Related Functions
- EVMProcessor.process_dual_band - Uses temporal filtering
- extract_pyramid_level - Provides tensors to filter
- calculate_frequency_fft - Analyzes filtered signals