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 EMG arrays.

resurfemg.preprocessing.ecg_removal.detect_ecg_peaks(ecg_raw, fs, peak_fraction=0.3, peak_width_s=None, peak_distance=None, bp_filter=True)

Detect ECG peaks in EMG signal.

param ecg_raw:

ecg signals to detect the ECG peaks in.

type ecg_raw:

~numpy.ndarray

param emg_raw:

emg signals to gate

type emg_raw:

~numpy.ndarray

param fs:

Sampling rate of the emg signals.

type fs:

int

param peak_fraction:

ECG peaks amplitude threshold relative to the specified fraction of the min-max values in the ECG signal

type peak_fraction:

float

param peak_width_s:

ECG peaks width threshold in samples.

type peak_width_s:

int

param peak_distance:

Minimum time between ECG peaks in samples.

type peak_distance:

int

param filter:

Bandpass filter the ecg_raw between 1-500 Hz before peak detection.

type filter:

bool

returns ecg_peak_idxs:

ECG peak indices

rtype ecg_peak_idxs:

numpy.ndarray[int]

resurfemg.preprocessing.ecg_removal.gating(emg_raw, peak_idxs, gate_width=205, method=1)

Eliminate peaks (e.g. QRS) from emg_raw 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) ————————————————————————— :param emg_raw: Signal to process :type emg_raw: ~numpy.ndarray :param peak_idxs: list of individual peak index places to be gated :type peak_idxs: ~list :param gate_width: width of the gate :type gate_width: int :param method: filling method of gate :type method: int

Returns emg_raw_gated:

the gated result

Rtype emg_raw_gated:

numpy.ndarray

resurfemg.preprocessing.ecg_removal.wavelet_denoising(emg_raw, ecg_peak_idxs, fs, hard_thresholding=True, n=4, wavelet_type='db2', fixed_threshold=4.5)

Shrinkage Denoising using a-trous wavelet decomposition (SWT). NB: This function assumes that the emg_raw has already been preprocessed for removal of baseline, powerline, and aliasing. N.B. This is a Python implementation of the SWT, as previously implemented in MATLAB by Jan Graßhoff. See Copyright notice below. ————————————————————————– Copyright 2019 Institute for Electrical Engineering in Medicine, University of Luebeck Jan Graßhoff

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ————————————————————————— :param emg_raw: 1D raw EMG data :type emg_raw: numpy.ndarray :param ecg_peak_idxs: list of R-peaks indices :type ecg_peak_idxs: numpy.ndarray :param fs: Sampling rate of emg_raw :type fs: int :param hard_thresholding: True: hard (default), False: soft :type hard_thresholding: bool :param n: True: decomposition level (default: 4) :type n: int :param wavelet_type: wavelet type (default: ‘db2’, see pywt.swt help) :type wavelet_type: str

Returns emg_clean:

cleaned EMG signal

Rtype emg_clean:

numpy.ndarray

Returns wav_dec:

wavelet decomposition

Rtype wav_dec:

numpy.ndarray

Returns thresholds:

threshold values

Rtype thresholds:

numpy.ndarray

Returns gate_bool_array:

gated signal based on R-peaks, where gate == 1

Rtype gate_bool_array:

numpy.ndarray