-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathresteeg.m
234 lines (174 loc) · 9.26 KB
/
resteeg.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
function CONFIG = resteeg(CONFIG)
%% ------------------------------------------------------------------------
% Import Data and Obtain Dataset Info
% -------------------------------------------------------------------------
% load EEG dataset if already exists, otherwise convert data to EEGLAB (.set) format
if exist([CONFIG.filepath filesep CONFIG.filename '_import.set'],'file') && ~CONFIG.FORCE_RUN_IMPORT
EEG = pop_loadset([CONFIG.filepath filesep CONFIG.filename '_import.set']);
tmp = load([CONFIG.report.directory filesep 'config_import.mat']);
CONFIG.rawinfo = tmp.config_import;
else
[EEG, CONFIG] = prep_import(CONFIG);
[EEG, CONFIG] = save_data(EEG,CONFIG,[CONFIG.filename '_import'],0);
end
%% ------------------------------------------------------------------------
% Preprocessing Pipeline
% -------------------------------------------------------------------------
% load preprocessed EEG data if already exists, otherwise apply preprocessing pipeline
if exist([CONFIG.filepath filesep CONFIG.filename '_prep.set'],'file') && ~CONFIG.FORCE_RUN_PREPROC
EEG = pop_loadset([CONFIG.filepath filesep CONFIG.filename '_prep.set']);
tmp = load([CONFIG.report.directory filesep 'config_prep.mat']);
CONFIG.prep = tmp.config_prep;
else
[EEG, CONFIG] = prep_proc(EEG,CONFIG);
[EEG, CONFIG] = save_data(EEG,CONFIG,[CONFIG.filename '_prep'],1);
end
%% ------------------------------------------------------------------------
% Power-related Measures
% -------------------------------------------------------------------------
% power spectral density
[EEG, CONFIG] = power_analysis(EEG,CONFIG);
% Short-time Fourier Transform (STFT)
[EEG, CONFIG] = time_freq_analysis(EEG,CONFIG);
%% ------------------------------------------------------------------------
% Statistics of Signals
% -------------------------------------------------------------------------
% report channel statistics
%% ------------------------------------------------------------------------
% Reported Clinical Biomarkers
% -------------------------------------------------------------------------
%% ------------------------------------------------------------------------
% Connectivity and Coherence Analysis
% -------------------------------------------------------------------------
% coherence between channel pairs
[EEG, CONFIG] = coherence_analysis(EEG,CONFIG);
% power-amplitude coupling
% power-phase coupling
%% ------------------------------------------------------------------------
% Entropy Measures
% -------------------------------------------------------------------------
%% ------------------------------------------------------------------------
% Source-level Analysis
% -------------------------------------------------------------------------
%% ------------------------------------------------------------------------
% Nonstationary Analysis of Brain Dynamics
% -------------------------------------------------------------------------
% HHM
% microstate analysis
% AMICA
% Nonlinear dynamic model / Dynamic causal modeling
%% ------------------------------------------------------------------------
% Generate Report
% -------------------------------------------------------------------------
if CONFIG.EXPORT_REPORT
[EEG, CONFIG] = gen_report_materials(EEG,CONFIG);
[EEG, CONFIG] = gen_report(EEG,CONFIG);
end
end
function [EEG, CONFIG] = save_data(EEG,CONFIG,filename,ISPREP)
% convert data to double precision
if CONFIG.double_precision
EEG.data = double(EEG.data);
EEG = eeg_checkset( EEG );
end
if CONFIG.SAVESET
if ~exist(CONFIG.report.directory,'file'), mkdir(CONFIG.report.directory); end
% save EEG data
pop_saveset(EEG,'filepath',CONFIG.filepath,'filename',filename);
fprintf('Saved EEG file ''%s'' under the folder ''%s''\n',filename, CONFIG.filepath);
% save raw data information
if ~ISPREP
config_import = CONFIG.rawinfo;
save([CONFIG.report.directory filesep 'config_import.mat'],'config_import');
else
config_prep = CONFIG.prep;
save([CONFIG.report.directory filesep 'config_prep.mat'],'config_prep');
end
end
end
function [EEG, CONFIG] = power_analysis(EEG,CONFIG)
% compute power spectra density (PSD)
[spectra,freqs] = spectopo(EEG.data, 0, EEG.srate); close
% Set the following frequency bands: delta=1-4, theta=4-8, alpha=8-13, beta=13-30, gamma=30-80.
CONFIG.report.power_delta = mean(10.^(spectra(:, freqs>=1 & freqs<4 )/10),2);
CONFIG.report.power_theta = mean(10.^(spectra(:, freqs>=4 & freqs<8 )/10),2);
CONFIG.report.power_alpha = mean(10.^(spectra(:, freqs>=8 & freqs<13 )/10),2);
CONFIG.report.power_beta = mean(10.^(spectra(:, freqs>=13 & freqs<30 )/10),2);
CONFIG.report.power_gamma = mean(10.^(spectra(:, freqs>=30 & freqs<50 )/10),2);
% compute relative power
total_power = sum([CONFIG.report.power_delta,CONFIG.report.power_theta,CONFIG.report.power_alpha, ...
CONFIG.report.power_beta,CONFIG.report.power_gamma],2);
CONFIG.report.rpower_delta = CONFIG.report.power_delta ./ total_power;
CONFIG.report.rpower_theta = CONFIG.report.power_theta ./ total_power;
CONFIG.report.rpower_alpha = CONFIG.report.power_alpha ./ total_power;
CONFIG.report.rpower_beta = CONFIG.report.power_beta ./ total_power;
CONFIG.report.rpower_gamma = CONFIG.report.power_gamma ./ total_power;
% compute frontal alpha asymmetry
try
frontal_channels = {'F3','F4','F7','F8'};
frontal_alpha = zeros(1,length(frontal_channels));
for chan_id = 1:length(frontal_channels)
ch = strcmp(frontal_channels{chan_id},CONFIG.prep.chanlocs_labels_pre);
frontal_alpha(chan_id) = CONFIG.report.power_alpha(ch);
end
CONFIG.report.frontal_alpha_asym_F34 = (frontal_alpha(1) - frontal_alpha(2)) / (frontal_alpha(1) + frontal_alpha(2));
CONFIG.report.frontal_alpha_asym_F78 = (frontal_alpha(3) - frontal_alpha(4)) / (frontal_alpha(3) + frontal_alpha(4));
catch
disp('Errors when computing frontal alpha asymmetry. Might be missing channels')
end
end
function [EEG, CONFIG] = time_freq_analysis(EEG,CONFIG)
if ~isempty(CONFIG.report.timefreq_plot_chan)
window_len = CONFIG.report.timefreq_window_len;
for chan_id = 1:length(CONFIG.report.timefreq_plot_chan)
channel = find(strcmpi({EEG.chanlocs.labels},CONFIG.report.timefreq_plot_chan{chan_id}));
if isempty(channel)
error('Incorrect channel label for time frequency plot')
end
window = hann(window_len*EEG.srate);
noverlap = floor(length(window)/2);
nfft = max(256, 2.^ceil(log2(length(window))));
[s,f,t] = spectrogram(EEG.data(channel,:),window,noverlap,nfft,EEG.srate);
freq_range = 1:(find(f>50,1)-1);
log_power = log(abs(s(freq_range,:)).^2);
figure, imagesc(t,f(freq_range),log_power); set(gca,'YDir','normal'); colorbar
caxis([prctile(log_power(:),0.25),max(log_power(:))]);
xlabel('Time (sec)'); ylabel('Frequency (Hz)'); set(gca,'fontsize',12); colormap('jet');
title(sprintf('Channel %s (log power)',EEG.chanlocs(channel).labels));
set(gcf,'position',[50,50,850,350])
filename = sprintf('tfplot_%s',CONFIG.report.timefreq_plot_chan{chan_id});
saveas(gcf,[CONFIG.report.directory filesep filename],'png'); close
end
end
end
function [EEG, CONFIG] = coherence_analysis(EEG,CONFIG)
try
if ~isempty(CONFIG.report.coh_chann_pair)
CONFIG.report.mscohere = zeros(5,length(CONFIG.report.coh_chann_pair)); % 5 bands x N channel-pairs
% iterate through all channel pairs
for pair_id = 1:length(CONFIG.report.coh_chann_pair)
channel_1 = find(strcmpi({EEG.chanlocs.labels},CONFIG.report.coh_chann_pair{pair_id}(1)));
channel_2 = find(strcmpi({EEG.chanlocs.labels},CONFIG.report.coh_chann_pair{pair_id}(2)));
% option 1: MATLAB magnitude-squared coherence
nfft = 2.^(ceil(log2(EEG.srate)));
noverlap = floor(nfft/2);
[coh_msconhere,freqs] = mscohere(EEG.data(channel_1,:),EEG.data(channel_2,:), ...
hann(nfft), noverlap, nfft, EEG.srate);
% plot coherence over frequency
freq_range = 1:(find(freqs>50,1)-1);
figure, plot(freqs(freq_range), coh_msconhere(freq_range));
xlabel('Frequency (Hz)'); ylabel('Magnitude-Squared Coherence'); set(gca,'fontsize',12);
filename = sprintf('mscoherence_%s-%s',EEG.chanlocs(channel_1).labels,EEG.chanlocs(channel_2).labels);
saveas(gcf,[CONFIG.report.directory filesep filename],'png'); close
% report ms-coherence
CONFIG.report.mscohere(1,pair_id) = mean(coh_msconhere(freqs>=1 & freqs<4 ));
CONFIG.report.mscohere(2,pair_id) = mean(coh_msconhere(freqs>=4 & freqs<8 ));
CONFIG.report.mscohere(3,pair_id) = mean(coh_msconhere(freqs>=8 & freqs<13 ));
CONFIG.report.mscohere(4,pair_id) = mean(coh_msconhere(freqs>=13 & freqs<30 ));
CONFIG.report.mscohere(5,pair_id) = mean(coh_msconhere(freqs>=30 & freqs<50 ));
end
end
catch
disp('Coherence analysis: channel labels were not correctly defined.')
end
end