a b/getData.m
1
% An example to get the BCI competition IV datasets 2a, 2b is the same
2
% Data from: http://www.bbci.de/competition/iv/
3
% using open-source toolbox Biosig on MATLAB: http://biosig.sourceforge.net/
4
% Just an example, you should change as you need.
5
6
function data = getData(subject_index)
7
8
subject_index = 6; % 1-9
9
10
%% T data
11
session_type = 'T'; % T and E
12
dir_1 = ['D:\MI\BCICIV_2a_gdf\A0',num2str(subject_index),session_type,'.gdf']; % set your path of the downloaded data
13
[s, HDR] = sload(dir_1);
14
15
% Label 
16
% label = HDR.Classlabel;
17
labeldir_1 = ['D:\MI\true_labels\A0',num2str(subject_index),session_type,'.mat'];
18
load(labeldir_1);
19
label_1 = classlabel;
20
21
% construct sample - data Section 1000*22*288
22
Pos = HDR.EVENT.POS; % use POS to get trials
23
% Dur = HDR.EVENT.DUR;
24
Typ = HDR.EVENT.TYP;
25
26
k = 0;
27
data_1 = zeros(1000,22,288);
28
for j = 1:length(Typ)
29
    if  Typ(j) == 768
30
        k = k+1;
31
        data_1(:,:,k) = s((Pos(j)+500):(Pos(j)+1499),1:22);
32
    end
33
end
34
35
% wipe off NaN
36
data_1(isnan(data_1)) = 0;
37
38
39
% E data
40
session_type = 'E';
41
dir_2 = ['D:\Lab\MI\BCICIV_2a_gdf\A0',num2str(subject_index),session_type,'.gdf'];
42
% dir = 'D:\Lab\MI\BCICIV_2a_gdf\A01E.gdf';
43
[s, HDR] = sload(dir_2);
44
45
% Label 
46
% label = HDR.Classlabel;
47
labeldir_2 = ['D:\Lab\MI\true_labels\A0',num2str(subject_index),session_type,'.mat'];
48
load(labeldir_2);
49
label_2 = classlabel;
50
51
% construct sample - data Section 1000*22*288
52
Pos = HDR.EVENT.POS;
53
% Dur = HDR.EVENT.DUR;
54
Typ = HDR.EVENT.TYP;
55
56
k = 0;
57
data_2 = zeros(1000,22,288);
58
for j = 1:length(Typ)
59
    if  Typ(j) == 768
60
        k = k+1;
61
        data_2(:,:,k) = s((Pos(j)+500):(Pos(j)+1499),1:22);
62
    end
63
end
64
65
% wipe off NaN
66
data_2(isnan(data_2)) = 0;
67
68
%% preprocessing
69
% option - band-pass filter
70
fc = 250; % sampling rate
71
Wl = 4; Wh = 40; % pass band
72
Wn = [Wl*2 Wh*2]/fc;
73
[b,a]=cheby2(6,60,Wn);
74
75
% a better filter for 4-40 Hz band-pass
76
% fc = 250;
77
% Wl = 4; Wh = 40; 
78
% Wn = [Wl*2 Wh*2]/fc;
79
% [b,a]=cheby2(8,20,Wn);
80
81
for j = 1:288
82
    data_1(:,:,j) = filtfilt(b,a,data_1(:,:,j));
83
    data_2(:,:,j) = filtfilt(b,a,data_2(:,:,j));
84
end
85
86
% option - a simple standardization
87
%{
88
eeg_mean = mean(data,3);
89
eeg_std = std(data,1,3); 
90
fb_data = (data-eeg_mean)./eeg_std;
91
%}
92
93
%% Save the data to a mat file 
94
data = data_1;
95
label = label_1;
96
% label = t_label + 1;
97
saveDir = ['D:\MI\standard_2a_data\A0',num2str(subject_index),'T.mat'];
98
save(saveDir,'data','label');
99
100
data = data_2;
101
label = label_2;
102
saveDir = ['D:\MI\standard_2a_data\A0',num2str(subject_index),'E.mat'];
103
save(saveDir,'data','label');
104
105
end
106
107
108