Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Treder committed Sep 12, 2012
2 parents fc9df0f + 3c0e414 commit 2896571
Show file tree
Hide file tree
Showing 24 changed files with 649 additions and 103 deletions.
11 changes: 11 additions & 0 deletions classification/applyClassifier.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function out= applyClassifier(fv, model, C, idx)
%out= applyClassifier(fv, classy, C, <idx>)


fv= proc_flaten(fv);

if ~exist('idx','var'),
out= C.applyFcn(C, fv.x);
else
out= C.applyFcn(C, fv.x(:,idx));
end
16 changes: 16 additions & 0 deletions classification/misc_getApplyFunc.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function applyFcn= misc_getApplyFunc(model)
%applyFcn= misc_getApplyFunc(model)


if isstruct(model),
func= misc_getFuncParam(model.classy);
else
func= misc_getFuncParam(model);
end

applyFcnName= ['apply_' func2str(func)];
if exist(applyFcnName, 'file'),
applyFcn= str2func(applyFcnName);
else
applyFcn= @apply_separatingHyperplane;
end
22 changes: 22 additions & 0 deletions classification/trainClassifier.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function [C,params]= trainClassifier(fv, classy, idx)
%C= trainClassifier(fv, classy, <idx>)

fv= proc_flaten(fv);
if exist('idx', 'var'),
fv.x= fv.x(:,idx);
fv.y= fv.y(:,idx);
end

if isstruct(classy),
%% classifier is given as model with free model parameters
model= classy;
classy= select_model(fv, model);
end

[func, params]= misc_getFuncParam(classy);
if isfield(fv,'classifier_param')
C= func(fv.x, fv.y, fv.classifier_param{:}, params{:});
else
C= func(fv.x, fv.y, params{:});
end
C.applyFcn= misc_getApplyFunc(classy);
339 changes: 339 additions & 0 deletions doc/LICENSE.txt

Large diffs are not rendered by default.

70 changes: 70 additions & 0 deletions doc/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
% BBCI Toolbox
% Copyright (c) 2001- 2012 Benjamin Blankertz et al.
%% add other authors?/which ones?
% Berlin Institute of Technology, Neurotechnology Group and Berlin Brain Computer Interface
%% add also Machine Learning group?
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License along
% with this program; if not, write to the Free Software Foundation, Inc.,
% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
%
% Published reports of research using this code (or a modified version, maintaining a significant
% portion of the original code) should cite an article dedicated on this toolbox (to be published soon)
% or the following article:
%
% Blankertz B, Tangermann M, Vidaurre C, Fazli S, Sannelli C, Haufe S, Maeder C, Ramsey LE, Sturm I,
% Curio G, M�ller KR, The Berlin Brain-Computer Interface: Non-Medical Uses of BCI Technology,
% Open Access Front Neuroscience, 4:198, 2010
% http://www.frontiersin.org/neuroprosthetics/10.3389/fnins.2010.00198/abstract
%% As soon as the an article especially devoted to the explanation of the toolbox is published,
%% this remark should be replaced by this other article
%
% Comments and bug reports are welcome. Please email to: [email protected].
% We would also appreciate hearing about how you used this code,
%


Requirements:
- Matlab Version 2007 or later and
- for data acquisition: BrainVision recorder + a BCI system

Download the most recent version of the code from
%% insert download link here
and data from
%% insert data download link here
and the PYFF toolbox from
%% insert PYFF download link here
%% will PYFF be packaged into this toolbox?

open matlab and to the BBCI Toolbox head directory
define the following path variables
> DATA_DIR='YOUR_FULL_DATA_DIR'
% here are the files from the recording are dumped in raw format:
> BBCI_RAW_DIR = 'YOUR_FULL_RAW_DIR'
% here are the files from the recording are dumped in .mat matlab format:
> BBCI_MAT_DIR ='YOUR_FULL_MAT_DIR'

% to intialize the toolbox type:

> startup_bbci_toolbox

% run the (off-line) demos in the 'demos' directory
> demo_analysis_ERD
% ERP Analysis:
> demo_analysis_ERPs
> demo_analysis_Spectra

%% the following examples to be included:
%% https://wiki.ml.tu-berlin.de/wiki/IDA/BerlinBCI/ToolBox/ToolboxPracticalExamples


2 changes: 1 addition & 1 deletion markers/mrk_defineClasses.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
misc_checkType(classDef, 'CELL');

nClasses= size(classDef,2);
mrk= struct('time', mk.time);
mrk= struct('time', mk.time, 'desc',mk.desc);
mrk.y= zeros(nClasses, numel(mrk.time));
for cc= 1:nClasses,
if isnumeric(classDef{1,cc}),
Expand Down
3 changes: 3 additions & 0 deletions markers/mrk_selectEvents.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
end

mrk.time= mrk.time(ev);
if isfield(mrk, 'desc'),
mrk.desc= mrk.desc(:,ev);
end
if isfield(mrk, 'y'),
mrk.y= mrk.y(:,ev);
end
Expand Down
6 changes: 4 additions & 2 deletions misc/misc_checkType.m
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,10 @@
if isempty(variable) || ...
( iscell(variable) && ndims(variable)==2 && ...
(size(variable,2)==2 || size(variable,2)==3) ),
ok= all(cellfun(@ischar, variable(:,1))) && ...
all(cellfun(@ischar, variable(:,3)));
ok= all(cellfun(@ischar, variable(:,1)));
if size(variable,2)==3,
ok= ok && all(cellfun(@ischar, variable(:,3)));
end
else
ok= 0;
end
Expand Down
14 changes: 8 additions & 6 deletions online/acquisition/bbci_acquire_offline.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,20 @@
};
state= opt_setDefaults(state, props, 1);
[cnt, mrk]= varargin{2:3};
if cnt.fs~=mrk.fs,
error('sampling rates in CNT and MRK not consistent');
end
state.lag= 1;
state.orig_fs= cnt.fs;
state.fs= cnt.fs;
state.cnt_step= round(state.blocksize/1000*cnt.fs);
state.cnt_idx= 1:state.cnt_step;
state.clab= cnt.clab;
state.cnt= cnt;
% -- transitional
if ~isfield(mrk, 'desc'),
mrk.desc= mrk.toe;
end
% --
state.mrk= mrk;
state.mrk.time= mrk.time;
state.start_time= tic;
output= {state};
elseif length(varargin)~=1,
Expand All @@ -91,9 +91,11 @@
return;
end
cntx= state.cnt.x(state.cnt_idx, :);
mrk_idx= find(state.mrk.pos>=state.cnt_idx(1) & ...
state.mrk.pos<=state.cnt_idx(end));
mrkTime= (state.mrk.pos(mrk_idx)-state.cnt_idx(1)+1)/state.fs*1000;
si= 1000/state.fs;
TIMEEPS= si/100;
mrk_idx= find(state.mrk.time-TIMEEPS > (state.cnt_idx(1)-1)*si & ...
state.mrk.time-TIMEEPS <= state.cnt_idx(end)*si);
mrkTime= state.mrk.time(mrk_idx) - (state.cnt_idx(1)-1)*si;
mrkDesc= state.mrk.desc(mrk_idx);
state.cnt_idx= state.cnt_idx + state.cnt_step;
output= {cntx, mrkTime, mrkDesc, state};
Expand Down
23 changes: 14 additions & 9 deletions online/apply_functions/bbci_apply_getSegment.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,22 @@


% Determine the indices in the ring buffer that correspond to the specified
% time interval. There are some rounding-issues here for some sampling rates.
% The following procedure should do well.
len= floor(diff(ival)/1000*signal.fs);
idx0= signal.ptr + (reference_time-signal.time)*signal.fs/1000;
idx_ival= round(idx0 + ival(1)/1000*signal.fs) + [0:len];
idx= 1 + mod(idx_ival-1, signal.size);
% time interval. There are rounding-issues here for some sampling rates.
% The following procedure should do ok.
si= 1000/signal.fs;
TIMEEPS= si/100;
len_sa= round(diff(ival)/si);
pos_zero= signal.ptr + ceil( (reference_time-signal.time-TIMEEPS)/si );
core_ival= [ceil(ival(1)/si) floor(ival(2)/si)];
addone= diff(core_ival)+1 < len_sa;
pos_end= pos_zero + floor(ival(2)/si) + addone;
idx= [-len_sa+1:0] + pos_end;
idx_ring= 1 + mod(idx-1, signal.size);

% Get requested segment from the ring buffer and store it into an EPO struct
epo.x= signal.x(idx,:);
epo.x= signal.x(idx_ring,:);
epo.clab= signal.clab;
timeival= (idx_ival([1 end])-idx0)*1000/signal.fs;
timeival= si*(core_ival + [1 addone]);
timeival= round(10000*timeival)/10000;
epo.t= linspace(timeival(1), timeival(2), length(idx));
epo.t= linspace(timeival(1), timeival(2), len_sa);
epo.fs= signal.fs;
2 changes: 2 additions & 0 deletions online/apply_functions/bbci_apply_initData.m
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,5 @@
[bbci, data]= bbci_apply_adaptation(bbci, data, 'init');

bbci.memo.tcstate= bbci_typechecking('off');
global BBCI_HISTORY
BBCI_HISTORY= 0;
3 changes: 2 additions & 1 deletion online/apply_functions/bbci_apply_setDefaults.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
end


props= {'source' struct 'STRUCT'
props= {'calibrate' struct 'STRUCT'
'source' struct 'STRUCT'
'marker' struct 'STRUCT'
'signal' struct 'STRUCT'
'feature' struct 'STRUCT'
Expand Down
16 changes: 8 additions & 8 deletions online/bbci_calibrate.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@
bbci_log_write(data, '#Calibration files from folder <%s>:', BC.folder);
file_counter = 1;
for k= 1:length(data.fileinfo),
for f = 1:length(data.fileinfo{k}),
msg= sprintf('File %d: %s <%s>, size %d', file_counter, data.fileinfo{k}(f).name, ...
data.fileinfo{k}(f).date, data.fileinfo{k}(f).bytes);
bbci_log_write(data, ['#' msg]);
file_counter = file_counter + 1;
end
for f = 1:length(data.fileinfo{k}),
msg= sprintf('File %d: %s <%s>, size %d', file_counter, data.fileinfo{k}(f).name, ...
data.fileinfo{k}(f).date, data.fileinfo{k}(f).bytes);
bbci_log_write(data, ['#' msg]);
file_counter = file_counter + 1;
end
end
bbci_log_write(data, '\n#Settings of BBCI:');
bbci_prettyPrint(data.log.fid, copy_fields(bbci, 'calibrate'));
bbci_prettyPrint(data.log.fid, struct_copyFields(bbci, 'calibrate'));

% Store original values for recovery via bbci_calibrate_reset
if ~isfield(bbci, 'default_settings'),
if isempty(bbci.calibrate.default_settings),
bbci.calibrate.default_settings= bbci.calibrate.settings;
end

Expand Down
13 changes: 7 additions & 6 deletions online/bbci_load.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
function data = bbci_load(bbci)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
function data= bbci_load(bbci)
%BBCI_LOAD - Load data set
%
%Synopsis:
% DATA= bbci_load(BBCI);
%


bbci= bbci_calibrate_setDefaults(bbci);
BC= bbci.calibrate;
Expand Down Expand Up @@ -28,6 +32,3 @@
data.mrk= BC.marker_fcn(data.mrk, BC.marker_param{:});
end
data.isnew= 1;

end

2 changes: 1 addition & 1 deletion online/calibrate_functions/bbci_calibrate_evalFeature.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


bbci_feature= bbciutil_transformProc2FcnParam(bbci_feature);
bbci_feature= set_defaults(bbci_feature, 'signal', 1);
bbci_feature= opt_setDefaults(bbci_feature, {'signal', 1});

feature= repmat(struct, [1 length(bbci_feature)]);
for f= 1:length(bbci_feature),
Expand Down
49 changes: 25 additions & 24 deletions online/calibrate_functions/bbci_calibrate_setDefaults.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,23 @@
bbci= [];
end

bbci= set_defaults(bbci, ...
'calibrate', struct);

bbci.calibrate= set_defaults(bbci.calibrate, ...
'folder', TODAY_DIR, ...
'file', '', ...
'read_fcn', @file_readBV, ...
'read_param', {}, ...
'marker_fcn', [], ...
'marker_param', {}, ...
'montage_fcn', @getElectrodePositions, ...
'montage_param', {}, ...
'fcn', [], ...
'settings', struct, ...
'log', struct, ...
'save', struct);
bbci= opt_setDefaults(bbci, {'calibrate' struct 'STRUCT'});

props= {'folder' TODAY_DIR 'CHAR'
'file' '' 'CHAR'
'read_fcn' @file_readBV 'FUNC'
'read_param' {} 'CELL'
'marker_fcn' [] 'FUNC'
'marker_param' {} 'CELL'
'montage_fcn' @mnt_setElectrodePositions 'FUNC'
'montage_param' {} 'CELL'
'fcn' [] 'FUNC'
'settings' struct 'STRUCT'
'default_settings' struct 'STRUCT'
'log' struct 'STRUCT'
'save' struct 'STRUCT'
};
bbci.calibrate= opt_setDefaults('bbci.calibrate', props);

default_save_file= 'bbci_classifier';
default_log_file= 'bbci_calibrate_log';
Expand All @@ -49,11 +50,11 @@
end
bbci= bbci_save_setDefaults(bbci, ...
'folder', bbci.calibrate.folder, ...
'file', default_save_file);

bbci.calibrate.log= ...
set_defaults(bbci.calibrate.log, ...
'output', 'screen&file', ...
'folder', bbci.calibrate.folder, ...
'file', default_log_file, ...
'force_overwriting', 0);
'file', default_save_file);

props= {'output' 'screen&file' 'BOOL|CHAR(screen file screen&file)'
'folder' bbci.calibrate.folder 'CHAR'
'file' default_log_file 'CHAR'
'force_overwriting' 0 '!BOOL'
};
bbci.calibrate.log= opt_setDefaults('bbci.calibrate.log', props);
Loading

0 comments on commit 2896571

Please sign in to comment.