9
9
from scipy .signal import butter , iirnotch , lfilter , lfilter_zi
10
10
from scipy .fft import fft
11
11
import math
12
+ import time
12
13
13
14
class EEGMonitor (QMainWindow ):
14
15
def __init__ (self ):
@@ -17,6 +18,9 @@ def __init__(self):
17
18
self .setWindowTitle ("Real-Time EEG Monitor with FFT and Brainwave Power" )
18
19
self .setGeometry (100 , 100 , 1200 , 800 )
19
20
21
+ self .stream_active = True # Flag to check if the stream is active
22
+ self .last_data_time = None # Variable to store the last data time
23
+
20
24
# Main layout split into two halves: top for EEG, bottom for FFT and Brainwaves
21
25
self .central_widget = QWidget ()
22
26
self .main_layout = QVBoxLayout (self .central_widget )
@@ -108,6 +112,7 @@ def __init__(self):
108
112
def update_plot (self ):
109
113
samples , _ = self .inlet .pull_chunk (timeout = 0.0 )
110
114
if samples :
115
+ self .last_data_time = time .time () # Store the last data time
111
116
for sample in samples :
112
117
raw_point = sample [0 ]
113
118
@@ -129,6 +134,13 @@ def update_plot(self):
129
134
time_axis = np .linspace (0 , 4 , len (plot_data ))
130
135
self .eeg_curve .setData (time_axis , plot_data )
131
136
137
+ else :
138
+ if self .last_data_time and (time .time () - self .last_data_time ) > 2 :
139
+ self .stream_active = False
140
+ print ("LSL stream disconnected!" )
141
+ self .timer .stop ()
142
+ self .close ()
143
+
132
144
def process_fft_and_brainpower (self ):
133
145
window = np .hanning (len (self .moving_window ))
134
146
buffer_windowed = np .array (self .moving_window ) * window
0 commit comments