findImageOrder.m 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. function [ prs ] = findImageOrder( nimages, npulses, maxrepeats, seed )
  2. %FINDIMAGEORDER Recover the order of images in an ImageSequence stimulus
  3. % [prs ] = findNewOrder( nimages, npulses, maxrepeats, seed )
  4. %
  5. % Input:
  6. % nimages: struct containing description of stimulus
  7. % (mincontrast, maxcontrast, ncontrasts, nangles)
  8. % npulses: number of pulses recorded from the experiment
  9. % maxrepeats: only analyze until the maxrepeats-th repeat
  10. % (use 'inf' for analyzing all the repeats)
  11. % seed: seed for ran1 (should be the same as in the experiment)
  12. %
  13. % Output:
  14. % prs: order in which the images were presented, where 1
  15. % marks the presentation of a gray sceen and 2,...,nimages+1
  16. % mark the presentation of the 1st, ..., nth image of the
  17. % list
  18. nrepeats = floor(npulses/nimages);
  19. nrepeats = min(nrepeats, maxrepeats); % Obey maxrepeats
  20. prs = zeros( (nimages+1) * nrepeats, 1 );
  21. for ii = 1:nrepeats
  22. [order, seed] = shuffleOrder(nimages+1, seed);
  23. prs( (ii-1)*(nimages+1) + (1:nimages+1) ) = order;
  24. end
  25. prs = prs - 1;
  26. end
  27. function [order, seed] = shuffleOrder(nelem, seed)
  28. % Fisher-Yates shuffle (initializes the vector in a random order)
  29. order = ones(nelem, 1);
  30. order(1) = 1;
  31. for ii = 2:nelem
  32. [jj, seed] = ran1(seed);
  33. jj = floor(2 + (ii-1)*jj);
  34. order(ii) = order(jj);
  35. order(jj) = ii;
  36. end
  37. end