spm_eeg_cfc.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. function spm_eeg_cfc(S)
  2. % Compute GLM for phase-amplitude and amplitude-amplitude coupling
  3. % FORMAT spm_eeg_cfc(S)
  4. %
  5. % Xamp = independent variable to be explained:
  6. % Xamp = B1*sin(Xphase) + B2*cos(Xphase) + B3*Xlowamp
  7. %
  8. % Additional regressors may be included
  9. % - overall estimates of PAC & AMP are obtained from continuous (or
  10. % concatenated) data
  11. % - statistical inference of these estimates is performed by dividing the
  12. % continuous time series into shorter epochs
  13. % - function writes out images of the estimated PAC & AMP, as well as their
  14. % p-values
  15. %__________________________________________________________________________
  16. %
  17. % References:
  18. % van Wijk et al. 2015 J Neurosci Methods
  19. %__________________________________________________________________________
  20. % Copyright (C) 2014 Wellcome Trust Centre for Neuroimaging
  21. % Bernadette van Wijk, Vladimir Litvak
  22. % $Id: spm_eeg_cfc.m 7316 2018-05-22 11:21:34Z bernadette $
  23. SVNrev = '$Rev: 7316 $';
  24. %-Startup
  25. %--------------------------------------------------------------------------
  26. spm('FnBanner', mfilename, SVNrev);
  27. spm('FigName','Cross-frequency coupling'); spm('Pointer','Watch');
  28. D = spm_eeg_load(S.D);
  29. if ~isfield(S, 'conditions') || isempty(S.conditions), S.conditions = D.condlist; end
  30. if ~iscell(S.conditions), S.conditions = {S.conditions}; end
  31. if ~isequal(D.transformtype, 'TF')
  32. error('The input should time-frequency dataset.');
  33. end
  34. allamp = [];
  35. allphase = [];
  36. cnt = 1;
  37. for i = 1:numel(S.regressors)
  38. fun = char(fieldnames(S.regressors{i}));
  39. S1{cnt} = S.regressors{i}.(fun);
  40. S1{cnt}.D = D;
  41. S1{cnt}.summarise = false;
  42. res = feval(['spm_eeg_regressors_' fun], S1{cnt});
  43. cnt = cnt + 1;
  44. switch fun
  45. case 'tfpower'
  46. allamp = spm_cat_struct(allamp, res);
  47. case 'tfphase'
  48. allphase = spm_cat_struct(allphase, res);
  49. end
  50. end
  51. allconfounds = [];
  52. for i = 1:numel(S.confounds)
  53. fun = char(fieldnames(S.confounds{i}));
  54. S1{cnt} = S.confounds{i}.(fun);
  55. S1{cnt}.D = D;
  56. S1{cnt}.summarise = false;
  57. res = feval(['spm_eeg_regressors_' fun], S1{cnt});
  58. cnt=cnt+1;
  59. allconfounds = spm_cat_struct(allconfounds, res);
  60. cnt = cnt + 1;
  61. end
  62. freqind = D.indfrequency(min(S.freqwin)):D.indfrequency(max(S.freqwin));
  63. if isempty(freqind) || any(isnan(freqind))
  64. error('Selected frequency window is invalid.');
  65. end
  66. data = spm_squeeze(mean(D(D.selectchannels(S.channels), freqind, :, D.indtrial(S.conditions, 'GOOD')), 1), 1);
  67. cut = round(D.fsample/4); %removed at start and end of each filter time series to avoid filter ringing - for trial type data this means a loss of samples per trial
  68. if size(data,3)>1
  69. datatype = 'trials';
  70. trialsamples = size(data, 2)-2*cut+1;
  71. nepochs = size(data,3);
  72. totalsamples = trialsamples*nepochs;
  73. disp(['number of epochs used for statistics: ', num2str(nepochs)]);
  74. else
  75. datatype = 'continuous';
  76. totalsamples = size(data, 2)-2*cut+1;
  77. trialsamples = round((S.window/1000)*D.fsample);
  78. nepochs = floor(totalsamples/trialsamples);
  79. disp(['number of epochs used for statistics: ', num2str(nepochs)]);
  80. end
  81. %-Get amplitude timeseries
  82. %--------------------------------------------------------------------------
  83. Famp = D.frequencies(freqind);
  84. for N = 1:length(freqind)
  85. fprintf('\nF amp = %.1f |\t', Famp(N));
  86. if strcmp(datatype,'trials')
  87. for k = 1:size(data, 3)
  88. amp_high = data(N, cut:end-cut, k);
  89. AMP(N,(k-1)*trialsamples+1:k*trialsamples) = amp_high;
  90. amp(N,k,:) = (amp_high - mean(amp_high))./std(amp_high);
  91. end
  92. AMP(N,:) = (AMP(N,:) - mean(AMP(N,:)))./std(AMP(N,:));
  93. elseif strcmp(datatype,'continuous')
  94. amp_high = data(N, cut:end-cut);
  95. AMP(N,:) = amp_high;
  96. for k = 1:nepochs
  97. amp(N,k,:) = AMP(N,(k-1)*trialsamples+1:k*trialsamples);
  98. amp(N,k,:) = (amp(N,k,:)-mean(amp(N,k,:)))./std(amp(N,k,:));
  99. end
  100. AMP(N,:) = (AMP(N,:)-mean(AMP(N,:)))./std(AMP(N,:));
  101. end
  102. end
  103. nsamples = size(data, 2);
  104. if strcmp(datatype,'trials')
  105. bad = spm_squeeze(any(D.badsamples(D.selectchannels(S.channels), cut:D.nsamples-cut, D.indtrial(S.conditions, 'GOOD')), 1), 1);
  106. BAD = zeros(1, size(AMP, 2));
  107. for k = 1:size(bad, 2)
  108. BAD((k-1)*trialsamples+1:k*trialsamples) = bad(:, k);
  109. end
  110. bad = bad';
  111. elseif strcmp(datatype,'continuous')
  112. BAD = spm_squeeze(any(D.badsamples(D.selectchannels(S.channels), ':', 1), 1), 1);
  113. BAD = BAD(cut:end-cut);
  114. bad = zeros(size(amp, 2), size(amp, 3));
  115. for k = 1:nepochs
  116. bad(k, :) = BAD((k-1)*trialsamples+1:k*trialsamples);
  117. end
  118. end
  119. %-Get phase time series
  120. %--------------------------------------------------------------------------
  121. SINE = {};
  122. sine = {};
  123. COSINE = {};
  124. cosine = {};
  125. for i = 1:numel(allphase)
  126. PHASE = allphase(i).R;
  127. nphase = 0.5*size(PHASE, 2);
  128. for j = 1:nphase
  129. ind = max(strfind(allphase(i).names{j}, '_'));
  130. phasefreq(i, j) = sscanf(allphase(1).names{j}(ind+1:end), '%fHz');
  131. fprintf('\nF phase = %.1f |\t', phasefreq(i, j));
  132. if strcmp(datatype,'trials')
  133. for k = 1:size(data, 3)
  134. phase_low = PHASE(((k-1)*nsamples+1):k*nsamples, j) ;
  135. SINE{i}(j,(k-1)*trialsamples+1:k*trialsamples) = phase_low(cut:end-cut);
  136. sine{i}(j,k,:) = phase_low(cut:end-cut);
  137. sine{i}(j,k,:) = (sine{i}(j,k,:)-mean(sine{i}(j,k,:)))./std(sine{i}(j,k,:));
  138. phase_low = PHASE(((k-1)*nsamples+1):k*nsamples, j + nphase) ;
  139. COSINE{i}(j,(k-1)*trialsamples+1:k*trialsamples) = phase_low(cut:end-cut);
  140. cosine{i}(j,k,:) = phase_low(cut:end-cut);
  141. cosine{i}(j,k,:) = (cosine{i}(j,k,:)-mean(cosine{i}(j,k,:)))./std(cosine{i}(j,k,:));
  142. end
  143. SINE{i}(j,:)=(SINE{i}(j,:)-mean(SINE{i}(j,:)))./std(SINE{i}(j,:));
  144. COSINE{i}(j,:)=(COSINE{i}(j,:)-mean(COSINE{i}(j,:)))./std(COSINE{i}(j,:));
  145. elseif strcmp(datatype,'continuous')
  146. phase_low = PHASE(:, j) ;
  147. SINE{i}(j,:) = phase_low(cut:end-cut);
  148. phase_low = PHASE(:, j+nphase) ;
  149. COSINE{i}(j,:) = phase_low(cut:end-cut);
  150. for k = 1:nepochs
  151. sine{i}(j, k,:) = SINE{i}(j,(k-1)*trialsamples+1:k*trialsamples);
  152. cosine{i}(j, k,:) = COSINE{i}(j,(k-1)*trialsamples+1:k*trialsamples);
  153. sine{i}(j,k,:) = (sine{i}(j,k,:)-mean(sine{i}(j,k,:)))./std(sine{i}(j,k,:));
  154. cosine{i}(j,k,:) = (cosine{i}(j,k,:)-mean(cosine{i}(j,k,:)))./std(cosine{i}(j,k,:));
  155. end
  156. SINE{i}(j,:)=(SINE{i}(j,:)-mean(SINE{i}(j,:)))./std(SINE{i}(j,:));
  157. COSINE{i}(j,:)=(COSINE{i}(j,:)-mean(COSINE{i}(j,:)))./std(COSINE{i}(j,:));
  158. end
  159. end
  160. end
  161. %-Get amplitude time series for low frequencies
  162. %--------------------------------------------------------------------------
  163. AMP_LOW = {};
  164. amp_low = {};
  165. for i = 1:numel(allamp)
  166. namp = size(allamp(i).R, 2);
  167. for j = 1:namp
  168. ind = max(strfind(allamp(i).names{j}, '_'));
  169. ampfreq(i, j) = sscanf(allamp(1).names{j}(ind+1:end), '%fHz');
  170. fprintf('\nF low amp = %.1f |\t', ampfreq(i, j));
  171. if strcmp(datatype,'trials')
  172. for k = 1:size(data, 3)
  173. amplow = allamp(i).R(((k-1)*nsamples+1):k*nsamples, j);
  174. AMP_LOW{i}(j,(k-1)*trialsamples+1:k*trialsamples)=amplow(cut:end-cut);
  175. amp_low{i}(j,k,:)=(amplow(cut:end-cut)-mean(amplow(cut:end-cut)))./std(amplow(cut:end-cut));
  176. end
  177. AMP_LOW{i}(j,:)=(AMP_LOW{i}(j,:)-mean(AMP_LOW{i}(j,:)))./std(AMP_LOW{i}(j,:));
  178. elseif strcmp(datatype,'continuous')
  179. amplow= allamp(i).R(:, j);
  180. AMP_LOW{i}(j,:)=amplow(cut:end-cut);
  181. for k=1:nepochs
  182. amp_low{i}(j,k,:)=AMP_LOW{i}(j,(k-1)*trialsamples+1:k*trialsamples);
  183. amp_low{i}(j,k,:)=(amp_low{i}(j,k,:)-mean(amp_low{i}(j,k,:)))./std(amp_low{i}(j,k,:));
  184. end
  185. AMP_LOW{i}(j,:)=(AMP_LOW{i}(j,:)-mean(AMP_LOW{i}(j,:)))./std(AMP_LOW{i}(j,:));
  186. end
  187. end
  188. end
  189. %-Set low frequency axis
  190. %--------------------------------------------------------------------------
  191. if isempty(amp_low); Flow=phasefreq;
  192. elseif isempty(cosine); Flow=ampfreq;
  193. else
  194. Flow = cat(1, phasefreq, ampfreq);
  195. % Could this possibly be relaxed?
  196. if any(any(diff(Flow, [], 1)))
  197. error('The frequency axes for all regressors should be identical.');
  198. else
  199. Flow = Flow(1, :);
  200. end
  201. end
  202. %-Get time series for confounders
  203. %--------------------------------------------------------------------------
  204. CONFOUNDS = {};
  205. confounds = {};
  206. for i = 1:numel(allconfounds)
  207. nconf = size(allconfounds(i).R, 2);
  208. for j = 1:nconf
  209. fprintf('\nConfound: %s |\t', allconfounds(i).names{j});
  210. if strcmp(datatype,'trials')
  211. for k = 1:size(data, 3)
  212. conf = allconfounds(i).R(((k-1)*nsamples+1):k*nsamples, j);
  213. CONFOUNDS{i}(j,(k-1)*trialsamples+1:k*trialsamples) = conf(cut:end-cut);
  214. confounds{i}(j,k,:) = (conf(cut:end-cut)-mean(conf(cut:end-cut)))./std(conf(cut:end-cut));
  215. end
  216. CONFOUNDS{i}(j,:)=(CONFOUNDS{i}(j,:)-mean(CONFOUNDS{i}(j,:)))./std(CONFOUNDS{i}(j,:));
  217. elseif strcmp(datatype,'continuous')
  218. conf = allconfounds(i).R(:, j);
  219. CONFOUNDS{i}(j,:) = conf(cut:end-cut);
  220. for k=1:nepochs
  221. confounds{i}(j,k,:) = CONFOUNDS{i}(j,(k-1)*trialsamples+1:k*trialsamples);
  222. confounds{i}(j,k,:)=(confounds{i}(j,k,:)-mean(confounds{i}(j,k,:)))./std(confounds{i}(j,k,:));
  223. end
  224. CONFOUNDS{i}(j,:) = (CONFOUNDS{i}(j,:)-mean(CONFOUNDS{i}(j,:)))./std(CONFOUNDS{i}(j,:));
  225. end
  226. end
  227. if nconf==1&&length(Flow)>1
  228. CONFOUNDS{i}=repmat(CONFOUNDS{i},length(Flow),1);
  229. confounds{i}=repmat(confounds{i},[length(Flow),1,1]);
  230. end
  231. end
  232. fprintf('\n\n')
  233. W = ones(length(BAD), 1);
  234. W(~~BAD) = exp(-256);
  235. W = spdiags(W, 0, length(W), length(W));
  236. %-Compute GLM
  237. %--------------------------------------------------------------------------
  238. spm_progress_bar('Init', length(Flow), 'Fitting GLM', 'Frequency nr');
  239. for j=1:length(Flow)
  240. fprintf('%.1f ',Flow(j))
  241. for N=1:length(Famp)
  242. % GLM for all data appended
  243. %------------------------------------------------------------------
  244. X=[];
  245. for nph=1:numel(allphase)
  246. X=[X;SINE{nph}(j,:);COSINE{nph}(j,:)];
  247. end
  248. for nam=1:numel(allamp)
  249. X=[X;AMP_LOW{nam}(j,:)];
  250. end
  251. for ncf=1:numel(allconfounds)
  252. X=[X;CONFOUNDS{ncf}(j,:)];
  253. end
  254. nreg=size(X,1);
  255. X = X*W;
  256. y = AMP(N,:)*W;
  257. V=[];
  258. c=ones(nreg,1);
  259. all_Beta(N,j,:)=y*pinv(X);
  260. all_SSy(N,j)=sum((y-mean(y)).^2);
  261. cnt=1;
  262. for nph=1:numel(allphase)
  263. all_residuals=y-(all_Beta(N,j,cnt).*X(cnt,:)+all_Beta(N,j,cnt+1).*X(cnt+1,:));
  264. all_SSe=sum((all_residuals-mean(all_residuals)).^2);
  265. all_r_pac{nph}(N,j)=real(sqrt((all_SSy(N,j)-all_SSe)/all_SSy(N,j)));
  266. all_Beta_sin{nph}(N,j)=all_Beta(N,j,cnt);
  267. all_Beta_cos{nph}(N,j)=all_Beta(N,j,cnt+1);
  268. cnt=cnt+2;
  269. end
  270. for nam=1:numel(allamp)
  271. all_residuals=y-(all_Beta(N,j,cnt).*X(cnt,:));
  272. all_SSe=sum((all_residuals-mean(all_residuals)).^2);
  273. all_r_amp{nam}(N,j)=real(sqrt((all_SSy(N,j)-all_SSe)/all_SSy(N,j)));
  274. all_Beta_amp{nam}(N,j)=all_Beta(N,j,cnt);
  275. cnt=cnt+1;
  276. end
  277. for ncf=1:numel(allconfounds)
  278. all_residuals=y-(all_Beta(N,j,cnt).*X(cnt,:));
  279. all_SSe=sum((all_residuals-mean(all_residuals)).^2);
  280. all_r_conf{ncf}(N,j)=real(sqrt((all_SSy(N,j)-all_SSe)/all_SSy(N,j)));
  281. all_Beta_conf{ncf}(N,j)=all_Beta(N,j,cnt);
  282. cnt=cnt+1;
  283. end
  284. all_residuals_total=y-(X'*squeeze(all_Beta(N,j,:)))';
  285. all_SSe_total=sum((all_residuals_total-mean(all_residuals_total)).^2);
  286. all_r_total(N,j)=real(sqrt((all_SSy(N,j)-all_SSe_total)/all_SSy(N,j)));
  287. %-GLM per trial
  288. %------------------------------------------------------------------
  289. k_good = 0;
  290. for k = 1:nepochs
  291. % Exclude epochs with mostly bad data
  292. if (sum(bad(k, :))/size(bad, 2))<0.5;
  293. k_good = k_good + 1;
  294. Wk = ones(size(bad, 2), 1);
  295. Wk(~~bad(k, :)) = exp(-256);
  296. Wk = spdiags(Wk, 0, length(Wk), length(Wk));
  297. Xk=[];
  298. for nph=1:numel(allphase)
  299. Xk=[Xk;squeeze(sine{nph}(j,k,:))';squeeze(cosine{nph}(j,k,:))'];
  300. end
  301. for nam=1:numel(allamp)
  302. Xk=[Xk;squeeze(amp_low{nam}(j,k,:))'];
  303. end
  304. for ncf=1:numel(allconfounds)
  305. Xk=[Xk;squeeze(confounds{ncf}(j,k,:))'];
  306. end
  307. Xk = Xk*Wk;
  308. nreg=size(Xk,1);
  309. yk = Wk*squeeze(amp(N,k,:));
  310. V=[];
  311. c=ones(nreg,1);
  312. Beta(:,k_good)=(yk'*pinv(Xk));
  313. cnt=1;
  314. for nph=1:numel(allphase)
  315. if S1{nph}.summarise
  316. Beta_sin{nph}(N,j,k_good)=Beta(cnt,k_good);
  317. Beta_cos{nph}(N,j,k_good)=Beta(cnt+1,k_good);
  318. cnt=cnt+2;
  319. end
  320. end
  321. if isempty(nph);nph=0;end
  322. for nam=1:numel(allamp)
  323. if S1{nph+nam}.summarise
  324. Beta_amp{nam}(N,j,k_good)=Beta(cnt,k_good);
  325. cnt=cnt+1;
  326. end
  327. end
  328. if isempty(nam);nam=0;end
  329. for ncf=1:numel(allconfounds)
  330. if S1{nph+nam+ncf}.summarise
  331. Beta_conf{ncf}(N,j,k_good)=Beta(cnt,k_good);
  332. cnt=cnt+1;
  333. end
  334. end
  335. end
  336. end %trials
  337. %-Test for significance
  338. %------------------------------------------------------------------
  339. cnt=1;
  340. for nph=1:numel(allphase)
  341. Xb=[];
  342. Xb(1:k_good,1)=ones(k_good,1);Xb(k_good+1:2*k_good,2)=ones(k_good,1);
  343. yb=[Beta(cnt,:),Beta(cnt+1,:)];
  344. c=[1;1];
  345. [Tb,df,Beta_b,xX,xCon]=spm_ancova(Xb,V,yb',c);
  346. F=Tb^2;
  347. p_pac{nph}(N,j)=1-spm_Fcdf(F,df(1),df(2));
  348. cnt=cnt+2;
  349. end
  350. for nam=1:numel(allamp)
  351. [H,P] = ttest(Beta(cnt,:));
  352. p_amp{nam}(N,j)=P;
  353. cnt=cnt+1;
  354. end
  355. for ncf=1:numel(allconfounds)
  356. [H,P] = ttest(Beta(cnt,:));
  357. p_conf{ncf}(N,j)=P;
  358. cnt=cnt+1;
  359. end
  360. Xb=[];
  361. yb=[];
  362. for i=1:nreg
  363. Xb((i-1)*k_good+1:i*k_good,i)=ones(k_good,1);
  364. yb=[yb,Beta(i,:)];
  365. end
  366. c=ones(nreg,1);
  367. [Tb,df,Beta_b,xX,xCon]=spm_ancova(Xb,V,yb',c);
  368. F_total=Tb^2;
  369. p_total(N,j)=1-spm_Fcdf(F_total,df(1),df(2));
  370. end %N
  371. spm_progress_bar('Set', j);
  372. end %j
  373. spm_progress_bar('Clear');
  374. %% - Plot results
  375. %--------------------------------------------------------------------------
  376. outname = [S.prefix 'cfc_' spm_file(D.fname, 'basename')];
  377. siglevel=.05;
  378. cnt=1;
  379. Fgraph = spm_figure('GetWin', outname); figure(Fgraph); clf
  380. nsub=ceil(length(S1))+1;
  381. for nph=1:numel(allphase)
  382. sig_pac{nph}=(p_pac{nph}<=siglevel);
  383. subplot(nsub,2,cnt),imagesc(Flow,Famp,all_r_pac{nph}),set(gca,'ydir','normal');title(S1{nph}.regname);colorbar;
  384. subplot(nsub,2,cnt+1),imagesc(Flow,Famp,sig_pac{nph}),set(gca,'ydir','normal');title(['significant p<.05']), colorbar;
  385. cnt=cnt+2;
  386. end
  387. if isempty(nph);nph=0;end
  388. for nam=1:numel(allamp)
  389. sig_amp{nam}=(p_amp{nam}<=siglevel);
  390. subplot(nsub,2,cnt),imagesc(Flow,Famp,all_Beta_amp{nam}),set(gca,'ydir','normal');title(S1{nph+nam}.regname);colorbar;
  391. subplot(nsub,2,cnt+1),imagesc(Flow,Famp,sig_amp{nam}),set(gca,'ydir','normal');title(['significant p<.05']), colorbar;
  392. cnt=cnt+2;
  393. end
  394. if isempty(nam);nam=0;end
  395. for ncf=1:numel(allconfounds)
  396. sig_cnf{ncf}=(p_conf{ncf}<=siglevel);
  397. subplot(nsub,2,cnt),imagesc(Flow,Famp,all_Beta_conf{ncf}),set(gca,'ydir','normal');title(S1{nph+nam+ncf}.regname);colorbar;
  398. subplot(nsub,2,cnt+1),imagesc(Flow,Famp,sig_cnf{ncf}),set(gca,'ydir','normal');title(['significant p<.05']), colorbar;
  399. cnt=cnt+2;
  400. end
  401. sig_total=(p_total<=siglevel);
  402. subplot(nsub,2,cnt),imagesc(Flow,Famp,all_r_total),set(gca,'ydir','normal');title('full model');colormap(flipud(hot));colorbar;
  403. subplot(nsub,2,cnt+1),imagesc(Flow,Famp,sig_total),set(gca,'ydir','normal');title(['significant p<.05']), colorbar;
  404. %%
  405. %-Write out images
  406. %--------------------------------------------------------------------------
  407. cnt=1;
  408. for nph=1:numel(allphase)
  409. image(cnt).val = all_r_pac{nph};
  410. image(cnt).label = ['r_pac_reg',num2str(nph)];
  411. cnt=cnt+1;
  412. image(cnt).val = p_pac{nph};
  413. image(cnt).label = ['p_pac_reg',num2str(nph)];
  414. cnt=cnt+1;
  415. image(cnt).val = sig_pac{nph};
  416. image(cnt).label = ['sig_pac_reg',num2str(nph)];
  417. cnt=cnt+1;
  418. image(cnt).val = all_Beta_sin{nph};
  419. image(cnt).label = ['r_Bsin_reg',num2str(nph)];
  420. cnt=cnt+1;
  421. image(cnt).val = all_Beta_cos{nph};
  422. image(cnt).label = ['r_Bcos_reg',num2str(nph)];
  423. cnt=cnt+1;
  424. if S1{nph}.summarise
  425. for k=1:nepochs
  426. image(cnt).val = squeeze(Beta_sin{nph}(:,:,k));
  427. image(cnt).label = ['trial',num2str(k),'_Bsin_reg',num2str(nph)];
  428. cnt=cnt+1;
  429. image(cnt).val = squeeze(Beta_cos{nph}(:,:,k));
  430. image(cnt).label = ['trial',num2str(k),'_Bcos_reg',num2str(nph)];
  431. cnt=cnt+1;
  432. end
  433. end
  434. end
  435. if isempty(nph),nph=0;end
  436. for nam=1:numel(allamp)
  437. image(cnt).val = all_Beta_amp{nam};
  438. image(cnt).label = ['c_amp_reg',num2str(nam)];
  439. cnt=cnt+1;
  440. image(cnt).val = p_amp{nam};
  441. image(cnt).label = ['p_amp_reg',num2str(nam)];
  442. cnt=cnt+1;
  443. image(cnt).val = sig_amp{nam};
  444. image(cnt).label = ['sig_amp_reg',num2str(nam)];
  445. cnt=cnt+1;
  446. if S1{nph+nam}.summarise
  447. for k=1:nepochs
  448. image(cnt).val = squeeze(Beta_amp{nam}(:,:,k));
  449. image(cnt).label = ['trial',num2str(k),'_Bamp_reg',num2str(nam)];
  450. cnt=cnt+1;
  451. end
  452. end
  453. end
  454. if isempty(nam),nam=0;end
  455. for ncf=1:numel(allconfounds)
  456. image(cnt).val = all_Beta_conf{ncf};
  457. image(cnt).label = ['r_conf_reg',num2str(ncf)];
  458. cnt=cnt+1;
  459. image(cnt).val = p_conf{ncf};
  460. image(cnt).label = ['p_conf_reg',num2str(ncf)];
  461. cnt=cnt+1;
  462. image(cnt).val = sig_conf{ncf};
  463. image(cnt).label = ['sig_conf_reg',num2str(ncf)];
  464. cnt=cnt+1;
  465. if S1{nph+nam+ncf}.summarise
  466. for k=1:nepochs
  467. image(cnt).val = squeeze(Beta_conf{ncf}(:,:,k));
  468. image(cnt).label = ['trial',num2str(k),'_Bconf_reg',num2str(ncf)];
  469. cnt=cnt+1;
  470. end
  471. end
  472. end
  473. image(cnt).val = all_r_total;
  474. image(cnt).label = ['r_total'];
  475. cnt=cnt+1;
  476. image(cnt).val = p_total;
  477. image(cnt).label = ['p_total'];
  478. cnt=cnt+1;
  479. image(cnt).val = sig_total;
  480. image(cnt).label = ['sig_total'];
  481. %% -Write out images
  482. %==========================================================================
  483. [sts, msg] = mkdir(D.path, outname);
  484. if ~sts, error(msg); end
  485. outdir = fullfile(D.path, outname);
  486. if length(Famp)>1
  487. dFamp = Famp(2)-Famp(1);
  488. else
  489. dFamp = 0;
  490. end
  491. if length(Flow)>1
  492. dFlow = Flow(2)-Flow(1);
  493. else
  494. dFlow = 0;
  495. end
  496. N = nifti;
  497. N.mat_intent = 'Aligned';
  498. N.mat = [...
  499. dFamp 0 0 Famp(1);...
  500. 0 dFlow 0 Flow(1);...
  501. 0 0 1 0;...
  502. 0 0 0 1];
  503. N.mat(1,4) = N.mat(1,4) - N.mat(1,1);
  504. N.mat(2,4) = N.mat(2,4) - N.mat(2,2);
  505. spm_progress_bar('Init', numel(image), 'Writing out images', 'Image');
  506. for i = 1:numel(image)
  507. N.dat = file_array(fullfile(outdir, [image(i).label '.nii']), size(image(i).val), 'FLOAT32-LE');
  508. create(N);
  509. N.dat(:, :) = image(i).val;
  510. spm_progress_bar('Set', i);
  511. end
  512. %-Cleanup
  513. %--------------------------------------------------------------------------
  514. spm_progress_bar('Clear');
  515. spm('FigName','Cross-frequency coupling: done'); spm('Pointer','Arrow');