function Chromatic_Grating_Stimulus(varargin) %=================================================================================== % Parameter Default Usage %=================================================================================== % % selecttextfile false Browse prompt to select the parameters text file % stimduration 600 Stimulus presentation duration in refresh rate unit (600 = 10 sec). % period 60 Duration for one period of the grating in refresh rate unit (60 = 1 sec). % gratingwidth 60 Grating widths in pixels % squarewave false Option to switch between sinusoid and square-wave gratings % maxcontrast 0.2 Maximum Weber contrast value (ranges from -1 to 1) % mincontrast -0.2 Minimum Weber contrast value (ranges from -1 to 1) % contrastdiff 0.02 Contrast steps from mincontrast to maxcontrast e.g. -20 : 2 : 20 % seed -1000 Starting value for random number generator (should be always negative) % redmeanintensity 0 Mean intensity for red gun of the screen % greenmeanintensity 0.5 Mean intensity for green gun of the screen % bluemeanintensity 0.5 Mean intensity for blue gun of the screen % screensize 864 x 480 Screen resolution, default value is the resolution of the lightcrafter projector % refreshrate 60 Screen refresh rate in Hz % fullscreen false Option to display the stimulus in full-screen mode % help false Option to check the list of available parameters % lmargin 0 Margins from left side of the screen (not available for this stimulus) % rmagin 0 Margins from right side of the screen (not available for this stimulus) % tmargin 0 Margins from top of the screen (not available for this stimulus) % bmargin 0 Margins from bottom of the screen (not available for this stimulus) % coneisolating false Option to activate opsin-isolation (excluded for simplicity) % %=================================================================================== para = read_stimulus_parameters(varargin{:}); if para.help, return; end % first make the contrast lists offcontrasts = fliplr(0:-para.contrastdiff:para.mincontrast); oncontrasts = 0:para.contrastdiff:para.maxcontrast; % put the lists together greencontrasts = [offcontrasts,oncontrasts]; bluecontrasts = [oncontrasts,offcontrasts]; numcontrasts = size(greencontrasts,2); % The darwing of the screen goes here monitorsize = get(0,'ScreenSize'); scpos = [monitorsize(3)/2-(para.screensize(1)/2), ... monitorsize(4)/2-(para.screensize(2)/2), para.screensize]; % make an screen like figure fh = figure('Menu','none','ToolBar','none','Position',scpos,'Color',0.5*[1 1 1]); fh.Name = 'Example of Chromatic Grating Stimulus, from Khani and Gollisch (2021)'; fh.Colormap = gray; ah = axes('Units','Normalize','Position',[0 0 1 1]); axis(ah,[0, para.screensize(1),0, para.screensize(2)]); axis(ah,'off'); framecounter= 0; seed = para.seed; while ishandle(fh) frameMod = mod(framecounter,para.stimduration); colorindexMod = floor(mod(framecounter/para.stimduration,numcontrasts)); % resest the color order using Fisher-Yates random permutations if colorindexMod == 0 && frameMod == 0 [colorder, seed] = fisher_Yates_shuffle_order(seed,1:numcontrasts); end thisphase = single(mod(framecounter / para.period + (1:para.screensize(1)) / (para.gratingwidth), 1)); grcont = greencontrasts( colorder( colorindexMod+1 )); blucont = bluecontrasts( colorder( colorindexMod+1 )); if para.squarewave % make squarewave gratings g = (thisphase < 0.5) .* grcont; g(thisphase >= 0.5) = g(thisphase >= 0.5) .* (-grcont); b = (thisphase < 0.5) .* blucont; b(thisphase >= 0.5) = b(thisphase >= 0.5) .* (-blucont); else % make sinosoial gratings g = sin(2 * pi* thisphase) .* grcont; b = sin(2 * pi* thisphase) .* blucont; end % convert to weber contrast g = para.greenmeanintensity + (g .* para.greenmeanintensity); b = para.bluemeanintensity + (b .* para.bluemeanintensity); % put together and make a 2d image stimframe = repmat(cat(3,g.*0,g,b),para.screensize(2),1); if framecounter == 0 s = imagesc(stimframe); axis(ah,[0, para.screensize(1),0, para.screensize(2)]); else s.CData = stimframe; end % this is to draw the frames relatively accurately. drawnow; java.lang.Thread.sleep(1/para.refreshrate*1e3); %pause(1/para.refreshrate); framecounter = framecounter+1; end end %--------------------------------------------------------------------------------------------------% %---------- sub-functions ----------% %--------------------------------------------------------------------------------------------------% function varargout = fisher_Yates_shuffle_order(seed,inputVec,varargin) % %%% fisher_Yates_shuffle_order %%% % % % This function generate psudorandom permution similar to randperm in MATLAB % but works with psudorandom number generator ran1. It also gives back the % most recent seed value to continue the permuation in case of repeated trials. % note that the direction of permutation is along x-axix or for columns of % MATALB not for the rows. % % % ===============================Inputs==================================== % % seed : seed value for random number generation. % inputVec : input vector used for permutation. % %================================Output==================================== % % testOrder : vector of permuted indices for the inputVec. % newSeed : the recent seed that used in the ran1 function. % outputVec : the permuted input vector along x-axis % % Note that the permution algorithem is based on Fisher-Yates shuffle % algorithem identical to what is used in the stimulus program. % for more info check : % https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle % % written by Mohammad, 01.02.2016 newSeed = seed; testOrder = zeros(1,size(inputVec,2)); testOrder(1) = 1; for i = 2:length(inputVec)-1 [randVal,newSeed] = ran1(newSeed); j = ceil(i*randVal); % based on Fischer-Yates algorithem testOrder(i) = testOrder(j); testOrder(j) = i; end testOrder = [testOrder(end),testOrder(1:end-1)]+1; % to match MATLAB indexing varargout{1} = testOrder; varargout{2} = newSeed; for j = 1:size(inputVec,1) varargout{3}(j,:) = inputVec(j,testOrder); end end %--------------------------------------------------------------------------------------------------% function paraout = read_stimulus_parameters(varargin) % first parse the user inputs p = inputParser(); % check the user options. p.addParameter('selecttextfile', false, @(x) islogical(x) || (isnumeric(x) && ismember(x,[0,1]))); p.addParameter('stimduration', 600, @isnumeric); p.addParameter('period', 60, @isnumeric); p.addParameter('gratingwidth', 60, @isnumeric); p.addParameter('squarewave', false, @(x) islogical(x) || (isnumeric(x) && ismember(x,[0,1]))); p.addParameter('contrastdiff', 0.02, @isnumeric); p.addParameter('mincontrast',-0.2, @isnumeric); p.addParameter('maxcontrast', 0.2, @isnumeric); p.addParameter('seed', -1000, @isnumeric); p.addParameter('redmeanintensity', 0, @isnumeric); p.addParameter('greenmeanintensity', 0.5, @isnumeric); p.addParameter('bluemeanintensity', 0.5, @isnumeric); p.addParameter('lmargin',0, @isnumeric); p.addParameter('rmargin', 0, @isnumeric); p.addParameter('bmargin', 0, @isnumeric); p.addParameter('tmargin', 0, @isnumeric); p.addParameter('coneisolating', true, @(x) islogical(x) || (isnumeric(x) && ismember(x,[0,1]))); p.addParameter('screensize', [864 480], @isnumeric); p.addParameter('refreshrate', 60, @isnumeric); p.addParameter('fullscreen', false, @(x) islogical(x) || (isnumeric(x) && ismember(x,[0,1]))); p.addParameter('help', false, @(x) islogical(x) || (isnumeric(x) && ismember(x,[0,1]))); p.parse(varargin{:}); % defualt parameters defpara = p.Results; if defpara.help help_info_for_parameters(defpara); paraout = defpara; return; end if defpara.selecttextfile % now read the text files [stimfile, stimpath] = uigetfile('*.txt','Select a chromatic integration stimulus parameter file','chromatic_integration.txt'); fid = fopen([stimpath,filesep,stimfile]); tline = fgetl(fid); while ischar(tline) tline = fgetl(fid); if tline == -1, break; end fn = extractBefore(tline,' = '); if isempty(fn), continue; end val = extractAfter(tline,' = '); % to convert to double if ~isnan(str2double(val)) val = str2double(val); end % to convert to logical if strcmp(val,'true'), val = true; end if strcmp(val,'false'), val = false; end % store the values in a structure stimpara.(fn) = val; end fclose(fid); % compare the text file to the defualt values and fill the missing parameters fn = fieldnames(defpara); for ii = 1:numel(fn) if isfield(stimpara,fn{ii}) paraout.(fn{ii}) = stimpara.(fn{ii}); else paraout.(fn{ii}) = defpara.(fn{ii}); end end else paraout = defpara; end if paraout.fullscreen monitorsize = get(0,'ScreenSize'); paraout.screensize = monitorsize(3:4) ; end end %--------------------------------------------------------------------------------------------------% function help_info_for_parameters(defpara) fn = fieldnames(defpara); fprintf(['\n\n',repmat('==',1,50),'\r\n']); fprintf([repmat(' ',1,35),'List of Stimulus Parameters\r\n']); fprintf([repmat('==',1,50),'\r\n']); maxtxtlen = max(cellfun(@numel,fn))+10; for ii = 1:numel(fn) g = repmat(' ',1,maxtxtlen - numel(fn{ii})); fprintf(['\t-- \t%s',g,':',repmat(' ',1,10),'%s\n'],fn{ii},num2str(defpara.(fn{ii}))); end fprintf(['\n',repmat('==',1,50),'\r\n']); end