find_pac_shf_several_CutGlue_segs.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. function pac = find_pac_shf_several_CutGlue_segs (SIG_pac, Fs, measure, ...
  2. SIG_mod, ph_freq_vec, amp_freq_vec)
  3. % Xiaxia
  4. % This function calculates a matrix of PAC values using either the ESC, MI(MVL)
  5. % or CFC measure.
  6. % It uses shuffled datasets to conduct a significance analysis of the PAC
  7. % values found
  8. % INPUTS:
  9. % sig_pac - high frequency,several cut-glue row signals;
  10. % Fs - sampling frequency;
  11. % measure - measure to be used - it should be: 'esc', 'mi' or 'cfc';
  12. % SIG_mod - low frequency,several cut-glue row signal;
  13. % ph_freq_vec - range of frequencies for low signal; for example 1:1:20
  14. % amp_freq_vec - range of frequencies for high signal; for example
  15. % 30:2:200
  16. % Output
  17. %pac.pacmat=pacmat;
  18. %pac.freqvec_ph---the phase frequency band;
  19. %pac.freqvec_amp---the amplitude frequency band;
  20. %pac.shf_data_mean---shuffle mean;
  21. %pac.shf_data_std----shuffle std;
  22. %pac.relat_mi-----Zscore of the pac.pacmat
  23. % Author: Angela Onslow, May 2010
  24. % set some paremeters:
  25. waitbar = 1;% waitbar - display progress in the command window; suggest 1 (Xiaxia)
  26. width = 7;% width - width of the wavelet filter; sugguest 7 (Xiaxia)
  27. nfft = ceil(Fs/(diff(ph_freq_vec(1:2)))); % nfft - the number of points in fft;
  28. num_shf = 50; % num_shf - the number of shuffled data sets to use during significancetesting; suggest 50 (Xiaxia)
  29. alpha = 0.05; % alpha - significance value to use; default = 0.05
  30. % Set up some parameters for clarity
  31. xbins = ceil((max(ph_freq_vec) - min(ph_freq_vec))/(diff(ph_freq_vec(1:2))));
  32. ybins = ceil((max(amp_freq_vec) - min(amp_freq_vec))/(diff(amp_freq_vec(1:2))));
  33. alpha = alpha/(xbins*ybins); % Uncomment to use Bonferonni Correction
  34. sig_pac=SIG_pac';
  35. sig_mod=SIG_mod';
  36. % Filtering phase and amplitude and glue together to obtain a long dataset
  37. if (strcmp(measure, 'esc')) ||(strcmp(measure, 'mi'))
  38. [filt_sig_mod, filt_sig_pac] =filt_signalsWAV_several_CutGlue_segs(sig_pac, sig_mod, Fs, ...
  39. ph_freq_vec, amp_freq_vec, measure, width);
  40. end
  41. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  42. % Create shuffled datasets and distribution of PAC values %%%%%%%%%%%%%%%%%
  43. if num_shf ~= 0
  44. if waitbar == 1
  45. fprintf('\nCreating shuffled data sets\n');
  46. end
  47. for s = 1:num_shf
  48. if strcmp(measure, 'esc')
  49. shuffled_sig_amp = shuffle_esc(filt_sig_pac, Fs);
  50. shf_pacmat_final(s,:,:) = find_pac_nofilt(shuffled_sig_amp, Fs, measure, filt_sig_mod, ph_freq_vec, amp_freq_vec,'n');
  51. end
  52. if strcmp(measure, 'mi')
  53. shuffled_sig_amp = shuffle_esc(filt_sig_pac, Fs);
  54. shf_pacmat_final(s,:,:) = find_pac_nofilt(shuffled_sig_amp, Fs, measure, filt_sig_mod, ph_freq_vec, amp_freq_vec,'n');
  55. end
  56. if strcmp(measure, 'cfc')
  57. shuffled_sig1 = shuffle_esc(sig_pac, Fs);
  58. shf_pacmat_final(s,:,:) = find_pac_nofilt(shuffled_sig1, Fs,measure, sig_mod, ph_freq_vec, amp_freq_vec,'n', 0, width, nfft);
  59. end
  60. % Display current computational step to user
  61. if waitbar == 1
  62. if s == 1
  63. fprintf('%03i%% ', floor((s/num_shf)*100));
  64. else
  65. fprintf('\b\b\b\b\b%03i%% ', floor((s/num_shf)*100));
  66. end
  67. if s == num_shf
  68. fprintf('\n');
  69. end
  70. end
  71. end
  72. %Find mean and standard deviation of shuffled data sets
  73. if strcmp(measure, 'mi')
  74. for i =1:ybins
  75. for j=1:xbins
  76. [shf_data_mean(i,j), shf_data_std(i,j)] = normfit(shf_pacmat_final(:,i,j));
  77. end
  78. end
  79. else
  80. shf_data_mean = squeeze (mean (shf_pacmat_final, 1));
  81. shf_data_std = squeeze (std (shf_pacmat_final, 1));
  82. end
  83. end
  84. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  85. % Calculate PAC measures %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  86. if strcmp(measure, 'esc')
  87. [pacmat, freqvec_ph, freqvec_amp] = find_pac_nofilt(filt_sig_pac, Fs, measure, filt_sig_mod, ph_freq_vec, amp_freq_vec, 'n', 1);
  88. end
  89. if strcmp(measure, 'mi')
  90. [pacmat, freqvec_ph, freqvec_amp] = find_pac_nofilt(filt_sig_pac, Fs, measure, filt_sig_mod, ph_freq_vec, amp_freq_vec, 'n', 1);
  91. end
  92. if strcmp(measure, 'cfc')
  93. [pacmat, freqvec_ph, freqvec_amp] = find_pac_nofilt(sig_pac, Fs, measure, sig_mod, ph_freq_vec, amp_freq_vec, 'n', 1, width, nfft);
  94. end
  95. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  96. % Compute significance %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  97. if num_shf ~= 0
  98. for i = 1:size(pacmat,1)
  99. for j = 1:size(pacmat,2)
  100. [h, p] = my_sig_test(pacmat(i,j), squeeze(shf_pacmat_final(:,i,j)), alpha);
  101. if h == 0
  102. pacmat(i,j) = 0;
  103. end
  104. end
  105. end
  106. pac.shf_data_mean=shf_data_mean;
  107. pac.shf_data_std=shf_data_std;
  108. end
  109. pac.pacmat=pacmat;
  110. pac.freqvec_ph=freqvec_ph;
  111. pac.freqvec_amp=freqvec_amp;
  112. pac=relat_shaf(pac);