Skip to content

Commit ead62f4

Browse files
committed
Adding comments to heartbeat_ecg file
1 parent cdd51e4 commit ead62f4

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

heartbeat_ecg.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,21 @@ def __init__(self):
1515
self.setWindowTitle("Real-Time ECG Monitor") # Set up GUI window
1616
self.setGeometry(100, 100, 800, 600)
1717

18-
self.plot_widget = PlotWidget(self)
19-
self.plot_widget.setBackground('w')
20-
self.plot_widget.showGrid(x=True, y=True)
18+
self.plot_widget = PlotWidget(self) # Create the plotting widget
19+
self.plot_widget.setBackground('w') # Set background color to white
20+
self.plot_widget.showGrid(x=True, y=True) # Show grid lines
2121

2222
# Heart rate label at the bottom
2323
self.heart_rate_label = QLabel(self)
2424
self.heart_rate_label.setStyleSheet("font-size: 20px; font-weight: bold; color: black;")
2525
self.heart_rate_label.setAlignment(Qt.AlignCenter)
2626

27+
# Layout setup - vertical layout for plot and label
2728
layout = QVBoxLayout()
2829
layout.addWidget(self.plot_widget)
2930
layout.addWidget(self.heart_rate_label)
3031

32+
# Set the central widget that holds all other widgets
3133
central_widget = QWidget()
3234
central_widget.setLayout(layout)
3335
self.setCentralWidget(central_widget)
@@ -36,6 +38,7 @@ def __init__(self):
3638
print("Searching for available LSL streams...")
3739
available_streams = pylsl.resolve_streams()
3840

41+
# Exit if no streams are found
3942
if not available_streams:
4043
print("No LSL streams found! Exiting...")
4144
sys.exit(0)
@@ -53,21 +56,21 @@ def __init__(self):
5356
print("Unable to connect to any LSL stream! Exiting...")
5457
sys.exit(0)
5558

56-
# Sampling rate
59+
# Get Sampling rate from the stream info.
5760
self.sampling_rate = int(self.inlet.info().nominal_srate())
5861
print(f"Sampling rate: {self.sampling_rate} Hz")
5962

6063
# Data and buffers
6164
self.buffer_size = self.sampling_rate * 10 # Fixed-size buffer for 10 seconds
6265
self.ecg_data = np.zeros(self.buffer_size) # Fixed-size array for circular buffer
6366
self.time_data = np.linspace(0, 10, self.buffer_size) # Fixed time array for plotting
64-
self.r_peaks = [] # Store the indices of R-peaks
65-
self.heart_rate = None
67+
self.r_peaks = [] # Store the indices of R-peaks
68+
self.heart_rate = None # Initialize heart rate variable
6669
self.current_index = 0 # Index for overwriting data
6770

6871
self.b, self.a = butter(4, 20.0 / (0.5 * self.sampling_rate), btype='low') # Low-pass filter coefficients
6972

70-
self.timer = pg.QtCore.QTimer() # Timer for updating the plot
73+
self.timer = pg.QtCore.QTimer() # Timer for updating the plot (every 10 ms)
7174
self.timer.timeout.connect(self.update_plot)
7275
self.timer.start(10)
7376

@@ -86,7 +89,7 @@ def __init__(self):
8689
self.moving_average_window_size = 5 # Initialize moving average buffer
8790
self.heart_rate_history = [] # Buffer to store heart rates for moving average
8891

89-
# Connect double-click event
92+
# Connect double-click event for zoom reset
9093
self.plot_widget.scene().sigMouseClicked.connect(self.on_double_click)
9194

9295
def on_double_click(self, event):
@@ -139,11 +142,11 @@ def calculate_heart_rate(self):
139142
# Update heart rate label with moving average & convert into int
140143
self.heart_rate_label.setText(f"Heart Rate: {int(moving_average_hr)} BPM")
141144
else:
142-
self.heart_rate_label.setText("Heart Rate: Calculating...")
145+
self.heart_rate_label.setText("Heart Rate: Calculating...") # Display message if not enough R-peaks detected
143146

144147
def plot_r_peaks(self, filtered_ecg):
145-
r_peak_times = self.time_data[self.r_peaks] # Extract the time of detected R-peaks
146-
r_peak_values = filtered_ecg[self.r_peaks]
148+
r_peak_times = self.time_data[self.r_peaks] # Extract the time of detected R-peaks
149+
r_peak_values = filtered_ecg[self.r_peaks] # Get corresponding ECG values
147150
self.r_peak_curve.setData(r_peak_times, r_peak_values) # Plot R-peaks as red dots
148151

149152
if __name__ == "__main__":

0 commit comments

Comments
 (0)