-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheeg_preprocessing.m
160 lines (124 loc) · 6.2 KB
/
eeg_preprocessing.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
%% EEG preprocessing
% Append words, filter, remove bad channels, remove bad ICA components,
% interpolation of bad channels.
clear all
clc
%%
for group = ["S", "P"] % S = non-autistic; P = autistic
for sub = [1:10] %Subject
dir = strcat('dados/',group,num2str(sub),'/online_data/');
while ~isfile(strcat(dir,'data_preprocessed/filtered_channeLoc.set'))
%%%%%%%%%%%%%%%%%%%% loading data and append words
eeg = [];
for j = 1:22 % word number
if exist(strcat(dir,'eeg_data/eeg_word',num2str(j),'.mat'))
disp(j)
word = pop_importdata('dataformat','matlab','data',...
strcat(dir,'eeg_data/eeg_word',num2str(j),'.mat'), 'srate',256);
ev = load(strcat(dir,'events/eeg_events_word',num2str(j),'.mat'));
word = pop_importevent(word, 'event', ev.events, 'fields', ...
{'type', 'latency'}, 'timeunit', NaN);
%Clean datasets
word = pop_select(word, 'time', ...
[double(word.event(1).latency/256-5) double(word.event(end).latency/256)]);
if ~(sub == 7 && group == "P")
if j==1
eeg = word;
else
eeg = pop_mergeset(eeg, word);
end
else
if j==2
eeg = word;
else
eeg = pop_mergeset(eeg, word);
end
end
end
end
%Save dataset in file
if ~exist(strcat(dir,'data_preprocessed/'))
mkdir(strcat(dir,'data_preprocessed/'))
end
eeg = pop_saveset(eeg, 'filename', 'merged_words', 'filepath', ...
char(strcat(dir,'data_preprocessed/')));
%%%%%%%%%%%%%%%%%%%% filtering data and adding channel locations
%Zero phase-shift filter: notch (47.5-52.5Hz) + band-pass (0.5-45Hz)
eeg = pop_eegfiltnew(eeg, 'locutoff', 47.5, 'hicutoff',52.5, 'revfilt', 1);
eeg = pop_eegfiltnew(eeg, 'locutoff', 0.5);
eeg = pop_eegfiltnew(eeg, 'hicutoff', 45);
%Add channel locations
loc_dir = 'channel_locations.ced';
eeg.chanlocs = readlocs(loc_dir);
orig_locs = eeg.chanlocs;
save('orig_locs.mat', 'orig_locs');
%Save dataset in file
eeg = pop_saveset(eeg, 'filename', 'filtered_channeLoc', 'filepath', ...
char(strcat(dir,'data_preprocessed/')));
end
%% %%%%%%%%%%%%%%%%%%%% Removing bad channels
while ~isfile(strcat(dir,'data_preprocessed/without_badChannels.set'))
eeg = pop_loadset('filename', 'filtered_channeLoc.set', 'filepath', ...
char(strcat(dir,'data_preprocessed/')));
% Bad channels
pop_eegplot(eeg, 1, 0, 0)
bad_channels = input("Which channels do you want to mark as BAD?" + ...
"\n0=None\n1=FPZ\n2=FZ\n3=FC1\n4=FCZ\n5=FC2\n6=C3\n7=CZ\n8=C4\n9=CPZ" + ...
"\n10=P3\n11=PZ\n12=P4\n13=P07\n14=POZ\n15=PO8\n16=OZ");
bad_channels = 0;
fprintf("bad_channels: ");
disp(bad_channels);
save(strcat(dir,'data_preprocessed/bad_channels.mat'), 'bad_channels');
%Remove bad channels
if bad_channels ~= 0
eeg = pop_select(eeg, 'nochannel', bad_channels);
end
%Save dataset in file
eeg = pop_saveset(eeg, 'filename', 'without_badChannels', 'filepath', ...
char(strcat(dir,'data_preprocessed/')));
end
%% %%%%%%%%%%%%%%%%%%%% Running ICA
while ~isfile(strcat(dir,'data_preprocessed/ICA.set'))
eeg = pop_loadset('filename', 'without_badChannels.set', 'filepath', ...
char(strcat(dir,'data_preprocessed/')));
load(strcat(dir,'data_preprocessed/bad_channels.mat'));
%ICA
eeg = pop_runica(eeg, 'chanind', [2:eeg.nbchan], 'icatype', 'runica', ...
'extended', 1);
%Save dataset in file
eeg = pop_saveset(eeg, 'filename', 'ICA', 'filepath', ...
char(strcat(dir,'data_preprocessed/')));
end
%% Remove bad ICA components (to remove artifacts/blinks)
while ~isfile(strcat(dir,'data_preprocessed/ICA_clean.set'))
eeg = pop_loadset('filename', 'ICA.set', 'filepath', ...
char(strcat(dir,'data_preprocessed/')));
%Reject bad ICA components
eeg = pop_selectcomps(eeg, [1:15]); %[1:16]
pop_eegplot(eeg, 0, 0, 0);
pop_eegplot(eeg, 1, 0, 0)
comp_rej = input("Which components do you want to reject?" +...
" If none, enter 0");
if comp_rej ~= 0
eeg = pop_subcomp(eeg, comp_rej, 1, [0]);
end
%Save dataset in file
eeg = pop_saveset(eeg, 'filename', 'ICA_clean', 'filepath', ...
char(strcat(dir,'data_preprocessed/')));
end
%% %%%%%%%%%%%%%%%%%%%% Interpolate bad channels
while ~isfile(strcat(dir,'data_preprocessed/interp.set'))
eeg = pop_loadset('filename', 'ICA_clean.set', 'filepath', ...
char(strcat(dir,'data_preprocessed/')));
load(strcat(dir, 'data_preprocessed/bad_channels.mat'));
load("orig_locs.mat");
%Interpolation of previously removed channels
if bad_channels ~= 0
eeg = interpol(eeg, orig_locs, 'spherical');
end
%Save dataset in file
eeg = pop_saveset(eeg, 'filename', 'interp', 'filepath', ...
char(strcat(dir,'data_preprocessed/')));
end
end
end