Overview
Signal analysis functions extract temporal signals from filtered video tensors and compute dominant frequencies using FFT (Fast Fourier Transform). These functions convert magnified EVM signals into heart rate and respiratory rate measurements in BPM/RPM.extract_temporal_signal
Extracts a 1D temporal signal by spatially averaging each frame in a video tensor.Parameters
Video tensor with shape
(T, H, W, C) where:- T = number of frames
- H = height
- W = width
- C = color channels (3 for BGR)
If
True, uses only the green channel (index 1 in BGR). If False, averages all channels.The green channel provides better signal-to-noise ratio for pulse detection.Returns
1D temporal signal with length T. Each value represents the spatial average of one frame.
Implementation
preprocess_signal
Preprocesses temporal signal through detrending and normalization.Parameters
1D temporal signal from
extract_temporal_signal.Returns
Detrended and normalized signal. Returns
None if:- Signal is None
- Signal has fewer than 20 samples
- Signal is constant (std < 1e-10)
- Detrended signal is constant (std < 1e-8)
Processing Steps
- Constant check: Reject if std < 1e-10
- Detrending: Remove linear trend using
scipy.signal.detrend - Constant check after detrend: Reject if std < 1e-8
- Normalization: Divide by standard deviation
apply_hamming_window
Applies Hamming window to reduce spectral leakage in FFT.Parameters
Temporal signal to window.
Returns
Signal multiplied by Hamming window.
Why Hamming Window?
The Hamming window reduces spectral leakage by smoothly tapering the signal at its edges. This improves FFT accuracy by minimizing artifacts from discontinuities.
calculate_power_spectrum
Computes power spectrum using FFT.Parameters
Temporal signal (typically preprocessed and windowed).
Frames per second (sampling rate).
Returns
Frequency values corresponding to FFT bins (in Hz).
Power spectrum (magnitude squared of FFT).
Implementation
find_dominant_frequency
Finds the dominant frequency within a specified physiological range.Parameters
FFT frequency array from
calculate_power_spectrum.Power spectrum from
calculate_power_spectrum.Minimum frequency in Hz to consider.
Maximum frequency in Hz to consider.
Minimum physiologically valid BPM/RPM.
Maximum physiologically valid BPM/RPM.
Returns
Dominant frequency in beats/breaths per minute. Returns
None if:- No frequencies in specified range
- Computed BPM outside [min_bpm, max_bpm] range
Algorithm
calculate_frequency_fft
Main API: Complete pipeline to calculate dominant frequency using FFT.Parameters
1D temporal signal from EVM processing.
Frames per second (video sampling rate).
Low cutoff frequency in Hz.
- Heart rate: 0.83 Hz (50 BPM)
- Respiratory rate: 0.18 Hz (11 RPM)
High cutoff frequency in Hz.
- Heart rate: 3.0 Hz (180 BPM)
- Respiratory rate: 0.5 Hz (30 RPM)
Minimum physiologically valid BPM/RPM.
- Heart rate: 40 BPM
- Respiratory rate: 8 RPM
Maximum physiologically valid BPM/RPM.
- Heart rate: 250 BPM
- Respiratory rate: 35 RPM
Returns
Detected frequency in BPM (heart rate) or RPM (respiratory rate). Returns
None if:- Signal preprocessing fails
- No dominant frequency detected
- Frequency outside physiological range
Complete Pipeline
Implementation
Usage Example: Heart Rate Detection
Complete example extracting heart rate from EVM output:Usage Example: Respiratory Rate Detection
Frequency Band Parameters
Heart Rate
Respiratory Rate
Hz to BPM Conversion
The conversion from frequency (Hz) to beats/breaths per minute:Examples
| Frequency (Hz) | Heart Rate (BPM) | Respiratory Rate (RPM) |
|---|---|---|
| 0.5 Hz | 30 BPM | 30 RPM |
| 1.0 Hz | 60 BPM | 60 RPM (too high for breathing) |
| 1.5 Hz | 90 BPM | - |
| 2.0 Hz | 120 BPM | - |
Signal Quality Checks
The pipeline includes multiple quality checks:- Minimum length: Signal must have ≥20 samples
- Constant signal: Rejected if std < 1e-10
- Post-detrend check: Rejected if std < 1e-8 after detrending
- Frequency range: Must be within [lowcut_hz, highcut_hz]
- Physiological range: Must be within [min_bpm, max_bpm]
Performance Metrics
Computational Cost
For a 200-sample signal on Raspberry Pi 4:- Preprocessing: ~2-3ms
- Hamming window: ~1ms
- FFT: ~5-10ms
- Peak finding: ~1-2ms
- Total: ~10-20ms per signal
Accuracy
Benchmark results on validation dataset:- Mean Absolute Error (MAE): < 5 BPM
- Root Mean Square Error (RMSE): < 7 BPM
- Within ±5 BPM: > 70%
- Within ±10 BPM: > 90%
- Correlation with ground truth: > 0.85
Error Handling
Robust error handling at each stage:- None input: Returns
None - Short signals: Returns
Nonefor < 20 samples - Constant signals: Returns
Noneto avoid division by zero - No peaks found: Returns
None - Out of range: Returns
Noneif BPM invalid
Related Functions
- process_video_evm_vital_signs - Uses FFT analysis
- EVMProcessor.process_dual_band - Generates temporal signals
- apply_temporal_bandpass - Pre-filters signals