eeg_autocorr_fftw.m 750 B

12345678910111213141516171819202122232425262728
  1. function resamp = eeg_autocorr_fftw(EEG, pct_data)
  2. if ~exist('pct_data', 'var') || isempty(pct_data)
  3. pct_data = 100;
  4. end
  5. nfft = 2^nextpow2(2*EEG.pnts-1);
  6. % calc autocorrelation
  7. fftw('planner', 'hybrid');
  8. ac = zeros(size(EEG.icaact, 1), nfft, EEG.trials);
  9. for it = 1:size(EEG.icaact, 1)
  10. X = fft(EEG.icaact(it, :, :), nfft, 2);
  11. ac(it, :, :) = abs(X).^2;
  12. end
  13. ac = ifft(mean(ac, 3), [], 2);
  14. if EEG.pnts < EEG.srate
  15. ac = [ac(:, 1:EEG.pnts, :) zeros(size(ac, 1), EEG.srate - EEG.pnts + 1)];
  16. else
  17. ac = ac(:, 1:EEG.srate + 1, :);
  18. end
  19. % normalize by 0-tap autocorrelation
  20. ac = bsxfun(@rdivide, ac(:, 1:EEG.srate + 1, :), ac(:, 1));
  21. % resample to 1 second at 100 samples/sec
  22. resamp = resample(ac', 100, EEG.srate)';
  23. resamp(:, 1) = [];