-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbf_output_image_powcorr.m
357 lines (281 loc) · 10 KB
/
bf_output_image_powcorr.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
function res = bf_output_image_powcorr(BF, S)
% Computes phase-amplitude coupling
% Copyright (C) 2012 Wellcome Trust Centre for Neuroimaging
% Vladimir Litvak using code bits from OSL library
% $Id$
%--------------------------------------------------------------------------
if nargin == 0
all = cfg_const;
all.tag = 'all';
all.name = 'All';
all.val = {1};
condlabel = cfg_entry;
condlabel.tag = 'condlabel';
condlabel.name = 'Condition label';
condlabel.strtype = 's';
condlabel.val = {''};
conditions = cfg_repeat;
conditions.tag = 'conditions';
conditions.name = 'Conditions';
conditions.help = {'Specify the labels of the conditions to be included in the inversion'};
conditions.num = [1 Inf];
conditions.values = {condlabel};
conditions.val = {condlabel};
whatconditions = cfg_choice;
whatconditions.tag = 'whatconditions';
whatconditions.name = 'What conditions to include?';
whatconditions.values = {all, conditions};
whatconditions.val = {all};
sametrials = cfg_menu;
sametrials.tag = 'sametrials';
sametrials.name = 'Trials same as for filters';
sametrials.labels = {'yes', 'no'};
sametrials.values = {true, false};
sametrials.val = {false};
sametrials.help = {'Take the same trials as used for filter computation',...
'This is useful for bootstrap.'};
woi = cfg_entry;
woi.tag = 'woi';
woi.name = 'Time window of interest';
woi.strtype = 'r';
woi.num = [1 2];
woi.val = {[-Inf Inf]};
woi.help = {'Time windows (in ms)'};
freqref = cfg_entry;
freqref.tag = 'freqref';
freqref.name = 'Reference frequencies';
freqref.strtype = 'r';
freqref.num = [1 Inf];
freqref.val = {20};
freqref.help = {'Frequencies in the reference channel'};
resref = cfg_entry;
resref.tag = 'resref';
resref.name = 'Reference resolutions';
resref.strtype = 'r';
resref.num = [1 Inf];
resref.val = {5};
resref.help = {'Frequency resolution for reference frequencies. Single value or vector'};
freq = cfg_entry;
freq.tag = 'freq';
freq.name = 'Data frequencies';
freq.strtype = 'r';
freq.num = [1 Inf];
freq.val = {20};
freq.help = {'First set of frequencies'};
res = cfg_entry;
res.tag = 'res';
res.name = 'Data resolutions';
res.strtype = 'r';
res.num = [1 Inf];
res.val = {5};
res.help = {'Frequency resolution for data frequencies. Single value or vector'};
refchan = cfg_entry;
refchan.tag = 'refchan';
refchan.name = 'Reference channel';
refchan.strtype = 's';
refchan.num = [1 Inf];
refchan.help = {'Reference channel name.'};
movavg = cfg_entry;
movavg.tag = 'movavg';
movavg.name = 'Moving average window';
movavg.strtype = 'r';
movavg.num = [1 1];
movavg.val = {100};
movavg.help = {'Time window for moving average of power envelope (ms).',...
'Specify 0 to not average'};
movavg = cfg_entry;
movavg.tag = 'movavg';
movavg.name = 'Moving average window';
movavg.strtype = 'r';
movavg.num = [1 1];
movavg.val = {100};
movavg.help = {'Time window for moving average of power envelope (ms).',...
'Specify 0 to not average'};
subsample = cfg_entry;
subsample.tag = 'subsample';
subsample.name = 'Subsample';
subsample.strtype = 'n';
subsample.num = [1 1];
subsample.val = {1};
subsample.help = {'Set to N to subsample the power to every Nth sample'};
shuffle = cfg_entry;
shuffle.tag = 'shuffle';
shuffle.name = 'Shuffle';
shuffle.strtype = 'w';
shuffle.num = [1 1];
shuffle.help = {'Shuffle the reference channel to produce the null case.',...
'Specify the number of shufflings'};
shuffle.val = {0};
modality = cfg_menu;
modality.tag = 'modality';
modality.name = 'Modality';
modality.help = {'Specify modality'};
modality.labels = {
'MEG'
'MEGPLANAR'
'EEG'
}';
modality.values = {
'MEG'
'MEGPLANAR'
'EEG'
}';
modality.val = {'MEG'};
image_powcorr = cfg_branch;
image_powcorr.tag = 'image_powcorr';
image_powcorr.name = 'Power correlations image';
image_powcorr.val = {whatconditions, sametrials, shuffle, woi, refchan, freqref, ....
resref, freq, res, movavg, subsample, modality};
res = image_powcorr;
return
elseif nargin < 2
error('Two input arguments are required');
end
D = BF.data.D;
S.woi = 1e-3*S.woi; % ms -> s
samples = D.indsample(S.woi(1)):D.indsample(S.woi(2));
nsamples = length(samples);
times = D.time(samples);
if isfield(S.whatconditions, 'all')
S.whatconditions.condlabel = D.condlist;
end
for i = 1:numel(S.whatconditions.condlabel)
if S.sametrials
trials{i} = BF.features.trials(strmatch(S.whatconditions.condlabel{i},...
D.conditions(BF.features.trials)));
else
trials{i} = D.indtrial(S.whatconditions.condlabel{i}, 'GOOD');
end
if isempty(trials{i})
error('No trials matched the selection.');
end
end
if isempty(trials)
error('No trials matched the selection, check the specified condition labels');
end
channels = BF.features.(S.modality).chanind;
U = BF.features.(S.modality).U;
nchan = size(U, 2);
alltrials = spm_vec(trials);
ntrials = length(alltrials);
nref = length(S.freqref);
nfreq = length(S.freq);
W = BF.inverse.(S.modality).W;
nvert = numel(W);
Y = U'*reshape(D(channels, samples, alltrials), nchan, []);
Y = reshape(Y, size(Y, 1), nsamples, ntrials);
Yr = squeeze(D(D.indchannel(S.refchan), samples, alltrials));
if size(Yr, 1) == 1
Yr = Yr';
end
spectrum = ft_specest_hilbert(Yr', times,...
'freqoi', S.freqref, 'width', S.resref, 'filttype', 'but', 'filtorder', 2,...
'filtdir', 'twopass', 'verbose', 0);
spectrum = reshape(permute(spectrum, [3 1 2]), nsamples, ntrials*nref);
spectrum = abs(spectrum);
if S.movavg
avwin = spm_hanning(1e-3*(S.movavg)*D.fsample);
spectrum = conv2(avwin, 1, spectrum, 'same');
end
spectrum = spectrum(1:S.subsample:end, :);
spectrum = detrend(spectrum);
spectrum = spectrum./repmat(std(spectrum), size(spectrum, 1), 1);
refsig = reshape(spectrum, [], ntrials, nref);
powc = nan(length(S.freqref), length(S.freq), nvert);
if S.shuffle
spowc = repmat(powc, [1 1 1 S.shuffle]);
sind = zeros(S.shuffle, ntrials);
for s = 1:S.shuffle
sind(s, :) = randperm(ntrials);
end
end
for f = 1:length(S.freq)
spm_progress_bar('Init', ntrials, ...
sprintf('Computing data spectra')); drawnow;
if ntrials > 100, Ibar = floor(linspace(1, ntrials,100));
else Ibar = 1:ntrials; end
Yh = 0*Y;
for i = 1:ntrials
Yh(: , : ,i) = spm_squeeze(ft_specest_hilbert(squeeze(Y(:,:, i)), times,...
'freqoi', S.freq(f), 'width', S.res(f), 'filttype', 'but', ...
'filtorder', 2, 'filtdir', 'twopass', 'verbose', 0), 2);
if ismember(i, Ibar)
spm_progress_bar('Set', i); drawnow;
end
end
Yh = reshape(Yh, nchan, []);
spm_progress_bar('Clear');
spm_progress_bar('Init', nvert, ...
sprintf('Scanning grid points image')); drawnow;
if nvert > 100, Ibar = floor(linspace(1, nvert,100));
else Ibar = 1:nvert; end
for i = 1:nvert
if ~isnan(W{i})
w = W{i};
sYh = w*Yh;
sYh = reshape(abs(sYh), nsamples, ntrials);
if S.movavg
sYh = conv2(avwin, 1, sYh, 'same');
end
sYh = sYh(1:S.subsample:end, :);
sYh = detrend(sYh);
sYh = sYh./repmat(std(sYh), size(sYh, 1), 1);
x = sYh(:);
for j = 1:nref
rYh = spm_squeeze(refsig(:,:, j), 3);
for shuffle = 0:S.shuffle
if shuffle
rYh = rYh(:, sind(shuffle, :));
end
y = rYh(:);
pinvx = pinv(x);
pe = pinvx*y;
r = y-x*pe;
vr = diag(r'*r/(size(y,1)-size(x,2)));
vrp = pinv(x'*x)*vr;
cs = pe/sqrt(vrp);
if shuffle
spowc(j, f, i, shuffle) = cs;
else
powc(j, f, i) = cs;
end
end
end
end
if ismember(i, Ibar)
spm_progress_bar('Set', i); drawnow;
end
end
end
spm_progress_bar('Clear');
if max(nfreq, nref)>1
image(1).val = squeeze(sum(sum(powc, 2), 1));
image(1).label = ['sumpowc_' spm_file(D.fname, 'basename')];
c = 2;
else
c = 1;
end
for f = 1:nref
for g = 1:nfreq
image(c).val = squeeze(powc(f, g, :));
image(c).label = ['powc_ref_' num2str(S.freqref(f)) 'Hz_meg_'...
num2str(S.freq(g)) 'Hz_' spm_file(D.fname, 'basename')];
c = c+1;
end
end
for shuffle = 1:S.shuffle
if max(nfreq, nref)>1
image(c).val = squeeze(sum(sum(spowc(:,:,:, shuffle), 2), 1));
image(c).label = ['shuffled' num2str(shuffle) '_sumpowc_' spm_file(D.fname, 'basename')];
c = c+1;
end
for f = 1:nref
for g = 1:nfreq
image(c).val = squeeze(spowc(f, g, :, shuffle));
image(c).label = ['shuffled' num2str(shuffle) '_powc_ref_' num2str(S.freqref(f)) 'Hz_meg_'...
num2str(S.freq(g)) 'Hz_' spm_file(D.fname, 'basename')];
c = c+1;
end
end
end
res = image;