cm_automatic_IC_detection_20170919.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. function [iclabels] = cm_automatic_IC_detection_20170919(data,icadata)
  2. % automatically detects IC most likely representing eye blinks, eye
  3. % movements, muscle artifacts related to eye blinks and eye movements, as
  4. % well as ECG artifacts
  5. %% get most likely eye & heart components
  6. ica = cell2mat(icadata.trial);
  7. dat = cell2mat(data.trial);
  8. %% find BLINK component
  9. % get ior channel
  10. ior = dat(find(strcmp(data.label,'VEOG')),:)';
  11. % correlation between ior and ICs
  12. for j = 1:size(ica,1)
  13. r_bli(j,1) = corr(ica(j,:)',ior);
  14. end; clear j
  15. % get "significant" correlation
  16. bli = cm_get_sig_corr(r_bli,2);
  17. % clear variables
  18. clear r_bli
  19. %% find MOVE component
  20. % get EOG channels
  21. eog = dat(find(strcmp(data.label,'HEOG')),:)';
  22. % correlation between EOG and ICs
  23. for j = 1:size(ica,1)
  24. r_mov(j,1) = corr(ica(j,:)',eog);
  25. end; clear j
  26. % delete blink component
  27. excl = [];
  28. if bli(1,3) == 1
  29. excl = [excl bli(1,1)];
  30. end
  31. % get "significant" correlation
  32. mov = cm_get_sig_corr(r_mov,2,excl);
  33. % clear variables
  34. clear r_mov excl
  35. %% find MOVE muscle spikes
  36. % prepare EOG channel data
  37. eog = [abs(diff(eog)); 0];
  38. % correlation between EOG and ICs
  39. for j = 1:size(ica,1)
  40. r_spk(j,1) = corr(ica(j,:)',eog);
  41. end; clear j
  42. % delete blink & move components
  43. excl = [];
  44. if bli(1,3) == 1
  45. excl = [excl bli(1,1)];
  46. end
  47. if mov(1,3) == 1
  48. excl = [excl mov(1,1)];
  49. end
  50. % get "significant" correlations
  51. spk = cm_get_sig_corr(r_spk,2,excl);
  52. % clear variables
  53. clear r_spk excl
  54. %% find BLINK muscle component
  55. % prepare components
  56. ica2 = ica.^2;
  57. % correlation between ior and squared ICs
  58. for j = 1:size(ica,1)
  59. r_msc(j,1) = abs(corr(ica2(j,:)',ior));
  60. end; clear j
  61. % delete blink & move components
  62. excl = [];
  63. if bli(1,3) == 1
  64. excl = [excl bli(1,1)];
  65. end
  66. if mov(1,3) == 1
  67. excl = [excl mov(1,1)];
  68. end
  69. if spk(1,3) == 1
  70. excl = [excl spk(1,1)];
  71. end
  72. % get "significant" correlations
  73. msc = cm_get_sig_corr(r_msc,2,excl);
  74. % clear variables
  75. clear ica2 excl
  76. %% find HEART component
  77. % get ecg
  78. ecg = dat(find(strcmp(data.label,'ECG')),:)';
  79. if ~prod(size(ecg))==0
  80. % correlation between ECG and ICs
  81. for j = 1:size(ica,1)
  82. r_hrt(j,1) = abs(corr(ica(j,:)',ecg));
  83. end; clear j
  84. % delete blink & move components
  85. excl = [];
  86. if bli(1,3) == 1
  87. excl = [excl bli(1,1)];
  88. end
  89. if mov(1,3) == 1
  90. excl = [excl mov(1,1)];
  91. end
  92. if spk(1,3) == 1
  93. excl = [excl spk(1,1)];
  94. end
  95. if msc(1,3) == 1
  96. excl = [excl msc(1,1)];
  97. end
  98. % get "significant" correlations
  99. hrt = cm_get_sig_corr(r_hrt,2,excl);
  100. % clear variables
  101. clear r_hrt ica2
  102. % existence of automatic hrt detection
  103. hrt_exist = 1;
  104. else
  105. % existence of automatic hrt detection
  106. hrt_exist = 0;
  107. end
  108. %% generate fields for ICA labels
  109. iclabels.nol = [];
  110. iclabels.oks = [];
  111. % blink components
  112. if bli(1,3) == 1
  113. iclabels.bli = bli(1,1);
  114. else
  115. iclabels.bli = [];
  116. end
  117. if msc(1,3) == 1
  118. iclabels.bli = [iclabels.bli msc(1,1)];
  119. end
  120. % eye movement components
  121. if mov(1,3) == 1
  122. iclabels.mov = mov(1,1);
  123. else
  124. iclabels.mov = [];
  125. end
  126. if spk(1,3) == 1
  127. iclabels.mov = [iclabels.mov spk(1,1)];
  128. end
  129. iclabels.tng = [];
  130. % ecg component
  131. if hrt_exist == 1
  132. if hrt(1,3) == 1
  133. iclabels.hrt = hrt(1,1);
  134. else
  135. iclabels.hrt = [];
  136. end
  137. else
  138. iclabels.hrt = [];
  139. end
  140. iclabels.art = [];
  141. iclabels.elc = [];
  142. iclabels.ref = [];
  143. iclabels.unc = [];
  144. % keep correlations
  145. iclabels.cbli = bli;
  146. iclabels.cblm = msc;
  147. iclabels.cmov = mov;
  148. iclabels.cspk = spk;
  149. if hrt_exist == 1
  150. iclabels.chrt = hrt;
  151. else
  152. iclabels.chrt = [];
  153. end
  154. % documentation
  155. iclabels.version = '20141217';
  156. %% subfunction: "get significant correlation"
  157. function ic = cm_get_sig_corr(r_ic,crit,excl)
  158. % Fisher Z transform
  159. Z = cm_fisher_Z_20130426(r_ic);
  160. % exclude data
  161. if exist('excl','var')
  162. Z(excl) = NaN;
  163. end
  164. % criterion
  165. mn = nanmean(Z);
  166. sd = nanstd(Z,1);
  167. % set up blink table
  168. ic(:,1) = 1:length(r_ic);
  169. ic(:,2) = r_ic;
  170. ic(:,3) = Z;
  171. ic(:,4) = abs(Z);
  172. % exclude data
  173. if exist('excl','var')
  174. ic(excl,4) = 0;
  175. end
  176. % sortrows
  177. ic = sortrows(ic,-4);
  178. % check "significance"
  179. for j = 1:length(r_ic);
  180. if ic(j,2) < 0
  181. ic(j,3) = (ic(j,3) < mn - crit*sd);
  182. elseif ic(j,2) > 0
  183. ic(j,3) = (ic(j,3) > mn + crit*sd);
  184. end
  185. end; clear j
  186. % delete Z values
  187. ic(:,4) = [];