-
Notifications
You must be signed in to change notification settings - Fork 0
/
Recording 6 channel data for MI.py
209 lines (172 loc) · 6.56 KB
/
Recording 6 channel data for MI.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""
Simple Python RDA client for the RDA tcpip interface of the BrainVision Recorder
It reads all the information from the recorded EEG,
prints EEG and marker information to the console and calculates and
prints the average power every second
Brain Products GmbH
Gilching/Freiburg, Germany
www.brainproducts.com
"""
# needs socket and struct library
from socket import *
from struct import *
import numpy as np
zero=[]
one=[]
two=[]
three=[]
four=[]
five=[]
# Marker class for storing marker information
class Marker:
def __init__(self):
self.position = 0
self.points = 0
self.channel = -1
self.type = ""
self.description = ""
# Helper function for receiving whole message
def RecvData(socket, requestedSize):
returnStream = b''
while len(returnStream) < requestedSize:
databytes = socket.recv(requestedSize - len(returnStream))
if databytes == b'':
raise RuntimeError("connection broken")
returnStream += databytes
return returnStream
# Helper function for splitting a raw array of
# zero terminated strings (C) into an array of python strings
def SplitString(raw):
stringlist = []
s = b""
for i in range(len(raw)):
if raw[i] != 0:
s += bytes([raw[i]])
else:
stringlist.append(s.decode())
s = b""
return stringlist
# Helper function for extracting eeg properties from a raw data array
# read from tcpip socket
def GetProperties(rawdata):
# Extract numerical data
(channelCount, samplingInterval) = unpack('<Ld', rawdata[:12])
# Extract resolutions
resolutions = []
for c in range(channelCount):
index = 12 + c * 8
restuple = unpack('<d', rawdata[index:index+8])
resolutions.append(restuple[0])
# Extract channel names
channelNames = SplitString(rawdata[12 + 8 * channelCount:])
return (channelCount, samplingInterval, resolutions, channelNames)
# Helper function for extracting eeg and marker data from a raw data array
# read from tcpip socket
def GetData(rawdata, channelCount):
# Extract numerical data
(block, points, markerCount) = unpack('<LLL', rawdata[:12])
# Extract eeg data as array of floats
data = []
for i in range(points * channelCount):
index = 12 + 4 * i
value = unpack('<f', rawdata[index:index+4])
data.append(value[0])
# Extract markers
markers = []
index = 12 + 4 * points * channelCount
for m in range(markerCount):
markersize = unpack('<L', rawdata[index:index+4])[0]
ma = Marker()
(ma.position, ma.points, ma.channel) = unpack('<LLl', rawdata[index+4:index+16])
typedesc = SplitString(rawdata[index+16:index+markersize])
ma.type = typedesc[0]
ma.description = typedesc[1]
markers.append(ma)
index = index + markersize
return (block, points, markerCount, data, markers)
##############################################################################################
#
# Main RDA routine
#
##############################################################################################
# Create a tcpip socket
con = socket(AF_INET, SOCK_STREAM)
# Connect to recorder host via 32Bit RDA-port
# adapt to your host, if recorder is not running on local machine
# change port to 51234 to connect to 16Bit RDA-port
con.connect(("localhost", 51244))
# Flag for main loop
finish = False
# data buffer for calculation, empty in beginning
data1s = []
# block counter to check overflows of tcpip buffer
lastBlock = -1
#### Main Loop ####
started=False
while not finish:
# print(finish)
# Get message header as raw array of chars
rawhdr = RecvData(con, 24)
# Split array into useful information id1 to id4 are constants
(id1, id2, id3, id4, msgsize, msgtype) = unpack('<llllLL', rawhdr)
# Get data part of message, which is of variable size
rawdata = RecvData(con, msgsize - 24)
# print("msgtype:",msgtype)
# Perform action dependent on the message type
if msgtype == 1:
# Start message, extract eeg properties and display them
(channelCount, samplingInterval, resolutions, channelNames) = GetProperties(rawdata)
# reset block counter
lastBlock = -1
print("Start")
print("Number of channels: " + str(channelCount))
print("Sampling interval: " + str(samplingInterval))
print("Resolutions: " + str(resolutions))
print("Channel Names: " + str(channelNames))
elif len(zero)==500:
print("this")
# Stop message, terminate program
print("Stop")
print(msgtype)
finish=True
elif msgtype==4:
# Data message, extract data and markers
(block, points, markerCount, data, markers) = GetData(rawdata, channelCount)
# Check for overflow
if lastBlock != -1 and block > lastBlock + 1:
print("*** Overflow with " + str(block - lastBlock) + " datablocks ***")
lastBlock = block
# Print markers, if there are some in actual block
if markerCount > 0:
for m in range(markerCount):
print("Marker " + markers[m].description + " of type " + markers[m].type)
# Put data at the end of actual buffer
data1s.extend(data)
# If more than 1s of data is collected, calculate average power, print it and reset data buffer
if len(data1s) > channelCount * 1000000 / samplingInterval:
index = int(len(data1s) - channelCount * 1000000 / samplingInterval)
data1s = data1s[index:]
# print(len(store),channelCount)
# print(store[0])
# Do not forget to respect the resolution !!!
zero.extend(data1s[0::6])
one.extend(data1s[1::6])
two.extend(data1s[2::6])
three.extend(data1s[3::6])
four.extend(data1s[4::6])
five.extend(data1s[5::6])
data1s=[]
elif msgtype == 3:
print("that")
# Stop message, terminate program
print("Stop")
print(msgtype)
finish = True
# ans=(input("class: "))
ans='l'
# anss=[ans for i in range(len(zero))]
store=[zero,one,two,three,four,five]
wwww=int(input())
np.save(f"Motor imagery\\Improved dataset\\{ans}_{wwww}.npy",np.array(store))
# Close tcpip connection
con.close()