-
Notifications
You must be signed in to change notification settings - Fork 29
/
getData.m
108 lines (85 loc) · 2.47 KB
/
getData.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
% An example to get the BCI competition IV datasets 2a, 2b is the same
% Data from: http://www.bbci.de/competition/iv/
% using open-source toolbox Biosig on MATLAB: http://biosig.sourceforge.net/
% Just an example, you should change as you need.
function data = getData(subject_index)
subject_index = 6; % 1-9
%% T data
session_type = 'T'; % T and E
dir_1 = ['D:\MI\BCICIV_2a_gdf\A0',num2str(subject_index),session_type,'.gdf']; % set your path of the downloaded data
[s, HDR] = sload(dir_1);
% Label
% label = HDR.Classlabel;
labeldir_1 = ['D:\MI\true_labels\A0',num2str(subject_index),session_type,'.mat'];
load(labeldir_1);
label_1 = classlabel;
% construct sample - data Section 1000*22*288
Pos = HDR.EVENT.POS; % use POS to get trials
% Dur = HDR.EVENT.DUR;
Typ = HDR.EVENT.TYP;
k = 0;
data_1 = zeros(1000,22,288);
for j = 1:length(Typ)
if Typ(j) == 768
k = k+1;
data_1(:,:,k) = s((Pos(j)+500):(Pos(j)+1499),1:22);
end
end
% wipe off NaN
data_1(isnan(data_1)) = 0;
% E data
session_type = 'E';
dir_2 = ['D:\Lab\MI\BCICIV_2a_gdf\A0',num2str(subject_index),session_type,'.gdf'];
% dir = 'D:\Lab\MI\BCICIV_2a_gdf\A01E.gdf';
[s, HDR] = sload(dir_2);
% Label
% label = HDR.Classlabel;
labeldir_2 = ['D:\Lab\MI\true_labels\A0',num2str(subject_index),session_type,'.mat'];
load(labeldir_2);
label_2 = classlabel;
% construct sample - data Section 1000*22*288
Pos = HDR.EVENT.POS;
% Dur = HDR.EVENT.DUR;
Typ = HDR.EVENT.TYP;
k = 0;
data_2 = zeros(1000,22,288);
for j = 1:length(Typ)
if Typ(j) == 768
k = k+1;
data_2(:,:,k) = s((Pos(j)+500):(Pos(j)+1499),1:22);
end
end
% wipe off NaN
data_2(isnan(data_2)) = 0;
%% preprocessing
% option - band-pass filter
fc = 250; % sampling rate
Wl = 4; Wh = 40; % pass band
Wn = [Wl*2 Wh*2]/fc;
[b,a]=cheby2(6,60,Wn);
% a better filter for 4-40 Hz band-pass
% fc = 250;
% Wl = 4; Wh = 40;
% Wn = [Wl*2 Wh*2]/fc;
% [b,a]=cheby2(8,20,Wn);
for j = 1:288
data_1(:,:,j) = filtfilt(b,a,data_1(:,:,j));
data_2(:,:,j) = filtfilt(b,a,data_2(:,:,j));
end
% option - a simple standardization
%{
eeg_mean = mean(data,3);
eeg_std = std(data,1,3);
fb_data = (data-eeg_mean)./eeg_std;
%}
%% Save the data to a mat file
data = data_1;
label = label_1;
% label = t_label + 1;
saveDir = ['D:\MI\standard_2a_data\A0',num2str(subject_index),'T.mat'];
save(saveDir,'data','label');
data = data_2;
label = label_2;
saveDir = ['D:\MI\standard_2a_data\A0',num2str(subject_index),'E.mat'];
save(saveDir,'data','label');
end