ecg_detect_beats: This function accepts a single ecg trace already scaled to...

View source: R/ecg_detect_beats.R

ecg_detect_beatsR Documentation

This function accepts a single ecg trace already scaled to millivolts, then runs the ECG trace through one or more WFDB QRS detection algorithms, which tend to detect heart beats with high fidelity, even in noisy data.

Description

This function accepts a single ecg trace already scaled to millivolts, then runs the ECG trace through one or more WFDB QRS detection algorithms, which tend to detect heart beats with high fidelity, even in noisy data.

Usage

ecg_detect_beats(
  ecg_trace,
  freq = 1000,
  wfdb_out_file = file.path(tempdir(), "ecg_out"),
  detectors = c("wqrs", "gqrs", "ecgpuwave", "sqrs"),
  flags = list(gqrs = "-o gqrs -m 1.0", wqrs = "-R -m 100", ecgpuwave =
    "-a ecgpuwave -n 1", sqrs = "-m 500"),
  correct_peaks = TRUE,
  wfdb_path = "/usr/local/wfdb/bin",
  signal_mult = 1000,
  adc_gain = 1000
)

Arguments

ecg_trace

This is the ECG trace that should be passed to WFDB. If a numeric vector is provided, then this will written to a temporary file and passed through WFDB. If a readable file is passed in, we assume that this is a single column file containing scaled ECG values. This file is read in, then processed through WFDB.

freq

The acquisition frequency in Hz. Default is 1000.

wfdb_out_file

The prefix of WFDB outputs generated by processing. Defaults to a temporary file

detectors

A vector of one or more WFDB QRS detection algorithms to be applied. Options are c("gqrs", "wqrs", "sqrs", "ecgpuwave").

flags

A named list of arguments passed to QRS detection algorithms. These should not include the call for what record to output (-r <out_name>).

correct_peaks

If TRUE, the detected QRS complexes are passed to a peak correction algorithm (ported from wfdb-python) that shifts the detected events to the local maximum within a small time window. This allows for small adjustments that can overcome algorithm failures or, more commonly, that shift the events to the R spikes for plotting convenience instead of the Q onset.

wfdb_path

location of the WFDB bin directory on this computer.

signal_mult

Value to multiply signal values by before importation by wrsamp

adc_gain

Gain used internally by WFDB to convert raw units to millivolts

Details

Note that WFDB is very picky about the analog-digital converter gain settings and can produce weird and invalid outputs if the input values are converted properly by wrsamp. In particular, if small or fractional values are provided in the first place, wrsamp will convert all of these to integers, resulting in all (or nearly all) zero values.

To work around this problem, we apply an x 1000 transformation to the input values and then also store a gain of 1000 in the header of the wfdb file. This keeps the millivolt scaling of the original data, but allows wfdb to process the data as expected.

Also note that the gqrs algorithm and other WFDB QRS detectors (I think) tend to return annotations for when the QRS complex begins, not when the spike occurs. If you want the annotations to have the R spike, turn on correct_peaks, which will pass the detected QRS complexes through the correct_peaks function (ported from wfdb-python).

Value

A list containing two data.table objects. $beat_data contains beat times, positions, HR, and RR for each detector. Shifted peaks are denoted with the suffix '_shift'. $ecg_df is a data.table containing the time and ecg trace of the raw data.

Author(s)

Michael Hallquist

Examples

 ## Not run: 
   ecg_data <- ecg_detect_beats("ecg_027_full.txt.bz2", detectors="gqrs")
 
## End(Not run)

PennStateDEPENdLab/experiment.pipeline documentation built on April 14, 2024, 9:37 p.m.