Preprocessing

Techniques for preprocessing respiratory signals before analysis, including noise reduction and general preprocessing methods.

Noise Reduction

Apply noise reduction techniques to respiratory signals to improve the quality of analysis.

Signal Preprocessing 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: - Multiple processing methods and functions - NumPy integration for numerical computations

Examples:

Basic usage:
>>> import numpy as np
>>> from vitalDSP.preprocess.noise_reduction import NoiseReduction
>>> signal = np.random.randn(1000)
>>> processor = NoiseReduction(signal)
>>> result = processor.process()
>>> print(f'Processing result: {result}')
vitalDSP.preprocess.noise_reduction.gaussian_denoising(signal, sigma=1.0)[source]

Applies Gaussian filtering for noise reduction.

Parameters:
  • signal (numpy.ndarray) – The input signal to be denoised.

  • sigma (float, optional (default=1.0)) – The standard deviation of the Gaussian kernel.

Returns:

denoised_signal – The denoised signal.

Return type:

numpy.ndarray

Examples

>>> signal = np.sin(np.linspace(0, 10, 100)) + np.random.normal(0, 0.1, 100)
>>> denoised_signal = gaussian_denoising(signal, sigma=1.5)
>>> print(denoised_signal)
vitalDSP.preprocess.noise_reduction.median_denoising(signal, kernel_size=3)[source]

Applies median filtering for noise reduction.

Parameters:
  • signal (numpy.ndarray) – The input signal to be denoised.

  • kernel_size (int, optional (default=3)) – The size of the median filter kernel.

Returns:

denoised_signal – The denoised signal.

Return type:

numpy.ndarray

Examples

>>> signal = np.array([1, 2, 3, 100, 5, 6, 7, 8, 9])
>>> denoised_signal = median_denoising(signal, kernel_size=3)
>>> print(denoised_signal)
[1 2 3 5 5 6 7 8 9]
vitalDSP.preprocess.noise_reduction.moving_average_denoising(signal, window_size)[source]

Applies moving average filtering for noise reduction.

Parameters:
  • signal (numpy.ndarray) – The input signal to be denoised.

  • window_size (int) – The size of the moving window.

Returns:

denoised_signal – The denoised signal.

Return type:

numpy.ndarray

Examples

>>> signal = np.sin(np.linspace(0, 10, 100)) + np.random.normal(0, 0.1, 100)
>>> denoised_signal = moving_average_denoising(signal, window_size=5)
>>> print(denoised_signal)
vitalDSP.preprocess.noise_reduction.savgol_denoising(signal, window_length, polyorder)[source]

Applies Savitzky-Golay filtering for noise reduction.

Parameters:
  • signal (numpy.ndarray) – The input signal to be denoised.

  • window_length (int) – The length of the filter window (must be odd).

  • polyorder (int) – The order of the polynomial to fit.

Returns:

denoised_signal – The denoised signal.

Return type:

numpy.ndarray

Examples

>>> signal = np.sin(np.linspace(0, 10, 100)) + np.random.normal(0, 0.1, 100)
>>> denoised_signal = savgol_denoising(signal, window_length=5, polyorder=2)
>>> print(denoised_signal)
vitalDSP.preprocess.noise_reduction.wavelet_denoising(signal, wavelet_name='haar', level=1)[source]

Performs wavelet denoising on the input signal using the custom wavelet transform.

Parameters:
  • signal (numpy.ndarray) – The input signal to be denoised.

  • wavelet_name (str, optional (default='haar')) – The type of wavelet to use.

  • level (int, optional (default=1)) – The level of wavelet decomposition.

Returns:

denoised_signal – The denoised signal.

Return type:

numpy.ndarray

Examples

>>> signal = np.sin(np.linspace(0, 10, 100)) + np.random.normal(0, 0.1, 100)
>>> denoised_signal = wavelet_denoising(signal, wavelet_name='haar', level=2)
>>> print(denoised_signal)

Signal Preprocessing

Preprocess respiratory signals to prepare them for subsequent analysis, including normalization and filtering.

Preprocessing Operations Module for Physiological Signal Processing

This module provides comprehensive preprocessing capabilities for physiological signals including ECG, PPG, EEG, and other vital signs. It implements various filtering techniques, noise reduction methods, and signal conditioning operations to prepare signals for analysis.

Author: vitalDSP Team Date: 2025-01-27 Version: 1.0.0

Key Features: - Multiple filtering types (bandpass, Butterworth, Chebyshev, elliptic) - Advanced noise reduction methods (wavelet, Savitzky-Golay, median, Gaussian) - Signal-specific preprocessing configurations - Respiratory signal optimization - Comprehensive parameter configuration - Integration with signal filtering modules

Examples:

Basic ECG preprocessing:
>>> import numpy as np
>>> from vitalDSP.preprocess.preprocess_operations import PreprocessConfig, preprocess_signal
>>> config = PreprocessConfig(
...     filter_type="bandpass",
...     lowcut=0.5,
...     highcut=40.0,
...     noise_reduction_method="wavelet"
... )
>>> processed_signal = preprocess_signal(ecg_signal, fs=250, config=config)
PPG preprocessing with Savitzky-Golay:
>>> ppg_config = PreprocessConfig(
...     filter_type="bandpass",
...     lowcut=0.5,
...     highcut=8.0,
...     noise_reduction_method="savgol",
...     window_length=21,
...     polyorder=3
... )
>>> processed_ppg = preprocess_signal(ppg_signal, fs=128, config=ppg_config)
Respiratory signal preprocessing:
>>> resp_config = PreprocessConfig(
...     filter_type="bandpass",
...     lowcut=0.1,
...     highcut=2.0,
...     respiratory_mode=True
... )
>>> processed_resp = preprocess_signal(resp_signal, fs=64, config=resp_config)
class vitalDSP.preprocess.preprocess_operations.PreprocessConfig(filter_type='bandpass', noise_reduction_method='wavelet', lowcut=0.1, highcut=10, order=4, wavelet_name='haar', level=1, window_length=5, polyorder=2, kernel_size=3, sigma=1.0, respiratory_mode=False, repreprocess=False)[source]

Bases: object

Configuration class for signal preprocessing, which includes filtering and noise reduction parameters.

filter_type

The type of filtering to apply. Options: ‘bandpass’, ‘butterworth’, ‘chebyshev’, ‘elliptic’, ‘ignore’.

Type:

str

noise_reduction_method

The noise reduction method to apply. Options: ‘wavelet’, ‘savgol’, ‘median’, ‘gaussian’, ‘moving_average’, ‘ignore’.

Type:

str

lowcut

The lower cutoff frequency for filtering.

Type:

float

highcut

The upper cutoff frequency for filtering.

Type:

float

order

The order of the filter.

Type:

int

wavelet_name

The name of the wavelet to use for wavelet-based noise reduction.

Type:

str

level

The level of wavelet decomposition.

Type:

int

window_length

The window length for Savitzky-Golay filtering.

Type:

int

polyorder

The polynomial order for Savitzky-Golay filtering.

Type:

int

kernel_size

The kernel size for median filtering.

Type:

int

sigma

The standard deviation for Gaussian filtering.

Type:

float

respiratory_mode

Apply the preprocessing function specifically for respiratory signals (e.g., PPG or ECG-derived respiration).

Type:

bool

repreprocess

Re preprocessing function

Type:

bool

Examples

>>> # Basic configuration for ECG preprocessing
>>> config = PreprocessConfig(
...     filter_type="bandpass",
...     noise_reduction_method="wavelet",
...     lowcut=0.5,
...     highcut=40.0,
...     order=4
... )
>>>
>>> # Configuration for PPG preprocessing with respiratory analysis
>>> config_ppg = PreprocessConfig(
...     filter_type="bandpass",
...     noise_reduction_method="savgol",
...     lowcut=0.5,
...     highcut=8.0,
...     respiratory_mode=True,
...     window_length=5,
...     polyorder=2
... )
>>>
>>> # Configuration for noise reduction only
>>> config_denoise = PreprocessConfig(
...     filter_type="ignore",
...     noise_reduction_method="median",
...     kernel_size=5
... )
vitalDSP.preprocess.preprocess_operations.estimate_baseline(signal, fs, method='moving_average', window_size=5)[source]

Estimate a stable baseline for a physiological signal.

Parameters:
  • signal (np.ndarray) – The physiological signal (e.g., PPG or ECG).

  • fs (float) – Sampling frequency of the signal in Hz.

  • method (str, optional) – Method for baseline estimation. Options are ‘moving_average’, ‘low_pass’, ‘polynomial_fit’, ‘median_filter’, and ‘wavelet’. Default is ‘moving_average’.

  • window_size (int, optional) – Window size in seconds for moving average or median filter. Default is 5.

Returns:

baseline – The estimated baseline of the signal.

Return type:

np.ndarray

vitalDSP.preprocess.preprocess_operations.preprocess_signal(signal, sampling_rate, filter_type='bandpass', lowcut=0.1, highcut=4.5, order=4, noise_reduction_method='wavelet', wavelet_name='haar', level=1, window_length=5, polyorder=2, kernel_size=3, sigma=1.0, respiratory_mode=False, repreprocess=False)[source]

Preprocesses the signal by applying bandpass filtering and noise reduction.

Parameters:
  • signal (numpy.ndarray) – The input signal to be preprocessed.

  • sampling_rate (float) – The sampling rate of the signal in Hz.

  • filter_type (str, optional (default="bandpass")) – The type of filtering to apply. Options: ‘bandpass’, ‘butterworth’, ‘chebyshev’, ‘elliptic’, ‘ignore’.

  • lowcut (float, optional (default=0.1)) – The lower cutoff frequency for bandpass filtering.

  • highcut (float, optional (default=4.5)) – The upper cutoff frequency for bandpass filtering.

  • order (int, optional (default=4)) – The order of the filter.

  • noise_reduction_method (str, optional (default="wavelet")) – The noise reduction method to apply. Options: ‘wavelet’, ‘savgol’, ‘median’, ‘gaussian’, ‘moving_average’, ‘ignore’.

  • wavelet_name (str, optional (default="db")) – The type of wavelet to use for wavelet denoising.

  • level (int, optional (default=1)) – The level of wavelet decomposition for wavelet denoising.

  • window_length (int, optional (default=5)) – The window length for Savitzky-Golay filtering.

  • polyorder (int, optional (default=2)) – The polynomial order for Savitzky-Golay filtering.

  • kernel_size (int, optional (default=3)) – The kernel size for median filtering.

  • sigma (float, optional (default=1.0)) – The standard deviation for Gaussian filtering.

  • respiratory_mode (bool, optional (default=False)) – If True, apply preprocessing specifically for respiratory signals (e.g., PPG or ECG-derived respiration).

Returns:

preprocessed_signal – The preprocessed signal.

Return type:

numpy.ndarray

Examples

>>> signal = np.sin(np.linspace(0, 10, 100)) + np.random.normal(0, 0.2, 100)
>>> sampling_rate = 1000
>>> preprocessed_signal = preprocess_signal(signal, sampling_rate, filter_type='bandpass', noise_reduction_method='wavelet')
>>> print(preprocessed_signal)
vitalDSP.preprocess.preprocess_operations.respiratory_filtering(signal, sampling_rate, lowcut=0.1, highcut=0.5, order=4)[source]

Filters the signal specifically for respiratory-related frequency bands.

Parameters:
  • signal (numpy.ndarray) – The input signal to be filtered.

  • sampling_rate (float) – The sampling rate of the signal in Hz.

  • lowcut (float, optional (default=0.1)) – The lower cutoff frequency for respiratory filtering.

  • highcut (float, optional (default=0.5)) – The upper cutoff frequency for respiratory filtering.

  • order (int, optional (default=4)) – The order of the filter.

Returns:

filtered_signal – The filtered respiratory signal.

Return type:

numpy.ndarray

Examples

>>> signal = np.sin(np.linspace(0, 10, 100)) + np.random.normal(0, 0.2, 100)
>>> sampling_rate = 1000
>>> filtered_signal = respiratory_filtering(signal, sampling_rate)
>>> print(filtered_signal)