`detect_rpeaks` implements the first part of the Pan & Tompkins algorithms to detect R peaks from a raw ECG signal.

detect_rpeaks(
  signal,
  sRate,
  lowcut = 0,
  highcut = 15,
  filter_order = 1,
  integration_window = 15,
  refractory = 200,
  return_index = FALSE
)

Arguments

signal

Numerical vector of ECG signal.

sRate

ECG signal sample rate.

lowcut

Butterworth bandpass filter low cut value.

highcut

Butterworth bandpass filter high cut value.

filter_order

Butterworth bandpass filter order value.

integration_window

Convolution window size.

refractory

Minimal space between peaks in milliseconds.

return_index

If TRUE, the index for each R peak is returned instead of the timing.

Value

A numeric vector of detected R peaks, expressed in seconds* from the start of the signal. This vector can be used in RHRV using `RHRV::LoadBeatVector()`.

*(or samples if return_index is TRUE)

References

Pan, Jiapu, and Willis J. Tompkins. "A real-time QRS detection algorithm." IEEE Trans. Biomed. Eng 32, no. 3 (1985): 230-236.

Examples

path <- paste0(tempdir(),"rec_1.dat")
download.file("https://physionet.org/files/ecgiddb/1.0.0/Person_01/rec_1.dat?download",path)
ecg <- readBin(path,integer(),500*30)
peaks <- detect_rpeaks(ecg, sRate = 500)
unlink(path)
print(peaks)
#>  [1]  0.708  3.206  4.140  5.060  6.276  6.886  7.756  9.440 11.024 11.762
#> [11] 13.986 14.740 16.322 17.138 18.000 19.708 19.986

ecg.df <- data.frame(ECG = ecg,Seconds = c(1:length(ecg))/500)
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.2.3
ggplot(ecg.df,aes(x = Seconds,y = ECG)) +
  geom_line() + theme_bw() +
  geom_vline(data.frame(p = peaks),mapping = aes(xintercept = p), linetype="dashed",color = "red")