withinStagePLIsurrogate.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. % do within-stage PLI surrogates after geting bump locations and define tempral windows for connectivity
  2. clear all
  3. close all
  4. clc
  5. nSurro = 200;
  6. buOff = 2; % FC stges everything between bumps except for bumps
  7. withBL = true;
  8. dimBL = [10, 6]; % form 40 - 10 to onset - 6 samples -> BL with 25 samples, 250 millisecons
  9. dataHSMM = '/hmmBu4RroiAveMap3StagBL.mat';
  10. savename = '/surroCirchmmBu4RroiAveMap3StagBL.mat';
  11. blOff = 40; % 400 milliseconds baseline
  12. nSj = 18;
  13. fs = 100;
  14. nRoi = 68;
  15. nFC = (nRoi^2)/2 - nRoi/2;
  16. savePath = './dataSave';
  17. dataPath = './data';
  18. dataMEG = './data/thetaROIs_R.mat';
  19. load([dataPath dataHSMM], 'proBump', 'x','y')
  20. load(dataMEG, 'roiaveR','xR', 'yR', 'subjtR')
  21. nStage = size(proBump, 3)+1;
  22. nStageBL= nStage;
  23. buOff_i = buOff * ones(1, nStage-1);
  24. buOff_i = [0, buOff_i];
  25. buOff_e = buOff * ones(1, nStage-1);
  26. buOff_e = [buOff_e, 0];
  27. if withBL,
  28. nStageBL = nStage + 1;
  29. buOff_i = [dimBL(1), buOff_i];
  30. buOff_e = [dimBL(2), buOff_e];
  31. end
  32. roiR = single(roiaveR);
  33. xd = xR;
  34. yd = yR;
  35. subj = subjtR;
  36. clear roiaveR xR yR subjtR
  37. pairs = combnk([1:nRoi],2);
  38. tooShort = [];
  39. pliSurro = zeros(nStageBL, nFC, nSurro, 'single');
  40. for sj = 1:nSj
  41. xj = x(subj==sj); % bumps
  42. yj = y(subj==sj);
  43. xdj = xd(subj==sj); % data
  44. ydj = yd(subj==sj);
  45. nTr = length(xj);
  46. lens = yj - xj + 1;
  47. pliTr = zeros(nStageBL, nFC, nSurro, nTr,'single');
  48. maxN = max(lens);
  49. buSampl = ones(nTr, nStage,'single');
  50. % location of bumps given by HsMM
  51. for bu = 1:nStage-1
  52. buSampl(:,bu+1) = round( [1:size(proBump,1)] * squeeze(proBump(:, subj==sj, bu)));
  53. end
  54. buSampl = horzcat(buSampl, lens);
  55. buSampl = buSampl + blOff;
  56. if withBL,
  57. buSampl = horzcat(ones(size(buSampl,1), 1), buSampl);
  58. end
  59. for tr = 1:nTr
  60. % pli: corss spectral power
  61. data = hilbert(roiR(xdj(tr):ydj(tr),:));
  62. data = repmat(data,1,1,nSurro);
  63. dataSu = zeros(size(data));
  64. raIx = uint32(randi(lens(tr)-1,1,nSurro));
  65. for si = 1:nSurro
  66. dataSu(:,:,si) = circshift(data(:,:,1), raIx(si));
  67. end
  68. % corss spectral density
  69. csd = data(:, pairs(:,1),:) .* conj(dataSu(:, pairs(:,2),:));
  70. % sample phase differences sign
  71. siCsd = sign( imag( csd )); % [samples, FC, surros]
  72. % stage mean PLI
  73. for s = 1:size(buSampl,2)-1
  74. if (buSampl(tr,s+1)-buOff_e(s)) - (buSampl(tr,s)+buOff_i(s)) <= 0,
  75. pliTr(s,:,:,tr) = 0; % [stages, FC, surro, trials]
  76. tooShort = vertcat(tooShort, [s,tr]);
  77. end
  78. pliTr(s,:,:,tr) = mean( siCsd( buSampl(tr,s)+buOff_i(s):buSampl(tr,s+1)-buOff_e(s) ,:,:), 1);
  79. end
  80. end
  81. if any(any(any(any(isnan(pliTr)))))
  82. aNaN = 1;
  83. end
  84. pliSurro(:,:,:) = squeeze(pliSurro(:,:,:)) + squeeze( mean(pliTr, 4) ) ./ nSj; % mean over subjects
  85. end
  86. p95a = prctile(abs(pliSurro),95,3); % minimum FC respects to Surrogates
  87. p98a = prctile(abs(pliSurro),98,3); % minimum FC respects to Surrogates
  88. %save([savePath savename], 'pliSurro','p95a', 'p98a', 'tooShort')