Skip to content

Commit 2ede52f

Browse files
committed
Implemented Moving Window concept: Buffer updates by adding 50 new samples and removing the previous 50 samples before calculating FFT.
1 parent e5a59a8 commit 2ede52f

File tree

1 file changed

+18
-29
lines changed

1 file changed

+18
-29
lines changed

ffteeg.py

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(self):
2727
self.eeg_plot_widget.showGrid(x=True, y=True)
2828
self.eeg_plot_widget.setLabel('bottom', 'EEG Plot')
2929
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)
3131
self.eeg_plot_widget.setMouseEnabled(x=False, y=True) # Disable zoom
3232
self.main_layout.addWidget(self.eeg_plot_widget)
3333

@@ -39,9 +39,9 @@ def __init__(self):
3939
self.fft_plot.setBackground('w')
4040
self.fft_plot.showGrid(x=True, y=True)
4141
self.fft_plot.setLabel('bottom', 'FFT')
42-
# self.fft_plot.setYRange(0, 25000, padding=0)
42+
# self.fft_plot.setYRange(0, 500, padding=0)
4343
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
4545
self.fft_plot.setAutoVisible(y=True) # Allow y-axis to autoscale
4646
self.bottom_layout.addWidget(self.fft_plot)
4747

@@ -74,13 +74,8 @@ def __init__(self):
7474
print(f"Sampling rate: {self.sampling_rate} Hz")
7575

7676
# 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)
8479

8580
self.b_notch, self.a_notch = iirnotch(50, 30, self.sampling_rate)
8681
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):
9388
self.timer.timeout.connect(self.update_plot)
9489
self.timer.start(20)
9590

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))
9792
self.fft_curve = self.fft_plot.plot(pen=pg.mkPen('r', width=1)) # FFT Colour is red
9893

9994
def update_plot(self):
@@ -106,29 +101,23 @@ def update_plot(self):
106101
band_filtered, self.zi_band = lfilter(self.b_band, self.a_band, notch_filtered, zi=self.zi_band)
107102
band_filtered = band_filtered[-1] # Get the current filtered point
108103

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)
112106

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)
115109
else:
116-
plot_data = np.concatenate((self.eeg_data[self.current_index:], self.eeg_data[:self.current_index]))
110+
self.process_fft_and_brainpower()
117111

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)
121113

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)
130117

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
132121
fft_result = np.abs(fft(buffer_windowed))[:len(buffer_windowed) // 2]
133122
fft_result /= len(buffer_windowed)
134123
freqs = np.fft.fftfreq(len(buffer_windowed), 1 / self.sampling_rate)[:len(buffer_windowed) // 2]

0 commit comments

Comments
 (0)