spm_eeg_avgfreq.m 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. function D = spm_eeg_avgfreq(S)
  2. % Average a TF-dataset over frequency to get a time-domain dataset
  3. % FORMAT D = spm_eeg_avgfreq(S)
  4. %
  5. % S - input struct
  6. % fields of S:
  7. % D - MEEG object or filename of M/EEG mat-file with epoched data
  8. % freqwin - frequency window to average over [default: [-Inf, Inf]]
  9. % prefix - prefix for the output file [default: 'P']
  10. %
  11. % Output:
  12. % D - MEEG object
  13. %__________________________________________________________________________
  14. % Copyright (C) 2012-2017 Wellcome Trust Centre for Neuroimaging
  15. % Vladimir Litvak
  16. % $Id: spm_eeg_avgfreq.m 7132 2017-07-10 16:22:58Z guillaume $
  17. SVNrev = '$Rev: 7132 $';
  18. %-Startup
  19. %--------------------------------------------------------------------------
  20. spm('FnBanner', mfilename, SVNrev);
  21. spm('FigName','Average over frequency'); spm('Pointer','Watch');
  22. if ~isfield(S, 'prefix'), S.prefix = 'P'; end
  23. if ~isfield(S, 'freqwin'), S.freqwin = [-Inf Inf]; end
  24. D = spm_eeg_load(S.D);
  25. if ~strncmpi(D.transformtype,'TF',2)
  26. error('This function only works on TF datasets.');
  27. end
  28. freqind = D.indfrequency(min(S.freqwin)):D.indfrequency(max(S.freqwin));
  29. if isempty(freqind) || any(isnan(freqind))
  30. error('Selected frequency window is invalid.');
  31. end
  32. %-Generate new MEEG object with new files
  33. %--------------------------------------------------------------------------
  34. Dnew = clone(D, [S.prefix fname(D)], [D.nchannels D.nsamples D.ntrials]);
  35. %-Averaged data
  36. %--------------------------------------------------------------------------
  37. spm_progress_bar('Init', D.ntrials, 'Trials processed');
  38. if D.ntrials > 100, Ibar = floor(linspace(1, D.ntrials, 100));
  39. else Ibar = 1:D.ntrials; end
  40. for i = 1:D.ntrials
  41. Dnew(:, :, i) = spm_squeeze(mean(D(:, freqind, :, i), 2), 2);
  42. if any(Ibar == i), spm_progress_bar('Set', i); end
  43. end
  44. spm_progress_bar('Clear');
  45. %-Save the new M/EEG dataset
  46. %--------------------------------------------------------------------------
  47. Dnew = Dnew.history(mfilename, S);
  48. save(Dnew);
  49. D = Dnew;
  50. %-Cleanup
  51. %--------------------------------------------------------------------------
  52. fprintf('%-40s: %30s\n','Completed',spm('time')); %-#
  53. spm('FigName','Average over frequency: done'); spm('Pointer','Arrow');