Signal Filtering Module for Physiological Signal Processing
This module provides comprehensive signal filtering capabilities for physiological
signals including ECG, PPG, EEG, and other vital signs. It implements various
filtering techniques including bandpass, lowpass, highpass, and notch filters
with multiple filter types and adaptive parameter optimization.
Author: vitalDSP Team
Date: 2025-01-27
Version: 1.0.0
A class for applying various filtering techniques to signals.
This class provides methods for common signal filtering tasks, such as applying moving averages,
Gaussian filters, Butterworth filters, and median filters. These techniques are essential for
preprocessing signals in various fields, including biomedical signal processing (e.g., ECG, EEG).
savgol_filter:staticmethod
Applies a Savitzky-Golay filter.
moving_average:function
Applies a moving average filter.
gaussian:function
Applies a Gaussian filter.
butterworth:function
Applies a Butterworth filter.
chebyshev:function
Applies a Chebyshev filter.
elliptic:function
Applies an Elliptic filter.
bandpass:function
Applies a bandpass filter using a selected filter type.
median:function
Applies a median filter.
_apply_iir_filter:function
Internal method to apply IIR filters.
Examples
>>> importnumpyasnp>>> fromvitalDSP.filtering.signal_filteringimportSignalFiltering>>>>>> # Example 1: Basic signal filtering>>> signal=np.random.randn(1000)# Simulated signal>>> sf=SignalFiltering(signal)>>> filtered_signal=sf.bandpass(lowcut=0.5,highcut=30,fs=256,order=4)>>> print(f"Filtered signal shape: {filtered_signal.shape}")>>>>>> # Example 2: Moving average filtering>>> ma_filtered=sf.moving_average(window_size=5)>>> print(f"Moving average filtered: {ma_filtered.shape}")>>>>>> # Example 3: Gaussian filtering>>> gaussian_filtered=sf.gaussian(sigma=1.0)>>> print(f"Gaussian filtered: {gaussian_filtered.shape}")>>>>>> # Example 4: Savitzky-Golay filtering>>> sg_filtered=SignalFiltering.savgol_filter(signal,window_length=5,polyorder=2)>>> print(f"Savitzky-Golay filtered: {sg_filtered.shape}")
The Bessel filter has a maximally flat group delay, which means it preserves
the waveform shape of filtered signals in the passband. This makes it ideal
for applications where maintaining pulse shape is critical, such as ECG and
PPG signal processing.
Parameters:
cutoff (float or list) – Cutoff frequency of the filter. For bandpass/bandstop, provide [low, high].
The Bessel filter is particularly useful for:
- ECG signal processing (preserves QRS complex shape)
- PPG signal processing (preserves pulse waveform)
- Applications requiring linear phase response
- Situations where overshoot and ringing must be minimized
Examples
>>> importnumpyasnp>>> fromvitalDSP.filtering.signal_filteringimportSignalFiltering>>> # ECG-like signal with sharp peaks>>> t=np.linspace(0,1,1000)>>> ecg_signal=np.sin(2*np.pi*1.2*t)+0.5*np.sin(2*np.pi*60*t)>>> sf=SignalFiltering(ecg_signal)>>> # Apply Bessel lowpass to remove high-frequency noise while preserving peak shape>>> filtered=sf.bessel(cutoff=50,fs=1000,order=4,btype='low')>>> print(f"Filtered signal shape: {filtered.shape}")
The Chebyshev Type II filter has a flat passband and equiripple stopband attenuation.
Unlike Type I, it has zeros in the stopband which provides sharper roll-off
characteristics. This filter is useful when you need better stopband attenuation
with acceptable passband characteristics.
Parameters:
cutoff (float or list) – Cutoff frequency of the filter. For bandpass/bandstop, provide [low, high].
Applies a moving average filter to the signal with optional repeated scanning.
This method smooths the signal by averaging neighboring data points within a defined window size.
Optionally, the smoothing can be repeated multiple times for enhanced effect. This technique is
commonly used to reduce random noise and reveal trends in signals like EEG, ECG, and PPG.
Parameters:
window_size (int) – The size of the moving window.
iterations (int, optional) – The number of times to apply the moving average for additional smoothing. Default is 1.
method (str, optional) – Padding method: ‘edge’ (default), ‘reflect’, or ‘constant’. Different methods may yield better results
for specific types of signals (e.g., vital signals like EEG, ECG, PPG).
Use an adaptive filter to remove artifacts correlated with a reference signal.
This method uses Least Mean Squares (LMS) adaptive filtering to iteratively adjust
the signal to minimize the error between the filtered signal and a reference signal.
It is particularly useful for removing artifacts that are correlated with another signal,
such as EOG artifacts in EEG recordings, motion artifacts in PPG, or respiratory artifacts.
If no reference signal is provided, the filter adapts towards zero (artifact removal/denoising).
Parameters:
reference_signal (numpy.ndarray, optional) – The reference signal correlated with the artifact. If None, adapts towards zero
(removes DC offset and baseline drift). Must have the same length as the input signal.
learning_rate (float, default=0.01) – The learning rate (step size) for the adaptive filter. Controls convergence speed.
Typical range: 0.001 - 0.5
- Lower values (0.001-0.01): Slower convergence, more stable
- Higher values (0.1-0.5): Faster convergence, may oscillate
num_iterations (int, default=100) – The number of iterations for adaptation. More iterations = better convergence.
Returns:
clean_signal – The artifact-removed signal with the same length as the input.
ValueError – If reference_signal length doesn’t match signal length.
Notes
Algorithm: Least Mean Squares (LMS) adaptive filtering
- The filter iteratively adjusts the signal based on the error
- Error = filtered_signal - reference_signal
- Update rule: filtered_signal -= learning_rate * error
Use Cases:
1. With reference signal: Remove correlated artifacts (EOG from EEG, motion from PPG)
2. Without reference signal: General denoising and DC offset removal
Convergence:
- Monitor the error reduction to ensure proper convergence
- If the filter diverges (error increases), reduce the learning rate
- Typical convergence: 50-200 iterations with learning_rate=0.01
Correct baseline drift by applying a high-pass filter.
This method is particularly effective for removing low-frequency baseline wander
in signals such as ECG or PPG, where baseline drift can obscure important features.
Parameters:
cutoff (float) – The cutoff frequency for the high-pass filter.
fs (float) – The sampling frequency of the signal.
Use Independent Component Analysis (ICA) to remove artifacts.
This enhanced version automatically handles 1D signals by creating synthetic
components. ICA separates the signal into independent components and allows
for the removal of specific components identified as artifacts.
For 1D signals (single channel), synthetic components are automatically
generated using derivatives and delayed versions, enabling ICA to separate
artifacts from the underlying physiological signal.
Parameters:
num_components (int) – The number of independent components to retain.
For 1D signals, this determines how many synthetic components to generate.
Recommended: 3-5 for good artifact separation.
max_iterations (int) – The maximum number of iterations for convergence.
tol (float) – The tolerance level for convergence.
seed (int) – The seed for random number generation to ensure reproducibility.
window_size (int, optional) – The size of the sliding window to create a multi-dimensional signal.
If None, automatic synthetic component generation is used for 1D signals.
step_size (int, optional) – The step size for the sliding window. Must be used with window_size.
batch_size (int, optional) – The batch size for IncrementalPCA to manage memory usage (legacy parameter).
Returns:
clean_signal – The artifact-removed signal (same shape as input).
For 1D Signals (Single Channel):
The method automatically creates synthetic components from:
- Original signal
- First derivative (captures rapid changes/spikes)
- Delayed version (captures temporal patterns)
- Second derivative (captures acceleration/motion artifacts)
- Smoothed version (captures baseline trends)
For Multi-Channel Signals:
Uses traditional windowing approach if window_size is specified.
Examples
>>> # Example 1: 1D signal (most common case)>>> importnumpyasnp>>> signal_1d=np.sin(2*np.pi*np.linspace(0,10,1000))+0.1*np.random.randn(1000)>>> ar=ArtifactRemoval(signal_1d)>>> clean=ar.ica_artifact_removal(num_components=3)>>> print(clean.shape)# (1000,)>>>>>> # Example 2: With windowing (for backward compatibility)>>> signal=np.array([1,2,3,4,5,6,7,8])>>> ar=ArtifactRemoval(signal)>>> clean=ar.ica_artifact_removal(num_components=1,window_size=4,step_size=2)>>> print(clean.shape)# (8,)
Use Principal Component Analysis (PCA) to remove artifacts.
This method removes artifacts by reconstructing the signal with a reduced number
of principal components, which can be particularly useful for signals with multiple
overlapping noise sources.
Parameters:
num_components (int) – The number of principal components to retain.
window_size (int, optional) – The size of each window used to segment the signal (default is 100).
overlap (int, optional) – The number of samples that each window should overlap (default is 50).
Remove noise using wavelet-based denoising with various mother wavelets.
This method decomposes the signal into approximation and detail coefficients using
wavelets, thresholds the detail coefficients, and reconstructs the signal. It is effective
for denoising signals where noise is present at multiple scales.
Parameters:
wavelet_type (str, optional) – The type of wavelet to use (‘haar’, ‘db’, ‘sym’, ‘coif’, ‘custom’). Default is ‘db’.
level (int, optional) – The level of decomposition. Higher levels capture more global features. Default is 1.
order (int, optional) – The order of the wavelet (used for ‘db’, ‘sym’, and ‘coif’ wavelets). Default is 4.
custom_wavelet (numpy.ndarray, optional) – A custom wavelet provided by the user if wavelet_type is ‘custom’. Default is None.
Returns:
clean_signal – The denoised signal with the same length as the original signal.
>>> # Example using a custom wavelet>>> custom_wavelet=np.array([0.2,0.5,0.2])>>> clean_signal=ar.wavelet_denoising(wavelet_type='custom',custom_wavelet=custom_wavelet)>>> print(clean_signal)
Advanced Signal Filtering Module for Physiological Signal Processing
This module implements advanced filtering techniques for physiological signals including
Kalman filtering, optimization-based filtering, gradient descent filtering, ensemble
filtering, convolution-based filtering, and attention-based filtering. These methods
are designed for complex scenarios such as real-time filtering, adaptive filtering,
and filtering in noisy environments.
Author: vitalDSP Team
Date: 2025-01-27
Version: 1.0.0
Key Features:
- Kalman filtering for real-time signal estimation
- Optimization-based filtering with custom loss functions
- Gradient descent adaptive filtering
- Ensemble filtering combining multiple methods
- Convolution-based filtering with custom kernels
- Attention-based dynamic filtering
A class for applying advanced filtering techniques to signals.
This class implements a variety of advanced filtering methods, including Kalman filters,
optimization-based filtering, gradient descent filtering, ensemble filtering, convolution-based
filtering, and attention-based filtering. These methods are designed to enhance signal processing
in complex scenarios such as real-time filtering, adaptive filtering, and filtering in noisy environments.
Apply an adaptive filter to the signal using the Least Mean Squares (LMS) algorithm.
The LMS adaptive filter is useful for real-time signal processing tasks where the characteristics
of the signal may change over time. The filter coefficients are updated iteratively to minimize
the error between the desired signal and the filter output.
Parameters:
desired_signal (numpy.ndarray) – The desired signal that the filter output should approximate.
mu (float, optional) – The step size or learning rate for the LMS algorithm. Default is 0.01.
filter_order (int, optional) – The order of the adaptive filter. Default is 4.
Returns:
filtered_signal – The filtered signal after applying the LMS adaptive filter.
Apply an attention-based filter to the signal using predefined or custom attention weights.
Attention-based filtering uses an attention mechanism to dynamically adjust the influence of
different parts of the signal. This method supports various types of attention mechanisms,
including uniform, linear, gaussian, and exponential, as well as custom-defined attention weights.
Parameters:
attention_type (str) – The type of attention weights to use (‘uniform’, ‘linear’, ‘gaussian’, ‘exponential’, ‘custom’).
custom_weights (numpy.ndarray, optional) – The custom attention weights to use if ‘custom’ is selected.
size (int, optional) – The size of the attention window for predefined attention weights. Default is 5.
**kwargs (dict, optional) – Additional parameters for specific attention types (e.g., ‘ascending’ for linear, ‘sigma’ for gaussian, ‘base’ for exponential).
Returns:
filtered_signal – The filtered signal after applying the attention-based filter.
Apply a convolution-based filter to the signal using predefined or custom kernels.
Convolution-based filtering is a powerful technique that applies a kernel to the signal to
achieve various effects such as smoothing, sharpening, and edge detection. This method allows
for the use of standard convolutional kernels or custom kernels provided by the user.
Parameters:
kernel_type (str) – The type of kernel to use (‘smoothing’, ‘sharpening’, ‘edge_detection’, ‘custom’).
custom_kernel (numpy.ndarray, optional) – The custom kernel to use if ‘custom’ is selected. This must be provided by the user.
kernel_size (int, optional) – The size of the kernel for predefined kernels. Default is 3.
Returns:
filtered_signal – The filtered signal after convolution.
Apply ensemble filtering by combining the results of multiple filters using various ensemble techniques.
This method aggregates the results of different filtering techniques to achieve a more robust
and reliable filtered signal. Ensemble methods like mean, weighted mean, bagging, and boosting
are implemented to leverage the strengths of multiple filters.
Parameters:
filters (list of callable) – A list of filtering functions to apply. Each function should return a filtered signal.
method (str) – The ensemble method to use (‘mean’, ‘weighted_mean’, ‘bagging’, ‘boosting’).
weights (list of float, optional) – Weights for weighted mean if ‘weighted_mean’ is chosen. Should match the number of filters.
num_iterations (int, optional) – Number of iterations for bagging/boosting. Default is 10.
learning_rate (float, optional) – Learning rate for boosting. Default is 0.01.
>>> signal=np.array([1,2,3,4,5,6])>>> af=AdvancedSignalFiltering(signal)>>> filters=[af.kalman_filter,af.kalman_filter]# Example with the same filter twice>>> filtered_signal=af.ensemble_filtering(filters,method='mean')>>> print(filtered_signal)
Apply a gradient descent adaptive filter to the signal.
This method uses gradient descent to iteratively minimize the difference between the signal and a
target signal. It is useful in scenarios where the signal needs to be progressively adjusted to match
a desired outcome. If no target is provided, the filter will adapt towards zero (denoising).
Adaptive Filtering:
This is a true adaptive filter that adjusts the signal iteratively based on the gradient of the
error between the filtered signal and the target. The learning rate controls how quickly the
filter adapts, and the number of iterations determines convergence.
Parameters:
target (numpy.ndarray, optional) – The target signal to adapt to. If None, adapts towards zero (removes DC offset and trends).
Must have the same length as the input signal.
learning_rate (float, default=0.01) – Learning rate for the gradient descent, controlling how much the signal is adjusted at each step.
Typical range: 0.001 - 0.5
- Lower values (0.001-0.01): Slower convergence, more stable
- Higher values (0.1-0.5): Faster convergence, may oscillate
iterations (int, default=100) – Number of iterations for the gradient descent process.
More iterations = better convergence to target, but slower processing
Returns:
filtered_signal – The filtered signal after applying gradient descent adaptive filtering.
Example 2: Denoising without target (remove DC and trends)
>>> # Signal with DC offset and noise>>> signal=np.sin(2*np.pi*0.5*np.linspace(0,4,200))+2.0# DC offset = 2.0>>> signal+=0.2*np.random.randn(len(signal))# Add noise>>>>>> af=AdvancedSignalFiltering(signal)>>> denoised=af.gradient_descent_filter(learning_rate=0.05,iterations=200)>>>>>> print(f"Original mean (DC): {np.mean(signal):.3f}")>>> print(f"Filtered mean (DC removed): {np.mean(denoised):.3f}")
The Kalman filter provides real-time signal estimation and noise reduction, making it suitable
for scenarios where signal measurements are noisy and need continuous estimation.
Parameters:
signal (numpy.ndarray, optional) – The signal to be filtered. If not provided, self.signal will be used.
R (float) – Measurement noise covariance. A lower value assumes more trust in the measurements.
Q (float) – Process noise covariance. A lower value assumes less uncertainty in the model.
Apply an optimization-based filter using a custom or predefined loss function.
This method uses optimization techniques to filter the signal by minimizing a specified loss
function. It can be customized to use different types of loss functions, such as mean squared error (MSE),
mean absolute error (MAE), Huber loss, and more, depending on the specific needs of the signal processing task.
Parameters:
target (numpy.ndarray) – The target signal to compare against. The filter will attempt to make the signal closer to this target.
loss_type (str) – The type of loss function to use (‘mse’, ‘mae’, ‘huber’, ‘smooth_l1’, ‘log_cosh’, ‘quantile’, or ‘custom’).
custom_loss_func (callable, optional) – A custom loss function provided by the user if ‘custom’ is selected for loss_type.
initial_guess (float) – Initial guess for the filter parameter, used as a starting point in optimization.
learning_rate (float) – Learning rate for the optimization, determining the step size of each iteration.
iterations (int) – Number of iterations for the optimization process.
Returns:
filtered_signal – The filtered signal after optimization.
Time Domain Features Module for Physiological Signal Processing
This module provides comprehensive time-domain feature extraction capabilities for
physiological signals such as ECG and PPG. It implements standard Heart Rate Variability
(HRV) metrics and additional time-domain analysis methods for characterizing signal
variability and patterns.
Author: vitalDSP Team
Date: 2025-01-27
Version: 1.0.0
Key Features:
- Standard HRV metrics (SDNN, RMSSD, NN50, pNN50)
- Advanced time-domain statistics (median, IQR, CVNN)
- HRV Triangular Index and TINN computation
- Successive difference analysis (SDSD)
- Comprehensive NN interval analysis
Computes the Triangular Interpolation of NN Interval Histogram (TINN).
TINN is the baseline width of the NN interval histogram triangle,
computed as the difference between the points N and M on the x-axis
where the triangular interpolation reaches the baseline.
Frequency Domain Features Module for Physiological Signal Processing
This module provides comprehensive frequency-domain feature extraction capabilities
for physiological signals including ECG, PPG, and other vital signs. It implements
Heart Rate Variability (HRV) analysis in the frequency domain with power spectral
density computation and autonomic nervous system assessment.
Author: vitalDSP Team
Date: 2025-01-27
Version: 1.0.0
Key Features:
- Power Spectral Density (PSD) computation
- Low-Frequency (LF) and High-Frequency (HF) power analysis
- LF/HF ratio for autonomic balance assessment
- Ultra-Low-Frequency (ULF) and Very-Low-Frequency (VLF) analysis
- Normalized frequency domain metrics (LFnu, HFnu)
- Total power computation across frequency bands
- Comprehensive HRV frequency domain analysis
Computes all nonlinear features of the signal, with an option to skip
time-consuming methods.
Parameters:
include_complex_methods (bool, optional) – Whether to compute the time-consuming
methods: compute_sample_entropy, compute_approximate_entropy, and
compute_recurrence_features. If None (default), automatically enables for
signals with ≥50 NN intervals.
**kwargs – Additional parameters for specific feature computations.
Returns:
A dictionary containing all the computed features.
>>> nn_intervals=[800,810,790,805,795]# NN intervals in ms>>> ecg_signal=np.random.randn(1000)# Example ECG signal>>> fs=1000# Sampling frequency in Hz>>> hrv=HRVFeatures(nn_intervals,ecg_signal,fs)>>> all_features=hrv.compute_all_features()>>> print(all_features)
Physiological Features Module for Physiological Signal Processing
This module provides comprehensive capabilities for physiological
signal processing including ECG, PPG, EEG, and other vital signs.
Author: vitalDSP Team
Date: 2025-01-27
Version: 1.0.0
Key Features:
- Object-oriented design with comprehensive classes
- Multiple processing methods and functions
- NumPy integration for numerical computations
- SciPy integration for advanced signal processing
- Configurable parameters and settings
Computes the R-R intervals (for ECG) or P-P intervals (for PPG) from the detected peaks.
Optionally applies a correction method to fix false beat detections.
Parameters:
correction_method (str) – Method to correct false detections (‘interpolation’, ‘resampling’, or ‘adaptive_threshold’).
Default is None (no correction).
threshold (int) – Threshold for adaptive correction methods in milliseconds. Default is 150 ms.
Returns:
The corrected R-R or P-P intervals in milliseconds.
Computes the Detrended Fluctuation Analysis (DFA) of the signal. DFA is used to assess
the fractal scaling properties of time-series data, especially in physiological signals.
Parameters:
order (int) – The order of the polynomial fit for detrending. Default is 1 (linear detrending).
Computes the fractal dimension of the signal using Higuchi’s method. Fractal dimension
is a measure of complexity, reflecting how the signal fills space as its scale changes.
Computes the largest Lyapunov exponent (LLE) of the signal. LLE measures the rate at
which nearby trajectories in phase space diverge, indicating chaotic behavior in the signal.
Computes approximate recurrence features by sampling point pairs. This approach significantly
reduces computation time for large datasets by avoiding the full pairwise distance calculations.
Parameters:
threshold (float) – The threshold to define recurrences. Default is 0.2.
sample_size (int) – The number of point pairs to sample. Default is 10,000.
Returns:
A dictionary containing approximate recurrence rate, determinism, and laminarity.
Computes the sample entropy of the signal. Sample entropy is a measure of signal complexity,
specifically used for detecting the regularity and unpredictability of fluctuations in a signal.
Computes the amplitude (max-min) of the waveform for specified intervals.
Parameters:
interval_type (str, optional) – The interval type to calculate the amplitude for:
- For ECG:
- “R-to-S”: Between R peak and S valley.
- “R-to-Q”: Between Q valley and R peak.
- “P-to-Q”: Between P peak and Q valley.
- “T-to-S”: Between S valley and T peak.
- “T-to-Baseline”: Between T peak and baseline.
- “R-to-Baseline”: Between R peak and baseline.
- “S-to-Baseline”: Between S valley and baseline.
- For PPG:
- “Sys-to-Notch”: Between systolic peak and dicrotic notch.
- “Notch-to-Dia”: Between dicrotic notch and diastolic peak.
- “Sys-to-Dia”: Between systolic and diastolic peaks.
- “Sys-to-Baseline”: Between systolic peak and baseline.
- “Notch-to-Baseline”: Between dicrotic notch and baseline.
- “Dia-to-Baseline”: Between diastolic peak and baseline.
signal_type (str, optional) – The type of signal: “ECG” or “PPG”. Default is “ECG”.
compare_to_baseline (bool, optional) – If True, compute amplitudes relative to a baseline.
Returns:
List[float]
Return type:
A list of amplitude values for each interval within a single complex.
Examples
>>> amplitude_values=wm.compute_amplitude(interval_type="R-to-S",signal_type="ECG")>>> print(f"Amplitude values for each complex in R-to-S interval: {amplitude_values}")
Compute the curvature of the waveform at specified points or critical points.
Parameters:
points (list or np.array, optional) – Indices of the points where the curvature is to be calculated. Ignored if option is specified.
option (str, optional) – Specifies the critical points to use for curvature calculation. Options for ECG are “p_peaks”, “q_valleys”,
“r_peaks”, “s_valleys”, “t_peaks”. Options for PPG are “troughs”, “systolic_peaks”, “diastolic_peaks”.
window (int, optional) – The number of points before and after each point to consider for curvature calculation.
Returns:
curvatures – Curvature values at the specified points or critical points.
Return type:
np.array
Example
>>> waveform=np.sin(np.linspace(0,2*np.pi,100))>>> wm=WaveformMorphology(waveform,signal_type="PPG")>>> curvatures=wm.compute_curvature(option="systolic_peaks")>>> print(f"Curvatures at systolic peaks: {curvatures}")
Compute the skewness of each complex in the signal.
Parameters:
signal_type (str, optional) – The type of signal, either “ECG” or “PPG”. Default is “ECG”.
Returns:
List[float]
Return type:
A list of skewness values, one for each complex.
Examples
>>> signal=np.random.randn(1000)>>> extractor=PhysiologicalFeatureExtractor(signal)>>> skewness_values=extractor.compute_skewness()>>> print(f"Skewness values for each complex: {skewness_values}")
Compute the slope of the waveform at specified points or critical points.
Parameters:
points (list or np.array, optional) – Indices of the points where the slope is to be calculated. Ignored if option is specified.
option (str, optional) – Specifies the critical points to use for slope calculation. Options for ECG are “p_peaks”, “q_valleys”,
“r_peaks”, “s_valleys”, “t_peaks”. Options for PPG are “troughs”, “systolic_peaks”, “diastolic_peaks”.
window (int, optional) – The number of points before and after each point to consider for slope calculation.
Returns:
slopes – Slope values at the specified points or critical points.
Return type:
np.array
Example
>>> waveform=np.sin(np.linspace(0,2*np.pi,100))>>> wm=WaveformMorphology(waveform,signal_type="ECG")>>> slopes=wm.compute_slope(option="r_peaks")>>> print(f"Slopes at R peaks: {slopes}")
The interval type to calculate the volume for:
- For ECG:
- “P-to-T”: Entire complex from P peak to T peak.
- “R-to-S”: Between R peak and S valley.
- “R-to-Q”: Between Q valley and R peak.
- “P-to-Q”: Between P peak and Q valley.
- “T-to-S”: Between S valley and T peak.
- For PPG:
- “Sys-to-Notch”: Between systolic peak and dicrotic notch.
- “Notch-to-Dia”: Between dicrotic notch and diastolic peak.
- “Sys-to-Dia”: Between systolic and diastolic peaks.
- “Sys-to-Sys”: Between consecutive systolic peaks (full PPG complex).
Default is “P-to-T” for ECG and “Sys-to-Notch” for PPG.
signal_type (str, optional) – The type of signal: “ECG” or “PPG”. Default is “ECG”.
mode (str, optional) – The area computation method (“peak” or “trough”). Default is “peak”.
- “peak”: Computes the area under the curve.
- “trough”: Computes the area bounded by troughs.
Returns:
List[float] – within a single complex.
Return type:
A list of volume values, each representing the area for the specified interval
Examples
>>> volume_values=wm.compute_volume(interval_type="R-to-S",signal_type="ECG")>>> print(f"Volume values for each complex in R-to-S interval: {volume_values}")
Detects the troughs (valleys) in the PPG waveform between systolic peaks.
In simple_mode, uses the minimum value between adjacent peaks; otherwise, uses flat segment detection.
Parameters:
systolic_peaks (np.array) – Indices of detected systolic peaks in the PPG waveform.
Returns:
troughs – Indices of the detected troughs between the systolic peaks.
Computes the area of the specified interval in the signal with the chosen summary type.
Parameters:
interval_type (str) – Type of interval to compute the area for. Options include:
- For PPG: “Sys-to-Notch”, “Notch-to-Dia”, “Sys-to-Dia”
- For ECG: “R-to-Q”, “R-to-S”, “QRS”, “T-to-S”
signal_type (str, optional) – The type of signal: “PPG” or “ECG”. Default is “PPG”.
summary_type (str, optional) – Specifies the summary statistic to return:
- ‘mean’: Mean of all areas
- ‘median’: Median of all areas
- ‘2nd_quartile’: 25th percentile (1st quartile)
- ‘3rd_quartile’: 75th percentile (3rd quartile)
- ‘full’: Returns all areas as a list
Default is ‘mean’.
Returns:
The summary statistic for the area, or the list of all areas if summary_type is ‘full’.
Returns NaN if an error occurs.
Computes the duration based on detected start and end points for a specified session type.
Parameters:
start_points (list, optional) – List of detected start indices in the signal. If None, defaults based on session_type.
end_points (list, optional) – List of detected end indices in the signal. If None, defaults based on session_type.
session_type (str, optional) – Specifies the type of session: “systolic”, “diastolic”, “qrs”, or “Custom”.
- For “systolic” and “diastolic”, appropriate start and end points will be detected if not provided.
- For “qrs”, QRS sessions are automatically detected and do not require start or end points.
- For “Custom”, start and end points must be provided as arguments.
summary_type (str, optional) – Type of summary to apply to the computed durations. Options are ‘mean’, ‘median’,
‘2nd_quartile’, ‘3rd_quartile’, or ‘full’ (returns all durations).
Returns:
The summarized duration based on summary_type, or a list of durations if summary_type is ‘full’.
summary_type (str, optional) – Specifies the summary statistic to return:
- ‘mean’: Mean heart rate
- ‘median’: Median heart rate
- ‘2nd_quartile’: 25th percentile (1st quartile)
- ‘3rd_quartile’: 75th percentile (3rd quartile)
- ‘full’: Returns all computed heart rates as a list
Default is ‘mean’.
Returns:
The summary statistic for heart rates, or a list of all heart rates if summary_type is ‘full’.
Returns NaN if an error occurs.
Calculate the trend slope of peak values using specified method.
Parameters:
peaks (list or np.ndarray) – The y-values of detected peaks (peak amplitudes).
method (str, optional) – The method to calculate trend slope. Options are ‘linear_regression’,
‘moving_average’, and ‘rate_of_change’. Default is ‘linear_regression’.
window_size (int, optional) – The window size for moving average calculation. Used only when method=’moving_average’.
Default is 5.
Returns:
The calculated trend slope. If ‘moving_average’ method is selected, returns an array of slopes.
Calculates the QRS amplitude by finding the maximum amplitude
between the R-to-S and R-to-Q intervals.
Parameters:
summary_type (str, optional) – Specifies the summary statistic to return:
- ‘mean’: Mean of all QRS amplitudes
- ‘median’: Median of all QRS amplitudes
- ‘2nd_quartile’: 25th percentile (1st quartile)
- ‘3rd_quartile’: 75th percentile (3rd quartile)
- ‘full’: Returns all QRS amplitudes as a list
Default is ‘mean’.
Returns:
The summary statistic for QRS amplitude, or the list of amplitudes if summary_type is ‘full’.
Returns NaN if an error occurs.
Computes the skewness of the signal based on the specified signal type and summary type.
Parameters:
signal_type (str, optional) – Type of signal to compute skewness for (e.g., ‘PPG’ or ‘ECG’). Default is ‘PPG’.
summary_type (str, optional) – Type of summary to apply to the computed skewness values. Options are ‘mean’, ‘median’,
‘2nd_quartile’, ‘3rd_quartile’, or ‘full’ (returns all skewness values).
Returns:
The summarized skewness value, or a list of skewness values if summary_type is ‘full’.
Computes the slope of the specified type (systolic, diastolic, QRS) using the chosen summary type.
Parameters:
slope_type (str, optional) – The type of slope to calculate. Options are:
- “systolic”: Slope from systolic peaks.
- “diastolic”: Slope from diastolic peaks.
- “qrs”: Slope from QRS R peaks.
Default is “systolic”.
window (int, optional) – The window size for slope calculation. Default is 5.
summary_type (str, optional) – Specifies the summary statistic to return:
- ‘mean’: Mean of all slopes
- ‘median’: Median of all slopes
- ‘2nd_quartile’: 25th percentile (1st quartile)
- ‘3rd_quartile’: 75th percentile (3rd quartile)
- ‘full’: Returns all slopes as a list
Default is ‘mean’.
Returns:
The summary statistic for the slopes, or the list of slopes if summary_type is ‘full’.
Returns NaN if an error occurs.
Respiratory Analysis Module for Physiological Signal Processing
This module provides comprehensive respiratory analysis capabilities for physiological
signals including PPG, ECG, and other vital signs. It implements multiple methods
for respiratory rate estimation including time-domain counting, frequency-domain
analysis, and advanced signal processing techniques.
Author: vitalDSP Team
Date: 2025-01-27
Version: 1.0.0
A class for analyzing respiratory patterns in physiological signals (e.g., PPG, ECG),
with built-in preprocessing, filtering, and noise reduction options.
Computes the respiratory rate from the signal after applying preprocessing,
using peak detection or zero-crossing methods.
Parameters:
method (str, optional) – Method used for breath detection. Options: ‘peaks’, ‘zero_crossing’, ‘time_domain’, ‘frequency_domain’, ‘fft_based’, ‘counting’. Default is ‘counting’.
correction_method (str, optional) – Method for correcting false detections. Options: ‘interpolation’, ‘adaptive_threshold’. Default is None.
min_breath_duration (float, optional) – Minimum breath duration in seconds. Default is 0.5s (30 breaths/min).
max_breath_duration (float, optional) – Maximum breath duration in seconds. Default is 6s (10 breaths/min).
preprocess_config (PreprocessConfig, optional) – Configuration for signal preprocessing (filtering and noise reduction). Default is None, and a default PreprocessConfig will be used.
Returns:
The estimated respiratory rate in breaths per minute.
Compute respiratory rate using multiple methods and return consensus estimate.
This method runs multiple RR estimation algorithms in parallel and combines
their results using median consensus, providing a more robust estimate than
any single method. It also returns quality metrics including standard deviation
and confidence scores.
Parameters:
preprocess_config (PreprocessConfig, optional) – Configuration for signal preprocessing. If None, uses default configuration.
methods (list of str, optional) – List of methods to use for ensemble estimation.
Default: [‘counting’, ‘fft_based’, ‘frequency_domain’, ‘time_domain’]
Available methods:
- ‘counting’: Peak detection with interval analysis (peak_detection_rr)
- ‘fft_based’: Fast Fourier Transform based estimation
- ‘frequency_domain’: Welch PSD based estimation
- ‘time_domain’: Autocorrelation based estimation
- ‘peaks’: Peak detection (old method, uses _detect_breaths_by_peaks)
- ‘zero_crossing’: Zero-crossing detection
Returns:
Dictionary containing:
- ‘respiratory_rate’: float, consensus RR estimate (median of valid methods)
- ‘individual_estimates’: dict, per-method estimates (method_name: rr_value)
- ‘std’: float, standard deviation across methods (measure of agreement)
- ‘confidence’: float, confidence score (0-1 scale, higher = more agreement)
- ‘n_methods’: int, number of methods that returned valid estimates
- ‘quality’: str, quality rating (‘high’, ‘medium’, ‘low’)
>>> # Use specific methods only>>> result=resp.compute_respiratory_rate_ensemble(methods=['fft_based','frequency_domain'])
Notes
The ensemble approach provides several advantages:
1. More robust to method-specific failures
2. Automatic outlier rejection (methods with implausible results)
3. Quality/confidence metrics for result interpretation
4. Better performance on noisy or challenging signals
Confidence scoring:
- confidence > 0.8: High agreement (std < 1 BPM) - Very reliable
- confidence 0.5-0.8: Medium agreement (std 1-3 BPM) - Reliable
- confidence < 0.5: Low agreement (std > 3 BPM) - Use with caution
If fewer than 2 methods return valid estimates, confidence is automatically
set to 0.3 (low) as consensus requires multiple opinions.
Estimate respiratory rate using the FFT (Fast Fourier Transform) method
with respiratory band filtering.
This method computes the FFT power spectrum and identifies the dominant
frequency within the physiological respiratory range (0.1-0.5 Hz or 6-30 BPM).
The respiratory band filtering prevents false detection of cardiac frequencies
or high-frequency noise artifacts.
Parameters:
signal (numpy.ndarray) – The input respiratory signal.
sampling_rate (float) – The sampling rate of the signal in Hz.
preprocess (str, optional) – The preprocessing method to apply before estimation (e.g., “bandpass”, “wavelet”).
freq_min (float, optional (default=0.1)) – Minimum respiratory frequency in Hz (6 BPM). Can be adjusted for different
populations (e.g., 0.05 Hz for very slow breathing).
freq_max (float, optional (default=0.5)) – Maximum respiratory frequency in Hz (30 BPM). Can be increased to 0.67 Hz
(40 BPM) for tachypnea or exercise conditions.
preprocess_kwargs (dict, optional) – Additional arguments for the preprocessing function.
Returns:
rr – Estimated respiratory rate in breaths per minute.
>>> # For exercise/tachypnea (up to 40 BPM)>>> rr=fft_based_rr(signal,sampling_rate=100,freq_max=0.67)
Notes
This implementation fixes the critical bug in the original version that searched
the entire frequency spectrum, often picking cardiac frequencies (1-2 Hz / 60-120 BPM)
instead of respiratory frequencies. The corrected version restricts the search to
the physiological respiratory band.
The method includes SNR validation to warn if the detected peak has low prominence,
which may indicate poor signal quality or unreliable estimation.
Estimate respiratory rate using peak detection with interval-based analysis.
This method detects breathing peaks in the respiratory signal and calculates
the rate based on the median interval between peaks, which is more robust than
simple peak counting. Peaks are validated against physiological constraints
(1.5-10 seconds between breaths).
Parameters:
signal (numpy.ndarray) – The input respiratory signal.
sampling_rate (float) – The sampling rate of the signal in Hz.
preprocess (str, optional) – The preprocessing method to apply before estimation (e.g., “bandpass”, “wavelet”).
min_peak_distance (float, optional (default=1.5)) – Minimum distance between peaks in seconds. Default 1.5s corresponds to 40 BPM max.
Previous default of 0.5s was too permissive (allowed 120 BPM).
height (float or None, optional) – Minimum height required for a peak. If None, uses adaptive threshold.
threshold (float or None, optional) – Minimum difference between a peak and its neighboring points.
prominence (float or None, optional) – Minimum prominence of peaks. If None, uses adaptive prominence (0.3 * signal std).
width (int or None, optional) – Minimum width required for a peak, measured as the number of samples.
preprocess_kwargs (dict, optional) – Additional arguments for the preprocessing function.
Returns:
rr – Estimated respiratory rate in breaths per minute.
>>> # For faster breathing (exercise), reduce min_peak_distance>>> rr=peak_detection_rr(signal,sampling_rate=100,min_peak_distance=1.0)
Notes
This implementation fixes critical bugs in the original version:
1. Changed from simple peak counting (num_peaks / duration) to interval-based analysis
2. Increased min_peak_distance default from 0.5s to 1.5s (40 BPM max instead of 120 BPM)
3. Added adaptive prominence threshold if not specified
4. Validates intervals against physiological range (1.5-10 seconds)
5. Uses median interval for robustness against outliers
6. Adds quality metrics (coefficient of variation) for breathing regularity
The interval-based approach is more accurate because:
- Handles irregular breathing patterns
- Robust to missed or false peaks
- Provides quality metrics for result confidence
Detect sleep apnea events based on amplitude thresholding.
Parameters:
signal (numpy.ndarray) – The input respiratory signal.
sampling_rate (float) – The sampling rate of the signal in Hz.
threshold (float) – Amplitude threshold below which an apnea event is detected.
min_duration (int, optional (default=10)) – Minimum duration (in seconds) for which the signal must be below the threshold to be considered an apnea event.
preprocess (str, optional) – The preprocessing method to apply before detection (e.g., “bandpass”, “wavelet”).
preprocess_kwargs (dict, optional) – Additional arguments for the preprocessing function.
Returns:
apnea_events – List of apnea events, each represented as a tuple (start_time, end_time) in seconds.
Fourier Transform Module for Physiological Signal Processing
This module provides comprehensive Fourier Transform capabilities for analyzing
the frequency content of physiological signals such as ECG, EEG, and PPG. It
implements both Discrete Fourier Transform (DFT) and Inverse Discrete Fourier
Transform (IDFT) with signal validation and error handling.
Author: vitalDSP Team
Date: 2025-01-27
Version: 1.0.0
Key Features:
- Discrete Fourier Transform (DFT) computation
- Inverse Discrete Fourier Transform (IDFT) reconstruction
- Signal validation and error handling
- Frequency domain analysis capabilities
- Signal reconstruction from frequency components
>>> # Zero out high frequencies>>> filtered_spectrum=frequency_spectrum.copy()>>> filtered_spectrum[20:]=0>>> filtered_signal=ft.compute_idft(filtered_spectrum)
A class to perform Fourier Transform for analyzing the frequency content in signals such as ECG/EEG.
The Fourier Transform is a mathematical technique that transforms a time-domain signal into its constituent
frequencies, providing insights into the signal’s frequency content. This class allows for both the computation
of the Discrete Fourier Transform (DFT) and the Inverse Discrete Fourier Transform (IDFT), making it possible
to analyze and reconstruct signals.
compute_dft:method
Computes the Discrete Fourier Transform (DFT) of the signal.
compute_idft:method
Computes the Inverse Discrete Fourier Transform (IDFT) to reconstruct the signal.
Compute the Discrete Fourier Transform (DFT) of the input signal.
The DFT converts the time-domain signal into the frequency domain, allowing for the analysis of its frequency
components. This is particularly useful in identifying periodicities, filtering, and spectral analysis of
biomedical signals like ECG and EEG.
Returns:
The frequency domain representation of the signal, where each element corresponds to a specific
frequency component.
Compute the Inverse Discrete Fourier Transform (IDFT) to reconstruct the time-domain signal.
The IDFT converts the frequency-domain data back into the time domain, reconstructing the original signal
from its frequency components. This is useful for understanding how different frequency components contribute
to the overall signal and for signal reconstruction after processing in the frequency domain.
Parameters:
frequency_content (numpy.ndarray) – The frequency domain representation of the signal, as obtained from the DFT.
Returns:
The time-domain signal reconstructed from its frequency components.
Wavelet Transform Module for Physiological Signal Processing
This module provides comprehensive wavelet transform capabilities for physiological
signals including ECG, PPG, EEG, and other vital signs. It implements Discrete
Wavelet Transform (DWT) with multiple mother wavelets and inverse transform
capabilities for signal analysis and reconstruction.
Author: vitalDSP Team
Date: 2025-01-27
Version: 1.0.0
Key Features:
- Discrete Wavelet Transform (DWT) implementation
- Multiple mother wavelets (Haar, Daubechies, Coiflets, etc.)
- Inverse Wavelet Transform for signal reconstruction
- Multi-level decomposition capabilities
- Signal length preservation options
- Integration with mother wavelet utilities
Perform the Discrete Wavelet Transform (DWT) on the signal.
Parameters:
level (int, optional) – The number of decomposition levels (default is 1).
Returns:
Wavelet coefficients as a list of arrays. coeffs[0] through
coeffs[-2] are detail coefficient arrays (one per level, in
order from finest to coarsest). coeffs[-1] is the final
approximation array.
A class to perform Discrete Cosine Transform (DCT) and its inverse (IDCT).
The DCT is widely used in signal processing, particularly for data compression (such as JPEG image compression)
and feature extraction. It transforms a signal from the time domain into the frequency domain, where most of the
signal information is compacted into a few coefficients. The inverse DCT (IDCT) reconstructs the signal from its
DCT coefficients.
compute_dct:method
Computes the DCT of the signal.
compute_idct:method
Computes the Inverse DCT to reconstruct the signal.
Compress the signal by zeroing out small DCT coefficients.
This method removes high-frequency components that are below the threshold, which often represent noise,
improving the signal’s reconstruction quality and reducing its size.
Parameters:
threshold (float, optional) – The threshold below which DCT coefficients are set to zero. Default is 0.1.
Compute the Discrete Cosine Transform (DCT) of the signal.
The DCT converts the input signal from the time domain to the frequency domain, emphasizing the low-frequency
components of the signal. It is particularly effective for compressing signals because it tends to concentrate
most of the signal’s energy into a few low-frequency components.
Parameters:
norm (str, optional) – Normalization type. Default is ‘ortho’ for orthogonal DCT, which is usually recommended.
Compute the Inverse Discrete Cosine Transform (IDCT) to reconstruct the signal.
The IDCT converts the DCT coefficients back into the time domain, reconstructing the original signal.
This process is essential in applications like image compression, where the signal is compressed using DCT
and then decompressed back to its original form using IDCT.
Parameters:
dct_coefficients (numpy.ndarray) – The DCT coefficients of the signal.
norm (str, optional) – Normalization type. Default is ‘ortho’ for orthogonal DCT.
Returns:
The time-domain signal reconstructed from its DCT coefficients.
A class to perform the Hilbert Transform, which is used to generate analytic signals.
The Hilbert Transform is a key tool in signal processing, particularly for generating the analytic signal from a real-valued signal. The analytic signal is complex, with the original signal as the real part and the Hilbert transform as the imaginary part. This is particularly useful in applications like QRS detection in ECG signals, where phase and amplitude information are crucial.
compute_hilbert:method
Computes the Hilbert Transform of the signal to obtain the analytic signal.
Compute the Hilbert Transform to obtain the analytic signal.
The Hilbert Transform is applied in the frequency domain by first taking the Fourier transform of the input signal, modifying the Fourier coefficients to zero out the negative frequencies, and then applying the inverse Fourier transform. This process effectively shifts the signal in such a way that the imaginary part represents the phase information, while the real part remains the original signal.
Returns:
The analytic signal with both real and imaginary components, where the real part is the original signal and the imaginary part is the Hilbert transform.
The analytic signal is often used in applications where the instantaneous amplitude and phase of the signal are required, such as in the detection of QRS complexes in ECG signals, modulation, and demodulation in communications, and envelope detection in various signal processing tasks.
Compute the envelope of the signal using the Hilbert Transform.
The envelope is the magnitude of the analytic signal and represents the instantaneous amplitude of the signal.
This is particularly useful in applications such as PPG signal analysis, where the envelope can be used to
assess pulse amplitude variations.
Compute the instantaneous phase of the signal using the Hilbert Transform.
The instantaneous phase is the phase angle of the analytic signal and is useful in applications such as
ECG analysis, where phase information can help in detecting the QRS complex or other waveform characteristics.
Anomaly Detection Module for Physiological Signal Processing
This module provides comprehensive anomaly detection capabilities for physiological
signals including ECG, PPG, EEG, and other vital signs. It implements multiple
detection methods including statistical approaches, machine learning techniques,
and frequency-domain analysis for identifying unusual patterns and artifacts.
Author: vitalDSP Team
Date: 2025-01-27
Version: 1.0.0
Key Features:
- Statistical anomaly detection (Z-score, IQR)
- Moving average and rolling window methods
- Local Outlier Factor (LOF) for density-based detection
- Fourier-based frequency domain analysis
- Real-time streaming anomaly detection
- Performance monitoring and optimization
Comprehensive Anomaly Detection for detecting anomalies in real-time from streaming data.
This class offers multiple methods to detect anomalies in a given signal, including statistical methods,
moving averages, Local Outlier Factor (LOF), and Fourier-based methods.
detect_anomalies:function
Detects anomalies using various methods including z-score, moving average, custom LOF, and more.
Examples
>>> importnumpyasnp>>> fromvitalDSP.advanced_computation.anomaly_detectionimportAnomalyDetection>>>>>> # Example 1: Z-score anomaly detection>>> signal=np.sin(np.linspace(0,10,100))+np.random.normal(0,0.1,100)>>> anomaly_detector=AnomalyDetection(signal)>>> anomalies_z_score=anomaly_detector.detect_anomalies(method="z_score",threshold=2.0)>>> print(f"Z-score anomalies: {len(anomalies_z_score)}")>>>>>> # Example 2: Moving average anomaly detection>>> anomalies_moving_avg=anomaly_detector.detect_anomalies(method="moving_average",window_size=5,threshold=0.5)>>> print(f"Moving average anomalies: {len(anomalies_moving_avg)}")>>>>>> # Example 3: LOF anomaly detection>>> anomalies_lof=anomaly_detector.detect_anomalies(method="lof",n_neighbors=20)>>> print(f"LOF anomalies: {len(anomalies_lof)}")>>>>>> # Example 4: FFT-based anomaly detection>>> anomalies_fft=anomaly_detector.detect_anomalies(method="fft",threshold=0.1)>>> print(f"FFT anomalies: {len(anomalies_fft)}")
Detect anomalies in the signal using the specified method.
Parameters:
method (str, optional) – The method to use for detecting anomalies. Options: ‘z_score’, ‘moving_average’, ‘lof’, ‘fft’, ‘threshold’.
Default is ‘z_score’.
**kwargs (additional arguments) – Additional parameters depending on the chosen method.
Advanced Computation Module for Physiological Signal Processing
This module provides Bayesian inference and optimization capabilities for physiological
signal processing including ECG, PPG, EEG, and other vital signs.
Author: vitalDSP Team
Date: 2025-01-27
Version: 1.0.1
Key Features:
- Gaussian Process modeling for signal prediction
- Bayesian Optimization for hyperparameter tuning
- Expected Improvement acquisition function
- Numerical stability improvements
- Graceful error handling
Numerical Stability: The default noise parameter (1e-5) provides good numerical
stability. Avoid using values smaller than 1e-7 as they can cause singular matrix errors.
Initialization: BayesianOptimization starts with 3 random samples to bootstrap
the Gaussian Process before beginning optimization.
Fallback: If numerical issues occur during optimization, the algorithm falls
back to random sampling to ensure it always returns a result.
A class implementing Bayesian Optimization for parameter tuning.
Bayesian Optimization is an efficient method for finding the minimum or maximum of an objective function, especially when the function is expensive to evaluate. It uses a Gaussian Process model to predict the function’s behavior and an acquisition function to determine the next point to sample.
The EI acquisition function is used to balance exploration and exploitation in Bayesian Optimization. It selects points that have the potential to improve upon the best observed value.
Parameters:
X (numpy.ndarray) – The points at which to evaluate the acquisition function.
xi (float, optional) – Exploration-exploitation trade-off parameter (default is 0.01).
The optimization process starts with a random sample to initialize the Gaussian Process,
then iteratively selects new points using the acquisition function to balance exploration
and exploitation.
A class implementing Gaussian Process (GP) for Bayesian Optimization.
Gaussian Process models are used to predict the mean and variance of an unknown function based on observed data. This is particularly useful in Bayesian Optimization, where the GP helps in selecting the next sample point to evaluate.
A comprehensive neural network-based filtering approach for adaptive signal processing.
This class supports various neural network architectures, including feedforward, convolutional, and recurrent networks.
It includes advanced features such as dropout for regularization, batch normalization for faster convergence,
and customizable training options.
train:method
Trains the neural network on the given signal.
apply_filter:method
Applies the trained neural network to filter the signal.
evaluate:method
Evaluates the performance of the neural network on a test signal.
The signal is divided into input-output pairs for supervised training.
This method prepares the data, then trains the network using the specified learning rate,
number of epochs, and batch size.
A comprehensive Reinforcement Learning Filter for adaptive signal processing.
This class provides methods to train filters using reinforcement learning algorithms such as
Q-learning, Deep Q-Networks (DQN), and Proximal Policy Optimization (PPO), and to apply these trained
filters to signals.
Q-learning updates a Q-table where each state-action pair has a value. The algorithm iterates over
episodes and updates the Q-values based on the reward received for each action taken.
Parameters:
episodes (int, optional) – The number of training episodes (default is 1000).
alpha (float, optional) – The learning rate (default is 0.1).
gamma (float, optional) – The discount factor (default is 0.99).
epsilon (float, optional) – The exploration rate for epsilon-greedy policy (default is 0.1).
Empirical Mode Decomposition (EMD) Module for Physiological Signal Processing
This module provides comprehensive Empirical Mode Decomposition capabilities for
physiological signals including ECG, PPG, EEG, and other vital signs. EMD is
particularly effective for analyzing non-linear and non-stationary signals by
decomposing them into Intrinsic Mode Functions (IMFs).
Author: vitalDSP Team
Date: 2025-01-27
Version: 1.0.0
Key Features:
- Empirical Mode Decomposition (EMD) implementation
- Intrinsic Mode Functions (IMFs) extraction
- Non-linear and non-stationary signal analysis
- Customizable stop criteria and IMF limits
- Signal decomposition and reconstruction
- Advanced signal analysis capabilities
Empirical Mode Decomposition (EMD) for decomposing non-linear and non-stationary signals into Intrinsic Mode Functions (IMFs).
EMD is particularly useful for analyzing signals that are non-linear and non-stationary, where traditional methods like Fourier Transform may not be effective.
emd:method
Performs the EMD on the input signal and returns the IMFs.
Examples
>>> importnumpyasnp>>> fromvitalDSP.advanced_computation.emdimportEMD>>>>>> # Example 1: Basic EMD decomposition>>> signal=np.sin(np.linspace(0,10,100))+0.5*np.random.normal(size=100)>>> emd=EMD(signal)>>> imfs=emd.emd()>>> print(f"Number of IMFs: {len(imfs)}")>>>>>> # Example 2: EMD with limited number of IMFs>>> emd_limited=EMD(signal)>>> imfs_limited=emd_limited.emd(max_imfs=3)>>> print(f"Limited IMFs: {len(imfs_limited)}")>>>>>> # Example 3: EMD with custom stop criterion>>> emd_custom=EMD(signal)>>> imfs_custom=emd_custom.emd(stop_criterion=0.01)>>> print(f"Custom stop criterion IMFs: {len(imfs_custom)}")
Perform Empirical Mode Decomposition (EMD) on the input signal.
Parameters:
max_imfs (int or None, optional) – Maximum number of IMFs to extract. If None, extract all possible IMFs (default is None).
stop_criterion (float, optional) – The stopping criterion for the sifting process, which controls how close the IMF is to an ideal IMF (default is 0.05).
max_sifting_iterations (int, optional) – Maximum number of sifting iterations per IMF to prevent infinite loops (default is 20).
max_decomposition_iterations (int, optional) – Maximum number of decomposition iterations to prevent excessive computation (default is 10).
Returns:
imfs – A list of IMFs (Intrinsic Mode Functions) extracted from the signal.
Each IMF represents a simple oscillatory mode embedded in the signal. The sum of all IMFs plus the final residual will reconstruct the original signal.
OPTIMIZATION: Added convergence limits to prevent infinite loops and improve reliability.
Deep Learning Models Module for Physiological Signal Processing
This module provides state-of-the-art deep learning architectures for physiological
signal analysis including ECG, PPG, EEG, and other vital signs. It implements
comprehensive neural network models with support for both TensorFlow and PyTorch
frameworks, enabling advanced signal classification, anomaly detection, and
sequence modeling.
Author: vitalDSP Team
Date: 2025-01-27
Version: 1.0.0
Key Features:
- 1D CNN for signal classification and feature extraction
- LSTM networks for sequence modeling and temporal dependencies
- Transformer architectures for long-range dependencies
- Autoencoder models for anomaly detection and denoising
- Pre-trained models and transfer learning capabilities
- Model training utilities and callbacks
- Cross-framework compatibility (TensorFlow/PyTorch)
Autoencoder Models Module for Physiological Signal Processing
This module provides comprehensive autoencoder architectures for physiological
signal analysis including ECG, PPG, EEG, and other vital signs. It implements
various autoencoder types for unsupervised anomaly detection, signal denoising,
dimensionality reduction, and feature learning.
Author: vitalDSP Team
Date: 2025-01-27
Version: 1.0.0
Key Features:
- Standard Autoencoder for basic reconstruction
- Variational Autoencoder (VAE) for probabilistic modeling
- Denoising Autoencoder for noise reduction
- Convolutional Autoencoder for spatial feature learning
- LSTM Autoencoder for temporal sequence modeling
- Comprehensive training and evaluation utilities
- Model saving and loading capabilities
vitalDSP Transformer Model for Physiological Signals
State-of-the-art Transformer architecture for long-range dependency modeling
in physiological signal analysis.
Features:
- Multi-head self-attention mechanism
- Positional encoding
- Layer normalization
- Feed-forward networks with residual connections
- Encoder-only (BERT-style) and Encoder-Decoder architectures
- Optimized for 1D time series data
Applications:
- Long ECG signal classification
- Multi-lead ECG interpretation
- EEG temporal pattern recognition
- Long-term signal forecasting
- Sequence-to-sequence tasks
Transformer model for physiological signal analysis.
Features:
- Multi-head self-attention for capturing long-range dependencies
- Positional encoding for sequence information
- Stacked encoder layers
- Classification or regression head
- Optimized for 1D time series
Parameters:
input_shape (tuple) – Shape of input sequences (sequence_length, n_features)
n_classes (int) – Number of output classes (use 1 for regression)
d_model (int, default=128) – Dimension of model (embedding dimension)
n_heads (int, default=8) – Number of attention heads
n_layers (int, default=4) – Number of transformer encoder layers
d_ff (int, default=512) – Dimension of feed-forward network
Use Cases:
- Adapt ECG classifier to different populations
- Transfer from large to small datasets
- Cross-domain transfer (e.g., ECG to PPG)
- Personalized model adaptation
Adapts model from source domain to target domain when distributions differ.
Techniques:
- Feature alignment
- Adversarial training
- Maximum Mean Discrepancy (MMD)
Use cases:
- Transfer from one patient population to another
- Adapt across different sensors/devices
- Cross-institution transfer
Examples
>>> fromvitalDSP.ml_models.transfer_learningimportDomainAdapter>>> importnumpyasnp>>>>>> # Source and target data from different domains>>> X_source=np.random.randn(500,187,1)>>> y_source=np.random.randint(0,3,500)>>> X_target=np.random.randn(100,187,1)*1.5# Different distribution>>>>>> # Create domain adapter>>> adapter=DomainAdapter(base_model,method='mmd')>>> adapter.fit(X_source,y_source,X_target,epochs=50)
Unfreezes part or all of the base model and trains with a small learning rate.
Best when:
- Target dataset is moderately sized
- Target task differs somewhat from source task
- More accuracy is needed than feature extraction provides
Strategies:
1. All-at-once: Unfreeze all layers, train with small LR
2. Progressive: Gradually unfreeze layers from top to bottom
3. Discriminative: Use different learning rates for different layers
Examples
>>> fromvitalDSP.ml_models.transfer_learningimportFineTuner>>> importnumpyasnp>>>>>> # Assuming we have a base model>>> finetuner=FineTuner(base_model,strategy='progressive')>>>>>> # Fine-tune with progressive unfreezing>>> X_train=np.random.randn(500,187,1)>>> y_train=np.random.randint(0,4,500)>>> finetuner.fit(X_train,y_train,n_classes=4,epochs=30)
Uses pre-trained model as fixed feature extractor, only training
new classification/regression head.
This is the fastest approach and works well when:
- Source and target tasks are similar
- Target dataset is small
- Computational resources are limited
Examples
>>> fromvitalDSP.ml_models.transfer_learningimportTransferFeatureExtractor>>> importnumpyasnp>>> importtensorflowastf>>>>>> # Create base model>>> base=tf.keras.Sequential([... tf.keras.layers.Conv1D(32,7,input_shape=(187,1)),... tf.keras.layers.MaxPooling1D(2),... tf.keras.layers.GlobalAveragePooling1D()... ])>>>>>> # Create feature extractor>>> extractor=TransferFeatureExtractor(base,freeze_base=True)>>>>>> # Fit to new task>>> X=np.random.randn(100,187,1)>>> y=np.random.randint(0,3,100)>>> extractor.fit(X,y,n_classes=3,epochs=10)
Pre-trained Models for Physiological Signal Analysis
This module provides a repository of pre-trained models for common physiological
signal analysis tasks, enabling transfer learning and quick deployment.
Features:
- Automatic model download and caching
- Pre-trained models for ECG, PPG, EEG
- Model versioning and metadata
- Easy fine-tuning interface
Provides utilities for:
- Listing available models
- Downloading models
- Managing model cache
- Model comparison
Examples
>>> fromvitalDSP.ml_models.pretrained_modelsimportModelHub>>>>>> # List all available models>>> hub=ModelHub()>>> models=hub.list_models()>>> print(models)>>>>>> # Filter models by signal type>>> ecg_models=hub.list_models(signal_type='ecg',task='classification')>>> print(ecg_models)>>>>>> # Download specific model>>> model=hub.get_model('ecg_classifier_mitbih')
ECG Autonomic Features Module for Physiological Signal Processing
This module provides comprehensive ECG feature extraction capabilities focusing
on autonomic nervous system analysis. It implements advanced algorithms for
detecting ECG waveform components, computing intervals, and identifying
arrhythmias for cardiovascular health assessment.
Author: vitalDSP Team
Date: 2025-01-27
Version: 1.0.0
Computes Respiratory Sinus Arrhythmia (RSA) from the PPG signal
by bandpass filtering pulse-to-pulse intervals in the respiratory
frequency band (0.15-0.4 Hz).
Returns:
RSA amplitude (std of respiratory-band pulse interval variability)
A class to extract various physiological features from ECG and PPG signals, such as durations,
areas, amplitude variability, slope ratios, and dicrotic notch locations.
Features for PPG:
- Systolic/diastolic duration, area, amplitude variability
- Signal skewness, slope, peak trends, and dicrotic notch locations
Features for ECG:
- QRS duration, area, T-wave area
- Amplitude variability, QRS-T ratios, and QRS slope
- Signal skewness, peak trends
Extract all physiological features from the signal for either ECG or PPG.
Parameters:
signal_type (str, optional) – The type of signal. Options: ‘ECG’, ‘PPG’, ‘EEG’. Default is “ECG”.
preprocess_config (PreprocessConfig, optional) – The configuration object for signal preprocessing. If None, default settings are used.
peak_config (dict, optional) – Configuration for peak detection parameters. If None, default settings are used.
Returns:
features – A dictionary containing the extracted features, such as durations, areas, amplitude variability,
slopes, skewness, peak trends, and dicrotic notch locations.
Signal Quality Assessment Module for Physiological Signal Processing
This module provides comprehensive signal quality assessment capabilities for
physiological signals including ECG, PPG, and other vital signs. It implements
various quality metrics such as Signal-to-Noise Ratio (SNR), Peak Signal-to-Noise
Ratio (PSNR), and Mean Square Error (MSE) to evaluate signal quality and the
impact of noise or processing artifacts.
Author: vitalDSP Team
Date: 2025-01-27
Version: 1.0.0
Key Features:
- Signal-to-Noise Ratio (SNR) computation
- Peak Signal-to-Noise Ratio (PSNR) calculation
- Mean Square Error (MSE) assessment
- Noise-based SNR estimation
- Comprehensive quality metrics for signal evaluation
A class to assess the quality of signals using various metrics.
This class provides methods to compute commonly used signal quality metrics such as
Signal-to-Noise Ratio (SNR), Peak Signal-to-Noise Ratio (PSNR), and Mean Square Error (MSE).
It can be used to evaluate the impact of noise or other processing on the original signal.
snr:function
Computes the Signal-to-Noise Ratio.
psnr:function
Computes the Peak Signal-to-Noise Ratio.
mse:function
Computes the Mean Square Error between the original and processed signals.
snr_of_noise:function
Computes the Signal-to-Noise Ratio given a noise signal.
Examples
>>> importnumpyasnp>>> fromvitalDSP.signal_quality_assessment.signal_qualityimportSignalQuality>>>>>> # Example 1: Basic signal quality assessment>>> original_signal=np.sin(np.linspace(0,10,1000))>>> noise=np.random.normal(0,0.1,1000)>>> noisy_signal=original_signal+noise>>> sq=SignalQuality(original_signal,noisy_signal)>>> print(f"SNR: {sq.snr():.2f} dB")>>> print(f"PSNR: {sq.psnr():.2f} dB")>>> print(f"MSE: {sq.mse():.6f}")>>>>>> # Example 2: Quality assessment with different noise levels>>> high_noise=np.random.normal(0,0.5,1000)>>> very_noisy_signal=original_signal+high_noise>>> sq_high_noise=SignalQuality(original_signal,very_noisy_signal)>>> print(f"High noise SNR: {sq_high_noise.snr():.2f} dB")>>>>>> # Example 3: Using noise signal directly>>> sq_noise=SignalQuality(original_signal)>>> snr_from_noise=sq_noise.snr_of_noise(noise)>>> print(f"SNR from noise: {snr_from_noise:.2f} dB")
Compute the Mean Square Error (MSE) between the original and processed signals.
MSE measures the average squared difference between the original and processed signals.
It is commonly used to quantify the error introduced by noise or signal processing.
Compute the Peak Signal-to-Noise Ratio (PSNR) of the signal.
PSNR compares the maximum possible signal power to the noise power. It is commonly
used in image and signal processing to assess the quality of signal reconstruction
or compression techniques.
Compute the Signal-to-Noise Ratio (SNR) of the signal.
SNR is a measure of signal quality that compares the level of the desired signal
to the level of background noise. A higher SNR indicates a cleaner signal with
less noise.
Compute the Signal-to-Noise Ratio (SNR) given a noise signal.
This method calculates the SNR by comparing the power of the original signal
to the power of the provided noise signal, without needing a processed signal.
Signal Quality Assessment Module for Physiological Signal Processing
This module provides comprehensive capabilities for physiological
signal processing including ECG, PPG, EEG, and other vital signs.
Author: vitalDSP Team
Date: 2025-01-27
Version: 1.0.0
Key Features:
- Object-oriented design with comprehensive classes
- Multiple processing methods and functions
- NumPy integration for numerical computations
- SciPy integration for advanced signal processing
- Signal validation and error handling
A class to compute various Signal Quality Index (SQI) metrics for assessing the quality of vital signals.
This class includes methods to evaluate signal quality based on different characteristics such as amplitude variability, baseline wander, zero-crossing consistency, waveform similarity, entropy, and more. These metrics are useful in ensuring that physiological signals like ECG, PPG, EEG, and respiratory signals are of high quality and reliable for further analysis.
Compute the amplitude variability SQI over segments of the signal.
Parameters:
window_size (int) – The size of each segment to compute the SQI.
step_size (int) – The step size to move the window for each segment.
threshold (float or tuple, optional) – The threshold to determine normal and abnormal segments.
threshold_type (str, optional) – The type of thresholding (‘below’, ‘above’, ‘range’). Default is ‘below’.
scale (str, optional) – The scaling method to apply to SQI values (‘zscore’, ‘iqr’, ‘minmax’). Default is ‘zscore’.
aggregate (bool, optional) – If True (default), returns the mean SQI value as a single float.
If False, returns detailed tuple (sqi_values, normal_segments, abnormal_segments).
Returns:
If aggregate=True: Mean SQI value as float.
If aggregate=False: Tuple of (sqi_values, normal_segments, abnormal_segments).
This metric evaluates the amount of baseline wander in the signal, which is unwanted low-frequency noise that can distort the true signal. Baseline wander is particularly important in ECG and PPG signals.
step_size (int) – The step size to slide the window.
threshold (float or tuple, optional) – Threshold to classify a segment as normal or abnormal.
threshold_type (str, optional) – The type of thresholding (‘below’, ‘above’, ‘range’). Default is ‘below’.
scale (str, optional) – The scaling method to apply to SQI values (‘zscore’, ‘iqr’, ‘minmax’). Default is ‘zscore’.
moving_avg_window (int, optional) – Size of the window for calculating the moving average. Default is 50.
Returns:
If aggregate=True: Mean SQI value as float.
If aggregate=False: Tuple of (sqi_values, normal_segments, abnormal_segments)
where sqi_values is array of SQI values for each segment, normal_segments and
abnormal_segments are lists of (start, end) indices.
This metric assesses the consistency of EEG band power, which is important for ensuring signal quality. Stable band power indicates a high-quality EEG signal.
Parameters:
band_power (numpy.ndarray) – Array of band power values for EEG.
step_size (int) – The step size to slide the window.
threshold (float or tuple, optional) – Threshold to classify a segment as normal or abnormal.
threshold_type (str, optional) – The type of thresholding (‘below’, ‘above’, ‘range’). Default is ‘below’.
scale (str, optional) – The scaling method to apply to SQI values (‘zscore’, ‘iqr’, ‘minmax’). Default is ‘zscore’.
Returns:
If aggregate=True: Mean SQI value as float.
If aggregate=False: Tuple of (sqi_values, normal_segments, abnormal_segments)
where sqi_values is array of SQI values for each segment, normal_segments and
abnormal_segments are lists of (start, end) indices.
step_size (int) – The step size to slide the window.
threshold (float or tuple, optional) – Threshold to classify a segment as normal or abnormal.
threshold_type (str, optional) – The type of thresholding (‘below’, ‘above’, ‘range’). Default is ‘below’.
scale (str, optional) – The scaling method to apply to SQI values (‘zscore’, ‘iqr’, ‘minmax’). Default is ‘zscore’.
Returns:
If aggregate=True: Mean SQI value as float.
If aggregate=False: Tuple of (sqi_values, normal_segments, abnormal_segments)
where sqi_values is array of SQI values for each segment, normal_segments and
abnormal_segments are lists of (start, end) indices.
This metric assesses the variability in RR intervals of ECG, which should be within a certain range for a healthy signal. Abnormal HRV can indicate issues with heart health or signal quality.
Parameters:
rr_intervals (numpy.ndarray) – Array of RR intervals.
step_size (int) – The step size to slide the window.
threshold (float or tuple, optional) – Threshold to classify a segment as normal or abnormal.
threshold_type (str, optional) – The type of thresholding (‘below’, ‘above’, ‘range’). Default is ‘below’.
scale (str, optional) – The scaling method to apply to SQI values (‘zscore’, ‘iqr’, ‘minmax’). Default is ‘zscore’.
Returns:
If aggregate=True: Mean SQI value as float.
If aggregate=False: Tuple of (sqi_values, normal_segments, abnormal_segments)
where sqi_values is array of SQI values for each segment, normal_segments and
abnormal_segments are lists of (start, end) indices.
step_size (int) – The step size to slide the window.
threshold (float or tuple, optional) – Threshold to classify a segment as normal or abnormal.
threshold_type (str, optional) – The type of thresholding (‘below’, ‘above’, ‘range’). Default is ‘below’.
scale (str, optional) – The scaling method to apply to SQI values (‘zscore’, ‘iqr’, ‘minmax’). Default is ‘zscore’.
Returns:
If aggregate=True: Mean SQI value as float.
If aggregate=False: Tuple of (sqi_values, normal_segments, abnormal_segments)
where sqi_values is array of SQI values for each segment, normal_segments and
abnormal_segments are lists of (start, end) indices.
step_size (int) – The step size to slide the window.
threshold (float or tuple, optional) – Threshold to classify a segment as normal or abnormal.
threshold_type (str, optional) – The type of thresholding (‘below’, ‘above’, ‘range’). Default is ‘below’.
scale (str, optional) – The scaling method to apply to SQI values (‘zscore’, ‘iqr’, ‘minmax’). Default is ‘zscore’.
Returns:
If aggregate=True: Mean SQI value as float.
If aggregate=False: Tuple of (sqi_values, normal_segments, abnormal_segments)
where sqi_values is array of SQI values for each segment, normal_segments and
abnormal_segments are lists of (start, end) indices.
This metric evaluates the overall quality of PPG signals based on amplitude variability and baseline wander. High-quality PPG signals should have minimal noise and stable amplitude.
step_size (int) – The step size to slide the window.
threshold (float or tuple, optional) – Threshold to classify a segment as normal or abnormal.
threshold_type (str, optional) – The type of thresholding (‘below’, ‘above’, ‘range’). Default is ‘below’.
scale (str, optional) – The scaling method to apply to SQI values (‘zscore’, ‘iqr’, ‘minmax’). Default is ‘zscore’.
Returns:
If aggregate=True: Mean SQI value as float.
If aggregate=False: Tuple of (sqi_values, normal_segments, abnormal_segments)
where sqi_values is array of SQI values for each segment, normal_segments and
abnormal_segments are lists of (start, end) indices.
step_size (int) – The step size to slide the window.
threshold (float or tuple, optional) – Threshold to classify a segment as normal or abnormal.
threshold_type (str, optional) – The type of thresholding (‘below’, ‘above’, ‘range’). Default is ‘below’.
scale (str, optional) – The scaling method to apply to SQI values (‘zscore’, ‘iqr’, ‘minmax’). Default is ‘zscore’.
Returns:
If aggregate=True: Mean SQI value as float.
If aggregate=False: Tuple of (sqi_values, normal_segments, abnormal_segments)
where sqi_values is array of SQI values for each segment, normal_segments and
abnormal_segments are lists of (start, end) indices.
This metric measures the entropy of the signal, which indicates the complexity or predictability of the signal. Lower entropy generally indicates a more regular and stable signal, while higher entropy suggests more randomness.
step_size (int) – The step size to slide the window.
threshold (float or tuple, optional) – Threshold to classify a segment as normal or abnormal.
threshold_type (str, optional) – The type of thresholding (‘below’, ‘above’, ‘range’). Default is ‘below’.
scale (str, optional) – The scaling method to apply to SQI values (‘zscore’, ‘iqr’, ‘minmax’). Default is ‘zscore’.
Returns:
If aggregate=True: Mean SQI value as float.
If aggregate=False: Tuple of (sqi_values, normal_segments, abnormal_segments)
where sqi_values is array of SQI values for each segment, normal_segments and
abnormal_segments are lists of (start, end) indices.
step_size (int) – The step size to slide the window.
threshold (float or tuple, optional) – Threshold to classify a segment as normal or abnormal.
threshold_type (str, optional) – The type of thresholding (‘below’, ‘above’, ‘range’). Default is ‘below’.
scale (str, optional) – The scaling method to apply to SQI values (‘zscore’, ‘iqr’, ‘minmax’). Default is ‘zscore’.
Returns:
If aggregate=True: Mean SQI value as float.
If aggregate=False: Tuple of (sqi_values, normal_segments, abnormal_segments)
where sqi_values is array of SQI values for each segment, normal_segments and
abnormal_segments are lists of (start, end) indices.
step_size (int) – The step size to slide the window.
threshold (float or tuple, optional) – Threshold to classify a segment as normal or abnormal.
threshold_type (str, optional) – The type of thresholding (‘below’, ‘above’, ‘range’). Default is ‘below’.
scale (str, optional) – The scaling method to apply to SQI values (‘zscore’, ‘iqr’, ‘minmax’). Default is ‘zscore’.
Returns:
If aggregate=True: Mean SQI value as float.
If aggregate=False: Tuple of (sqi_values, normal_segments, abnormal_segments)
where sqi_values is array of SQI values for each segment, normal_segments and
abnormal_segments are lists of (start, end) indices.
Compute the waveform similarity SQI using various similarity metrics.
This metric compares the similarity between consecutive waveforms in the signal. High similarity indicates that the signal is stable and free from significant artifacts.
step_size (int) – The step size to slide the window.
reference_waveform (numpy.ndarray) – A reference waveform to compare against.
threshold (float or tuple, optional) – Threshold to classify a segment as normal or abnormal.
threshold_type (str, optional) – The type of thresholding (‘below’, ‘above’, ‘range’). Default is ‘below’.
scale (str, optional) – The scaling method to apply to SQI values (‘zscore’, ‘iqr’, ‘minmax’). Default is ‘zscore’.
similarity_method (str, optional) – The method to use for similarity calculation (‘correlation’, ‘custom’). Default is ‘correlation’.
Returns:
If aggregate=True: Mean SQI value as float.
If aggregate=False: Tuple of (sqi_values, normal_segments, abnormal_segments)
where sqi_values is array of SQI values for each segment, normal_segments and
abnormal_segments are lists of (start, end) indices.
This metric assesses the number of zero crossings in the signal, which should be consistent in high-quality signals. Irregular zero crossings can indicate noise or instability in the signal.
step_size (int) – The step size to slide the window.
threshold (float or tuple, optional) – Threshold to classify a segment as normal or abnormal.
threshold_type (str, optional) – The type of thresholding (‘below’, ‘above’, ‘range’). Default is ‘below’.
scale (str, optional) – The scaling method to apply to SQI values (‘zscore’, ‘iqr’, ‘minmax’). Default is ‘zscore’.
Returns:
If aggregate=True: Mean SQI value as float.
If aggregate=False: Tuple of (sqi_values, normal_segments, abnormal_segments)
where sqi_values is array of SQI values for each segment, normal_segments and
abnormal_segments are lists of (start, end) indices.
Peak Detection Module for Physiological Signal Processing
This module provides comprehensive peak detection capabilities for various
physiological signals including ECG, PPG, EEG, respiratory, and arterial blood
pressure (ABP) signals. It implements multiple detection algorithms optimized
for different signal types and characteristics.
Author: vitalDSP Team
Date: 2025-01-27
Version: 1.0.0
Health Report Generator Module for Physiological Signal Analysis
This module provides comprehensive health report generation capabilities for
physiological signal analysis including ECG, PPG, EEG, and other vital signs.
It implements automated report creation with visualization, interpretation,
and multi-threaded processing for efficient large-scale analysis.
Author: vitalDSP Team
Date: 2025-01-27
Version: 1.0.0
Key Features:
- Automated health report generation
- Multi-threaded visualization processing
- Comprehensive feature analysis and interpretation
- HTML report templates and rendering
- Performance monitoring and optimization
- Parallel processing for large datasets
A class to generate a health report based on feature data, including interpretations, visualizations, and contradictions/correlations.
This class handles the process of interpreting feature data (e.g., heart rate variability, ECG/PPG data),
generating visualizations for features, and rendering the final HTML report. It uses fully concurrent processing:
- Feature interpretation: Concurrent processing using ThreadPoolExecutor (CPU-bound, thread-safe)
- Visualization generation: Concurrent processing using ProcessPoolExecutor (matplotlib process-safe)
>>> fromvitalDSP.health_analysis.health_report_generatorimportHealthReportGenerator>>>>>> # Example 1: Basic health report generation>>> feature_data={"nn50":45,"rmssd":70,"sdnn":120}>>> generator=HealthReportGenerator(feature_data,segment_duration="5_min")>>> report_html=generator.generate()>>> print(f"Report generated: {len(report_html)} characters")>>>>>> # Example 2: Health report with custom configuration>>> generator_custom=HealthReportGenerator(... feature_data,... segment_duration="1_min",... feature_config_path="path/to/custom_config.yml"... )>>> report_custom=generator_custom.generate()>>>>>> # Example 3: Health report with filtering>>> report_filtered=generator.generate(filter_status="above_range")>>> print("Filtered report for above-range parameters only")
Generate visualizations in parallel using ProcessPoolExecutor to avoid matplotlib threading issues.
:param visualizer: The visualizer instance.
:type visualizer: HealthReportVisualizer
:param feature_data: Dictionary of features and their values.
:type feature_data: dict
:param output_dir: Directory to save the visualizations.
:type output_dir: str
:param processes: Number of processes for parallel visualization.
:type processes: int
Downsample large datasets by selecting every nth value.
:param values: The dataset to downsample.
:type values: list or np.array
:param factor: The downsampling factor.
:type factor: int
Generates the complete health report by interpreting the features, generating visualizations, and rendering an HTML report.
The report will include the description of each feature, its interpretation (in-range, above range, or below range),
any detected contradictions, correlations, and visualizations for each feature.
A class responsible for interpreting feature values based on a YAML configuration file.
This class loads a YAML file that contains the normal ranges, interpretation, contradictions, and correlations for different features.
It then provides methods to interpret feature values, check for contradictions, and identify correlations.
Interprets the value of a given feature based on its configuration in the YAML file.
This method compares the feature value with its normal range and provides interpretation,
which could indicate whether the value is in-range, below-range, or above-range.
Parameters:
feature_name (str) – The name of the feature to interpret (e.g., “NN50”, “RMSSD”).
value (float) – The value of the feature to be interpreted.
segment_duration (str) – The duration of the segment, either ‘1 min’ or ‘5 min’. Default is ‘1 min’.
Returns:
A dictionary containing:
”description”: The description of the feature.
”normal_range”: The normal range of the feature for the given segment duration.
”interpretation”: The interpretation of the feature value (whether in-range, below-range, or above-range).
”contradiction”: Contradiction explanation with other related features.
”correlation”: Correlation explanation with other related features.
>>> engine=InterpretationEngine()>>> result=engine.interpret_feature("NN50",45,"5 min")>>> print(result){ "description": "Number of significant changes in heart rate. High NN50 suggests healthy parasympathetic activity.", "normal_range": [40, 150], "interpretation": "Normal parasympathetic activity. No immediate concern.", "contradiction": "Low NN50 contradicts high RMSSD, as both should indicate parasympathetic activity.", "correlation": "Positively correlated with RMSSD, as both represent short-term heart rate variability."}
Comprehensive REST API for digital signal processing in healthcare applications.
Provides access to all core VitalDSP functionality including filtering, feature extraction,
respiratory analysis, signal quality assessment, and more.
Registers the callback to toggle the sidebar between expanded and collapsed states.
Also toggles the icon between the hamburger icon and a right-pointing arrow.
Note: Time domain callbacks have been migrated to time_domain_callbacks.py.
This file now only contains helper functions for frequency domain analysis.
No callbacks are registered here anymore.
Signal filtering callbacks for vitalDSP webapp.
Handles traditional filtering, advanced filtering, artifact removal, neural filtering, and ensemble filtering.
Uses actual vitalDSP functions for all computations.
Note: Preprocessing (filtering, denoising) should be done on the filtering page.
This function receives already-filtered signal data from store-filtered-signal.
Create the main respiratory signal plot with annotations.
Note: Preprocessing (filtering, denoising) should be done on the filtering page.
This function receives already-filtered signal data from store-filtered-signal.
Generate comprehensive respiratory analysis results using vitalDSP.
Note: Preprocessing (filtering, denoising) should be done on the filtering page.
This function receives already-filtered signal data from store-filtered-signal.
Format large numbers with appropriate scaling and units.
Parameters:
value – Numerical value to format
precision – Number of decimal places
use_scientific – Force scientific notation
as_integer – Format as integer (for counts)
unit – Explicit unit string (e.g., ‘ms’, ‘Hz’). If provided, no unit conversion is applied.
Returns:
Formatted string with appropriate scaling
Note
When unit is specified (e.g., ‘ms’), the function assumes the value is already in the
correct unit and will NOT apply scaling conversions (no m/k prefixes).
Comprehensive respiratory rate analysis callbacks for vitalDSP webapp.
Handles all respiratory features including estimation methods, sleep apnea detection, and fusion analysis.
Perform Granger causality tests to determine if one time series can predict another.
The Granger causality test evaluates whether the past values of one time series can provide statistically significant information about the future values of another time series.
Parameters:
data (numpy.ndarray) – The input data array with shape (n_samples, 2), where the first column is the dependent variable.
max_lag (int) – Maximum lag to consider for causality.
verbose (bool, optional) – If True, prints out detailed test statistics (default is False).
Returns:
Granger causality test results, including F-test statistics for each lag.