Chromatic_Grating_Stimulus.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. function Chromatic_Grating_Stimulus(varargin)
  2. %===================================================================================
  3. % Parameter Default Usage
  4. %===================================================================================
  5. %
  6. % selecttextfile false Browse prompt to select the parameters text file
  7. % stimduration 600 Stimulus presentation duration in refresh rate unit (600 = 10 sec).
  8. % period 60 Duration for one period of the grating in refresh rate unit (60 = 1 sec).
  9. % gratingwidth 60 Grating widths in pixels
  10. % squarewave false Option to switch between sinusoid and square-wave gratings
  11. % maxcontrast 0.2 Maximum Weber contrast value (ranges from -1 to 1)
  12. % mincontrast -0.2 Minimum Weber contrast value (ranges from -1 to 1)
  13. % contrastdiff 0.02 Contrast steps from mincontrast to maxcontrast e.g. -20 : 2 : 20
  14. % seed -1000 Starting value for random number generator (should be always negative)
  15. % redmeanintensity 0 Mean intensity for red gun of the screen
  16. % greenmeanintensity 0.5 Mean intensity for green gun of the screen
  17. % bluemeanintensity 0.5 Mean intensity for blue gun of the screen
  18. % screensize 864 x 480 Screen resolution, default value is the resolution of the lightcrafter projector
  19. % refreshrate 60 Screen refresh rate in Hz
  20. % fullscreen false Option to display the stimulus in full-screen mode
  21. % help false Option to check the list of available parameters
  22. % lmargin 0 Margins from left side of the screen (not available for this stimulus)
  23. % rmagin 0 Margins from right side of the screen (not available for this stimulus)
  24. % tmargin 0 Margins from top of the screen (not available for this stimulus)
  25. % bmargin 0 Margins from bottom of the screen (not available for this stimulus)
  26. % coneisolating false Option to activate opsin-isolation (excluded for simplicity)
  27. %
  28. %===================================================================================
  29. para = read_stimulus_parameters(varargin{:});
  30. if para.help, return; end
  31. % first make the contrast lists
  32. offcontrasts = fliplr(0:-para.contrastdiff:para.mincontrast);
  33. oncontrasts = 0:para.contrastdiff:para.maxcontrast;
  34. % put the lists together
  35. greencontrasts = [offcontrasts,oncontrasts];
  36. bluecontrasts = [oncontrasts,offcontrasts];
  37. numcontrasts = size(greencontrasts,2);
  38. % The darwing of the screen goes here
  39. monitorsize = get(0,'ScreenSize');
  40. scpos = [monitorsize(3)/2-(para.screensize(1)/2), ...
  41. monitorsize(4)/2-(para.screensize(2)/2), para.screensize];
  42. % make an screen like figure
  43. fh = figure('Menu','none','ToolBar','none','Position',scpos,'Color',0.5*[1 1 1]);
  44. fh.Name = 'Example of Chromatic Grating Stimulus, from Khani and Gollisch (2021)';
  45. fh.Colormap = gray;
  46. ah = axes('Units','Normalize','Position',[0 0 1 1]);
  47. axis(ah,[0, para.screensize(1),0, para.screensize(2)]);
  48. axis(ah,'off');
  49. framecounter= 0;
  50. seed = para.seed;
  51. while ishandle(fh)
  52. frameMod = mod(framecounter,para.stimduration);
  53. colorindexMod = floor(mod(framecounter/para.stimduration,numcontrasts));
  54. % resest the color order using Fisher-Yates random permutations
  55. if colorindexMod == 0 && frameMod == 0
  56. [colorder, seed] = fisher_Yates_shuffle_order(seed,1:numcontrasts);
  57. end
  58. thisphase = single(mod(framecounter / para.period + (1:para.screensize(1)) / (para.gratingwidth), 1));
  59. grcont = greencontrasts( colorder( colorindexMod+1 ));
  60. blucont = bluecontrasts( colorder( colorindexMod+1 ));
  61. if para.squarewave
  62. % make squarewave gratings
  63. g = (thisphase < 0.5) .* grcont;
  64. g(thisphase >= 0.5) = g(thisphase >= 0.5) .* (-grcont);
  65. b = (thisphase < 0.5) .* blucont;
  66. b(thisphase >= 0.5) = b(thisphase >= 0.5) .* (-blucont);
  67. else
  68. % make sinosoial gratings
  69. g = sin(2 * pi* thisphase) .* grcont;
  70. b = sin(2 * pi* thisphase) .* blucont;
  71. end
  72. % convert to weber contrast
  73. g = para.greenmeanintensity + (g .* para.greenmeanintensity);
  74. b = para.bluemeanintensity + (b .* para.bluemeanintensity);
  75. % put together and make a 2d image
  76. stimframe = repmat(cat(3,g.*0,g,b),para.screensize(2),1);
  77. if framecounter == 0
  78. s = imagesc(stimframe);
  79. axis(ah,[0, para.screensize(1),0, para.screensize(2)]);
  80. else
  81. s.CData = stimframe;
  82. end
  83. % this is to draw the frames relatively accurately.
  84. drawnow;
  85. java.lang.Thread.sleep(1/para.refreshrate*1e3);
  86. %pause(1/para.refreshrate);
  87. framecounter = framecounter+1;
  88. end
  89. end
  90. %--------------------------------------------------------------------------------------------------%
  91. %---------- sub-functions ----------%
  92. %--------------------------------------------------------------------------------------------------%
  93. function varargout = fisher_Yates_shuffle_order(seed,inputVec,varargin)
  94. %
  95. %%% fisher_Yates_shuffle_order %%%
  96. %
  97. %
  98. % This function generate psudorandom permution similar to randperm in MATLAB
  99. % but works with psudorandom number generator ran1. It also gives back the
  100. % most recent seed value to continue the permuation in case of repeated trials.
  101. % note that the direction of permutation is along x-axix or for columns of
  102. % MATALB not for the rows.
  103. %
  104. %
  105. % ===============================Inputs====================================
  106. %
  107. % seed : seed value for random number generation.
  108. % inputVec : input vector used for permutation.
  109. %
  110. %================================Output====================================
  111. %
  112. % testOrder : vector of permuted indices for the inputVec.
  113. % newSeed : the recent seed that used in the ran1 function.
  114. % outputVec : the permuted input vector along x-axis
  115. %
  116. % Note that the permution algorithem is based on Fisher-Yates shuffle
  117. % algorithem identical to what is used in the stimulus program.
  118. % for more info check :
  119. % https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
  120. %
  121. % written by Mohammad, 01.02.2016
  122. newSeed = seed;
  123. testOrder = zeros(1,size(inputVec,2));
  124. testOrder(1) = 1;
  125. for i = 2:length(inputVec)-1
  126. [randVal,newSeed] = ran1(newSeed);
  127. j = ceil(i*randVal); % based on Fischer-Yates algorithem
  128. testOrder(i) = testOrder(j);
  129. testOrder(j) = i;
  130. end
  131. testOrder = [testOrder(end),testOrder(1:end-1)]+1; % to match MATLAB indexing
  132. varargout{1} = testOrder;
  133. varargout{2} = newSeed;
  134. for j = 1:size(inputVec,1)
  135. varargout{3}(j,:) = inputVec(j,testOrder);
  136. end
  137. end
  138. %--------------------------------------------------------------------------------------------------%
  139. function paraout = read_stimulus_parameters(varargin)
  140. % first parse the user inputs
  141. p = inputParser(); % check the user options.
  142. p.addParameter('selecttextfile', false, @(x) islogical(x) || (isnumeric(x) && ismember(x,[0,1])));
  143. p.addParameter('stimduration', 600, @isnumeric);
  144. p.addParameter('period', 60, @isnumeric);
  145. p.addParameter('gratingwidth', 60, @isnumeric);
  146. p.addParameter('squarewave', false, @(x) islogical(x) || (isnumeric(x) && ismember(x,[0,1])));
  147. p.addParameter('contrastdiff', 0.02, @isnumeric);
  148. p.addParameter('mincontrast',-0.2, @isnumeric);
  149. p.addParameter('maxcontrast', 0.2, @isnumeric);
  150. p.addParameter('seed', -1000, @isnumeric);
  151. p.addParameter('redmeanintensity', 0, @isnumeric);
  152. p.addParameter('greenmeanintensity', 0.5, @isnumeric);
  153. p.addParameter('bluemeanintensity', 0.5, @isnumeric);
  154. p.addParameter('lmargin',0, @isnumeric);
  155. p.addParameter('rmargin', 0, @isnumeric);
  156. p.addParameter('bmargin', 0, @isnumeric);
  157. p.addParameter('tmargin', 0, @isnumeric);
  158. p.addParameter('coneisolating', true, @(x) islogical(x) || (isnumeric(x) && ismember(x,[0,1])));
  159. p.addParameter('screensize', [864 480], @isnumeric);
  160. p.addParameter('refreshrate', 60, @isnumeric);
  161. p.addParameter('fullscreen', false, @(x) islogical(x) || (isnumeric(x) && ismember(x,[0,1])));
  162. p.addParameter('help', false, @(x) islogical(x) || (isnumeric(x) && ismember(x,[0,1])));
  163. p.parse(varargin{:});
  164. % defualt parameters
  165. defpara = p.Results;
  166. if defpara.help
  167. help_info_for_parameters(defpara);
  168. paraout = defpara;
  169. return;
  170. end
  171. if defpara.selecttextfile
  172. % now read the text files
  173. [stimfile, stimpath] = uigetfile('*.txt','Select a chromatic integration stimulus parameter file','chromatic_integration.txt');
  174. fid = fopen([stimpath,filesep,stimfile]);
  175. tline = fgetl(fid);
  176. while ischar(tline)
  177. tline = fgetl(fid);
  178. if tline == -1, break; end
  179. fn = extractBefore(tline,' = ');
  180. if isempty(fn), continue; end
  181. val = extractAfter(tline,' = ');
  182. % to convert to double
  183. if ~isnan(str2double(val))
  184. val = str2double(val);
  185. end
  186. % to convert to logical
  187. if strcmp(val,'true'), val = true; end
  188. if strcmp(val,'false'), val = false; end
  189. % store the values in a structure
  190. stimpara.(fn) = val;
  191. end
  192. fclose(fid);
  193. % compare the text file to the defualt values and fill the missing parameters
  194. fn = fieldnames(defpara);
  195. for ii = 1:numel(fn)
  196. if isfield(stimpara,fn{ii})
  197. paraout.(fn{ii}) = stimpara.(fn{ii});
  198. else
  199. paraout.(fn{ii}) = defpara.(fn{ii});
  200. end
  201. end
  202. else
  203. paraout = defpara;
  204. end
  205. if paraout.fullscreen
  206. monitorsize = get(0,'ScreenSize');
  207. paraout.screensize = monitorsize(3:4) ;
  208. end
  209. end
  210. %--------------------------------------------------------------------------------------------------%
  211. function help_info_for_parameters(defpara)
  212. fn = fieldnames(defpara);
  213. fprintf(['\n\n',repmat('==',1,50),'\r\n']);
  214. fprintf([repmat(' ',1,35),'List of Stimulus Parameters\r\n']);
  215. fprintf([repmat('==',1,50),'\r\n']);
  216. maxtxtlen = max(cellfun(@numel,fn))+10;
  217. for ii = 1:numel(fn)
  218. g = repmat(' ',1,maxtxtlen - numel(fn{ii}));
  219. fprintf(['\t-- \t%s',g,':',repmat(' ',1,10),'%s\n'],fn{ii},num2str(defpara.(fn{ii})));
  220. end
  221. fprintf(['\n',repmat('==',1,50),'\r\n']);
  222. end