An electrocardiogram (ECG or EKG) is a test that records the electrical activity of the heart. Calculating the heart rate from an ECG is a common and important diagnostic tool. The rate is typically expressed in beats per minute (bpm).
function calculateEcgRate() {
var rrIntervalInput = document.getElementById("rrInterval");
var calibrationRateInput = document.getElementById("calibrationRate");
var resultDiv = document.getElementById("result");
var rrInterval = parseFloat(rrIntervalInput.value);
var calibrationRate = parseFloat(calibrationRateInput.value);
resultDiv.innerHTML = ""; // Clear previous results
if (isNaN(rrInterval) || isNaN(calibrationRate)) {
resultDiv.innerHTML = "Please enter valid numbers for all fields.";
return;
}
if (rrInterval <= 0) {
resultDiv.innerHTML = "R-R Interval must be a positive value.";
return;
}
if (calibrationRate <= 0) {
resultDiv.innerHTML = "ECG Paper Speed must be a positive value.";
return;
}
// Method 1: Using R-R Interval (most accurate for irregular rhythms)
// Heart Rate (bpm) = 60 / R-R Interval (seconds)
var rateFromRR = 60 / rrInterval;
// Method 2: Using large boxes (for regular rhythms)
// Assume standard ECG paper where 1 small box = 0.04 seconds and 1 large box = 5 small boxes = 0.20 seconds.
// The calibrationRate input allows for non-standard paper speeds.
// Number of seconds per large box = 5 * 0.04 = 0.20 seconds (if paper speed is 25 mm/sec)
// If paper speed is different, we can infer the duration of a large box if we know the mm per box.
// However, a more direct method for regular rhythms is counting QRS complexes in a set time.
// A simpler approach for calculators is often using the R-R interval or counting complexes in 6 seconds.
// Let's stick to the R-R interval as it's universally applicable and more precise.
// We will also show a calculation based on the number of large boxes between R waves if the user knows it.
var rateDisplay = "Heart Rate: " + rateFromRR.toFixed(2) + " bpm";
// For educational purposes, let's add a common method if the rhythm is regular.
// This method assumes the user can count the large boxes.
// A common way to estimate is to count the number of large boxes between two consecutive R waves.
// Heart Rate (bpm) = 300 / (Number of large boxes between R-R)
// We can derive the number of large boxes from the R-R interval and calibration speed if needed.
// Number of large boxes = R-R Interval (seconds) / (Duration of one large box in seconds)
// Assuming a standard large box is 5 small boxes, and a small box is 0.04s (at 25mm/s).
// Duration of one large box = 0.20 seconds (at 25mm/s).
// If calibrationRate is provided, we can calculate the duration of a small box: 1 sec / (calibrationRate mm/sec * mm per small box).
// A more direct calculation for regular rhythms using the 300/large boxes method needs the *number* of boxes, not the speed.
// Let's provide the R-R interval method as the primary and most reliable for a calculator.
resultDiv.innerHTML = rateDisplay;
}
#ecgRateCalculator {
font-family: sans-serif;
max-width: 500px;
margin: 20px auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
#ecgRateCalculator h1 {
text-align: center;
margin-bottom: 20px;
color: #333;
}
.calculator-inputs {
margin-bottom: 20px;
}
.input-group {
margin-bottom: 15px;
}
.input-group label {
display: block;
margin-bottom: 5px;
font-weight: bold;
color: #555;
}
.input-group input[type="number"] {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
#ecgRateCalculator button {
display: block;
width: 100%;
padding: 12px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s ease;
}
#ecgRateCalculator button:hover {
background-color: #0056b3;
}
#result {
margin-top: 20px;
padding: 15px;
background-color: #e9ecef;
border: 1px solid #ced4da;
border-radius: 4px;
text-align: center;
font-size: 1.1em;
}
#result p {
margin: 0;
}