find_pac_nofilt.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. function [pacmat, freqvec_ph, freqvec_amp] = find_pac_nofilt (sig_pac, Fs,...
  2. measure, sig_mod, ph_freq_vec, amp_freq_vec, plt, waitbar, width, nfft,...
  3. dataname, sig_pac_name, sig_mod_name)
  4. %function [pacmat, freqvec_ph, freqvec_amp] = find_pac_nofilt (sig_pac, Fs,...
  5. % measure, sig_mod, ph_freq_vec, amp_freq_vec, plt, waitbar, width, nfft,...
  6. % dataname, sig_pac_name, sig_mod_name)
  7. %
  8. % This function calculates a matrix of PAC values using either the ESC, MI
  9. % or CFC measure.
  10. % It assumes that the input is a prefiltered signal since this function
  11. % does not include any filtering. The output is a matrix of PAC values and
  12. % a plot of this matrix (depending on the value of the 'plt' argument).
  13. % This signal can only take single vector signals as input but the
  14. % provision for multiple trials will be added (partly implemented for the
  15. % CFC measure)
  16. %
  17. % Basic function call:
  18. % function pacmat = find_pac_nofilt(sig_pac, Fs, measure)
  19. %
  20. % REQUIRED INPUTS:
  21. % sig_pac - signal suspected of containing PAC
  22. % Fs - sampling frequency
  23. % measure - measure to be used - it should be: 'esc', 'mi' or 'cfc'
  24. %
  25. % The function can be executed with many optional inputs:
  26. % function pacmat = find_pac_nofilt(sig_pac, Fs, measure, ...
  27. % sig_mod, ph_freq_vec, amp_freq_vec, plt, width, nfft, dataname,...
  28. % sig_pac_name, sig_mod_name)
  29. %
  30. % OPTIONAL INPUTS:
  31. % sig_mod - signal containing modulating frequency; default = sig_pac
  32. % ph_freq_vec - range of frequencies for modulating signal; default = 1:5:101
  33. % amp_freq_vec - range of frequencies for PAC signal; default = 1:5:101
  34. % plt - flag indicating whether the output should be plotted - it should
  35. % be 'y' or 'n'; default = 'y'
  36. % width - width of the wavelet filter; default = 7
  37. % nfft - the number of points in fft; default = 200
  38. % dataname - the name of the dataset to be included as a graph title;
  39. % default = ''
  40. % sig_pac_name - the name of sig_pac to be printed on the y-axis; default = ''
  41. % sig_mod_name - the name of sig_mod to be printed on the x-axis; default = ''
  42. %
  43. % Author: Angela Onslow, May 2010
  44. % Checks of input variables %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  45. if nargin < 4
  46. sig_mod = sig_pac;
  47. end
  48. if nargin < 5
  49. ph_freq_vec = 1:5:101;
  50. end
  51. if nargin < 6
  52. amp_freq_vec = 1:5:101;
  53. end
  54. if nargin < 7
  55. plt = 'y';
  56. end
  57. if nargin < 8
  58. waitbar = 0;
  59. end
  60. if nargin < 9
  61. width = 7;
  62. end
  63. if nargin < 10
  64. nfft = ceil(Fs/(diff(ph_freq_vec(1:2))));
  65. end
  66. if nargin < 11
  67. dataname = '';
  68. end
  69. if nargin < 12
  70. sig_pac_name = '';
  71. end
  72. if nargin < 13
  73. sig_mod_name = '';
  74. end
  75. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  76. % Set up some parameters for clarity/storage
  77. xbins = ceil((max(ph_freq_vec) - min(ph_freq_vec))/(diff(ph_freq_vec(1:2))));
  78. ybins = ceil((max(amp_freq_vec) - min(amp_freq_vec))/(diff(amp_freq_vec(1:2))));
  79. cent_freq_vec = zeros(xbins,1);
  80. cent_freq_vec2 = zeros(ybins, 1);
  81. if isa(sig_pac, 'cell')
  82. % Data represents filtered signals
  83. num_trials = size(sig_pac{1,1},2);
  84. else
  85. % Data represents signals
  86. num_trials = size(sig_pac,2);
  87. end
  88. if waitbar == 1
  89. counter = 0;
  90. countermax = xbins*ybins;
  91. fprintf('\nCalculating PAC values\n');
  92. end
  93. for i =1:xbins
  94. upper_bin = min(ph_freq_vec)+i*(diff(ph_freq_vec(1:2)));
  95. lower_bin = upper_bin-(diff(ph_freq_vec(1:2)));
  96. cent_freq_vec(i) = lower_bin + floor((upper_bin- lower_bin)/2);
  97. end
  98. for i =1:ybins
  99. upper_bin = min(amp_freq_vec)+i*(diff(amp_freq_vec(1:2)));
  100. lower_bin = upper_bin-(diff(amp_freq_vec(1:2)));
  101. cent_freq_vec2(i) = lower_bin + floor((upper_bin- lower_bin)/2);
  102. end
  103. freqvec_amp = cent_freq_vec2;
  104. freqvec_ph = cent_freq_vec;
  105. % Calculate PAC measures %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  106. if (strcmp(measure, 'esc') || strcmp(measure, 'mi'))
  107. pacmat = zeros(ybins, xbins);
  108. for i = 1:ybins
  109. for j = 1:xbins
  110. % Calculate matrix of PAC values
  111. if strcmp(measure, 'esc')
  112. pacmat(i,j) = esc_measure(sig_mod{1,j}, ...
  113. sig_pac{1,i}, 'y');
  114. end
  115. if strcmp(measure, 'mi')
  116. % Pacmat full of raw mi values, not yet normalized
  117. pacmat(i,j) = mi_measure(sig_mod{1,j}, sig_pac{1,i});
  118. end
  119. % Display current computational step to user
  120. if waitbar ==1
  121. counter = counter+1;
  122. if counter == 1
  123. fprintf('%03i%% ', floor((counter/countermax)*100));
  124. else
  125. fprintf('\b\b\b\b\b%03i%% ', floor((counter/countermax)*100));
  126. end
  127. if counter == countermax
  128. fprintf('\n');
  129. end
  130. end
  131. end
  132. end
  133. end
  134. if strcmp(measure, 'cfc')
  135. % Calculate matrix of PAC values (these have been averaged over trials)
  136. [pacmat, freqvec_ph, freqvec_amp] = cfc_measure(sig_pac, ...
  137. sig_mod, 'y',ph_freq_vec, freqvec_amp, Fs, nfft, width, waitbar);
  138. end
  139. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  140. % Plots %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  141. if strcmp(plt, 'y')
  142. pac_plot_fun(pacmat, freqvec_ph, freqvec_amp, measure, sig_pac_name,...
  143. sig_mod_name, dataname, sig_pac, Fs, sig_mod);
  144. end
  145. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%