spm_phase_shuffle.m 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. function [y] = spm_phase_shuffle(x,n)
  2. % phase-shuffling of a vector
  3. % FORMAT [y] = spm_phase_shuffle(x,[n])
  4. % x - data matrix (time-series in columns)
  5. % n - optional window length for windowed shuffling
  6. %__________________________________________________________________________
  7. % Copyright (C) 2007-2015 Wellcome Trust Centre for Neuroimaging
  8. % Karl Friston
  9. % $Id: spm_phase_shuffle.m 6654 2015-12-22 12:55:36Z spm $
  10. try
  11. % randomise phase - WFT
  12. %----------------------------------------------------------------------
  13. k = 1:fix(n/2);
  14. for i = 1:size(x,2);
  15. C = spm_wft(x(:,i),k,n);
  16. W = abs(C).*exp(1i*angle(C(randperm(size(C,1)),:)));
  17. y(:,i) = spm_iwft(W,k,n)';
  18. end
  19. catch
  20. % randomise phase - FT
  21. %----------------------------------------------------------------------
  22. n = size(x,1);
  23. s = fft(x);
  24. i = 2:ceil(n/2);
  25. r = rand(length(i),size(x,2))*2*pi - pi;
  26. p = zeros(n,size(x,2));
  27. p(i,:) = r;
  28. p(n - i + 2,:) = -r;
  29. s = abs(s).*exp(1i*p);
  30. y = real(ifft(s));
  31. end