resurfemg.preprocessing.ecg_removal module

Copyright 2022 Netherlands eScience Center and University of Twente Licensed under the Apache License, version 2.0. See LICENSE for details.

This file contains functions to eliminate ECG artifacts from

with various EMG arrays.

resurfemg.preprocessing.ecg_removal.compute_ICA_n_comp(emg_samples, use_all_leads=True, desired_leads=(0, 2))

A function that performs an independant component analysis (ICA) meant for EMG data that includes stacked arrays, there should be at least two arrays but there can be more. This differs from helper_functions.compute_ICA_two_comp_multi because you can get n leads back instead of only two.

Parameters:
  • emg_samples (ndarray) – Original signal array with three or more layers

  • use_all_leads (bool) – True if all leads used, otherwise specify leads

  • desired_leads (tuple) – tuple of leads to use starting from 0

Returns:

Arrays of independent components (ECG-like and EMG)

Return type:

ndarray

resurfemg.preprocessing.ecg_removal.compute_ICA_n_comp_selective_zeroing(emg_samples, ecg_lead_to_remove, use_all_leads=True, desired_leads=(0, 2))

A function that performs an independant component analysis (ICA) meant for EMG data that includes stacked arrays, there should be at least two arrays but there can be more. In this ICA one lead is put to zero before reconstruction. This should probably be the ECG lead.

Parameters:
  • emg_samples (ndarray) – Original signal array with three or more layers

  • ecg_lead_to_remove (int) – Lead number counting from zero to get rid of

  • use_all_leads (bool) – True if all leads used, otherwise specify leads

  • desired_leads (tuple) – tuple of leads to use starting from 0

Returns:

Arrays of independent components (ECG-like and EMG)

Return type:

ndarray

resurfemg.preprocessing.ecg_removal.compute_ICA_two_comp_selective(emg_samples, use_all_leads=True, desired_leads=(0, 2))

A function that performs an independant component analysis (ICA) meant for EMG data that includes stacked arrays, there should be at least two arrays but there can be more.

Parameters:
  • emg_samples (ndarray) – Original signal array with three or more layers

  • use_all_leads (bool) – True if all leads used, otherwise specify leads

  • desired_leads (tuple) – tuple of leads to use starting from 0

Returns:

Two arrays of independent components (ECG-like and EMG)

Return type:

ndarray

resurfemg.preprocessing.ecg_removal.compute_ica_two_comp(emg_samples)

A function that performs an independent component analysis (ICA) meant for EMG data that includes three stacked arrays.

Parameters:

emg_samples (ndarray) – Original signal array with three layers

Returns:

Two arrays of independent components (ECG-like and EMG)

Return type:

ndarray

resurfemg.preprocessing.ecg_removal.compute_ica_two_comp_multi(emg_samples)

A function that performs an independant component analysis (ICA) meant for EMG data that includes stacked arrays, there should be at least two arrays but there can be more.

Parameters:

emg_samples (ndarray) – Original signal array with three or more layers

Returns:

Two arrays of independent components (ECG-like and EMG)

Return type:

ndarray

resurfemg.preprocessing.ecg_removal.find_peaks_in_ecg_signal(ecg_signal, lower_border_percent=50)

This function assumes you have isolated an ecg-like signal with QRS peaks “higher” (or lower) than ST waves. In this case it can be applied to return an array of ECG peak locations. NB: This function assumes that the ECG signal has already been through a bandpass or low-pass filter or has little baseline drift.

Parameters:
  • ecg_signal (ndarray) – frequency array sampled at in Hertz

  • low_border_percent (int) – percentage max below which no peaks expected

Returns:

tuple first element peak locations, next a dictionary of info

Return type:

tuple

resurfemg.preprocessing.ecg_removal.gating(src_signal, gate_peaks, gate_width=205, method=1)

Eliminate peaks (e.g. QRS) from src_signal using gates of width gate_width. The gate either filled by zeros or interpolation. The filling method for the gate is encoded as follows: 0: Filled with zeros 1: Interpolation samples before and after 2: Fill with average of prior segment if exists otherwise fill with post segment 3: Fill with running average of RMS (default)

Parameters:
  • src_signal – Signal to process

  • gate_peaks (~list) – list of individual peak index places to be gated

  • gate_width (int) – width of the gate

  • method (int) – filling method of gate

Returns:

src_signal_gated, the gated result

Return type:

ndarray

resurfemg.preprocessing.ecg_removal.pick_highest_correlation_array(components_tuple, ecg_lead)

Here we have a function that takes a tuple with the two parts of ICA and the array containing the ECG recording, and finds the ICA component with the highest similarity to the ECG. Data should not have been finally filtered to envelope level

Parameters:
  • components_tuple (Tuple[ndarray, ndarray]) – tuple of two arrays representing different signals

  • ecg_lead (numpy.ndarray) – array containing the ECG recording

Returns:

Array with the highest correlation coefficient to the ECG lead (should usually be the ECG)

Return type:

ndarray

resurfemg.preprocessing.ecg_removal.pick_highest_correlation_array_multi(components, ecg_lead)

Here we have a function that takes a tuple with n parts of ICA and the array defined by the user as the ECG recording, and finds the ICA component with the highest similarity to the ECG. Data should not have been finally filtered to envelope level

Parameters:
  • components (ndarray) – n-dimensional array representing different components. Each row is a component.

  • ecg_lead (numpy.ndarray) – array containing the ECG recording

Returns:

Index of the array with the highest correlation coefficient to the ECG lead (should usually be the ECG)

Return type:

int

resurfemg.preprocessing.ecg_removal.pick_lowest_correlation_array(components_tuple, ecg_lead)

Here we have a function that takes a tuple with the two parts of ICA and the array containing the ECG recording, and finds the ICA component with the lowest similarity to the ECG. Data should not have been finally filtered to envelope level

Parameters:
  • components_tuple (Tuple[ndarray, ndarray]) – tuple of two arrays representing different signals

  • ecg_lead (numpy.ndarray) – array containing the ECG recording

Returns:

Array with the lowest correlation coefficient to the ECG lead (should usually be the EMG as opposed to ECG)

Return type:

ndarray

resurfemg.preprocessing.ecg_removal.pick_more_peaks_array(components_tuple)

Here we have a function that takes a tuple with the two parts of ICA, and finds the one with more peaks and anti-peaks. The EMG if without a final envelope will have more peaks

Note

Data should not have been finally filtered to envelope level

Parameters:

components_tuple (Tuple[ndarray, ndarray]) – tuple of two arrays representing different signals

Returns:

Array with more peaks (should usually be the EMG as opposed to ECG)

Return type:

ndarray