Home > NoiseTools > nt_phase_scramble.m

# nt_phase_scramble

## PURPOSE

y=nt_phase_scramble(x) - scramble time but preserve autocorrelation

## SYNOPSIS

function y=nt_phase_scramble(x)

## DESCRIPTION

    y=nt_phase_scramble(x) - scramble time but preserve autocorrelation

      y: scrambled data

      x: data to scramble (time X channels, or time X channels X trials

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

## SOURCE CODE

    0001 function y=nt_phase_scramble(x)
    0002 %y=nt_phase_scramble(x) - scramble time but preserve autocorrelation
    0003 %
    0004 %  y: scrambled data
    0005 %
    0006 %  x: data to scramble (time X channels, or time X channels X trials
    0007 %
    0008
    0009 if nargin<1; error('!'); end
    0010 [nsample,nchan,ntrial]=size(x);
    0011 if rem(nsample,2)~=0 % pad to multiple of 2
    0012     x(nsample+1,:,:)=x(nsample,:,:);
    0013 end
    0014
    0015 for iTrial=1:ntrial
    0016     xx=x(:,:,iTrial);
    0017     yy=fft(xx);
    0018     if 1
    0019          phasor=exp(-2*pi*j*rand(size(x,1)/2-1,1)); % same for all channels
    0020          yy=yy.*repmat([1;phasor; 1; conj(flipud(phasor))],1,nchan);
    0021     else
    0022         phasor=exp(-2*pi*j*rand(size(x,1)/2-1,nchan)); % channel-specific
    0023         yy=yy.*[ones(1,nchan);phasor; ones(1,nchan); conj(flipud(phasor))];
    0024     end
    0025     yy=real(ifft(yy));
    0026     y(:,:,iTrial)=yy;
    0027 end
    0028
    0029 y=y(1:nsample,:,:); % remove padding if need
    0030