shuffle_esc.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. function shf_sig = shuffle_esc(sig, Fs)
  2. % function shf_sig = shuffle_esc(sig, Fs)
  3. %
  4. % This function shuffles a signal using random insertion type shuffling.
  5. % The signal 'sig' is divided into a number of sections, equal to the
  6. % number of seconds of data or 1000, which ever is smaller. These sections
  7. % are of random lengths, chosen from a uniform distribution. The sections
  8. % are then randomly re-ordered.
  9. % The input signal 'sig' must contain several seconds on data (>2) and must
  10. % be a vector (i.e. number of trials = 1) or a cell array containing
  11. % multiple signals
  12. %
  13. % INPUTS:
  14. % sig - input signal which is to be shuffled, passed as a column vector
  15. % Fs - sampling frequency of 'sig', in Hz
  16. %
  17. % OUTPUTS:
  18. % shf_sig - either a vector or a cell array depending on the input 'sig'.
  19. % When used within find_pac_shf.m this output is of the same dimension as
  20. % filt_sig_mod and filt_sig_pac: number of cells - number of frequency bins
  21. % and each cell element is a matrix(num_data_points, num_trials)
  22. %
  23. % Author: Rafal Bogacz, Angela Onslow, May 2010
  24. sig_type = class(sig);
  25. if iscell(sig)
  26. ybins = size(sig,1);
  27. xbins = size(sig,2);
  28. shf_sig_cell = cell(size(sig));
  29. num_sec = ceil((size(sig{1,1},1))/Fs);
  30. else
  31. shf_sig = [];
  32. num_sec = ceil((size(sig,1))/Fs);
  33. end
  34. if num_sec > 1000
  35. num_sec = 1000;
  36. end
  37. switch sig_type
  38. case 'double'
  39. % Choose num_sec random 'cut' positions
  40. dpsplit = ceil(size(sig,1).*rand(num_sec,1));
  41. % Arrange these in ascending order
  42. dpsplit = sort (dpsplit);
  43. start(1)=1;
  44. start(2:num_sec)=dpsplit(1:num_sec-1);
  45. ending(1:num_sec-1)=dpsplit(1:num_sec-1)-1;
  46. ending(num_sec) = size(sig,1);
  47. order = randperm(num_sec); % Function randperm: Random permutation
  48. for c = 1:num_sec
  49. %shuffle the signal
  50. shf_sig = [shf_sig; sig(start(order(c)):ending(order(c)),:)];
  51. end
  52. case 'cell'
  53. for i = 1:ybins
  54. for j = 1:xbins
  55. current_sig = sig{i,j};
  56. % Choose num_sec random 'cut' positions
  57. dpsplit = ceil(size(sig{1,1},1).*rand(num_sec,1));
  58. % Arrange these in ascending order
  59. dpsplit = sort (dpsplit);
  60. start(1)=1;
  61. start(2:num_sec)=dpsplit(1:num_sec-1);
  62. ending(1:num_sec-1)=dpsplit(1:num_sec-1)-1;
  63. ending(num_sec) = size(sig{1,1},1);
  64. order = randperm(num_sec);
  65. for c = 1:num_sec
  66. %shuffle the signal
  67. shf_sig_cell{i,j} = [shf_sig_cell{i,j}; current_sig(start(order(c)):ending(order(c)),:)];
  68. end
  69. end
  70. end
  71. shf_sig = shf_sig_cell;
  72. end