How to Calculate Rr Interval in Matlab
RR interval is a fundamental measurement in cardiac physiology representing the time between consecutive R waves in an electrocardiogram (ECG). Calculating RR intervals in MATLAB is essential for heart rate variability (HRV) analysis and cardiovascular research. This guide explains how to accurately compute RR intervals using MATLAB's built-in functions and custom algorithms.
What is RR Interval?
The RR interval is the time duration between two consecutive R waves in an ECG signal. It's typically measured in milliseconds (ms) and provides critical information about heart rate variability. RR intervals are fundamental for:
- Calculating heart rate (HR = 60,000/RR interval in ms)
- Assessing heart rate variability (HRV)
- Detecting arrhythmias and abnormal heart rhythms
- Analyzing autonomic nervous system activity
Key Point: RR intervals are inversely proportional to heart rate. A shorter RR interval indicates a faster heart rate.
MATLAB Methods for RR Interval Calculation
MATLAB provides several approaches to calculate RR intervals from ECG data:
- Peak Detection: Using
findpeaksto identify R-wave peaks - Signal Processing: Applying bandpass filters and derivative-based methods
- Template Matching: Comparing against known R-wave templates
- Wavelet Analysis: Using wavelet transforms for feature extraction
Basic RR Interval Formula:
RRi = tRi+1 - tRi
Where tRi is the time of the i-th R-wave peak
Step-by-Step Guide
1. Load and Preprocess ECG Data
First, load your ECG signal and preprocess it:
% Load ECG data
load('ecg_data.mat'); % Assuming data is in variable 'ecg'
% Preprocess: Remove baseline wander and noise
fs = 1000; % Sampling frequency
ecg_filtered = bandpass(ecg, [5 15], fs);
2. Detect R-Wave Peaks
Use MATLAB's peak detection functions:
% Find R-wave peaks
[peaks, locs] = findpeaks(ecg_filtered, 'MinPeakHeight', 0.5, 'MinPeakDistance', 0.2*fs);
% Calculate RR intervals
rr_intervals = diff(locs)/fs * 1000; % Convert to milliseconds
3. Validate and Post-Process
Implement quality checks and artifact removal:
% Remove intervals outside physiological range
rr_intervals = rr_intervals(rr_intervals > 300 & rr_intervals < 2000);
% Calculate mean RR interval
mean_rr = mean(rr_intervals);
Example Calculation
Consider an ECG signal with the following R-wave peak locations (in samples at 1000 Hz sampling rate):
| Peak # | Sample Location | Time (ms) |
|---|---|---|
| 1 | 1000 | 1000 |
| 2 | 1500 | 1500 |
| 3 | 2000 | 2000 |
| 4 | 2500 | 2500 |
The calculated RR intervals would be:
- RR1 = 1500 - 1000 = 500 ms
- RR2 = 2000 - 1500 = 500 ms
- RR3 = 2500 - 2000 = 500 ms
This results in a constant heart rate of 120 beats per minute (60,000/500).