-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspm_eeg_inv_results.m
233 lines (184 loc) · 8.32 KB
/
spm_eeg_inv_results.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 [D] = spm_eeg_inv_results(D)
% Contrast of evoked responses and power for an MEG-EEG model
% FORMAT [D] = spm_eeg_inv_results(D)
% Requires:
%
% D.inv{i}.contrast.woi - (n x 2) time (ms) window[s] of interest
% D.inv{i}.contrast.fboi - frequency window of interest
% D.inv{i}.contrast.type - 'evoked' or 'induced'
%
% This routine will create a contrast for each trial type and will compute
% induced responses in terms of power (over trials) if requested; otherwise
% the power in D.inv{i}.contrast.GW corresponds to the evoked power.
%__________________________________________________________________________
% Copyright (C) 2007-2017 Wellcome Trust Centre for Neuroimaging
% Karl Friston
% $Id: spm_eeg_inv_results.m 7094 2017-06-06 11:14:10Z guillaume $
%-MEEG data structure
%==========================================================================
try
model = D.inv{D.val};
catch
model = D.inv{end};
D.val = numel(D.inv);
end
%-Defaults
%--------------------------------------------------------------------------
try, woi = model.contrast.woi; catch, woi = model.inverse.woi; end
try, foi = model.contrast.fboi; catch, foi = []; end
try, type = model.contrast.type; catch, type = 'evoked'; end
try, Disp = model.contrast.display; catch, Disp = 1; end
%-Ensure contrast woi is within inversion woi
%--------------------------------------------------------------------------
woi(:,1) = max(woi(:,1),model.inverse.woi(1));
woi(:,2) = min(woi(:,2),model.inverse.woi(2));
if ~any(foi)
foi = [];
end
fprintf('%-40s: %30s','Computing contrast','...please wait'); %-#
% inversion parameters
%--------------------------------------------------------------------------
J = model.inverse.J; % Trial average MAP estimate
T = model.inverse.T; % temporal projector
U = model.inverse.U; % spatial projector[s]
Is = model.inverse.Is; % Indices of ARD vertices
Ic = model.inverse.Ic; % Indices of channels
It = model.inverse.It; % Indices of time bins
pst = model.inverse.pst; % peristimulus time (ms)
Nd = model.inverse.Nd; % number of mesh dipoles
Nb = size(T,1); % number of time bins
Nc = size(U,1); % number of channels
Nw = size(woi,1); % number of contrast windows
Nj = numel(J); % number of conditions
try
scale = model.inverse.scale; % Trial average MAP estimate
catch
scale = 1;
end
%-Time-frequency contrast
%==========================================================================
model.contrast.W = {};
model.contrast.JW = {};
model.contrast.GW = {};
for w = 1:Nw
% get [Gaussian] time window
%--------------------------------------------------------------------------
fwhm = max(diff(woi(w,:)),8);
t = exp(-4*log(2)*(pst(:) - mean(woi(w,:))).^2/(fwhm^2));
t = t/sum(t);
% get frequency space and put PST subspace into contrast (W -> T*T'*W)
%--------------------------------------------------------------------------
if ~isempty(foi)
wt = 2*pi*pst(:)/1000;
W = [];
for f = foi(1):foi(end)
W = [W sin(f*wt) cos(f*wt)];
end
W = diag(t)*W;
W = spm_svd(W,1);
else
W = t(:);
end
TW = T'*W;
TTW = T*TW;
% MAP projector and conditional covariance
%==========================================================================
qC = model.inverse.qC*trace(TTW'*model.inverse.qV*TTW);
qC = max(qC,0);
% cycle over trial types
%==========================================================================
try
trial = model.inverse.trials;
catch
trial = D.condlist;
end
for i = 1:Nj
% induced or evoked
%------------------------------------------------------------------
iw = (w - 1)*Nj + i;
CW{iw} = W;
switch(type)
% energy of conditional mean
%--------------------------------------------------------------
case{'evoked'}
JW{iw} = J{i}*TW(:,1);
GW{iw} = sum((J{i}*TW).^2,2) + qC;
% mean energy over trials
%--------------------------------------------------------------
case{'induced'}
JW{iw} = sparse(0);
JWWJ = sparse(0);
c = D.indtrial(trial{i}, 'GOOD');
% conditional expectation of contrast (J*W) and its energy
%----------------------------------------------------------
Nt = length(c);
spm_progress_bar('Init',Nt,sprintf('condition %d',i),'trials');
for j = 1:Nt
if ~strcmp(D.modality(1,1), 'Multimodal')
% unimodal data
%--------------------------------------------------
Y = D(Ic{1},It,c(j))*TTW;
Y = U{1}*Y*scale;
else
% multimodal data
%--------------------------------------------------
for k = 1:length(U)
Y = D(Ic{k},It,c(j))*TTW;
UY{k,1} = U{k}*Y*scale(k);
end
Y = spm_cat(UY);
end
MYW = model.inverse.M*Y;
JW{iw} = JW{iw} + MYW(:,1);
JWWJ = JWWJ + sum(MYW.^2,2);
spm_progress_bar('Set',j)
end
spm_progress_bar('Clear')
% conditional expectation of total energy (source space GW)
%----------------------------------------------------------
JW{iw} = JW{iw}/Nt;
GW{iw} = JWWJ/Nt + qC;
case 'trials'
JW{iw} = {};
JWWJ = {};
c = D.indtrial(trial{i}, 'GOOD');
% conditional expectation of contrast (J*W) and its energy
%----------------------------------------------------------
Nt = length(c);
spm_progress_bar('Init',Nt,sprintf('condition %d',i),'trials');
for j = 1:Nt
if ~strcmp(D.modality(1,1), 'Multimodal')
% unimodal data
%--------------------------------------------------
Y = D(Ic{1},It,c(j))*TTW;
Y = U{1}*Y*scale;
else
% multimodal data
%--------------------------------------------------
for k = 1:length(U)
Y = D(Ic{k},It,c(j))*TTW;
UY{k,1} = U{k}*Y*scale(k);
end
Y = spm_cat(UY);
end
MYW = model.inverse.M*Y;
JW{iw}{j} = MYW(:,1);
GW{iw}{j} = sum(MYW.^2,2) + qC;
spm_progress_bar('Set',j)
end
spm_progress_bar('Clear')
end
end
%-Save results
%======================================================================
model.contrast.woi = woi;
model.contrast.fboi = foi;
model.contrast.W = CW;
model.contrast.JW = JW;
model.contrast.GW = GW;
end
D.inv{D.val} = model;
fprintf('%s%30s\n',repmat(sprintf('\b'),1,30),'...done'); %-#
%-Display
%==========================================================================
if Disp && ~spm('CmdLine'), spm_eeg_inv_results_display(D); end