spm_eeg_avgtime.m 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. function D = spm_eeg_avgtime(S)
  2. % Average a TF-dataset over time to get a spectrum dataset
  3. % FORMAT D = spm_eeg_avgtime(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. % timewin - time window to average over {in PST ms} [default: [-Inf,Inf]]
  9. % prefix - prefix for the output file [default: 'S']
  10. %
  11. %
  12. % Output:
  13. % D - MEEG object
  14. %
  15. %__________________________________________________________________________
  16. % Copyright (C) 2012-2017 Wellcome Trust Centre for Neuroimaging
  17. % Vladimir Litvak
  18. % $Id: spm_eeg_avgtime.m 7132 2017-07-10 16:22:58Z guillaume $
  19. SVNrev = '$Rev: 7132 $';
  20. %-Startup
  21. %--------------------------------------------------------------------------
  22. spm('FnBanner', mfilename, SVNrev);
  23. spm('FigName','Average over time'); spm('Pointer','Watch');
  24. if ~isfield(S, 'prefix'), S.prefix = 'P'; end
  25. if ~isfield(S, 'timewin'), S.timewin = [-Inf Inf]; end
  26. D = spm_eeg_load(S.D);
  27. if ~strncmpi(D.transformtype,'TF',2)
  28. error('This function only works on TF datasets.');
  29. end
  30. timeind = D.indsample(1e-3*(min(S.timewin))):D.indsample(1e-3*(max(S.timewin)));
  31. if isempty(timeind) || any(isnan(timeind))
  32. error('Selected time window is invalid.');
  33. end
  34. %-Generate new MEEG object with new files
  35. %--------------------------------------------------------------------------
  36. Dnew = clone(D, [S.prefix fname(D)], [D.nchannels D.nfrequencies 1 D.ntrials]);
  37. %-Averaged data
  38. %--------------------------------------------------------------------------
  39. spm_progress_bar('Init', D.ntrials, 'Trials processed');
  40. if D.ntrials > 100, Ibar = floor(linspace(1, D.ntrials, 100));
  41. else Ibar = 1:D.ntrials; end
  42. for i = 1:D.ntrials
  43. Dnew(:, :, :, i) = spm_squeeze(mean(D(:, :, timeind, i), 3), 3);
  44. if D.trialonset(i) ~= 0
  45. Dnew = trialonset(Dnew, i, D.trialonset(i)+ mean(D.time([timeind(1) timeind(end)])));
  46. end
  47. if any(Ibar == i), spm_progress_bar('Set', i); end
  48. end
  49. Dnew = timeonset(Dnew, mean(D.time([timeind(1) timeind(end)])));
  50. spm_progress_bar('Clear');
  51. %-Save the new M/EEG dataset
  52. %--------------------------------------------------------------------------
  53. Dnew = Dnew.history(mfilename, S);
  54. save(Dnew);
  55. D = Dnew;
  56. %-Cleanup
  57. %--------------------------------------------------------------------------
  58. fprintf('%-40s: %30s\n','Completed',spm('time')); %-#
  59. spm('FigName','Average over time: done'); spm('Pointer','Arrow');