-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.py
56 lines (52 loc) · 2.25 KB
/
connect.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#------------------ Importing Libraries ----------------
'''Data Preprocessing'''
import numpy as np
'''OpenBCI'''
from pyOpenBCI import OpenBCICyton
'''LSL'''
from pylsl import StreamInfo, StreamOutlet
#------------------ Importing Libraries ----------------
#------------------ OpenBCI Recording ------------------
def record(self):
"""
Records EEG and AUX data from an OpenBCI board and saves it to a file.
This function creates an LSL stream for EEG and AUX data, initializes the stream outlets,
opens a file for writing, and starts streaming data from the OpenBCI board. The recorded
data is then written to the file with the appropriate scaling factors and labels.
Args:
self: The instance of the class.
Returns:
None
"""
SCALE_FACTOR_EEG = (4500000)/24/(2**23-1) #uV/count
SCALE_FACTOR_AUX = 0.002 / (2**4)
print("Creating LSL stream for EEG. \nName: OpenBCIEEG\nID: OpenBCItestEEG\n")
info_eeg = StreamInfo('OpenBCIEEG', 'EEG', 8, 250, 'float32', 'OpenBCItestEEG')
print("Creating LSL stream for AUX. \nName: OpenBCIAUX\nID: OpenBCItestEEG\n")
info_aux = StreamInfo('OpenBCIAUX', 'AUX', 3, 250, 'float32', 'OpenBCItestAUX')
outlet_eeg = StreamOutlet(info_eeg)
outlet_aux = StreamOutlet(info_aux)
file_out = open('newest_rename.csv', 'a')
file_out.truncate(0)
file_out.write('ch1,ch2,ch3,ch4,ch5,ch6,ch7,ch8,aux1,aux2,aux3,label\n')
def lsl_streamers(sample):
file_in = open('tempVal.txt', 'r')
input = file_in.readline()
lbl = ''
if input != '':
lbl = input
else:
lbl = 'norm'
outlet_eeg.push_sample(np.array(sample.channels_data)*SCALE_FACTOR_EEG)
outlet_aux.push_sample(np.array(sample.aux_data)*SCALE_FACTOR_AUX)
#print(sample.channels_data*SCALE_FACTOR_EEG, sample.aux_data*SCALE_FACTOR_AUX, lbl)
for datai in sample.channels_data:
file_out.write(str(datai*SCALE_FACTOR_EEG) + ',')
for dataj in sample.aux_data:
file_out.write(str(dataj*SCALE_FACTOR_AUX) + ',')
file_out.write(str(lbl) + '\n')
file_in.close()
board = OpenBCICyton()
board.start_stream(lsl_streamers)
file_out.close()
#------------------ OpenBCI Recording ------------------