MWB_artfdetec_freq.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. function outdat = MWB_artfdetec_freq(cfg,data)
  2. %--------------------------------------------------------------------------
  3. % required arguments:
  4. %--------------------------------------------------------------------------
  5. % data - according to ft_preprocessing output
  6. % cfg.freq.foi
  7. %
  8. %--------------------------------------------------------------------------
  9. % optional arguments
  10. %--------------------------------------------------------------------------
  11. % cfg.visualize
  12. % cfg.visualtype = {'chan'}, or {'trial'}, or {'chan','trial'};
  13. %
  14. %--------------------------------------------------------------------------
  15. % MWB, 06/02/2014
  16. %--------------------------------------------------------------------------
  17. %--------------------------------------------------------------------------
  18. % default settings and sanity checks
  19. %--------------------------------------------------------------------------
  20. if ~isfield(cfg,'channel'), cfg.channel = 'all'; end
  21. if ~isfield(cfg,'method'), error('you have to specify the method to use: freq, kurt, corr \n'); end
  22. if ~isfield(cfg,'visualize'), plotflag = 0; else plotflag = cfg.visualize; end
  23. if ~isfield(cfg,'foi'), error('give freq-range to check - cfg.freq.foi = [lF,hF]'); end
  24. if ~isfield(cfg,'pad'), cfg.pad = 5; end
  25. %--------------------------------------------------------------------------
  26. %--------------------------------------------------------------------------
  27. % specify settings for fft via ft_freqanalysis
  28. %--------------------------------------------------------------------------
  29. fftcfg = [];
  30. fftcfg.method = 'mtmfft';
  31. fftcfg.output = 'pow';
  32. fftcfg.channel = cfg.channel;
  33. fftcfg.keeptrials = 'yes';
  34. fftcfg.pad = cfg.pad;
  35. fftcfg.foilim = cfg.foi;
  36. fftcfg.taper = 'hanning';
  37. %--------------------------------------------------------------------------
  38. %--------------------------------------------------------------------------
  39. % compute FFT
  40. %--------------------------------------------------------------------------
  41. fftdat = ft_freqanalysis(fftcfg,data);
  42. %--------------------------------------------------------------------------
  43. %--------------------------------------------------------------------------
  44. % compute relevant z-scores
  45. %--------------------------------------------------------------------------
  46. freqavgFFT = squeeze(mean(fftdat.powspctrm,3));
  47. % compute log
  48. freqavgFFT = log(freqavgFFT);
  49. % compute distribution parameters across all mean-freq values
  50. tmp.vec = reshape(freqavgFFT,[prod(size(freqavgFFT)),1]);
  51. % get freq-distr. without outliers ...
  52. tmp.clean_vec = MWB_iterative_outlierdetection(tmp.vec,2);
  53. % compute mean and std
  54. tmp.m = mean(tmp.clean_vec); tmp.s = std(tmp.clean_vec); tmp = rmfield(tmp,'clean_vec');
  55. % convert to z-scores
  56. zavgFFT = (freqavgFFT - tmp.m) ./ tmp.s;
  57. clear tmp;
  58. %--------------------------------------------------------------------------
  59. %--------------------------------------------------------------------------
  60. % compute outputs
  61. %--------------------------------------------------------------------------
  62. outdat.label = fftdat.label;
  63. % collect some information about trials
  64. if isfield(data,'sampleinfo')
  65. outdat.sampleinfo = data.sampleinfo;
  66. end
  67. if isfield(data,'cfg')
  68. if isfield(data.cfg,'trl')
  69. outdat.trl = data.cfg.trl;
  70. end
  71. end
  72. % channel markers
  73. outdat.chan.mean(:,1) = mean(zavgFFT,1);
  74. tmp.m = mean(outdat.chan.mean); tmp.s = std(outdat.chan.mean);
  75. outdat.chan.zscore(:,1) = (outdat.chan.mean - tmp.m)./ tmp.s; clear tmp;
  76. % trial markers
  77. outdat.trial.mean(:,1) = mean(zavgFFT,2);
  78. tmp.m = mean(outdat.trial.mean); tmp.s = std(outdat.trial.mean);
  79. outdat.trial.zscore(:,1) = (outdat.trial.mean - tmp.m)./ tmp.s; clear tmp;
  80. %--------------------------------------------------------------------------
  81. %--------------------------------------------------------------------------
  82. % visualize the results?
  83. %--------------------------------------------------------------------------
  84. if plotflag
  85. if ~isfield(cfg,'visualtype'), cfg.visualtype = {'chan','trial'}; end
  86. MWB_artfdetec_plotdetecres(cfg,outdat);
  87. end
  88. %--------------------------------------------------------------------------