Advertisement
Guest User

Alex's Powcorr code

a guest
Oct 8th, 2018
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. %This script assumes preprocessing has already been done in EEGLab.
  2.  
  3. %%
  4. close all;
  5. tic
  6.  
  7. %%
  8. top = 'O:\Labmembers\Alex'; %Base folder pathing
  9. addpath ('C:\Users\xxvi\Documents\EEG Lab'); %Add EEGlab
  10. startup; %Function from the EEGLab folder adds field trip and runs ft_defaults
  11. loadspec=fullfile(top,'Sample Files'); %Concatenate path to data
  12. savespec =fullfile(top,'Sample Files');
  13.  
  14. %%Read & convert data
  15. subj = {'0066'};
  16. event = {'1ppi'};
  17. for s = 1:length(subj)
  18.  
  19. for e = 1:length(event)
  20.  
  21. %Read data: basic structure
  22. [ALLEEG,EEG,CURRENTSET,ALLCOM] = eeglab;
  23. %Load set
  24. EEG = pop_loadset( 'filename', ['NAPLS_01_',subj{s}, '_',event{e},'_mr.set'],'filepath', loadspec);
  25. %Store in EEG array. Why add 0 if auto indexes into next space?
  26. [ALLEEG, EEG, CURRENTSET] = eeg_store( ALLEEG, EEG, 0 );
  27. EEG = eeg_checkset( EEG ); %Checks structure consistency
  28.  
  29. %convert to FLDTRP
  30. E2F = eeglab2fieldtrip( EEG, 'preprocessing') ;
  31. %Rewrite to savespec directory
  32. fn1=fullfile(savespec, ['NAPLS_01_',subj{s}, '_',event{e},'_mr.mat']);
  33. save(fn1,'E2F');
  34.  
  35. end
  36. end
  37.  
  38. %%
  39. %Power analysis via wavelet method, multiplication in frequency domain
  40. for s = 1:length(subj)
  41.  
  42. for e = 1:length(event)
  43.  
  44. load( fullfile(savespec, ['NAPLS_01_',subj{s}, '_',event{e},'_mr.mat']))
  45.  
  46. cfg = [];
  47. cfg.method = 'wavelet';
  48. cfg.foi = 4:0.5:8; %Delta: 1-4, Theta: 4-8, Alpha: 8-12, Beta: 13-30, Gamma: 30-50
  49. cfg.toi = -.5:.01:2.5;
  50. %Use thin wavelets to separate time into subwindows for network analysis/time. Try 3,5
  51. cfg.width = 3./cfg.foi;
  52. cfg.output = 'powandcsd'; %Return power and cross-spectra
  53. cfg.channel = 'all';
  54. cfg.channelcmb ={'all' 'all'}; %Channel pairs
  55. cfg.trials ='all';
  56. cfg.keeptrials = 'yes'; %'yes' returns individual trials
  57. cfg.pad = 'nextpow2'; %Resolution
  58. Freq = ft_freqanalysis(cfg,E2F);
  59.  
  60. %Freq.powspctrm = squeeze(Freq.powspctrm); %Eliminate singleton dimension(why am I getting that anyways?)
  61. %Freq.crsspctrm = squeeze(Freq.crsspctrm); %JK this doesn't help
  62.  
  63. %Change filename for different frequencies
  64. fn2 = fullfile(savespec, ['NAPLS_01_',subj{s}, '_',event{e},'_mr','theta.mat']);
  65. save(fn2,'Freq');
  66.  
  67. end
  68. end
  69.  
  70. %%
  71. %Power correlations between EEG channels
  72. for s = 1:length(subj)
  73.  
  74. for e = 1:length(event)
  75.  
  76. %Change filename for different frequencies
  77. load (fullfile(savespec, ['NAPLS_01_',subj{s}, '_',event{e},'_mr','theta.mat']));
  78.  
  79. cfg = [];
  80. cfg.method = 'powcorr';
  81. fpg.output = 'powcorr';
  82. cfg.channel = 'all'; %All channels
  83. cfg.channelcmb ={'all' 'all'}; %All channel pairs
  84. powcorr = ft_connectivityanalysis(cfg,Freq);
  85.  
  86. %Change filename for different frequencies
  87. fn3=fullfile(savespec, ['NAPLS_01_',subj{s}, '_',event{e},'_mr','thetapowcorr.mat']);
  88. save(fn3, 'powcorr');
  89.  
  90. end
  91. end
  92.  
  93. %%
  94. %Phase-locking value from Freq data
  95. for s = 1:length(subj)
  96.  
  97. for e = 1:length(event)
  98.  
  99. %Change filename for different frequencies
  100. load (fullfile(savespec, ['NAPLS_01_',subj{s}, '_',event{e},'_mr','theta.mat']));
  101.  
  102. cfg = [];
  103. cfg.method = 'plv';
  104. cfg.channel = 'all'; %All channels
  105. cfg.channelcmb ={'all' 'all'}; %All channel pairs
  106. plv = ft_connectivityanalysis(cfg,Freq);
  107.  
  108. %Change filename for different frequencies
  109. fn4 = fullfile(savespec, ['NAPLS_01_',subj{s}, '_',event{e},'_mr','thetaPLV.mat']);
  110. save(fn4, 'plv');
  111.  
  112. end
  113. end
  114.  
  115. %%
  116. %Spectral coherence from Freq data
  117. for s = 1:length(subj)
  118.  
  119. for e = 1:length(event)
  120.  
  121. %Change filename for different frequencies
  122. load (fullfile(savespec, ['NAPLS_01_',subj{s}, '_',event{e},'_mr','theta.mat']));
  123.  
  124. cfg = [];
  125. cfg.method = 'coh';
  126. cfg.channel = 'all'; %All channels
  127. cfg.channelcmb ={'all' 'all'}; %All channel pairs
  128. coh = ft_connectivityanalysis(cfg,Freq);
  129.  
  130. fn5 = fullfile(savespec, ['NAPLS_01_',subj{s}, '_',event{e},'_mr','thetacoh.mat']);
  131. save(fn5, 'coh');
  132.  
  133. end
  134. end
  135.  
  136. %%
  137. %Plot power correlations or phase value
  138. for s = 1:length(subj)
  139.  
  140. for e = 1:length(event)
  141.  
  142. %Change filename for different frequencies
  143. fullfile(savespec, ['NAPLS_01_',subj{s}, '_',event{e},'_mr','thetapowcorr.mat']);
  144.  
  145. cfg = [];
  146. %cfg.parameter = 'plvspctrm';
  147. cfg.parameter = 'powcorrspctrm';
  148. cfg.xlim = 'maxmin';
  149. cfg.zlim = 'maxmin';
  150. cfg.channel = 'all';
  151. figure;
  152. ft_connectivityplot(cfg,powcorr);
  153.  
  154. end
  155. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement