-
Notifications
You must be signed in to change notification settings - Fork 5
/
muse_recorder.m
260 lines (229 loc) · 8.12 KB
/
muse_recorder.m
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
%MUSE RECORDER(FILE_NAME, DURATION, PORT, PRESET)
% 1. Executes the application muse-io.exe (Installed with the Muse SDK)
% https://sites.google.com/a/interaxon.ca/muse-developer-site/home
% 2. Reads the OSC packages from the muse-io.exe using the PORT and PRESET
% specified. PORT defualt is 5000, and PRESET default is 14
% 3. Saves the MUSE configuration in a TXT file FILE_NAME_conf.txt
% 4. Saves the EEG samples in CSV file FILE_NALE_eeg.csv
% 5. Saves teh Acceleration data in CSV file FILE_NAME_eeg.csv
% The recording will finished when the there is daa from DURATION seconds,
% or when there is a communication problem with the Muse headband
%
% IMPORTANT if the file already exist, the data will be appended at the end
% of the file. Make sure to have a new name for a new record
%
% Raymundo Cassani
% November 2014
%
% Examples
% muse_recorder('experiment1', [], [] ,[])
% will record data from the Muse headband using port 5000 and
% defualt preset (14)
%
% muse_recorder('experiment2', 30, 6000 ,[])
% will record data from the Muse headband during 30 seconds
% using port 6000, defualt preset (14)
%
function muse_recorder(file_name, duration, port, preset)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%% OBTAIN PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Check FILE_NAME
if isempty(file_name)
error('File name must be provided')
end
% Check DUATION
if isempty(duration)
duration = -1;
end
% Check PORT
if isempty(port)
port = 5000;
end
% Check PRESET
if isempty(preset)
preset = 14;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%% FILES AND PATHS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
path_file = pwd;
eeg_file = [path_file, '\', file_name, '_eeg.csv'];
acc_file = [path_file, '\', file_name, '_acc.csv'];
conf_file = [path_file, '\', file_name, '_conf.txt'];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% START muse-io.exe AND CONNECTION WIT IT%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Check if the Instrumentation Control Toolbox is present
tbName = 'Instrument Control Toolbox';
verInfo = ver;
tbFlag = any(strcmp(tbName, {verInfo.Name}));
% Obtain release year
release = sscanf(version('-release'),'%d%s');
releaseYear = release(1);
% OCS Paths
% Note that these paths dependes of the muse-io.exe version, in this case
% V3.4.0
oscPathV3_4_0{1,1} = '/muse/eeg';
oscPathV3_4_0{1,2} = 'fff';
oscPathV3_4_0{2,1} = '/muse/acc';
oscPathV3_4_0{2,2} = 'fff';
oscPathV3_4_0{3,1} = '/muse/config';
oscPathV3_4_0{3,2} = 's';
% Server parameters
% These parameters configure where the data fom muse-io.exe will be sent
ip = '0.0.0.0'; %Localhost
% PORT was defined above
timeoutSec = 10; %In seconds
% Starts muse-io.exe
% Preset 14 set the Muse headset to deliver 4 channels:
% {'TP9'; 'FP1'; 'FP2'; 'TP10'}
system(['start "Running: muse-io.exe --preset ' num2str(preset),'" "C:\Program Files (x86)\Muse\muse-io.exe" --preset ' num2str(preset) ' --osc osc.tcp://localhost:' num2str(port)]);
% This flag (tcpFlag) indicates if the TCP connection will be done using the
% TCP/IP objects(tcpFlag == true) Instrumentation Control Toolbox and Relase >2011 are needed;
% OR using
% Java ServerSocker (tcpFlag == false)
tcpFlag = tbFlag && releaseYear > 2011;
if tcpFlag
tcpServer=tcpip(ip, port, 'NetworkRole', 'server');
tcpServer.InputBufferSize = 5000;
tcpServer.Timeout = timeoutSec;
%Open a connection. This will not return until a connection is received.
fopen(tcpServer);
else
import java.net.ServerSocket
import java.net.InetSocketAddress
import java.io.*
serverISA = InetSocketAddress(ip, port);
serverSSocket = ServerSocket(port,0,serverISA.getAddress);
serverSSocket.setSoTimeout(timeoutSec*1000);
%Open a connection. This will not return until a connection is received.
serverSocket = serverSSocket.accept;
serverInputStream = serverSocket.getInputStream();
serverDIS = DataInputStream(serverInputStream);
end
%Size of buffers to Read EEG ACC
%As the EEG output sampling frequency is 220Hz
fse = 220;
fsa = 50;
secBuffer = 20;
eegName = {'TP9'; 'FP1'; 'FP2'; 'TP10'};
eegBuffer = zeros([fse*secBuffer,numel(eegName)]);
accName = {'F/B'; 'U/D'; 'R/L'};
accBuffer = zeros([fsa*secBuffer,numel(accName)]);
eegCounter = 0;
plot1 = true;
conf1 = true;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%% CREATE FILES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
writecsv(eeg_file,eegName);
writecsv(acc_file,accName);
fid = fopen(conf_file ,'a+');
fclose(fid);
tic;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%% START RECORDING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure()
while true
if tcpFlag
try %Catch Matlab error
a = fread(tcpServer, 4); %How large is the package (# bytes)
catch err;
break
end
bytesToRead = double(swapbytes(typecast(uint8(a),'int32')));
try %Catch Matlab error
bytesData = fread(tcpServer,bytesToRead);
catch err;
break
end
else %Utilising Java Classes
for ind = 1:4 %How large is the package (# bytes)
try
a(ind) = DISread(serverDIS,'uint8');
catch e %catch "Java exception occurred"
break
end
end
bytesToRead = double(swapbytes(typecast(uint8(a),'int32')));
bytesData = zeros(bytesToRead,1);
for ind = 1:bytesToRead
try
bytesData(ind) = DISread(serverDIS,'uint8');
catch e; %catch "Java exception occurred"
break
end
end
if ind ~= bytesToRead
break
end
end
[oscPath, oscTag, oscData] = splitOscMessage(bytesData);
data = oscFormat(oscTag,oscData);
switch oscPath
case oscPathV3_4_0{1,1} %The message contains EEG data
eegBuffer = [eegBuffer(2:end, :); cell2mat(data)];
writecsv(eeg_file,cell2mat(data));
eegCounter = eegCounter+1;
case oscPathV3_4_0{2,1} %The message contains Acceleration data
accBuffer = [accBuffer(2:end, :); cell2mat(data)];
writecsv(acc_file,cell2mat(data));
case oscPathV3_4_0{3,1} %The message contains Configuration data
%Show configuration
if conf1
conf = data{1}(2:end-2);
Ctmp = textscan(conf,'%s','delimiter',',');
C = strrep( Ctmp{1}, '"','');
msgbox(C,'Muse Configuration' );
C = strrep( Ctmp{1}, '"','');
fid = fopen(conf_file ,'a+');
cellfun(@(x) fprintf(fid, '%s', [x, sprintf('\r\n')]),C,'UniformOutput', false);
fclose(fid);
conf1 = false;
end
otherwise
%Do nothing
%More cases can be added to treat other paths
end
% End recording if DURATION is positive different to zero
% and DURATION seconds have been recorded
if duration > 0 && toc > duration
break
end
%Plot every 88 EEG samples approx 200ms
if eegCounter == 88
if plot1
subplot(2,1,1);
time = 0:1/fse:secBuffer-1/fse;
h1 = plot(time,eegBuffer);
legend(eegName, 'Location','EastOutside');
xlabel('Time (s)')
ylabel('Voltage (uV)')
subplot(2,1,2);
time = 0:1/fsa:secBuffer-1/fsa;
h2= plot(time,accBuffer);
xlabel('Time (s)')
ylabel('Acceleration (mG)')
legend(h2, accName, 'Location','EastOutside');
plot1 = false;
else
cell1 = (num2cell(eegBuffer,1))';
set(h1,{'ydata'},cell1);
cell2 = (num2cell(accBuffer,1))';
set(h2,{'ydata'},cell2);
end
drawnow;
eegCounter = 0;
end % if eegCounter
end %while true
if tcpFlag
fclose(tcpServer);
delete(tcpServer);
else
serverSocket.close();
serverSSocket.close();
end
display('End of Acquisition');