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
)

Arguments

rr_intervals

Numeric vector of RR intervals in milliseconds

window_size

Size of the moving window for artifact detection. Default is 7 intervals

threshold

Threshold for artifact detection as a proportion. Default is 0.2 (20% deviation from moving average)

centered_window

Logical indicating whether the moving window should be centered around each point. Default is FALSE (backward-looking window)

Value

A logical vector of the same length as rr_intervals, where TRUE indicates the RR interval is considered valid (not an artifact)

Details

The algorithm works by:

  1. Computing a moving average of RR intervals within a sliding window

  2. Comparing each interval to the moving average

  3. Flagging intervals that deviate by more than the threshold proportion

  4. 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.

Examples

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]
} # }