/
BrainScanner.m
500 lines (383 loc) · 15.9 KB
/
BrainScanner.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
% Brain Scanner Application
function varargout = BrainScanner(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @main_OpeningFcn, ...
'gui_OutputFcn', @main_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before main is made visible.
function main_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to main (see VARARGIN)
% Choose default command line output for main
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
%% Load libraries
%if ~exist('MARDv2','file')
% addpath(genpath('functions')); end
addpath(genpath('functions'))
if ~exist('rescaling', 'file');
addpath('libraries/BASS_v01112012'); end
if ~exist('lsl_loadlib','file')
addpath(genpath('libraries/liblsl-Matlab')); end
if ~exist('asr_process','file')
addpath(genpath('libraries/BCILAB')); end
if ~exist('mtspecgramc','file')
addpath(genpath('libraries/chronux_2_11')); end
try
lib = lsl_loadlib(env_translatepath('libraries/liblsl-Matlab:/bin'));
catch
lib = lsl_loadlib();
end
%% Setup stream and settings
opts.eegStreamName = 'openvibeSignal'; %'EEG';
opts.bufferrange = 10; % (in seconds)
opts.channelrange = 1:24;
opts.samplingRate = 250;
opts.blockSize = 32;
opts.refreshrate = 1/(2*opts.blockSize/opts.samplingRate); % Maybe change this!
% Pre processing for viewer
[opts.filterB, opts.filterA] = butter(4,[1 45]/(opts.samplingRate/2));
opts.artefactRemoval = 0;
opts.filter = 0;
opts.reref = 0;
opts.bad_chans=[];%
% Options for experiment
[opts_exp.filterB, opts_exp.filterA] = butter(4,[5 15]/(opts.samplingRate/2));
opts_exp.artefactRemoval = 0;
opts_exp.filter = 0;
opts_exp.reref = 0;
opts_exp.bad_chans=opts.bad_chans;% e.g. [5 21];
opts.experiment = opts_exp;
% artifact removal
opts.artefactRemoval = 0;
% plotting
opts.numSamplesToPlot = 1000;
opts.rangeChannelPlot = 100;
% Model setup
basis = load('model/basis_functions_24ch.mat','Qg');
basisFunctions = basis.Qg; % 569 basis functions uni-lateral
opts.basisFunctions = basisFunctions;
easyCapModelFull = load('model/Gain_mbraintrain_24ch.mat','Gain');
easyCapModelFull=easyCapModelFull.Gain;
easyCapModelFull(opts.bad_chans,:)=[];
no_chan=size(easyCapModelFull,1);
easyCapModelFull=(eye(no_chan)-1/no_chan)*easyCapModelFull; % set gain to have average reference
opts.forwardModel = easyCapModelFull*basisFunctions';
opts.numSources = size(basisFunctions,1);
channels=load('model/Gain_mbraintrain_24ch.mat','Channel');
opts.channames={channels.Channel(:).Name};clear channels
vertface = load('model/vertface_24ch_ICBMtemp');
opts.faces = vertface.face;
opts.verts = vertface.vert;
opts.numChannels = 24-numel(opts.bad_chans);
% prediction
opts.print_predicted_label = 1;
%opts.QG = basis.QG;
% % Old Model setup
% basis = load('model/basisFunctions.mat');
% basisFunctions = basis.IDX2; % 776 basis functions uni-lateral
% opts.basisFunctions = basisFunctions;
%
% easyCapModelFull = importdata('model/easyCapModel.mat');
% opts.forwardModel = easyCapModelFull(:, basisFunctions);
% opts.numSources = numel(basisFunctions);
%
% vertface = load('model/vertface');
% opts.faces = vertface.face2;
% opts.verts = vertface.vert2;
% opts.QG = basis.QG;
% basisFunctions = sort(randperm(size(easyCapModelFull,2), numSources));
% forwardModel = easyCapModelFull(:, basisFunctions);
% opts.numSources = 400;
% Other
opts.recoveryMethod = 'teVG';%'MARD';
if strcmp(opts.recoveryMethod,'teVG');
try
load('gamma.mat', 'gmedian');
opts.gamma = nanmedian(gmedian);
fprintf('Loaded gamma value = %3.2f from file gamma.mat\n', opts.gamma);
catch
opts.gamma=-75;
fprintf('File gamma.mat not found. Using default value for gamma = %3.2f.\n', opts.gamma);
end;
end
opts.saveData = 0;
opts.log = 0;
opts.verbose = 1;
opts.trainVG = 0;
opts.numSamplesCalibrationVGData=4*opts.blockSize;
handles.eeg = EEGStream(lib, opts, handles);
initialize_gui(hObject, handles, false);
% UIWAIT makes main wait for user response (see UIRESUME)
% uiwait(handles.figure1);
%% Stimuli experiment settings
function runExperiment(handles)
handles.eeg.options.saveData = 1;
handles.eeg.options.log = 1;
% --- Outputs from this function are returned to the command line.
function varargout = main_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --------------------------------------------------------------------
function initialize_gui(fig_handle, handles, isreset)
% If the metricdata field is present and the reset flag is false, it means
% we are we are just re-initializing a GUI by calling it from the cmd line
% while it is up. So, bail out as we dont want to reset the data.
% Update handles structure
guidata(handles.figure1, handles);
% --- Executes on button press in togglebutton1.
function togglebutton1_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if hObject.Value
if handles.eeg.isConnected
handles.text13.String = 'Live';
else
handles.text13.String = 'Replay';
end
handles.pushbutton5.Enable = 'off';
handles.togglebutton6.Enable = 'off';
handles.eeg.start();
else
handles.text13.String = 'Off';
handles.eeg.stop();
handles.togglebutton10.Value = 0;
handles.togglebutton11.Value = 0;
handles.togglebutton14.Value = 0;
handles.pushbutton5.Enable = 'on';
handles.togglebutton6.Enable = 'on';
end
% --- Executes on button press in togglebutton2.
function togglebutton2_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if hObject.Value
handles.text14.String = 'On';
else
handles.text14.String = 'Off';
end
handles.eeg.options.filter = hObject.Value;
% --- Executes on button press in togglebutton3.
function togglebutton3_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if hObject.Value
handles.text15.String = 'On';
else
handles.text15.String = 'Off';
end
handles.eeg.options.reref = hObject.Value;
% --- Executes on button press in togglebutton4.
function togglebutton4_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if hObject.Value
handles.text16.String = 'On';
else
handles.text16.String = 'Off';
end
handles.eeg.options.zeromean = hObject.Value;
% --- Executes on button press in togglebutton6.
function togglebutton6_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if hObject.Value
if handles.eeg.connect()
handles.text17.String = 'Connected';
handles.togglebutton1.Enable = 'on';
handles.text23.String = 'No file';
handles.togglebutton18.Enable = 'off';
else
hObject.Value = 0;
handles.togglebutton18.Enable = 'on';
end
else
if handles.eeg.isConnected
handles.eeg.disconnect(); end;
handles.togglebutton1.Enable = 'off';
handles.text17.String = 'Off';
handles.togglebutton18.Enable = 'on';
end
% --- Executes on button press in togglebutton14.
function togglebutton14_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton14 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if ~hObject.Value
handles.eeg.closeFigure(handles.eeg.TimeFigure); end;
handles.eeg.showTiming = hObject.Value;
% --- Executes on button press in togglebutton10.
function togglebutton10_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton10 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if ~hObject.Value
handles.eeg.closeFigure(handles.eeg.DataFigure); end;
handles.eeg.showData = hObject.Value;
% --- Executes on button press in togglebutton11.
function togglebutton11_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if ~hObject.Value
handles.eeg.closeFigure(handles.eeg.BrainFigure); end;
handles.eeg.showBrain = hObject.Value;
% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.eeg.stop();
delete(hObject);
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[fileName, path] = uigetfile('*.csv','Select the saved EEG data file (.csv)');
if fileName
handles.text23.String = 'File loaded';
handles.togglebutton1.Enable = 'on';
handles.eeg.replayFileName = strcat(path,fileName);
handles.eeg.replayDataFile=[];
else
disp('No file selected');
end
% --- Executes on button press in togglebutton16.
function togglebutton16_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton16 (see GCBO)
if hObject.Value
handles.text24.String = 'On';
handles.eeg.setup();
else
handles.text24.String = 'Off';
end
handles.eeg.options.saveData = hObject.Value;
handles.eeg.options.log = hObject.Value;
% --- Executes on button press in togglebutton18.
function togglebutton18_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton18 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.eeg.options.trainVG = hObject.Value;
% Hint: get(hObject,'Value') returns toggle state of togglebutton18
% --- Executes on button press in togglebutton19.
function togglebutton19_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton19 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if hObject.Value
handles.text27.String = 'On';
%[options.filterB, options.filterA] = butter(4,[5 15]/(handles.eeg.options.samplingRate/2));
%options.samplingRate=handles.eeg.options.samplingRate;
handles.eeg.ClassificationModel=trainModel(handles.eeg.options);
else
handles.text27.String = 'Off';
end
handles.eeg.options.trainModel = hObject.Value;
% --- Executes on button press in apply_asr_button.
function apply_asr_button_Callback(hObject, eventdata, handles)
% hObject handle to apply_asr_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of apply_asr_button
if hObject.Value
handles.text_asr.String = 'On';
else
handles.text_asr.String = 'Off';
end
handles.eeg.options.artefactRemoval = hObject.Value;
% --- Executes on button press in load_asr_button.
function load_asr_button_Callback(hObject, eventdata, handles)
% hObject handle to load_asr_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of load_asr_button
fprintf('Choose calibration data file:\n');
[filename_data, PathName] = uigetfile('*.csv');
fprintf('Loading data file %s\n',filename_data)
calib_data = csvread([PathName,filename_data], 1, 0);%% columns 0
calib_data = calib_data(:, 1:24);
handles.eeg.asr_state = asr_calibrate(preprocess(calib_data', handles.eeg.options), handles.eeg.options.samplingRate);
handles.text_viewer_asr_loaded.String = 'Loaded';
% --- Executes on button press in button_filter_exp.
function button_filter_exp_Callback(hObject, eventdata, handles)
% hObject handle to button_filter_exp (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of button_filter_exp
if hObject.Value
handles.text30.String = 'On';
else
handles.text30.String = 'Off';
end
handles.eeg.options.experiment.filter = hObject.Value;
% --- Executes on button press in button_reref_exp.
function button_reref_exp_Callback(hObject, eventdata, handles)
% hObject handle to button_reref_exp (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of button_reref_exp
if hObject.Value
handles.text31.String = 'On';
else
handles.text31.String = 'Off';
end
handles.eeg.options.experiment.reref = hObject.Value;
% --- Executes on button press in togglebutton25.
function togglebutton25_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton25 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of togglebutton25
% --- Executes on button press in button_asr_exp.
function button_asr_exp_Callback(hObject, eventdata, handles)
% hObject handle to button_asr_exp (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of button_asr_exp
if hObject.Value
handles.text33.String = 'On';
else
handles.text33.String = 'Off';
end
handles.eeg.options.experiment.artefactRemoval = hObject.Value;
% --- Executes on button press in button_load_asr_exp.
function button_load_asr_exp_Callback(hObject, eventdata, handles)
% hObject handle to button_load_asr_exp (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of button_load_asr_exp
fprintf('Choose calibration data file:\n');
[filename_data, PathName] = uigetfile('*.csv');
fprintf('Loading data file %s\n',filename_data)
calib_data = csvread([PathName,filename_data], 1, 0);%% columns 0
calib_data = calib_data(:, 1:24);
handles.eeg.options.experiment.asr_state = asr_calibrate(preprocess(calib_data', handles.eeg.options), handles.eeg.options.samplingRate);
handles.text35.String = 'Loaded';