resptools

resp_estimation

calc_rqi(resp_sig, resampling_rate=10.0, rqi_method=['autocorr', 'hjorth'])[source]

Calculates respiratory quality index for the respiratory signal.

Parameters:
  • resp_sig (ArrayLike) – Respiratory signal.

  • resampling_rate (float, optional) – Sampling rate (after resampling, Hz) of the respiratory signal. Defaults to 10.

  • rqi_method (list, optional) – Method for calculating respiratory quality index. Defaults to [‘autocorr’,’hjorth’].

Returns:

Dictionary of calculated RQIs.

Return type:

dict

estimate_rr(resp_sig, resampling_rate=10.0, method='peakdet', delta=0.001)[source]

Estimates respiratory rate from the respiratory signal.

Parameters:
  • resp_sig (ArrayLike) – Respiratory signal.

  • sampling_rate (float) – Sampling rate of the respiratory signal (resampling rate, Hz). Defaults to 10.

  • method (str) – Method used for respiratory rate estimation. Can be one of ‘peakdet’ or ‘xcorr’. Defaults to ‘peakdet’. Uses ‘rsp_rate’ function from the Neurokit2 library if the method is ‘xcorr’.

  • delta (float, optional) – Parameter of ‘peakdet’ method. Defaults to 0.001.

  • resampling_rate (float) –

Returns:

Estimated respiratory rate (breaths/minutes).

Return type:

float

extract_resp_sig(sig, peaks_locs, troughs_locs, sampling_rate, mod_type=['AM', 'FM', 'BW'], resampling_rate=10.0)[source]

Extracts the respiratory signal(s) using the modulations resulted from respiratory activity and returns a dictionary of the respiratory signal(s).

Parameters:
  • sig (ArrayLike) – Input signal (PPG or ECG).

  • peaks_locs (ArrayLike) – Input signal peak locations.

  • troughs_locs (ArrayLike) – Input signal trough locations.

  • sampling_rate (float) – Sampling rate of the input signal.

  • mod_type (list, optional) – Modulation type: ‘AM’ for amplitude modulation, ‘FM’ for frequency modulation, ‘BW’ for baseline wander. Defaults to [‘AM’,’FM’,’BW’].

  • resampling_rate (float, optional) – Sampling rate of the extracted respiratory signal. Defaults to 10 (Hz).

Raises:
  • ValueError – If sampling rate is not greater than 0.

  • ValueError – If resampling rate is not greater than 0.

  • ValueError – If lengths of peak and trough location arrays do not match!

Returns:

Dictionary of extracted respiratory signals.

Return type:

dict

filter_resp_sig(resampling_rate=10.0, rsp_clean_method='khodadad2018', **kwargs)[source]

Filters extracted respiratory signal(s) and returns a dictionary of cleaned signal(s). Uses ‘rsp_clean’ function from the Neurokit2 library.

Parameters:
  • resampling_rate (float, optional) – Resampling rate. Defaults to 10.

  • rsp_clean_method (str, optional) – Method to clean the respiratory signal. Defaults to ‘khodadad2018’.

Return type:

dict

kwargs:

am_sig (Array, optional):Respiratory signal calculated from amplitude modulation. Defaults to None. am_x (Array, optional):x-axis values of amplitude modulation signal. Defaults to None. fm_sig (Array, optional):Respiratory signal calculated from frequency modulation. Defaults to None. fm_x (Array, optional):x-axis values of frequency modulation signal. Defaults to None. bw_sig (Array, optional):Respiratory signal calculated from baseline wander. Defaults to None. bw_x (Array, optional):x-axis values of baseline wander signal. Defaults to None.

Returns:

Dictionary of filtered respiratory signals and x-axis values for each signal.

Return type:

dict

Parameters:
  • resampling_rate (float) –

  • rsp_clean_method (str) –

fuse_rr(rr_est, rqi=None, fusion_method='smartfusion')[source]

Fuses respiratory rates calculated from different modulation types.

Parameters:
  • rr_est (Array) – Respiratory rate estimations.

  • rqi (Array, optional) – Respiratory quality indices. Defaults to None.

  • fusion_method (str, optional) – Fusion method. Can be one of ‘SmartFusion’ of ‘QualityFusion’. Defaults to ‘SmartFusion’.

Raises:
  • ValueError – If lengths of ‘rr_est’ and ‘rqi’ arrays do not match.

  • ValueError – If method is ‘QualityFusion’ and RQI values are not provided.

  • ValueError – If method is not one of ‘SmartFusion’ and ‘QualityFusion’.

Returns:

Fused respiratory rate

Return type:

float

resp_plot

plot_resp(signals, peaks=None, sampling_rate=None, timestamp_resolution=None, method='matplotlib', show_peaks=True, figsize=(18.5, 10.5), width=1050, height=600)[source]

Generates plots for respiration signal(s).

Parameters:
  • signals (dict) – The dictionary of signals to be plotted.

  • peaks (dict, optional) – The dictionary of peaks to be plotted. Defaults to None.

  • sampling_rate (float, optional) – Sampling rate of the signal. Defaults to None.

  • timestamp_resolution (str, optional) – Timestamp resolution. Defaults to None.

  • method (str, optional) – Package to generate plots. Defaults to ‘matplotlib’.

  • show_peaks (bool, optional) – If True, peaks are plotted. Defaults to True.

  • figsize (tuple, optional) – Figure size for matplotlib. Defaults to (18.5, 10.5).

  • float (width) – Figure width for Plotly. Defaults to 1050.

  • optional) – Figure width for Plotly. Defaults to 1050.

  • height (float, optional) – Figure height for Plotly. Defaults to 600.

  • width (float) –

Raises:

ValueError – If method is not ‘matplotlib’ or ‘plotly’.

resp_preprocess

elim_vhf(sig, sampling_rate)[source]

Eliminates very high frequencies prior to respiratory rate estimation procedure. The cutoff frequencies are determined considering the frequency range of the respiration.

Parameters:
  • sig (Array) – Input signal (ECG or PPG).

  • sampling_rate (float) – Sampling frequency of the input signal (Hz).

Returns:

Filtered signal.

Return type:

Array

elim_vlf(sig, sampling_rate)[source]

Eliminates very low frequencies prior to respiratory rate estimation procedure. The cutoff frequencies are determined considering the frequency range of the respiration.

Parameters:
  • sig (Array) – Input signal (ECG or PPG).

  • sampling_rate (float) – Sampling frequency of the input signal (Hz).

Returns:

Filtered signal.

Return type:

Array