PhysioECG provides a complete pipeline for electrocardiogram (ECG) analysis built on the PhysioExperiment data model. This vignette demonstrates the core workflow: R-peak detection, RR interval computation, ectopic beat handling, and heart rate variability (HRV) analysis across time-domain, frequency-domain, and nonlinear methods.
PhysioECG includes data generators for creating synthetic ECG
signals. The make_ecg() function produces a
PhysioExperiment with Gaussian-shaped R-peaks at regular intervals.
# Create a 10-second ECG recording at 500 Hz with heart rate of 72 bpm
pe <- make_ecg(n_time = 5000, n_channels = 1, sr = 500, heart_rate = 72)
peFor testing with realistic noise, use
make_ecg_noisy():
Before analysis, assess signal quality to identify channels with poor recording conditions.
When R-peaks have been detected, passing them to
ecgSignalQuality() enables QRS-based SNR estimation for
more accurate quality metrics.
Remove low-frequency baseline wander before R-peak detection for best results.
The ecgDetectRpeaks() function implements the
Pan-Tompkins algorithm with adaptive dual-threshold classification. It
automatically handles inverted ECG signals.
The result is a data.frame with columns channel,
sample, time_sec, and amplitude
for each detected R-peak.
Compute RR intervals from the detected R-peaks. The resulting data.frame is the standard input for all HRV analysis functions.
Before HRV analysis, identify and correct ectopic (abnormal) beats that can bias HRV metrics.
# Detect ectopic beats using local median comparison
rr_checked <- ecgQualityCheck(rr, threshold_ms = 300)
table(rr_checked$is_ectopic)Correct ectopic beats by interpolation or removal:
Compute standard time-domain HRV metrics: SDNN, RMSSD, pNN50, mean RR, and mean heart rate.
The Task Force of the European Society of Cardiology (1996) recommends a minimum recording length of 5 minutes for short-term HRV analysis.
Compute frequency-domain HRV metrics using Welch’s method or the Lomb-Scargle periodogram:
# Welch's method (resamples to uniform 4 Hz grid)
hrv_freq <- ecgHRVfreq(rr_corrected, method = "welch")
hrv_freq# Lomb-Scargle periodogram (handles uneven sampling directly)
hrv_freq_ls <- ecgHRVfreq(rr_corrected, method = "lomb")
hrv_freq_lsStandard frequency bands are VLF (0.003–0.04 Hz), LF (0.04–0.15 Hz), and HF (0.15–0.4 Hz). Custom bands can be specified:
Compute nonlinear HRV metrics including Poincare plot descriptors (SD1, SD2), sample entropy, and detrended fluctuation analysis (DFA).
Here is a full pipeline from raw ECG to HRV metrics:
# 1. Load or simulate ECG data
pe <- make_ecg(n_time = 60000, n_channels = 1, sr = 500, heart_rate = 68)
# 2. Assess signal quality
sq <- ecgSignalQuality(pe)
# 3. Correct baseline wander
pe <- ecgBaselineCorrect(pe, method = "highpass", cutoff = 0.5,
output_assay = "clean")
# 4. Detect R-peaks on cleaned signal
peaks <- ecgDetectRpeaks(pe, assay_name = "clean")
# 5. Compute RR intervals
rr <- ecgRRintervals(pe, peaks)
# 6. Detect and correct ectopic beats
rr <- ecgQualityCheck(rr, threshold_ms = 300)
rr <- ecgRRcorrect(rr, method = "interpolate")
# 7. Compute HRV metrics
hrv_time <- ecgHRVtime(rr)
hrv_freq <- ecgHRVfreq(rr, method = "welch")
hrv_nl <- ecgHRVnonlinear(rr)
# 8. Combine results
list(
time_domain = hrv_time,
freq_domain = hrv_freq,
nonlinear = hrv_nl
)