-
Notifications
You must be signed in to change notification settings - Fork 0
/
BCIalgorithmFromCSP_4task.m
158 lines (142 loc) · 4.98 KB
/
BCIalgorithmFromCSP_4task.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
function accuracy = BCIalgorithmFromCSP_4task(classTrain,eegTrain,classTest,eegTest,ch)
% BCIALGORITHMFROMCSP_4TASK encloses the piece of the FBCSP algorithm after
% the filter-bank for both training and evaluation of the model, hence it
% implements the Common Spatial Pattern (CSP) + the Features Selection
% (MIBIF) + the Classifier (NBPW, naive bayesian parzen window) in their
% multi-class version (4 classes in particular)
%
% INPUT:
% 'classTrain' is the 1D array containing class labels for the training
% data 'eegTrain';
%
% 'eegTrain' is the 3D array containing training EEG signals; its
% dimensions must be #trials x #samples x #bands; if multiple channels
% are available, signals associated to the same trial and different
% channels are consecutive: hence #trials is actually #trials x #ch; the
% labels associated to these signals will be organized accordingly. Note
% that data must be already filtered by the filter bank;
%
% 'classTest' is the 1D array containing class labels for the test data
% 'eegTest'; they should be unknown when using the BCI, but here they are
% used to evaluate the model performance;
%
% 'eegTest' is the 3D array containing test EEG signals; its
% dimensions must be #trials x #samples x #bands; if multiple channels
% are available, signals associated to the same trial and different
% channels are consecutive: hence #trials is actually #trials x #ch; the
% labels associated to these signals will be organized accordingly. Note
% that data must be already filtered by the filter bank;
%
% OUTPUT:
% 'accuracy' is the model performance indicator calculated by comparing
% the known test data labels with the labels guessed by the trained
% model;
%
% 'predicted' is 1D array with predicted labels for the 'eegTest' data;
% if a single trial is furnished as test, this consists of the single
% label associated to the signal to be classified, and the accuracy can
% either be 0% or 100% (wrong or right).
%
% authors: A. Esposito
% correspondence: anthony.esp@live.it
% last update: 2020/09/03
%
% NOTE: future upgrades of this script could comprehend returning the
% parameters identifying the model for classifying new data
%
k_MIBIF = 5;
% TRAINING
class = classTrain;
eeg = eegTrain;
% SPATIAL FILTERING
if (length(ch) == 1)
% No spatial filter in case of a single channel
nb = size(eeg,3);
V = zeros(size(eeg,1),nb);
for i = 1:nb
E = eeg(:,:,i);
C = E*E';
V(:,i) = log(diag(C)/trace(C));
end
V1 = V;
V2 = V;
V3 = V;
V4 = V;
Y = class;
m = 0; % with this value, the MIBIF will understand that there is no spatial filter applied
else
if (length(ch) < 4)
% The Common Spatial Pattern algorithm is applicable
% but 'm' cannot be 2
m = 1;
else
m = 2;
end
[W1, W2, W3, W4] = CSPtrain(eeg, class, ch, m);
[V1, V2, V3, V4, Y] = CSPapply(eeg, class, W1, W2, W3, W4);
end
% FEATURES SELECTION
% Indexes with maximum Mutual-Information and their complementary
I1 = MIBIF(V1,Y,1,m,k_MIBIF);
I2 = MIBIF(V2,Y,2,m,k_MIBIF);
I3 = MIBIF(V3,Y,3,m,k_MIBIF);
I4 = MIBIF(V4,Y,4,m,k_MIBIF);
% Selection
f1 = V1(:,I1);
f2 = V2(:,I2);
f3 = V3(:,I3);
f4 = V4(:,I4);
% CLASSIFIER TRAINING
% Composite vector (Naive Bayesian training)
f = [f1, f2, f3, f4];
cl = Y;
% EVALUATION
class = classTest;
eeg = eegTest;
% SPATIAL FILTERING
if (length(ch) == 1)
% No spatial filter in case of a single channel
nb = size(eeg,3);
V = zeros(size(eeg,1),nb);
for i = 1:nb
E = eeg(:,:,i);
C = E*E';
V(:,i) = log(diag(C)/trace(C));
end
V1 = V;
V2 = V;
V3 = V;
V4 = V;
Y = class;
else
[V1, V2, V3, V4, Y] = CSPapply(eeg, class, W1, W2, W3, W4);
end
% FEATURES SELECTION
% Selection
feval1 = V1(:,I1);
feval2 = V2(:,I2);
feval3 = V3(:,I3);
feval4 = V4(:,I4);
% CLASSIFICATION
% Composite vector
feval = [feval1, feval2, feval3, feval4];
cleval = Y;
% Naive Bayesian Parzen Window classifier
nt = length(cleval);
proba = zeros(nt,1);
index = zeros(nt,1);
ind = index;
for i = 1:nt
pwx1 = NBPW(f1, cl, feval1(i,:), 1);
pwx2 = NBPW(f2, cl, feval2(i,:), 2);
pwx3 = NBPW(f3, cl, feval3(i,:), 3);
pwx4 = NBPW(f4, cl, feval4(i,:), 4);
[proba(i), index(i)] = max([pwx1 pwx2 pwx3 pwx4]);
end
ind(index==1) = 1;
ind(index==2) = 2;
ind(index==3) = 3;
ind(index==4) = 4;
% Classification accuracy
accuracy = (sum(ind == cleval)/nt)*100;
end