@@ -27,7 +27,7 @@ def __init__(self):
27
27
self .eeg_plot_widget .showGrid (x = True , y = True )
28
28
self .eeg_plot_widget .setLabel ('bottom' , 'EEG Plot' )
29
29
self .eeg_plot_widget .setYRange (- 5000 , 5000 , padding = 0 )
30
- self .eeg_plot_widget .setXRange (0 , 2 , padding = 0 )
30
+ self .eeg_plot_widget .setXRange (0 , 4 , padding = 0 )
31
31
self .eeg_plot_widget .setMouseEnabled (x = False , y = True ) # Disable zoom
32
32
self .main_layout .addWidget (self .eeg_plot_widget )
33
33
@@ -39,9 +39,9 @@ def __init__(self):
39
39
self .fft_plot .setBackground ('w' )
40
40
self .fft_plot .showGrid (x = True , y = True )
41
41
self .fft_plot .setLabel ('bottom' , 'FFT' )
42
- # self.fft_plot.setYRange(0, 25000 , padding=0)
42
+ # self.fft_plot.setYRange(0, 500 , padding=0)
43
43
self .fft_plot .setXRange (0 , 50 , padding = 0 ) # Set x-axis to 0 to 50 Hz
44
- self .fft_plot .setMouseEnabled (x = False , y = False ) # Disable zoom
44
+ # self.fft_plot.setMouseEnabled(x=False, y=False) # Disable zoom
45
45
self .fft_plot .setAutoVisible (y = True ) # Allow y-axis to autoscale
46
46
self .bottom_layout .addWidget (self .fft_plot )
47
47
@@ -74,13 +74,8 @@ def __init__(self):
74
74
print (f"Sampling rate: { self .sampling_rate } Hz" )
75
75
76
76
# Data and Buffers
77
- self .one_second_buffer = deque (maxlen = self .sampling_rate ) # 1-second buffer
78
- self .buffer_size = self .sampling_rate * 10
79
- self .moving_window_size = self .sampling_rate * 2 # 2-second window
80
-
81
- self .eeg_data = np .zeros (self .buffer_size )
82
- self .time_data = np .linspace (0 , 10 , self .buffer_size )
83
- self .current_index = 0
77
+ self .eeg_data = deque (maxlen = 500 ) # Initialize moving window with 500 samples
78
+ self .moving_window = deque (maxlen = 500 ) # 500 samples for FFT and power calculation (sliding window)
84
79
85
80
self .b_notch , self .a_notch = iirnotch (50 , 30 , self .sampling_rate )
86
81
self .b_band , self .a_band = butter (4 , [0.5 / (self .sampling_rate / 2 ), 48.0 / (self .sampling_rate / 2 )], btype = 'band' )
@@ -93,7 +88,7 @@ def __init__(self):
93
88
self .timer .timeout .connect (self .update_plot )
94
89
self .timer .start (20 )
95
90
96
- self .eeg_curve = self .eeg_plot_widget .plot (self . time_data , self . eeg_data , pen = pg .mkPen ('b' , width = 1 )) #EEG Colour is blue
91
+ self .eeg_curve = self .eeg_plot_widget .plot (pen = pg .mkPen ('b' , width = 1 ))
97
92
self .fft_curve = self .fft_plot .plot (pen = pg .mkPen ('r' , width = 1 )) # FFT Colour is red
98
93
99
94
def update_plot (self ):
@@ -106,29 +101,23 @@ def update_plot(self):
106
101
band_filtered , self .zi_band = lfilter (self .b_band , self .a_band , notch_filtered , zi = self .zi_band )
107
102
band_filtered = band_filtered [- 1 ] # Get the current filtered point
108
103
109
- # Update the EEG plot
110
- self .eeg_data [self .current_index ] = band_filtered
111
- self .current_index = (self .current_index + 1 ) % self .buffer_size
104
+ # Update EEG data buffer
105
+ self .eeg_data .append (band_filtered )
112
106
113
- if self .current_index == 0 :
114
- plot_data = self .eeg_data
107
+ if len ( self .moving_window ) < 500 :
108
+ self .moving_window . append ( band_filtered )
115
109
else :
116
- plot_data = np . concatenate (( self .eeg_data [ self . current_index :], self . eeg_data [: self . current_index ]) )
110
+ self .process_fft_and_brainpower ( )
117
111
118
- recent_data = plot_data [- self .moving_window_size :]
119
- recent_time = np .linspace (0 , len (recent_data ) / self .sampling_rate , len (recent_data ))
120
- self .eeg_curve .setData (recent_time , recent_data )
112
+ self .moving_window = deque (list (self .moving_window )[50 :] + [band_filtered ], maxlen = 500 )
121
113
122
- self .one_second_buffer .append (band_filtered ) # Add the filtered point to the 1-second buffer
123
- if len (self .one_second_buffer ) == self .sampling_rate : # Process FFT and brainwave power
124
- self .process_fft_and_brainpower ()
125
- self .one_second_buffer .clear ()
126
-
127
- def process_fft_and_brainpower (self ):
128
- window = np .hanning (len (self .one_second_buffer )) # Apply Hanning window to the buffer
129
- buffer_windowed = np .array (self .one_second_buffer ) * window
114
+ plot_data = np .array (self .eeg_data )
115
+ time_axis = np .linspace (0 , 4 , len (plot_data ))
116
+ self .eeg_curve .setData (time_axis , plot_data )
130
117
131
- # Perform FFT
118
+ def process_fft_and_brainpower (self ):
119
+ window = np .hanning (len (self .moving_window ))
120
+ buffer_windowed = np .array (self .moving_window ) * window
132
121
fft_result = np .abs (fft (buffer_windowed ))[:len (buffer_windowed ) // 2 ]
133
122
fft_result /= len (buffer_windowed )
134
123
freqs = np .fft .fftfreq (len (buffer_windowed ), 1 / self .sampling_rate )[:len (buffer_windowed ) // 2 ]
0 commit comments