findLocallySparseSubunitFlashOrder.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. function [stimMat, cta, ctb, screenMat, spaceVecX, spaceVecY] =...
  2. findLocallySparseSubunitFlashOrder(nframes, stimdesc)
  3. %FINDLOCALLYSPARSESUBUNITFLASHORDER
  4. Nx = stimdesc.Nx;
  5. Ny = stimdesc.Ny;
  6. screenNx=Nx*stimdesc.nstixelsx;
  7. screenNy=Ny*stimdesc.nstixelsy;
  8. %think of screen coordinates again
  9. spaceVecX=stimdesc.lmargin+0.5+stimdesc.stixelwidth*(0:screenNx-1)+stimdesc.stixelwidth/2;
  10. spaceVecY=stimdesc.bmargin+0.5+stimdesc.stixelheight*(0:screenNy-1)+stimdesc.stixelheight/2;
  11. gapx=stimdesc.gapwidth; gapy=stimdesc.gapheight;
  12. seedlocation=stimdesc.seedlocation;
  13. seedcombination=stimdesc.seedcombination;
  14. locationlist = (0:Nx*Ny-1)';
  15. neighbourlist = getneighbourlist(Nx, Ny, gapx, gapy);
  16. [cta, ctb] = genLists(stimdesc); ncombis=numel(cta);
  17. stimMat=zeros(nframes, Ny*Nx);
  18. possibleXYlocs = zeros(floor(numel(locationlist)/min(cellfun(@numel,neighbourlist))),1);
  19. %outside loop is faster
  20. [randvals, ~]=ran1(seedlocation, nframes*numel(possibleXYlocs)); randIdx=1;
  21. for ii = 1:nframes
  22. availablelist = locationlist;
  23. currXYlocs = possibleXYlocs;
  24. idx = 1;
  25. while numel(availablelist)>0
  26. randval=randvals(randIdx); randIdx=randIdx+1;
  27. stixelInd=floor(randval * size(availablelist,1))+1;
  28. toadd=availablelist(stixelInd);
  29. toremove = neighbourlist{Ny * mod(toadd,Nx) + floor(toadd/Nx)+1};
  30. %using the mex function for speed
  31. availablelist(ismembc(availablelist,sort(toremove))) = [];
  32. currXYlocs(idx) = toadd+1;
  33. idx = idx+1;
  34. end
  35. [randomvalues, seedcombination]=ran1(seedcombination, idx-1);
  36. stimMat(ii, currXYlocs(1:idx-1))=ceil(randomvalues*ncombis);
  37. end
  38. stimMat=reshape(stimMat, nframes, Nx, Ny);
  39. stimMat=flip(permute(stimMat, [1 3 2]),2);
  40. stimMat=sparse(reshape(stimMat,nframes,[]));
  41. %stimMat=ndSparse(stimMat);
  42. screenMat=zeros(nframes,screenNy, screenNx);
  43. n=max([stimdesc.nstixelsy, stimdesc.nstixelsx]);
  44. p = mod(1 : n, 2); C = bsxfun(@xor, p', p);
  45. C=C(1:stimdesc.nstixelsy, 1:stimdesc.nstixelsx);
  46. for icon=1:numel(cta)
  47. [fInd,yInd,xInd]=ind2sub([nframes Ny Nx],find(stimMat==icon));
  48. stixelMat=zeros(stimdesc.nstixelsy, stimdesc.nstixelsx);
  49. stixelMat(~C)=ctb(icon);
  50. stixelMat(C)=cta(icon);
  51. for ii=1:numel(fInd)
  52. screenMat(fInd(ii), ...
  53. (yInd(ii)-1)*stimdesc.nstixelsy+(1:stimdesc.nstixelsy),...
  54. (xInd(ii)-1)*stimdesc.nstixelsx+(1:stimdesc.nstixelsx))=stixelMat;
  55. end
  56. end
  57. end
  58. function neighbourlist = getneighbourlist(nx, ny, gapx, gapy)
  59. neighbourlist = cell(nx*ny,1);
  60. idx = 1;
  61. for x = 0: nx-1
  62. for y = 0: ny-1
  63. localneigbour=[];
  64. for currX = x-gapx : x+gapx
  65. for currY = y-gapy : y+gapy
  66. if (currX >=0 && currY >= 0 )
  67. localneigbour = [localneigbour, nx * currY + currX]; %#ok
  68. end
  69. end
  70. end
  71. neighbourlist{idx} = localneigbour;
  72. idx = idx+1;
  73. end
  74. end
  75. end
  76. function [cta, ctb] = genLists(stimdesc)
  77. nAngles = stimdesc.nangles;
  78. minCon = stimdesc.mincontrast;
  79. maxCon = stimdesc.maxcontrast;
  80. nContrasts = stimdesc.ncontrasts;
  81. angles = (0:(nAngles-1)) * 2*pi/nAngles;
  82. cosAngs = cos(angles);
  83. sinAngs = sin(angles);
  84. radii = minCon + (0:(nContrasts-1)) * (maxCon - minCon)/(nContrasts-1);
  85. cta = zeros( sum(radii >= 1e-5)*nAngles, 1 );
  86. ctb = zeros( sum(radii >= 1e-5)*nAngles, 1 );
  87. idx = 1; % Start from 2nd element of (cta,ctb) [first element = (0,0)]
  88. for ii = 1:nAngles
  89. cosAng = cosAngs(ii);
  90. sinAng = sinAngs(ii);
  91. for jj = 1:nContrasts
  92. r = radii(jj);
  93. if r < 1e-5; continue; end
  94. cta(idx) = r * cosAng;
  95. ctb(idx) = r * sinAng;
  96. idx = idx + 1;
  97. end
  98. end
  99. end