Chromatic_Integration_Stimulus.m 10 KB

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