function presentStaticGratings_Rapid() % Save Directory sDir= 'X:\Brian'; %%%%%% ver 3 %%%%%%%%% % added flashing diode at beginning and end of stim presentation % added closall screen command at end % added esc key press to terminate session early Screen('Preference', 'SkipSyncTests', 1) Screen('Preference', 'VisualDebugLevel', 1); % in seconds stimDuration = 250/1000; % stimulus duration in ms, no rest between isiDuration = 0; % no gray screen between stimulus blocks, set to zero initialDuration = 2000/1000; screenDistance = 25; % cm; numRepeats = 4; % create a grating parameter table spfreq_range = 0.02:0.02:0.3;% range of spatial frequencies angle_range = 0:15:165;% range of angles to use gratingindexcount = 0; for spfreqind = 1:length(spfreq_range) for angleind = 1:length(angle_range) gratingindexcount = gratingindexcount + 1; grating_space(gratingindexcount,1) = gratingindexcount; grating_space(gratingindexcount,2) = angle_range(angleind); grating_space(gratingindexcount,3) = spfreq_range(spfreqind); end end for repeatind = 1:numRepeats randomOrder(:,repeatind) = randperm(size(grating_space,1))'; end randomOrder = randomOrder(:); % sine wave amplitude ("contrast"), 0.5 = black-white (balanced), 0.0 = full gray amplitude = 0.5; sine_params.amplitude=amplitude; sine_params.grating_space=grating_space; %% --------------------------------------------------------- sca; % Here we call some default settings for setting up Psychtoolbox PsychDefaultSetup(2); HideCursor; screens = Screen('Screens'); % Draw we select the maximum of these numbers. So in a situation where we % have two screens attached to our monitor we will draw to the external % screen. When only one screen is attached to the monitor we will draw to % this. % For help see: help max screenNumber = max(screens); % Define black (white will be 1 and black 0). This is because % luminace values are (in general) defined between 0 and 1. % For help see: help BlackIndex white=WhiteIndex(screenNumber); black=BlackIndex(screenNumber); gray=(white+black)/2; % Open an on screen window and color it black % For help see: Screen OpenWindow? [window, windowRect] = PsychImaging('OpenWindow', screenNumber, black); % Get the size of the on screen window in pixels % For help see: Screen WindowSize? [screenXpixels, screenYpixels] = Screen('WindowSize', window); pixPerCM_width= screenXpixels/64.135;%64.135 pixPerCM_height= screenYpixels/40.01;%40.01 %Get center of screen %[xCenter, yCenter] = RectCenter(windowRect); diode = 1; if diode diodeDest = [0 0 60 60]; diodeOn= ones(60,60)*white; diodeOn_tex=Screen('MakeTexture', window, diodeOn); diodeOff= ones(60,60)*black; diodeOff_tex=Screen('MakeTexture', window, diodeOff); end % Make a base Rect of 200 by 200 pixels. This is the rect which defines the % size of our square in pixels. Rects are rectangles, so the % sides do not have to be the same length. The coordinates define the top % left and bottom right coordinates of our rect [top-left-x top-left-y % bottom-right-x bottom-right-y]. The easiest thing to do is set the first % two coordinates to 0, then the last two numbers define the length of the % rect in X and Y. The next line of code then centers the rect on a % particular location of the screen. %set up gray screen with diode Screen('FillRect',window,gray); Screen('DrawTexture', window, diodeOff_tex, [], [diodeDest],[]); Screen('Flip', window); % add while ~KbCheck % wait to start the stim set end %want to flicker diode to signal start of session diodeTargetDuration = 1500/1000; diodeFlickerFreqHz = 5; % how long each pattern is shown continuously diode_flicker_duration = 1/diodeFlickerFreqHz; % we set the actual stimulus block duration as a multiple of requested flickering frequency diode_flickers_per_block = ceil(diodeFlickerFreqHz*diodeTargetDuration); %inter stimulus interval STARTTIME = tic; diode_timestamps=nan(1,10000); diode_states=nan(1,10000); FLIPCOUNT = 0; for i=1:diode_flickers_per_block tic if mod(i,2)==0 diodeColor = diodeOn_tex; else diodeColor = diodeOff_tex; end Screen('DrawTexture', window, diodeColor, [], [diodeDest],[]); Screen('Flip',window); FLIPCOUNT=FLIPCOUNT+1;diode_timestamps(FLIPCOUNT)=toc(STARTTIME);diode_states(FLIPCOUNT)=mod(i,2); WaitSecs(diode_flicker_duration-toc); end %find the refresh rate of the screen in seconds Screen('FillRect',window,gray); Screen('DrawTexture', window, diodeOff_tex, [], [diodeDest],[]); Screen('Flip', window); ifi = Screen('GetFlipInterval', window); onedegree = 2*screenDistance*tand(0.5); onecm = pixPerCM_width; % Phase is the phase shift in degrees (0-360 etc.)applied to the sine grating: % Compute increment of phase shift per redraw: % Build a procedural sine grating texture for a grating with a support of % res x res pixels and a RGB color offset of 0.5 -- a 50% gray. [w,h]=Screen('WindowSize',screenNumber); res=ceil(sqrt(w^2+h^2)); gratingtex = CreateProceduralSineGrating(window,res, res, [0.5 0.5 0.5 0.0]);%,(13/10)*screenYpixels/2 WaitSecs(initialDuration); DIODESTATE = 0; fprintf('\nStarting loop\n\n'); %begin stimulus presentation stimNumber = length(randomOrder); Phase = 0; for r=1:stimNumber DIODESTATE = mod(DIODESTATE+1,2); FLIPCOUNT=FLIPCOUNT+1;diode_timestamps(FLIPCOUNT)=toc(STARTTIME);diode_states(FLIPCOUNT)=DIODESTATE; tic; Screen('DrawTexture', window, gratingtex,[],[],180-grating_space(randomOrder(r),2),[],[],[],[],[],[Phase,grating_space(randomOrder(r),3)/onedegree/onecm,amplitude,0]); if DIODESTATE==1 Screen('DrawTexture', window, diodeOn_tex, [], [diodeDest],[]); else Screen('DrawTexture', window, diodeOff_tex, [], [diodeDest],[]); end Screen('Flip', window); pause(stimDuration); t=toc; %fprintf('Block %i duration was %fms, diode state %i\n',r,t*1000,DIODESTATE); if KbCheck sca; return; end end Screen('FillRect',window,gray); Screen('DrawTexture', window, diodeOff_tex, [], [diodeDest],[]); Screen('Flip', window); endPause = initialDuration; WaitSecs(endPause); %save data %flash diode for i=1:diode_flickers_per_block tic if mod(i,2)==0 diodeColor = diodeOn_tex; else diodeColor = diodeOff_tex; end Screen('DrawTexture', window, diodeColor, [], [diodeDest],[]); Screen('Flip',window); FLIPCOUNT=FLIPCOUNT+1;diode_timestamps(FLIPCOUNT)=toc(STARTTIME);diode_states(FLIPCOUNT)=mod(i,2); WaitSecs(diode_flicker_duration-toc); end Screen('FillRect',window,gray); Screen('DrawTexture', window, diodeOff_tex, [], [diodeDest],[]); Screen('Flip', window); FLIPCOUNT=FLIPCOUNT+1;diode_timestamps(FLIPCOUNT)=toc(STARTTIME);diode_states(FLIPCOUNT)=0; diode_timestamps(FLIPCOUNT+1:end)=[]; diode_states(FLIPCOUNT+1:end)=[]; params.stimType='HARTLEY_linearorispfreq_bj_02to3.m'; params.initialDuration= initialDuration; params.isiDuration= isiDuration; params.stimDuration= stimDuration; params.stimNumber= stimNumber; params.blockId = randomOrder; params.AllBlocks=grating_space; params.StimBlocks = grating_space; params.diode_timestamps = diode_timestamps; params.diode_states = diode_states; spath=pwd;%'C:\Users\skVisStim2\Documents\MATLAB\JanneProjectStimuli\savedParamFiles\'; %spath='P:\Pati\Scripts\SaveFileTesting\';j datetimename= datestr(now, 'yyyy-mm-dd_HHMMSS'); sname=['locomotionExperiment_HARTLEY_' datetimename]; save([spath,filesep,sname,'.mat'],'initialDuration','isiDuration','stimDuration','stimNumber','randomOrder','grating_space'); % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% sk saving helper % currdir= pwd; cd (sDir); listDir= dir; indx= find([listDir.isdir]); listDir= listDir(indx); listDir= listDir(arrayfun(@(x) x.name(1), listDir) ~= '.'); dirDatenum= [listDir.datenum]; [~,indx]= sort(dirDatenum, 'descend'); listDir= listDir(indx); saveFolder_2pComputer= listDir(1).name; saveFolder_2pComputer= [sDir '\' saveFolder_2pComputer]; cd(saveFolder_2pComputer) listDirFiles= dir; listDirFiles= listDirFiles(arrayfun(@(x) x.isdir(1), listDirFiles) == 0); listDirFiles= listDirFiles(arrayfun(@(x) x.name(1), listDirFiles ) ~= '.'); s=struct2cell(listDirFiles); s=s(1,:); indS= strfind(s, '.sbx'); indS= find(~cellfun(@isempty,indS)); listDirFiles= listDirFiles(indS); dirFilesDatenum= [listDirFiles.datenum]; [~,indx]= sort(dirFilesDatenum, 'descend'); listDirFiles= listDirFiles(indx); savePrefixSharingName= listDirFiles(1).name; saveSharingName= [savePrefixSharingName(1:end-4) '_vsParam']; cd(currdir) % sk added Aug 2016 outFileName= saveSharingName; % outFileName='visStimParams'; params.test=1; try if strfind(listDir(1).date, date) %mkdir(saveFolder_2pComputer,'visStim' ) save([saveFolder_2pComputer '\' outFileName], '-STRUCT','params'); disp('saved vis stim params on 2-P aq computer at location:') disp([saveFolder_2pComputer '\' outFileName]) else disp('no new folder made today, please transfer param file manually.'); end catch disp('Not sharing volume with PC, please transfer param file manually.'); end while ~KbCheck % wait to start the stim set end % ShowCursor; Screen('CloseAll');