MWB_artfdetec_kurt.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. function outdat = MWB_artfdetec_kurt(cfg,data)
  2. %--------------------------------------------------------------------------
  3. % required arguments:
  4. %--------------------------------------------------------------------------
  5. % data - according to ft_preprocessing output
  6. %
  7. %--------------------------------------------------------------------------
  8. % optional arguments
  9. %--------------------------------------------------------------------------
  10. % cfg.visualize
  11. % cfg.visualtype = {'chan'}, or {'trial'}, or {'chan','trial'};
  12. %
  13. %--------------------------------------------------------------------------
  14. % MWB, 06/02/2014
  15. %--------------------------------------------------------------------------
  16. %--------------------------------------------------------------------------
  17. % default settings and sanity checks
  18. %--------------------------------------------------------------------------
  19. if ~isfield(cfg,'channel'), cfg.channel = 'all'; end
  20. if ~isfield(cfg,'method'), error('you have to specify the method to use: freq, kurt, corr \n'); end
  21. if ~isfield(cfg,'visualize'), plotflag = 0; else plotflag = cfg.visualize; end
  22. if ~isfield(cfg,cfg.method), cfg.(cfg.method) = []; end
  23. % method_cfg = cfg.(cfg.method);
  24. % if ~isfield(method_cfg,'foi'), error('give freq-range to check - cfg.freq.foi = [lF,hF]'); end
  25. % if ~isfield(method_cfg,'pad'), method_cfg.pad = 5; end
  26. %--------------------------------------------------------------------------
  27. %--------------------------------------------------------------------------
  28. % collect some info ...
  29. %--------------------------------------------------------------------------
  30. nr.trl = length(data.trial);
  31. %--------------------------------------------------------------------------
  32. %--------------------------------------------------------------------------
  33. % find selected channels ...
  34. %--------------------------------------------------------------------------
  35. selchan = ft_channelselection(cfg.channel,data.label);
  36. for iC = 1:length(selchan)
  37. tmpidx(iC,1) = find(strcmp(selchan{iC},data.label));
  38. end
  39. for iT = 1:nr.trl
  40. data.trial{iT} = data.trial{iT}(tmpidx,:);
  41. end
  42. data.label = data.label(tmpidx);
  43. clear tmpidx iT iC selchan;
  44. %--------------------------------------------------------------------------
  45. %--------------------------------------------------------------------------
  46. % compute kurtosis
  47. %--------------------------------------------------------------------------
  48. fprintf('computing kurtosis \n');
  49. for iT = 1:nr.trl;
  50. tmp = data.trial{iT};
  51. avgKURT(iT,:) = kurtosis(tmp,0,2);
  52. end
  53. clear tmp;
  54. %--------------------------------------------------------------------------
  55. %--------------------------------------------------------------------------
  56. % compute relevant z-scores
  57. %--------------------------------------------------------------------------
  58. % compute log
  59. avgKURT = log(avgKURT);
  60. % compute distribution parameters across all mean-freq values
  61. tmp.vec = reshape(avgKURT,[prod(size(avgKURT)),1]);
  62. % get freq-distr. without outliers ...
  63. tmp.clean_vec = MWB_iterative_outlierdetection(tmp.vec,2);
  64. % compute mean and std
  65. tmp.m = mean(tmp.clean_vec); tmp.s = std(tmp.clean_vec); tmp = rmfield(tmp,'clean_vec');
  66. % convert to z-scores
  67. zavgKURT = (avgKURT - tmp.m) ./ tmp.s;
  68. clear tmp;
  69. %--------------------------------------------------------------------------
  70. %--------------------------------------------------------------------------
  71. % compute outputs
  72. %--------------------------------------------------------------------------
  73. outdat.label = data.label;
  74. % collect some information about trials
  75. if isfield(data,'sampleinfo')
  76. outdat.sampleinfo = data.sampleinfo;
  77. end
  78. if isfield(data,'cfg')
  79. if isfield(data.cfg,'trl')
  80. outdat.trl = data.cfg.trl;
  81. end
  82. end
  83. % channel markers
  84. outdat.chan.mean(:,1) = sum(zavgKURT,1);
  85. tmp.m = mean(outdat.chan.mean); tmp.s = std(outdat.chan.mean);
  86. outdat.chan.zscore(:,1) = (outdat.chan.mean - tmp.m)./ tmp.s; clear tmp;
  87. % trial markers
  88. outdat.trial.mean(:,1) = sum(zavgKURT,2);
  89. tmp.m = mean(outdat.trial.mean); tmp.s = std(outdat.trial.mean);
  90. outdat.trial.zscore(:,1) = (outdat.trial.mean - tmp.m)./ tmp.s; clear tmp;
  91. %--------------------------------------------------------------------------
  92. %--------------------------------------------------------------------------
  93. % visualize the results?
  94. %--------------------------------------------------------------------------
  95. if plotflag
  96. if ~isfield(cfg,'visualtype'), cfg.visualtype = {'chan','trial'}; end
  97. MWB_artfdetec_plotdetecres(cfg,outdat);
  98. end
  99. %--------------------------------------------------------------------------