@@ -15,19 +15,21 @@ def __init__(self):
15
15
self .setWindowTitle ("Real-Time ECG Monitor" ) # Set up GUI window
16
16
self .setGeometry (100 , 100 , 800 , 600 )
17
17
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
21
21
22
22
# Heart rate label at the bottom
23
23
self .heart_rate_label = QLabel (self )
24
24
self .heart_rate_label .setStyleSheet ("font-size: 20px; font-weight: bold; color: black;" )
25
25
self .heart_rate_label .setAlignment (Qt .AlignCenter )
26
26
27
+ # Layout setup - vertical layout for plot and label
27
28
layout = QVBoxLayout ()
28
29
layout .addWidget (self .plot_widget )
29
30
layout .addWidget (self .heart_rate_label )
30
31
32
+ # Set the central widget that holds all other widgets
31
33
central_widget = QWidget ()
32
34
central_widget .setLayout (layout )
33
35
self .setCentralWidget (central_widget )
@@ -36,6 +38,7 @@ def __init__(self):
36
38
print ("Searching for available LSL streams..." )
37
39
available_streams = pylsl .resolve_streams ()
38
40
41
+ # Exit if no streams are found
39
42
if not available_streams :
40
43
print ("No LSL streams found! Exiting..." )
41
44
sys .exit (0 )
@@ -53,21 +56,21 @@ def __init__(self):
53
56
print ("Unable to connect to any LSL stream! Exiting..." )
54
57
sys .exit (0 )
55
58
56
- # Sampling rate
59
+ # Get Sampling rate from the stream info.
57
60
self .sampling_rate = int (self .inlet .info ().nominal_srate ())
58
61
print (f"Sampling rate: { self .sampling_rate } Hz" )
59
62
60
63
# Data and buffers
61
64
self .buffer_size = self .sampling_rate * 10 # Fixed-size buffer for 10 seconds
62
65
self .ecg_data = np .zeros (self .buffer_size ) # Fixed-size array for circular buffer
63
66
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
66
69
self .current_index = 0 # Index for overwriting data
67
70
68
71
self .b , self .a = butter (4 , 20.0 / (0.5 * self .sampling_rate ), btype = 'low' ) # Low-pass filter coefficients
69
72
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)
71
74
self .timer .timeout .connect (self .update_plot )
72
75
self .timer .start (10 )
73
76
@@ -86,7 +89,7 @@ def __init__(self):
86
89
self .moving_average_window_size = 5 # Initialize moving average buffer
87
90
self .heart_rate_history = [] # Buffer to store heart rates for moving average
88
91
89
- # Connect double-click event
92
+ # Connect double-click event for zoom reset
90
93
self .plot_widget .scene ().sigMouseClicked .connect (self .on_double_click )
91
94
92
95
def on_double_click (self , event ):
@@ -139,11 +142,11 @@ def calculate_heart_rate(self):
139
142
# Update heart rate label with moving average & convert into int
140
143
self .heart_rate_label .setText (f"Heart Rate: { int (moving_average_hr )} BPM" )
141
144
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
143
146
144
147
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
147
150
self .r_peak_curve .setData (r_peak_times , r_peak_values ) # Plot R-peaks as red dots
148
151
149
152
if __name__ == "__main__" :
0 commit comments