findNewOrder.m 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. function [ cta, ctb, totalflashes, prs ] = findNewOrder( stimdesc, npulses, maxrepeats, seed )
  2. %FINDNEWORDER Recover the order of cta,ctb from the new SubUnitFlash stimulus
  3. % The new stimulus only has radial components and more
  4. % parameters such as range of radial search, number of angles,
  5. % number of contrasts, etc. It also uses a different shuffling which
  6. % might give a different ordering than the one Michael has used
  7. % before.
  8. %
  9. % [ cta, ctb, totalflashes, prs ] = findNewOrder( stimdesc, npulses, maxrepeats, seed )
  10. %
  11. % Input:
  12. % stimdesc: struct containing description of stimulus
  13. % (mincontrast, maxcontrast, ncontrasts, nangles)
  14. % npulses: number of pulses recorded from the experiment
  15. % maxrepeats: only analyze until the maxrepeats-th repeat
  16. % (use 'inf' for analyzing all the repeats)
  17. % seed: seed for ran1 (should be the same as in the experiment)
  18. %
  19. % Output:
  20. % cta: contrast at subtiles A, ctb: contrast at subtiles B
  21. % totalflashes: total number of flashes in one repeat
  22. % prs: order in which the contrasts are used
  23. %
  24. [cta, ctb] = genLists(stimdesc);
  25. totalflashes = numel(cta);
  26. nrepeats = floor(npulses/totalflashes);
  27. nrepeats = min(nrepeats, maxrepeats); % Obey maxrepeats
  28. prs = zeros( totalflashes * nrepeats, 1 );
  29. for ii = 1:nrepeats
  30. [order, seed] = shuffleOrder(totalflashes, seed);
  31. prs( (ii-1)*totalflashes + (1:totalflashes) ) = order;
  32. end
  33. end
  34. function [order, seed] = shuffleOrder(nelem, seed)
  35. % Fisher-Yates shuffle (initializes the vector in a random order)
  36. order = ones(nelem, 1);
  37. order(2) = 2;
  38. for ii = 3:nelem
  39. [jj, seed] = ran1(seed);
  40. jj = floor(2 + (ii-1)*jj);
  41. order(ii) = order(jj);
  42. order(jj) = ii;
  43. end
  44. end
  45. function [cta, ctb] = genLists(stimdesc)
  46. nAngles = stimdesc.nangles;
  47. minCon = stimdesc.mincontrast;
  48. maxCon = stimdesc.maxcontrast;
  49. nContrasts = stimdesc.ncontrasts;
  50. angles = (0:(nAngles-1)) * 2*pi/nAngles;
  51. cosAngs = cos(angles);
  52. sinAngs = sin(angles);
  53. radii = minCon + (0:(nContrasts-1)) * (maxCon - minCon)/(nContrasts-1);
  54. %radii = (0:(nContrasts)) * (maxCon - minCon)/(2*nContrasts);
  55. cta = zeros( sum(radii >= 1e-5)*nAngles + 1, 1 );
  56. ctb = zeros( sum(radii >= 1e-5)*nAngles + 1, 1 );
  57. idx = 2; % Start from 2nd element of (cta,ctb) [first element = (0,0)]
  58. for ii = 1:nAngles
  59. cosAng = cosAngs(ii);
  60. sinAng = sinAngs(ii);
  61. for jj = 1:nContrasts
  62. r = radii(jj);
  63. if r < 1e-5; continue; end
  64. cta(idx) = r * cosAng;
  65. ctb(idx) = r * sinAng;
  66. idx = idx + 1;
  67. end
  68. end
  69. end