Search code examples
psychtoolbox

Psychtoolbox fixed duration of stimuli


I am trying to record participant responses to visual stimuli which need to be shown for a fixed duration (500ms). However, I would also like to record their responses after this fixed duration within a 1 sec time window from stimulus onset. The following code allows me to record responses only when a key is pressed during the presentation of the stimulus (500ms).

    StimulusSecs = 0.50001;
    StimDuration = round(StimulusSecs / ifi);
    ISISecs = 0.50001;
    ISI = round(ISISecs / ifi);
    ITISecs = [1.00001, 2.00001, 3.00001];
    intertrial = round(ITISecs / ifi);
    waitframes = 1;
    vbl = Screen('Flip', mainwin);
    t2wait = 1;

    [~, Onset]=Screen('Flip', mainwin); keyIsDown=0; rt=0;

    tic % Stimulus Onset

    for frame = 1:StimDuration
    Screen('DrawTexture', mainwin, pics(picCounter));
    vbl = Screen('Flip',mainwin, vbl + (waitframes - 0.5) * ifi);
    [keyIsDown, secs, keyCode] = KbCheck;
    FlushEvents('keyDown');
    if keyIsDown
        if keyCode(spaceKey)
            rt = 1000.*(secs-Onset);
            keypressed=find(keyCode); 
            % break; is missing as this would 
            % escape the loop
        elseif (secs-Onset) > t2wait
            break;
        elseif keyCode(escKey)
            ShowCursor; fclose(outfile); Screen('CloseAll');
            return;
        end
        keyIsDown=0; keyCode=0; keypressed=0;
    end
end
S2dur = toc;

I am new to psychoolbox and any help would be greatly appreciated!!!


Solution

  • You're currently re-drawing and re-flipping the stimulus for every frame. If these are static images, you don't need to do that, as you only need to re-draw and flip when you want what is on screen to change. Here is an example assuming a static image. The key response loop operates over the whole t2wait period. Within that loop, a conditional statement compares if time allotted for the stimulus has elapsed, and if so, remove it from the screen:

    StimulusSecs = 0.50001;
    StimDuration = round(StimulusSecs / ifi);
    ISISecs = 0.50001;
    ISI = round(ISISecs / ifi);
    ITISecs = [1.00001, 2.00001, 3.00001];
    intertrial = round(ITISecs / ifi);
    waitframes = 1;
    vbl = Screen('Flip', mainwin);
    t2wait = 1;
    
    tic % Stimulus Onset
    
    [~, Onset] = Screen('DrawTexture', mainwin, pics(picCounter));
    OnScreen = 1; % variable to indicate whether the stim is still onscreen
    keyPressed = 0; % variable will be 0 until the space key is pressed
    rt=0;
    while (GetSecs - Onset) <=  t2wait
    
        [keyIsDown, secs, keyCode] = KbCheck;
    
        if keyPressed == 0 && keyIsDown
            if keyCode(spaceKey)
                rt = 1000.*(secs-Onset);
                keypressed=find(keyCode);
            elseif keyCode(escKey)
                ShowCursor; fclose(outfile); Screen('CloseAll');
                return;
            end
        end
    
        % turn the stimulus off if StimulusSecs has elapsed
        if OnScreen == 1 && ((GetSecs - Onset) >= StimulusSecs)
            Screen('Flip',wPtr);
            OnScreen = 0;
        end
    
        % Wait 1 ms before checking  again to prevent
        % overload of the machine at elevated priority
        WaitSecs(0.001);
    end