ECG Signal to Heart Rate Calculator
Verify your MATLAB calculations by converting Sampling Frequency and R-R intervals into BPM.
How to Calculate Heart Rate from ECG in MATLAB
Calculating heart rate from an Electrocardiogram (ECG) signal in MATLAB involves detecting the prominent R-peaks in the QRS complex and measuring the time difference between them. The calculator above helps you verify your manual calculations or debug your code logic.
The Logic Behind the Calculation
Heart rate is essentially the reciprocal of the R-R interval. In a digital signal processing context within MATLAB, you are dealing with discrete samples rather than continuous time. To convert the sample distance to beats per minute (BPM), you apply the following formula:
1. Convert Samples to Seconds:
Time (s) = Number of Samples / Sampling Frequency (Fs)
2. Convert Time to BPM:
BPM = 60 / Time (s)
Combining these, the direct formula used in the calculator above is:
BPM = (60 * Fs) / RR_samples
MATLAB Implementation Guide
To implement this programmatically, you typically use the findpeaks function to locate the R-waves. Here is a standard snippet of MATLAB code to calculate heart rate from a raw ECG vector:
Troubleshooting Common Errors
- Noisy Signal: If your calculated BPM is extremely high (e.g., >200), your
findpeaksthreshold might be too low, detecting T-waves as R-peaks. Use a high-pass filter or increase 'MinPeakHeight'. - Drifting Baseline: Ensure you remove the baseline wander (low-frequency noise) before peak detection to ensure accurate R-peak amplitude measurement.
- Variable Heart Rate: In cases of arrhythmia, calculating the mean R-R interval might obscure variability. It is often better to calculate the "Instantaneous Heart Rate" for every interval.