withinStagePLI.m 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. % get bump locations and define tempral windows for connectivity
  2. clear all
  3. close all
  4. clc
  5. buOff = 2; % FC stges everything between bumps except fro bumps
  6. blOff = 40; % 400 milliseconds baseline
  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. nSj = 18;
  11. fs = 100;
  12. nRoi = 68;
  13. nFC = (nRoi^2)/2 - nRoi/2;
  14. dataPath = './data';
  15. dataMEG = './data/thetaROIs_R.mat';
  16. saveFile = './dataSave' ;
  17. folderModel = dataHSMM(1:end-4);
  18. load([dataPath dataHSMM], 'proBump','x','y')
  19. load(dataMEG, 'roiaveR','xR', 'yR', 'subjtR')
  20. nStage = size(proBump, 3)+1;
  21. nStageBL= nStage;
  22. buOff_i = buOff * ones(1, nStage-1);
  23. buOff_i = [0, buOff_i];
  24. buOff_e = buOff * ones(1, nStage-1);
  25. buOff_e = [buOff_e, 0];
  26. if withBL,
  27. nStageBL = nStage + 1;
  28. buOff_i = [dimBL(1), buOff_i];
  29. buOff_e = [dimBL(2), buOff_e];
  30. end
  31. roiR = single(roiaveR);
  32. xd = xR;
  33. yd = yR;
  34. subj = subjtR;
  35. clear roiaveR xR yR subjtR
  36. pairs = combnk([1:nRoi],2);
  37. tooShort = [];
  38. nTr = length(x);
  39. lens = y - x + 1;
  40. pliTr = zeros(nStageBL, nFC, nTr, 'single');
  41. maxN = max(lens);
  42. buSampl = ones(nTr, nStage,'single');
  43. % location of bumps given by HsMM
  44. for bu = 1:nStage-1
  45. buSampl(:,bu+1) = round( [1:maxN] * squeeze(proBump(:,:,bu)) );
  46. end
  47. buSampl = horzcat(buSampl, lens);
  48. buSampl = buSampl + blOff;
  49. if withBL,
  50. buSampl = horzcat(ones(size(buSampl,1), 1), buSampl);
  51. end
  52. for tr = 1:nTr
  53. yTr = y(tr) - x(tr) + 1;
  54. data = hilbert(roiR(xd(tr):yd(tr),:));
  55. % common spectral density
  56. csd = data(:, pairs(:,1)) .* conj(data(:, pairs(:,2)));
  57. % sample phase differences sign
  58. siCsd = sign( imag( csd ));
  59. % stage mean PLI
  60. for s = 1:size(buSampl,2)-1
  61. if (buSampl(tr,s+1)-buOff_e(s)) - (buSampl(tr,s)+buOff_i(s)) <= 0,
  62. pliTr(s,:,tr) = 0;
  63. tooShort = vertcat(tooShort, [s,tr]);
  64. end
  65. pliTr(s,:,tr) = mean( siCsd( buSampl(tr,s)+buOff_i(s):buSampl(tr,s+1)-buOff_e(s) ,:), 1);
  66. end
  67. end
  68. % average by subjects
  69. pliSj = zeros(nStageBL, nFC, nSj);
  70. for sj = 1:nSj
  71. pliSj(:,:,sj) = squeeze( mean(pliTr(:,:,subj==sj), 3) ); % mean over subjects
  72. end
  73. pliR = squeeze( mean(pliSj,3) );
  74. save2path = [saveFile folderModel];
  75. %save([save2path '/FCraw.mat'], 'pliR')