Identifies artifacts in RR intervals by comparing each interval to a moving average of surrounding intervals. This implements a robust artifact detection algorithm commonly used in HRV analysis.
detect_rr_artifacts(
rr_intervals,
window_size = 7,
threshold = 0.2,
centered_window = FALSE
)Numeric vector of RR intervals in milliseconds
Size of the moving window for artifact detection. Default is 7 intervals
Threshold for artifact detection as a proportion. Default is 0.2 (20% deviation from moving average)
Logical indicating whether the moving window should be centered around each point. Default is FALSE (backward-looking window)
A logical vector of the same length as rr_intervals, where TRUE
indicates the RR interval is considered valid (not an artifact)
The algorithm works by:
Computing a moving average of RR intervals within a sliding window
Comparing each interval to the moving average
Flagging intervals that deviate by more than the threshold proportion
Using a centered or backward-looking window based on centered_window
This is a key preprocessing step for reliable HRV analysis, as artifacts can significantly affect calculated metrics.
if (FALSE) { # \dontrun{
rr_data <- c(800, 850, 400, 900, 820, 890, 870) # 400 is likely an artifact
valid_mask <- detect_rr_artifacts(rr_data, window_size = 5, threshold = 0.2)
clean_rr <- rr_data[valid_mask]
} # }