-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_powerByBand.py
39 lines (31 loc) · 1.44 KB
/
read_powerByBand.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import time
from brainflow.board_shim import BoardShim, BrainFlowInputParams, LogLevels, BoardIds
from brainflow.data_filter import DataFilter, WindowOperations, DetrendOperations
def main():
BoardShim.enable_dev_board_logger()
# use synthetic board for demo
params = BrainFlowInputParams()
board_id = BoardIds.SYNTHETIC_BOARD.value
board_descr = BoardShim.get_board_descr(board_id)
sampling_rate = int(board_descr['sampling_rate'])
board = BoardShim(board_id, params)
board.prepare_session()
board.start_stream()
BoardShim.log_message(LogLevels.LEVEL_INFO.value, 'start sleeping in the main thread')
time.sleep(10)
nfft = DataFilter.get_nearest_power_of_two(sampling_rate)
data = board.get_board_data()
board.stop_stream()
board.release_session()
eeg_channels = board_descr['eeg_channels']
# second eeg channel of synthetic board is a sine wave at 10Hz, should see huge alpha
eeg_channel = eeg_channels[1]
# optional detrend
DataFilter.detrend(data[eeg_channel], DetrendOperations.LINEAR.value)
psd = DataFilter.get_psd_welch(data[eeg_channel], nfft, nfft // 2, sampling_rate,
WindowOperations.BLACKMAN_HARRIS.value)
band_power_alpha = DataFilter.get_band_power(psd, 7.0, 13.0)
band_power_beta = DataFilter.get_band_power(psd, 14.0, 30.0)
print("alpha/beta:%f", band_power_alpha / band_power_beta)
if __name__ == "__main__":
main()