Sending EEG triggers right at the time of stimulus presentation

Hi all,

I’m running an EEG study using Psychtoolbox, and I would like to send a trigger to the EEG recording equipment as soon as my stimuli are presented. I’ve come up with two ways to do this, but I feel one of them is inaccurate, I just want to double-check which one.

Here’s the code for a single trial of my experiment – it’s a flanker task where participants have to identify which way a central arrow is pointing in a row of distractor arrows. On each trial, the distractor arrows are presented first – after a blank screen -, 150 ms before the onset of the target. The target is then on screen until the participant’s response.

I present the distractors in a for loop, as seen below, and the target in a while loop (because I have to monitor for participants' response). My question is: is it better to flip the stimulus on screen outside of the loops, and send the trigger there – as seen for the distractor -, or should I have the first presentation of the stimulus within the for and/or while loop, and have the trigger sent the way I’m doing it for the target (with the if statement)?

Thank you so much!

The code:


DrawFormattedText(mainwin, '', 'center', 'center', textcolor);

            Screen('DrawingFinished', mainwin);

            vbl = Screen('Flip', mainwin);

        for frames = 1:BlankFrames - 1  % show blank screen for this many frames

            DrawFormattedText(mainwin, '', 'center', 'center', textcolor);

            Screen('DrawingFinished', mainwin);

            vbl = Screen('Flip', mainwin,  vbl + ((waitframes - 0.5) * ifi));

        end

        Screen('DrawTexture', mainwin, Distractor);

        Screen('DrawingFinished', mainwin);

        vbl = Screen('Flip', mainwin,  vbl + ((waitframes - 0.5) * ifi));

        fwrite(s1, dist_trig);  %%%%%%% this is the EEG trigger %%%%%%% 

        for frames2 = 1:distFrames-1

            Screen('DrawTexture', mainwin, Distractor);

            Screen('DrawingFinished', mainwin);

            vbl = Screen('Flip', mainwin,  vbl + ((waitframes - 0.5) * ifi));

        end

        timeStart = GetSecs;keyIsDown=0; correct=0; rt=0; rt2=0;

        flips = 1;

        while (GetSecs - timeStart) < 3  %%%participants have 3 seconds to respond%%%

            Screen('DrawTexture', mainwin, Target);

            Screen('DrawingFinished', mainwin);

            vbl = Screen('Flip', mainwin, vbl + ((waitframes - 0.5) * ifi));

            if flips == 1

                 fwrite(s1, trigger); %%%%%%% this is the 2nd EEG trigger %%%%%%% 

            end

            flips = flips + 1;

            [keyIsDown, secs, keyCode] = KbCheck;

            FlushEvents('keyDown');

            if keyIsDown

                nKeys = sum(keyCode);

                if nKeys==1

                    if keyCode(Left)||keyCode(Right)||keyCode(Down)||keyCode(Up)

                        rt = 1000.*(secs-timeStart);

                        keypressed=find(keyCode);

%                         fwrite(s1, trigger2);

                        break;

                    elseif keyCode(escKey)

                        ShowCursor; fclose(outfile);  Screen('CloseAll'); return

                    end

                    keyIsDown=0; keyCode=0;

                end

            else

                keypressed = 0; %the resp column in output is 0 if no button is pressed

            end

        end


(Just an additional comment to clarify some things: since the distractor is a static image, I suspect a for loop might not be the recommended way to go. Originally I wanted to use just the when arguments of the Flip commands to specify how long that should be on screen for but I was getting tons of missed flips that way - I might have been doing that wrong, I'm not sure, but doing it this way, with for loops, reduced the percentage of missed flips below 1%, so I'm quite okay with this, unless there's some reason why this is suboptimal.)
Hi Mate,

your EEG triggers are in principle at the correct location (directly after the Flip-command showing the respective stimulus). Your concept of stimulus presentation and timing is, however, flawed. This will not work reliably. I suggest starting with one of the demos available in the PsychDemos folder and also online or Mario’s PDF for an explanation of the underlying concepts (mentioned in one of the recent discussions).

A stimulus is shown on the screen until the next flip command. You do not have to redraw a stimulus for every frame. Find some simplified sample code for what I assume you want to achieve. To successively display three different images (incl. blank) you only need three flips. Timing is done with the Screen('Flip', win, …) command.

Other: Your RT will be off by at least one frame. Your keyboard will be checked only once per frame (in your code). Keyboard in general is not really suitable for RT measurement. Flip (by default) clears the backbuffer; you do not have to draw an empty string to clear the screen.

Hope this helps,
Andreas

mainwin = Screen('OpenWindow', 0);
textcolor = [ 0 0 0 ];
ifi = Screen('GetFlipInterval', mainwin);

blankDur = 1;
SOA = 0.150;
maxRT = 3;

% Blank screen for 1 s
t0 = Screen('Flip', mainwin);

% Distractor for 150 ms
% Screen('DrawTexture', mainwin, Distractor);
DrawFormattedText(mainwin, 'Distractor', 'center', 'center', textcolor); % Replace by DrawTexture
tDist = Screen('Flip', mainwin, t0 + blankDur - 0.5 * ifi);

fwrite(s1, dist_trig); %%%%%%% this is the EEG trigger %%%%%%%

% Target for max 3 s
% Screen('DrawTexture', mainwin, Target);
DrawFormattedText(mainwin, 'Target', 'center', 'center', textcolor); % Replace by DrawTexture
timeStart = Screen('Flip', mainwin, tDist + SOA - 0.5 * ifi);

fwrite(s1, trigger); %%%%%%% this is the 2nd EEG trigger %%%%%%%

[secs, keyCode] = KbWait(1, [], timeStart + maxRT);
rt = secs - timeStart
find( keyCode )

sca

> Am 05.06.2018 um 12:03 schrieb mate.gyurkovics@... [PSYCHTOOLBOX] <PSYCHTOOLBOX@yahoogroups.com>:
>
>
>
> Hi all,
>
> I’m running an EEG study using Psychtoolbox, and I would like to send a trigger to the EEG recording equipment as soon as my stimuli are presented. I’ve come up with two ways to do this, but I feel one of them is inaccurate, I just want to double-check which one.
>
> Here’s the code for a single trial of my experiment – it’s a flanker task where participants have to identify which way a central arrow is pointing in a row of distractor arrows. On each trial, the distractor arrows are presented first – after a blank screen -, 150 ms before the onset of the target. The target is then on screen until the participant’s response.
>
> I present the distractors in a for loop, as seen below, and the target in a while loop (because I have to monitor for participants' response). My question is: is it better to flip the stimulus on screen outside of the loops, and send the trigger there – as seen for the distractor -, or should I have the first presentation of the stimulus within the for and/or while loop, and have the trigger sent the way I’m doing it for the target (with the if statement)?
>
>
> Thank you so much!
>
> The code:
>
>
>
> DrawFormattedText(mainwin, '', 'center', 'center', textcolor);
>
> Screen('DrawingFinished', mainwin);
>
> vbl = Screen('Flip', mainwin);
>
> for frames = 1:BlankFrames - 1 % show blank screen for this many frames
>
> DrawFormattedText(mainwin, '', 'center', 'center', textcolor);
>
> Screen('DrawingFinished', mainwin);
>
> vbl = Screen('Flip', mainwin, vbl + ((waitframes - 0.5) * ifi));
>
> end
>
> Screen('DrawTexture', mainwin, Distractor);
>
> Screen('DrawingFinished', mainwin);
>
> vbl = Screen('Flip', mainwin, vbl + ((waitframes - 0.5) * ifi));
>
> fwrite(s1, dist_trig); %%%%%%% this is the EEG trigger %%%%%%%
>
> for frames2 = 1:distFrames-1
>
> Screen('DrawTexture', mainwin, Distractor);
>
> Screen('DrawingFinished', mainwin);
>
> vbl = Screen('Flip', mainwin, vbl + ((waitframes - 0.5) * ifi));
>
> end
>
> timeStart = GetSecs;keyIsDown=0; correct=0; rt=0; rt2=0;
>
> flips = 1;
>
> while (GetSecs - timeStart) < 3 %%%participants have 3 seconds to respond%%%
>
> Screen('DrawTexture', mainwin, Target);
>
> Screen('DrawingFinished', mainwin);
>
> vbl = Screen('Flip', mainwin, vbl + ((waitframes - 0.5) * ifi));
>
> if flips == 1
>
> fwrite(s1, trigger); %%%%%%% this is the 2nd EEG trigger %%%%%%%
>
> end
>
> flips = flips + 1;
>
> [keyIsDown, secs, keyCode] = KbCheck;
>
> FlushEvents('keyDown');
>
> if keyIsDown
>
> nKeys = sum(keyCode);
>
> if nKeys==1
>
> if keyCode(Left)||keyCode(Right)||keyCode(Down)||keyCode(Up)
>
> rt = 1000.*(secs-timeStart);
>
> keypressed=find(keyCode);
>
> % fwrite(s1, trigger2);
>
> break;
>
> elseif keyCode(escKey)
>
> ShowCursor; fclose(outfile); Screen('CloseAll'); return
>
> end
>
> keyIsDown=0; keyCode=0;
>
> end
>
> else
>
> keypressed = 0; %the resp column in output is 0 if no button is pressed
>
> end
>
> end
>
>
>
>
>
>
Dear Andreas,

wow, thank you very much for the extremely helpful response. And for the code as well.

As I mentioned in my second comment, I suspected/was aware that the for loop way of presenting these stimuli is needlessly complicated, however, when I do it the way you suggested, missed flip rate goes from about 0.8% to 3-5%. (While obviously the number of flips itself decreases.) Is this a serious issue?

Re RT: thanks for the suggestions regarding KbWait - I avoided that because the documentation suggests that it introduces uncertainty (http://docs.psychtoolbox.org/KbWait - is the uncertainty introduced by the while loop worse?) and because I'm not sure how to make only certain keys allowable with KbWait. 

I implemented some of the changes you kindly suggested - here's what a single trial looks like right now (it's long, I apologize). (the triggers are commented out right now.)


%trial events are as follows:
%one trial: 1) blank for 500 ms, then 2) distractors 150 ms before target, then 3) target until
%response 4) then 1000 ms blank or ERROR / TOO SLOW feedback, finally 5) ITI
%between 0 and 400 ms (I know I could merge this with the blank at the beginning of the trial
%but I might replace that period with a fixation cross)

% Blank screen for .5 s
        t0 = Screen('Flip', mainwin);
        
        % Distractor for 150 ms
        Screen('DrawTexture', mainwin, Distractor);
        tDist = Screen('Flip', mainwin, t0 + blankDur - 0.5 * ifi);
%         fwrite(s1, dist_trig); %%%%%%% this is the EEG trigger %%%%%%%
        
% Target for max 3 s
        Screen('DrawTexture', mainwin, Target);
        timeStart = Screen('Flip', mainwin, tDist + SOA - 0.5 * ifi);
        
%         fwrite(s1, trigger); %%%%%%% this is the 2nd EEG trigger %%%%%%%
        keypressed = 0; rt = 0; rt2 = 0; correct = 0;
        while (GetSecs - timeStart) < 3 %%% I switched back to KbCheck and while loop for the reasons stated above
            [keyIsDown, secs, keyCode] = KbCheck;
            FlushEvents('keyDown');
            if keyIsDown
                nKeys = sum(keyCode);
                if nKeys==1
                    if keyCode(Left)||keyCode(Right)||keyCode(Down)||keyCode(Up)
                        rt = 1000.*(secs-timeStart);
                        keypressed=find(keyCode);
%                         fwrite(s1, trigger2);
                        break;
                    elseif keyCode(escKey)
                        ShowCursor; fclose(outfile);  Screen('CloseAll'); return
                    end
                    keyIsDown=0; keyCode=0;
                end
            else
                keypressed = 0; %the resp column in output is 0 if no button is pressed
            end
        end

%Now to generate feedback for error and time-out trials
%fbDur is 1, as I want to present the feedback screen for 1000 ms

        if keypressed == 0 %indicates timeout errors
            DrawFormattedText(mainwin, 'TOO SLOW', 'center', 'center', errorcol);
            Screen('DrawingFinished', mainwin);
            Fbstart = Screen('Flip', mainwin); %%%%%%%%% I cannot put a when argument here, as the interval between this flip and the previous one (the target onset) is not fixed, it depends on the trial RT 
            ITIstart = Screen('Flip', mainwin, Fbstart + fbDur - 0.5 * ifi);
            ITIsec = ITIs(z) / 1000; %%%%%%%%% randomly drawn from a pool of ITIs
            Screen('Flip', mainwin, ITIstart + ITIsec - 0.5 * ifi);
            
        else
            if keyCode(Left) && (Target == lll || Target == rlr || Target == dld || Target == ulu)
                correct = 1;
                ITIsec = ITIs(z) / 1000; full_dur = fbDur + ITIsec;
                int_start = Screen('Flip', mainwin); %%%%%%%%% once again, no when argument can be specified for this
                Screen('Flip', mainwin, int_start + full_dur - 0.5 * ifi);
            elseif keyCode(Right) && (Target == lrl || Target == rrr || Target == drd || Target == uru)
                correct = 1; 
                ITIsec = ITIs(z) / 1000; full_dur = fbDur + ITIsec;
                int_start = Screen('Flip', mainwin); 
                Screen('Flip', mainwin, int_start + full_dur - 0.5 * ifi); 
            elseif keyCode(Down) && (Target == ldl || Target == rdr || Target == ddd || Target == udu)
                correct = 1;
                ITIsec = ITIs(z) / 1000; full_dur = fbDur + ITIsec;
                int_start = Screen('Flip', mainwin);
                Screen('Flip', mainwin, int_start + full_dur - 0.5 * ifi);
            elseif keyCode(Up) && (Target == lul || Target == rur || Target == dud || Target == uuu)
                correct = 1;
                ITIsec = ITIs(z) / 1000; full_dur = fbDur + ITIsec;
                int_start = Screen('Flip', mainwin);
                Screen('Flip', mainwin, int_start + full_dur - 0.5 * ifi); 
            else
                corrs(z) = 0;
                DrawFormattedText(mainwin, 'ERROR', 'center', 'center', errorcol);
                Fbstart = Screen('Flip', mainwin);
                ITIstart = Screen('Flip', mainwin, Fbstart + fbDur - 0.5 * ifi); tic
                ITIsec = ITIs(z) / 1000;
                Screen('Flip', mainwin, ITIstart + ITIsec - 0.5 * ifi); tocs(tc) = toc; tc = tc + 1;
            end
        end


Could the flips called without when arguments in the feedback bit be responsible for all the missed flips?
Hi Mate,

> As I mentioned in my second comment, I suspected/was aware that the for loop way of presenting these stimuli is needlessly complicated, however, when I do it the way you suggested, missed flip rate goes from about 0.8% to 3-5%. (While obviously the number of flips itself decreases.) Is this a serious issue?
As you already suspected, the message on missed flips in only indicative of real problems if *all* the flips in your script have a when argument. In case you do not know the timestamp in advance (as for example as here for response feedback) you could work with the PredictVisualOnsetForTime function. Personally, I prefer to collect the critical returned vbl timestamps (t0, tDist, timeStart here) per trial, write them to a log file and check for missed flips at the end of the block. Report back including detailed system specifications, if you have when arguments for all flips and still perceive missed flips.

> Re RT: thanks for the suggestions regarding KbWait - I avoided that because the documentation suggests that it introduces uncertainty (http://docs.psychtoolbox.org/KbWait- is the uncertainty introduced by the while loop worse?)
In my opinion, regular keyboards are unsuitable for RT collection anyway. The latencies and jitter standard keyboards introduce easily exceed the uncertainties due to loop throttling in KbWait. As always, there are exceptions. There was a discussion on the list some weeks ago on suitable keyboards, e.g. supporting high USB rates. See slides 62-66 here for an explanation why regular keyboards are bad (complete read highly recommended!):
http://peterscarfe.com/assets/ptbtutorial-ecvp2013.pdf

Tight loops are potentially problematic as they put very high load on the system and various OS respond differently to this situation. This is the reason why KBWait only checks every 5 ms.

> and because I'm not sure how to make only certain keys allowable with KbWait.
This is indeed not supported by KbWait, iirc. However, you do not use the feature in your call to KbCeck either (where it is supported). Providing a „scanList“ is supposed to considerably speed up KbCheck.

> I implemented some of the changes you kindly suggested - here's what a single trial looks like right now (it's long, I apologize). (the triggers are commented out right now.)
Too long to check, sorry. The use of FlushEvents does not make sense in this context as you do not use any event queues.

Hope this helps!
Andreas

> %trial events are as follows:
> %one trial: 1) blank for 500 ms, then 2) distractors 150 ms before target, then 3) target until
> %response 4) then 1000 ms blank or ERROR / TOO SLOW feedback, finally 5) ITI
> %between 0 and 400 ms (I know I could merge this with the blank at the beginning of the trial
> %but I might replace that period with a fixation cross)
>
> % Blank screen for .5 s
> t0 = Screen('Flip', mainwin);
>
> % Distractor for 150 ms
> Screen('DrawTexture', mainwin, Distractor);
> tDist = Screen('Flip', mainwin, t0 + blankDur - 0.5 * ifi);
> % fwrite(s1, dist_trig); %%%%%%% this is the EEG trigger %%%% %%%
>
> % Target for max 3 s
> Screen('DrawTexture', mainwin, Target);
> timeStart = Screen('Flip', mainwin, tDist + SOA - 0.5 * ifi);
>
> % fwrite(s1, trigger); %%%%%%% this is the 2nd EEG trigger %%%%%%%
> keypressed = 0; rt = 0; rt2 = 0; correct = 0;
> while (GetSecs - timeStart) < 3 %%% I switched back to KbCheck and while loop for the reasons stated above
> [keyIsDown, secs, keyCode] = KbCheck;
> FlushEvents('keyDown');
> if keyIsDown
> ; nKeys = sum(keyCode);
> if nKeys==1
> if keyCode(Left)||keyCode(Right)||keyCode(Down)||keyCode(Up)
> rt = 1000.*(secs-timeStart);
> keypressed=find(keyCode);
> % fwrite(s1, trigger2);
> break;
> elseif keyCode(escKey)
> ShowCursor; fclose(outfile); Screen('CloseAll'); return
> &nb sp; end
> keyIsDown=0; keyCode=0;
> end
> else
> keypressed = 0; %the resp column in output is 0 if no button is pressed
> end
> end
>
> %Now to generate feedback for error and time-out trials
> %fbDur is 1, as I want to present the feedback screen for 1000 ms
>
> if keypressed == 0 %indicates timeout errors
> DrawFormattedText(mainwin, 'TOO SLOW', 'center', 'center', errorcol);
> Screen ('DrawingFinished', mainwin);
> Fbstart = Screen('Flip', mainwin); %%%%%%%%% I cannot put a when argument here, as the interval between this flip and the previous one (the target onset) is not fixed, it depends on the trial RT
> ITIstart = Screen('Flip', mainwin, Fbstart + fbDur - 0.5 * ifi);
> ITIsec = ITIs(z) / 1000; %%%%%%%%% randomly drawn from a pool of ITIs
> Screen('Flip', mainwin, ITIstart + ITIsec - 0.5 * ifi);
>
> else
> if keyCode(Left) && (Target == lll || Target == rlr || Target == dld || Target == ulu)
> correct = 1;
> ITIsec = ITIs(z) / 1000; full_dur = fbDur + ITIsec;
> int_start = Screen('Flip', mainwin); %%%%%%%%% once again, no when argument can be specified for this
> Screen('Flip', mainwin, int_start + full_dur - 0.5 * ifi);
> elseif keyCode(Right) && (Target == lrl || Target == rrr || Target == drd || Target == uru)
> correct = 1;
> ITIsec = ITIs(z) / 1000; full_dur = fbDur + ITIsec;
> int_start = Screen('Flip', mainwin);
> Screen('Flip', mainwin, int_start + full_dur - 0.5 * ifi);
> elseif keyCode(Down) && (Target == ldl || Target == rdr || Target == ddd || Target == udu)
> correct = 1;
> ITIsec = ITIs(z) / 1000; full_dur = fbDur + ITIsec;
> int_start = Screen('Flip', mainwin);
> Screen('Flip', mainwin, int_start + full_dur - 0.5 * ifi);
> elseif keyCode(Up) && (Target == lul || Target == rur || Target == dud || Target == uuu)
> correct = 1;
> ITIsec = ITIs(z) / 1000; full_d ur = fbDur + ITIsec;
> int_start = Screen('Flip', mainwin);
> Screen('Flip', mainwin, int_start + full_dur - 0.5 * ifi);
> else
> corrs(z) = 0;
> DrawFormattedText(mainwin, 'ERROR', 'center', 'center', errorcol);
> Fbstart = Screen('Flip', mainwin);
> ITIstart = Screen('Flip', mainwin, Fbstart + fbDur - 0.5 * ifi); tic
> ITIsec = ITIs(z) / 1000;
> Screen('Flip', mainwin, ITIstart + ITIsec - 0.5 * ifi); tocs(tc) = toc; tc = tc + 1;
> end
> end
>
>
> Could the flips called without when arguments in the feedback bit be responsible for all the missed flips?
>
>
>

Hello,


Thank you once again for the detailed and helpful response. I’ll try to get to the bottom of where the missed flips are coming from. When you say you check that afterwards using an event log, can you elaborate what that means? I understand that you could log the time stamps on every trial, but how do you then tell if a flip was missed? Do you calculate the time between flip events, and see if they are fairly close to what you set (e.g., blank duration, distractor duration), e.g., within one refresh rate of that? Obviously, this cannot be done for events that depend on external input (e.g., participant’s response), such as the feedback but if all the fixed duration flips are okay then by process of elimination the non-fixed flips (flips without when arguments) are the problem? (Which is not an issue, as I’m mostly interested in getting the stimulus onset right.)


Thanks,

Máté

> When you say you check that afterwards using an event log, can you elaborate what that means? I understand that you could log the time stamps on every trial, but how do you then tell if a flip was missed? Do you calculate the time between flip events, and see if they are fairly close to what you set (e.g., blank duration, distractor duration), e.g., within one refresh rate of that?
This is one option, yes. If the requested delay is an integer multiple of the ifi (as it should be) the observed delay should be very close to the requested (< 1ms; note that refresh rates are frequently non-integer by design, e.g., 59.9 Hz; resulting in small but consistent differences for longer delays, e.g., 301 instead of 300 ms).

Alternatively, you could also log both, the requested and the observed flip time per trial: for example "tDist + SOA - 0.5 * ifi" and "timeStart" in case of your target. The difference must not exceed half a frame.

> Obviously, this cannot be done for events that depend on external input (e.g., participant’s response), such as the feedback
This can be done using PredictVisualOnsetForTime (but indeed usually it's not worth it).

> but if all the fixed duration flips are okay then by process of elimination the non-fixed flips (flips without when arguments) are the problem? (Which is not an issue, as I’m mostly interested in getting the stimulus onset right.)
Yes.

Best,
Andreas

> Am 09.06.2018 um 20:34 schrieb mate.gyurkovics@... [PSYCHTOOLBOX] <PSYCHTOOLBOX@yahoogroups.com>:
>
>
>
> Hello,
>
>
>
> Thank you once again for the detailed and helpful response. I’ll try to get to the bottom of where the missed flips are coming from. When you say you check that afterwards using an event log, can you elaborate what that means? I understand that you could log the time stamps on every trial, but how do you then tell if a flip was missed? Do you calculate the time between flip events, and see if they are fairly close to what you set (e.g., blank duration, distractor duration), e.g., within one refresh rate of that? Obviously, this cannot be done for events that depend on external input (e.g., participant’s response), such as the feedback but if all the fixed duration flips are okay then by process of elimination the non-fixed flips (flips without when arguments) are the problem? (Which is not an issue, as I’m mostly interested in getting the stimulus onset right.)
>
>
>
> Thanks,
>
> Máté
>
>
>
>
Thanks again for the response.

I implemented the changes, and am now logging time stamps and requested deadlines. Weirdly on a 144 Hz refresh rate monitor the 150 ms distractor presentation was wildly off (by as much as 30 ms on certain trials) based on the time stamps. (The presentation of the target stimulus missed the deadline on all trials basically.) I tried expressing the distractor duration as a multiple of the ifi (e.g., 24 * ifi) but even then presentation was off - as long as I tried to get it to be around 150 ms. It worked much better when I went down to 138 or up to 180, and I have no idea why.

I have no issues like this with the for-loop variant, and the missed flip rate is also much nicer there. When you say this setup will not work reliably, can you elaborate what you mean?

Best,
Mate
Please send the absolutely minimal code demonstrating the problem (on your machine). Optimally just one or two flips with delays in a loop including the logic for timestamp logging. Please, include the output of VBLSyncTest (preferably incl. Figures) after increasing verbosity (Screen('Preference', 'Verbosity', 10)).

> I have no issues like this with the for-loop variant, and the missed flip rate is also much nicer there. When you say this setup will not work reliably, can you elaborate what you mean?
Hard to believe. If the system is not able to perform a flip at the expected time after hundred something msecs why should it be better doing it every 7 msec? The lower relative number of missed flips might be a side effect of having orders of magnitudes more flips. I would consider re-drawing and flipping every frame at 144 Hz to be taxing even for a reliable and well-configured system (but possibly I misunderstood something in your code).

Best,
Andreas

> Am 12.06.2018 um 16:49 schrieb mate.gyurkovics@... [PSYCHTOOLBOX] <PSYCHTOOLBOX@yahoogroups.com>:
>
>
>
> Thanks again for the response.
>
> I implemented the changes, and am now logging time stamps and requested deadlines. Weirdly on a 144 Hz refresh rate monitor the 150 ms distractor presentation was wildly off (by as much as 30 ms on certain trials) based on the time stamps. (The presentation of the target stimulus missed the deadline on all trials basically.) I tried expressing the distractor duration as a multiple of the ifi (e.g., 24 * ifi) but even then presentation was off - as long as I tried to get it to be around 150 ms. It worked much better when I went down to 138 or up to 180, and I have no idea why.
>
> I have no issues like this with the for-loop variant, and the missed flip rate is also much nicer there. When you say this setup will not work reliably, can you elaborate what you mean?
>
> Best,
> Mate
>
>
>
I am really confused about what's happening, so I really appreciate all your help.

Here's the "demo trial" code that illustrates the problem:

(It runs 10 trials not just 1, because for just the first one timing can be quite accurate.)

[mainwin, screenrect] = Screen(0, 'OpenWindow');
ifi = Screen('GetFlipInterval', mainwin);
textcolor = [ 0 0 0 ];

blankDur = 0.5;
SOA = .15;


actual_blank = [];
dist_flip_delay = [];
actual_distractor = [];
targ_flip_delay = [];
for i = 1:10
    t0 = Screen('Flip', mainwin);
    DrawFormattedText(mainwin, 'Distractor', 'center', 'center', textcolor);
    tDist = Screen('Flip', mainwin, t0 + blankDur - 0.5 * ifi);
    %         fwrite(s1, dist_trig);
    DrawFormattedText(mainwin, 'Target', 'center', 'center', textcolor);
    timeStart = Screen('Flip', mainwin, tDist + SOA - 0.5 * ifi); %%%this is the problematic flip
    %         fwrite(s1, trigger);
    keypressed = 0; rt = 0; rt2 = 0; correct = 0;
    while (GetSecs - timeStart) < 1
        [keyIsDown, secs, keyCode] = KbCheck;
        FlushEvents('keyDown'); %still haven't removed this, sorry
        if keyIsDown
            nKeys = sum(keyCode);
            if nKeys==1
                break
            end
        else
            keypressed = 0;
        end
    end
    
    actual_blank(i) = tDist - t0; 
    dist_flip_delay(i) = tDist - (t0 + blankDur - 0.5 * ifi); 
    actual_distractor(i) = timeStart - tDist; 
    targ_flip_delay(i) = timeStart - (tDist + SOA - 0.5 * ifi); 
end

sca

The problem: when SOA is set to .15 on a Windows 7, 144 Hz computer almost ALL of the actual_distractor durations will be off by sometimes as much as 30 ms. On a Windows 10, 60 Hz timing is very accurate but that's my own PC I can't use it for data collection.

When I set SOA to be 26 * ifi on the 144 Hz computer, performance gets better, but it STILL misses 2-3 out of every block of ten...

I ran VBLSyncTest on both computers, but here are the results for just the data collection one (Win7, 144 Hz).

I've attached the figures and the output, which is now extremely verbose.



The forum attachment function appears to be broken. Please provide the attachment some other way (Dropbox?).

Please try the attached code (note the order of arguments for Screen('OpenWindow',0)!) and report the output.

Best,
Andreas

[mainwin, screenrect] = Screen('OpenWindow', 0);
ifi = Screen('GetFlipInterval', mainwin);
textcolor = [ 0 0 0 ];

blankDur = 0.5;
SOA = .1527;
RT = 1;

tBlock = Screen('Flip', mainwin);
t0 = tBlock;
for i = 1:10
t0 = Screen('Flip', mainwin, t0 + blankDur + SOA + RT - 0.8 * ifi);
DrawFormattedText(mainwin, 'Distractor', 'center', 'center', textcolor);
tDist( i ) = Screen('Flip', mainwin, t0 + blankDur - 0.8 * ifi);
% fwrite(s1, dist_trig);
DrawFormattedText(mainwin, 'Target', 'center', 'center', textcolor);
tTarDrawn( i ) = GetSecs;
timeStartReq( i ) = tDist( i ) + SOA - 0.8 * ifi;
timeStart( i ) = Screen('Flip', mainwin, timeStartReq( i ) ); %%%this is the problematic flip
% fwrite(s1, trigger);
end

tTarDrawn - tBlock
timeStartReq - tBlock
timeStart - tBlock
timeStart - timeStartReq
tTarDrawn - tDist
timeStart - tDist

sca


> Am 13.06.2018 um 12:26 schrieb mate.gyurkovics@... [PSYCHTOOLBOX] <PSYCHTOOLBOX@yahoogroups.com>:
>
> [Attachment(s) from mate.gyurkovics@... [PSYCHTOOLBOX] included below]
>
> I am really confused about what's happening, so I really appreciate all your help.
>
> Here's the "demo trial" code that illustrates the problem:
>
> (It runs 10 trials not just 1, because for just the first one timing can be quite accurate.)
>
> [mainwin, screenrect] = Screen(0, 'OpenWindow');
> ifi = Screen('GetFlipInterval', mainwin);
> textcolor = [ 0 0 0 ];
>
> blankDur = 0.5;
> SOA = .15;
>
>
> actual_blank = [];
> dist_flip_delay = [];
> actual_distractor = [];
> targ_flip_delay = [];
> for i = 1:10
> t0 = Screen('Flip', mainwin);
> DrawFormattedText(mainwin, 'Distractor', 'center', 'center', textcolor);
> tDist = Screen('Flip', mainwin, t0 + blankDur - 0.5 * ifi);
> % fwrite(s1, dist_trig);
> DrawFormattedText(mainwin, 'Target', 'center', 'center', textcolor);
> timeStart = Screen('Flip', mainwin, tDist + SOA - 0.5 * ifi); %%%this is the problematic flip
> % fwrite(s1, trigger);
> keypressed = 0; rt = 0; rt2 = 0; correct = 0;
> while (GetSecs - timeStart) < 1
> [keyIsDown, secs, keyCode] = KbCheck;
> FlushEvents('keyDown'); %still haven't removed this, sorry
> if keyIsDown
> nKeys = sum(keyCode);
> if nKeys==1
> break
> &nbs p; end
> else
> keypressed = 0;
> end
> end
>
> actual_blank(i) = tDist - t0;
> dist_flip_delay(i) = tDist - (t0 + blankDur - 0.5 * ifi);
> actual_distractor(i) = timeStart - tDist;
> targ_flip_delay(i) = timeStart - (tDist + SOA - 0.5 * ifi);
> end
>
> sca
>
> The problem: when SOA is set to .15 on a Windows 7, 144 Hz computer almost ALL of the actual_distractor durations will be off by sometimes as much as 30 ms. On a Windows 10, 60 Hz timing is very accurate but that's my own PC I can't use it for data collection.
>
> When I set SOA to be 26 * ifi on the 144 Hz computer, performance gets better, but it STILL misses 2-3 out of every block of ten...
>
> I ran VBLSyncTest on both computers, but here are the results for just the data collection one (Win7, 144 Hz).
>
> I've attached the figures and the output, which is now extremely verbose.
>
>
>
>
>
>
While I try to figure out how to share the figures with you, here's the output of that code:

tTarDrawn – tBlock: 2.1605    3.8101    5.4633    7.1159    8.7692   10.4219   12.0746   13.7276   15.3803   17.0331

timeStartReq – tBlock: 2.2999    3.9526    5.6053    7.2581    8.9108   10.5635   12.2163   13.8690   15.5217   17.1744

timeStart – tBlock: 2.3055    3.9930    5.6387    7.2845    8.9373   10.6039   12.2566   13.9024   15.5551   17.2078

timeStart – timeStartReq: 0.0056    0.0404    0.0334    0.0265    0.0265    0.0403    0.0403    0.0334    0.0334    0.0334

tTarDrawn – tDist: 0.0077    0.0046    0.0051    0.0049    0.0055    0.0055    0.0055    0.0058    0.0057    0.0058

timeStart – tDist: 0.1528    0.1875    0.1806    0.1736    0.1736    0.1875    0.1875    0.1805    0.1805    0.1806 - you can clearly see the problem here


Also, know that my screen was set to verbose, I saw a message I've never seen before: "PTB-DEBUG: Swaprequest too close to last swap vbl (0.152897 secs) or between forbidden scanline 1 and 50. Delaying..." I suspect this is the cause of the issue. Is there a way around this? 


> timeStart – timeStartReq: 0.0056 0.0404 0.0334 0.0265 0.0265 0.0403 0.0403 0.0334 0.0334 0.0334
Ok, there is indeed a severe problem with this setup.

> While I try to figure out how to share the figures with you
The console text output (VBLSyncTest with verbosity 10) is more important. Please post here as text.

Have you tried lower refresh rates (60 Hz?; you do not need 144 Hz for this experiment)? Does the problem persist?

Best,
Andreas

> Am 13.06.2018 um 15:07 schrieb mate.gyurkovics@... [PSYCHTOOLBOX] <PSYCHTOOLBOX@yahoogroups.com>:
>
>
>
> While I try to figure out how to share the figures with you, here's the output of that code:
>
> tTarDrawn – tBlock: 2.1605 3.8101 5.4633 7.1159 8.7692 10.4219 12.0746 13.7276 15.3803 17.0331
> timeStartReq – tBlock: 2.2999 3.9526 5.6053 7.2581 8.9108 10.5635 12.2163 13.8690 15.5217 17.1744
> timeStart – tBlock: 2.3055 3.9930 5.6387 7.2845 8.9373 10.6039 12.2566 13.9024 15.5551 17.2078
> timeStart – timeStartReq: 0.0056 0.0404 0.0334 0.0265 0.0265 0.0403 0.0403 0.0334 0.0334 0.0334
> tTarDrawn – tDist: 0.0077 0.0046 0.0051 0.0049 0.0055 0.0055 0.0055 0.0058 0.0057 0.0058
> timeStart – tDist: 0.1528 0.1875 0.1806 0.1736 0.1736 0.1875 0.1875 0.1805 0.1805 0.1806 - you can clearly see the problem here
>
> Also, know that my screen was set to verbose, I saw a message I've never seen before: "PTB-DEBUG: Swaprequest too close to last swap vbl (0.152897 secs) or between forbidden scanline 1 and 50. Delaying..." I suspect this is the cause of the issue. Is there a way around this?
>
>
>
>
O, wow, it'd be good to figure out what the issue is then.

I'll paste the output at the end of this post.

I did try a Windows 10, 60 Hz computer, the problem more or less disappears there. Unfortunately, I don't think I'll be able to change the monitor in the EEG lab room, unless I can convince everyone that it's absolutely vital.

PTB-INFO: This is Psychtoolbox-3 for Microsoft Windows, under Matlab 64-Bit (Version 3.0.14 - Build date: Jun  6 2017).

PTB-INFO: Support status on this operating system release: Windows version 6.1 partially supported, but no longer tested at all.

PTB-INFO: Type 'PsychtoolboxVersion' for more detailed version information.

PTB-INFO: Most parts of the Psychtoolbox distribution are licensed to you under terms of the MIT License, with

PTB-INFO: some restrictions. See file 'License.txt' in the Psychtoolbox root folder for the exact licensing conditions.

 

PTB-DEBUG: PsychOSOpenOnscreenWindow: Entering Win32 specific window setup...

PTB-DEBUG: Checking for DWM desktop compositor support...  ... DWM available on this Vista (or later) system. Binding controls ... ...done

PTB-INFO: DWM desktop compositor is currently enabled.

PTB-INFO: Will disable DWM because a regular fullscreen onscreen window is opened -> We want best timing and performance.

PTB-INFO: DWM desktop compositor is now disabled.

PTB-DEBUG: Switching display for screen 0 to fullscreen mode with resolution 1920 x 1080 with bpc = 32 @ 144 Hz.

PTB-DEBUG: PsychOSOpenOnscreenWindow: Window parameters computed, display switch to fullscreen done (if needed). Registering window class...

PTB-DEBUG: Window enumeration done. Our hostwindow is HWND=0000000000080228, Name: 'MATLAB R2014b'

 

PTB-DEBUG: PsychOSOpenOnscreenWindow: Window class registered - Creating GDI window...

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): WM_SIZE!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: Created onscreen window has position 0 x 0 and a size of 1920 x 1080.

PTB-DEBUG: PsychOSOpenOnscreenWindow: Window created - Pixelformat selection...

PTB-DEBUG: PsychOSOpenOnscreenWindow: ChoosePixelFormat(), SetPixelFormat() done... Creating OpenGL context...

PTB-DEBUG: PsychOSOpenOnscreenWindow: Context created - Activating and binding context...

PTB-DEBUG: PsychOSOpenOnscreenWindow: Online - glewInit()...

PTB-INFO: Using GLEW version 2.0.0 for automatic detection of OpenGL extensions...

PTB-DEBUG: PsychOSOpenOnscreenWindow: Mastercontext created, activated and bound - Enabling multisampling if needed...

PTB-DEBUG: Window and master OpenGL context creation finished.

PTB-DEBUG: Before slaveWindow context sharing: glGetString reports 00000000187E27A0 pointer...

PTB-DEBUG: After slaveWindow context sharing: glGetString reports 00000000187E27A0 pointer...

PTB-DEBUG: Final low-level window setup: ShowWindow(), SetCapture(), diagnostics...

PTB-DEBUG: OpenGL initialization of all master-/slave-/shared-/userspace contexts finished...

PTB-DEBUG: Final low-level window setup: ShowWindow(), SetCapture(), diagnostics...

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: Executed SetForegroundWindow() and SetFocus() on window to optimize pageflipping and timing.

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): WM_PAINT!

PTB-DEBUG: Final low-level window setup finished. Continuing with OS-independent setup.

PTB-DEBUG: PPM file magic is P6

 -> Ok

# CREATOR: GIMP PNM Filter Version 1.1

PTB-DEBUG: Recognized splash image of 656 x 304 pixels, maxlevel 255. Loading...

 

 

OpenGL-Vendor / renderer / version are: NVIDIA Corporation - GeForce GTX 1070/PCIe/SSE2 - 4.6.0 NVIDIA 388.13

 

 

OpenGL-Extensions are: GL_AMD_multi_draw_indirect GL_AMD_seamless_cubemap_per_texture GL_AMD_vertex_shader_viewport_index GL_AMD_vertex_shader_layer GL_ARB_arrays_of_arrays GL_ARB_base_instance GL_ARB_bindless_texture GL_ARB_blend_func_extended GL_ARB_buffer_storage GL_ARB_clear_buffer_object GL_ARB_clear_texture GL_ARB_clip_control GL_ARB_color_buffer_float GL_ARB_compatibility GL_ARB_compressed_texture_pixel_storage GL_ARB_conservative_depth GL_ARB_compute_shader GL_ARB_compute_variable_group_size GL_ARB_conditional_render_inverted GL_ARB_copy_buffer GL_ARB_copy_image GL_ARB_cull_distance GL_ARB_debug_output GL_ARB_depth_buffer_float GL_ARB_depth_clamp GL_ARB_depth_texture GL_ARB_derivative_control GL_ARB_direct_state_access GL_ARB_draw_buffers GL_ARB_draw_buffers_blend GL_ARB_draw_indirect GL_ARB_draw_elements_base_vertex GL_ARB_draw_instanced GL_ARB_enhanced_layouts GL_ARB_ES2_compatibility GL_ARB_ES3_compatibility GL_ARB_ES3_1_compatibility GL_ARB_ES3_2_compatibility GL_ARB_explicit_attrib_location GL_ARB_explicit_uniform_location GL_ARB_fragment_coord_conventions GL_ARB_fragment_layer_viewport GL_ARB_fragment_program GL_ARB_fragment_program_shadow GL_ARB_fragment_shader GL_ARB_fragment_shader_interlock GL_ARB_framebuffer_no_attachments GL_ARB_framebuffer_object GL_ARB_framebuffer_sRGB GL_ARB_geometry_shader4 GL_ARB_get_program_binary GL_ARB_get_texture_sub_image GL_ARB_gl_spirv GL_ARB_gpu_shader5 GL_ARB_gpu_shader_fp64 GL_ARB_gpu_shader_int64 GL_ARB_half_float_pixel GL_ARB_half_float_vertex GL_ARB_imaging GL_ARB_indirect_parameters GL_ARB_instanced_arrays GL_ARB_internalformat_query GL_ARB_internalformat_query2 GL_ARB_invalidate_subdata GL_ARB_map_buffer_alignment GL_ARB_map_buffer_range GL_ARB_multi_bind GL_ARB_multi_draw_indirect GL_ARB_multisample GL_ARB_multitexture GL_ARB_occlusion_query GL_ARB_occlusion_query2 GL_ARB_parallel_shader_compile GL_ARB_pipeline_statistics_query GL_ARB_pixel_buffer_object GL_ARB_point_parameters GL_ARB_point_sprite GL_ARB_polygon_offset_clamp GL_ARB_post_depth_coverage GL_ARB_program_interface_query GL_ARB_provoking_vertex GL_ARB_query_buffer_object GL_ARB_robust_buffer_access_behavior GL_ARB_robustness GL_ARB_sample_locations GL_ARB_sample_shading GL_ARB_sampler_objects GL_ARB_seamless_cube_map GL_ARB_seamless_cubemap_per_texture GL_ARB_separate_shader_objects GL_ARB_shader_atomic_counter_ops GL_ARB_shader_atomic_counters GL_ARB_shader_ballot GL_ARB_shader_bit_encoding GL_ARB_shader_clock GL_ARB_shader_draw_parameters GL_ARB_shader_group_vote GL_ARB_shader_image_load_store GL_ARB_shader_image_size GL_ARB_shader_objects GL_ARB_shader_precision GL_ARB_shader_storage_buffer_object GL_ARB_shader_subroutine GL_ARB_shader_texture_image_samples GL_ARB_shader_texture_lod GL_ARB_shading_language_100 GL_ARB_shader_viewport_layer_array GL_ARB_shading_language_420pack GL_ARB_shading_language_include GL_ARB_shading_language_packing GL_ARB_shadow GL_ARB_sparse_buffer GL_ARB_sparse_texture GL_ARB_sparse_texture2 GL_ARB_sparse_texture_clamp GL_ARB_spirv_extensions GL_ARB_stencil_texturing GL_ARB_sync GL_ARB_tessellation_shader GL_ARB_texture_barrier GL_ARB_texture_border_clamp GL_ARB_texture_buffer_object GL_ARB_texture_buffer_object_rgb32 GL_ARB_texture_buffer_range GL_ARB_texture_compression GL_ARB_texture_compression_bptc GL_ARB_texture_compression_rgtc GL_ARB_texture_cube_map GL_ARB_texture_cube_map_array GL_ARB_texture_env_add GL_ARB_texture_env_combine GL_ARB_texture_env_crossbar GL_ARB_texture_env_dot3 GL_ARB_texture_filter_anisotropic GL_ARB_texture_filter_minmax GL_ARB_texture_float GL_ARB_texture_gather GL_ARB_texture_mirror_clamp_to_edge GL_ARB_texture_mirrored_repeat GL_ARB_texture_multisample GL_ARB_texture_non_power_of_two GL_ARB_texture_query_levels GL_ARB_texture_query_lod GL_ARB_texture_rectangle GL_ARB_texture_rg GL_ARB_texture_rgb10_a2ui GL_ARB_texture_stencil8 GL_ARB_texture_storage GL_ARB_texture_storage_multisample GL_ARB_texture_swizzle GL_ARB_texture_view GL_ARB_timer_query GL_ARB_transform_feedback2 GL_ARB_transform_feedback3 GL_ARB_transform_feedback_instanced GL_ARB_transform_feedback_overflow_query GL_ARB_transpose_matrix GL_ARB_uniform_buffer_object GL_ARB_vertex_array_bgra GL_ARB_vertex_array_object GL_ARB_vertex_attrib_64bit GL_ARB_vertex_attrib_binding GL_ARB_vertex_buffer_object GL_ARB_vertex_program GL_ARB_vertex_shader GL_ARB_vertex_type_10f_11f_11f_rev GL_ARB_vertex_type_2_10_10_10_rev GL_ARB_viewport_array GL_ARB_window_pos GL_ATI_draw_buffers GL_ATI_texture_float GL_ATI_texture_mirror_once GL_S3_s3tc GL_EXT_texture_env_add GL_EXT_abgr GL_EXT_bgra GL_EXT_bindable_uniform GL_EXT_blend_color GL_EXT_blend_equation_separate GL_EXT_blend_func_separate GL_EXT_blend_minmax GL_EXT_blend_subtract GL_EXT_compiled_vertex_array GL_EXT_Cg_shader GL_EXT_depth_bounds_test GL_EXT_direct_state_access GL_EXT_draw_buffers2 GL_EXT_draw_instanced GL_EXT_draw_range_elements GL_EXT_fog_coord GL_EXT_framebuffer_blit GL_EXT_framebuffer_multisample GL_EXTX_framebuffer_mixed_formats GL_EXT_framebuffer_multisample_blit_scaled GL_EXT_framebuffer_object GL_EXT_framebuffer_sRGB GL_EXT_geometry_shader4 GL_EXT_gpu_program_parameters GL_EXT_gpu_shader4 GL_EXT_multi_draw_arrays GL_EXT_packed_depth_stencil GL_EXT_packed_float GL_EXT_packed_pixels GL_EXT_pixel_buffer_object GL_EXT_point_parameters GL_EXT_polygon_offset_clamp GL_EXT_post_depth_coverage GL_EXT_provoking_vertex GL_EXT_raster_multisample GL_EXT_rescale_normal GL_EXT_secondary_color GL_EXT_separate_shader_objects GL_EXT_separate_specular_color GL_EXT_shader_image_load_formatted GL_EXT_shader_image_load_store GL_EXT_shader_integer_mix GL_EXT_shadow_funcs GL_EXT_sparse_texture2 GL_EXT_stencil_two_side GL_EXT_stencil_wrap GL_EXT_texture3D GL_EXT_texture_array GL_EXT_texture_buffer_object GL_EXT_texture_compression_dxt1 GL_EXT_texture_compression_latc GL_EXT_texture_compression_rgtc GL_EXT_texture_compression_s3tc GL_EXT_texture_cube_map GL_EXT_texture_edge_clamp GL_EXT_texture_env_combine GL_EXT_texture_env_dot3 GL_EXT_texture_filter_anisotropic GL_EXT_texture_filter_minmax GL_EXT_texture_integer GL_EXT_texture_lod GL_EXT_texture_lod_bias GL_EXT_texture_mirror_clamp GL_EXT_texture_object GL_EXT_texture_shared_exponent GL_EXT_texture_sRGB GL_EXT_texture_sRGB_decode GL_EXT_texture_storage GL_EXT_texture_swizzle GL_EXT_timer_query GL_EXT_transform_feedback2 GL_EXT_vertex_array GL_EXT_vertex_array_bgra GL_EXT_vertex_attrib_64bit GL_EXT_window_rectangles GL_EXT_import_sync_object GL_IBM_rasterpos_clip GL_IBM_texture_mirrored_repeat GL_KHR_context_flush_control GL_KHR_debug GL_EXT_memory_object GL_EXT_memory_object_win32 GL_EXT_win32_keyed_mutex GL_KHR_parallel_shader_compile GL_KHR_no_error GL_KHR_robust_buffer_access_behavior GL_KHR_robustness GL_EXT_semaphore GL_EXT_semaphore_win32 GL_KTX_buffer_region GL_NV_alpha_to_coverage_dither_control GL_NV_bindless_multi_draw_indirect GL_NV_bindless_multi_draw_indirect_count GL_NV_bindless_texture GL_NV_blend_equation_advanced GL_NV_blend_equation_advanced_coherent GL_NVX_blend_equation_advanced_multi_draw_buffers GL_NV_blend_minmax_factor GL_NV_blend_square GL_NV_clip_space_w_scaling GL_NV_command_list GL_NV_compute_program5 GL_NV_conditional_render GL_NV_conservative_raster GL_NV_conservative_raster_dilate GL_NV_conservative_raster_pre_snap_triangles GL_NV_copy_depth_to_color GL_NV_copy_image GL_NV_depth_buffer_float GL_NV_depth_clamp GL_NV_draw_texture GL_NV_draw_vulkan_image GL_NV_ES1_1_compatibility GL_NV_ES3_1_compatibility GL_NV_explicit_multisample GL_NV_fence GL_NV_fill_rectangle GL_NV_float_buffer GL_NV_fog_distance GL_NV_fragment_coverage_to_color GL_NV_fragment_program GL_NV_fragment_program_option GL_NV_fragment_program2 GL_NV_fragment_shader_interlock GL_NV_framebuffer_mixed_samples GL_NV_framebuffer_multisample_coverage GL_NV_geometry_shader4 GL_NV_geometry_shader_passthrough GL_NV_gpu_program4 GL_NV_internalformat_sample_query GL_NV_gpu_program4_1 GL_NV_gpu_program5 GL_NV_gpu_program5_mem_extended GL_NV_gpu_program_fp64 GL_NV_gpu_shader5 GL_NV_half_float GL_NV_light_max_exponent GL_NV_multisample_coverage GL_NV_multisample_filter_hint GL_NV_occlusion_query GL_NV_packed_depth_stencil GL_NV_parameter_buffer_object GL_NV_parameter_buffer_object2 GL_NV_path_rendering GL_NV_path_rendering_shared_edge GL_NV_pixel_data_range GL_NV_point_sprite GL_NV_primitive_restart GL_NV_query_resource GL_NV_query_resource_tag GL_NV_register_combiners GL_NV_register_combiners2 GL_NV_sample_locations GL_NV_sample_mask_override_coverage GL_NV_shader_atomic_counters GL_NV_shader_atomic_float GL_NV_shader_atomic_float64 GL_NV_shader_atomic_fp16_vector GL_NV_shader_atomic_int64 GL_NV_shader_buffer_load GL_NV_shader_storage_buffer_object GL_NV_stereo_view_rendering GL_NV_texgen_reflection GL_NV_texture_barrier GL_NV_texture_compression_vtc GL_NV_texture_env_combine4 GL_NV_texture_multisample GL_NV_texture_rectangle GL_NV_texture_rectangle_compressed GL_NV_texture_shader GL_NV_texture_shader2 GL_NV_texture_shader3 GL_NV_transform_feedback GL_NV_transform_feedback2 GL_NV_uniform_buffer_unified_memory GL_NV_vertex_array_range GL_NV_vertex_array_range2 GL_NV_vertex_attrib_integer_64bit GL_NV_vertex_buffer_unified_memory GL_NV_vertex_program GL_NV_vertex_program1_1 GL_NV_vertex_program2 GL_NV_vertex_program2_option GL_NV_vertex_program3 GL_NV_viewport_array2 GL_NV_viewport_swizzle GL_NVX_conditional_render GL_NVX_gpu_memory_info GL_NVX_multigpu_info GL_NVX_nvenc_interop GL_NV_shader_thread_group GL_NV_shader_thread_shuffle GL_KHR_blend_equation_advanced GL_KHR_blend_equation_advanced_coherent GL_SGIS_generate_mipmap GL_SGIS_texture_lod GL_SGIX_depth_texture GL_SGIX_shadow GL_SUN_slice_accum GL_WIN_swap_hint WGL_EXT_swap_control

 

PTB-DEBUG: Interrogating Low-level renderer capabilities for onscreen window with handle 10:

Indicator variables: FBO's 1, ATI_texture_float 1, ARB_texture_float 1, Vendor NVIDIA Corporation, Renderer GeForce GTX 1070/PCIe/SSE2.

Indicator variables: maxcolorattachments = 8, maxrectangletexturesize = 32768, maxnativealuinstructions = 65536.

GPU supports non-power-of-two textures.

Basic framebuffer objects with rectangle texture rendertargets supported --> RGBA8 rendertargets with blending.

Framebuffer objects support fast blitting between each other.

Framebuffer objects support anti-aliasing via multisampling.

Framebuffer objects support single-pass multisample resolve blits and image rescaling.

Hardware supports floating point textures of 16bpc and 32bpc float format.

Assuming NV30 core or later...

Assuming NV40 core or later (maxcolattachments=8): Hardware supports floating point blending and filtering on 16bpc float format.

Hardware also supports floating point framebuffers of 16bpc and 32bpc float format.

Hardware supports full 32 bit floating point precision shading.

Assuming G80 core or later (maxtexsize=32768): Hardware supports full floating point blending and filtering on 16bpc and 32bpc float format.

Assuming hardware supports native OpenGL primitive smoothing (points, lines).

No compiled in support for OpenML OML_sync_control extension.

PTB-DEBUG: Interrogation done.

 

PTB-DEBUG: glClear splash image top-left reference pixel: 255 255 255

PTB-INFO: Threshold Settings for successfull video refresh calibration are: maxStdDev = 0.200000 msecs, maxDeviation = 10.000000 %, minSamples = 50, maxDuration = 5.000000 secs.

PTB-DEBUG: Could not enable REALTIME_PRIORITY_CLASS scheduling! Retrying with HIGH_PRIORITY_CLASS...

PTB-INFO: The detected endline of the vertical blank interval is equal or lower than the startline. This indicates

PTB-INFO: that i couldn't detect the duration of the vertical blank interval and won't be able to correct timestamps

PTB-INFO: for it. This will introduce a very small and constant offset (typically << 1 msec). Read 'help BeampositionQueries'

PTB-INFO: for how to correct this, should you really require that last few microseconds of precision.

PTB-INFO: Btw. this can also mean that your systems beamposition queries are slightly broken. It may help timing precision to

PTB-INFO: enable the beamposition workaround, as explained in 'help ConserveVRAMSettings', section 'kPsychUseBeampositionQueryWorkaround'.

PTB-DEBUG: Could not enable REALTIME_PRIORITY_CLASS scheduling! Retrying with HIGH_PRIORITY_CLASS...

 

 

PTB-DEBUG: Output of all acquired samples of calibration run follows:

PTB-DEBUG: Sample 0: 0.000000

PTB-DEBUG: Sample 1: 0.006947

PTB-DEBUG: Sample 2: 0.006958

PTB-DEBUG: Sample 3: 0.006922

PTB-DEBUG: Sample 4: 0.006915

PTB-DEBUG: Sample 5: 0.006931

PTB-DEBUG: Sample 6: 0.006951

PTB-DEBUG: Sample 7: 0.006944

PTB-DEBUG: Sample 8: 0.006940

PTB-DEBUG: Sample 9: 0.006996

PTB-DEBUG: Sample 10: 0.006888

PTB-DEBUG: Sample 11: 0.006956

PTB-DEBUG: Sample 12: 0.006937

PTB-DEBUG: Sample 13: 0.006944

PTB-DEBUG: Sample 14: 0.006946

PTB-DEBUG: Sample 15: 0.007008

PTB-DEBUG: Sample 16: 0.006909

PTB-DEBUG: Sample 17: 0.006982

PTB-DEBUG: Sample 18: 0.006899

PTB-DEBUG: Sample 19: 0.006955

PTB-DEBUG: Sample 20: 0.006935

PTB-DEBUG: Sample 21: 0.006960

PTB-DEBUG: Sample 22: 0.006942

PTB-DEBUG: Sample 23: 0.006942

PTB-DEBUG: Sample 24: 0.006929

PTB-DEBUG: Sample 25: 0.006945

PTB-DEBUG: Sample 26: 0.006973

PTB-DEBUG: Sample 27: 0.006930

PTB-DEBUG: Sample 28: 0.006945

PTB-DEBUG: Sample 29: 0.006933

PTB-DEBUG: Sample 30: 0.006940

PTB-DEBUG: Sample 31: 0.006959

PTB-DEBUG: Sample 32: 0.006946

PTB-DEBUG: Sample 33: 0.006968

PTB-DEBUG: Sample 34: 0.006941

PTB-DEBUG: Sample 35: 0.006954

PTB-DEBUG: Sample 36: 0.006908

PTB-DEBUG: Sample 37: 0.006948

PTB-DEBUG: Sample 38: 0.006946

PTB-DEBUG: Sample 39: 0.006944

PTB-DEBUG: Sample 40: 0.006922

PTB-DEBUG: Sample 41: 0.006931

PTB-DEBUG: Sample 42: 0.006944

PTB-DEBUG: Sample 43: 0.006943

PTB-DEBUG: Sample 44: 0.006953

PTB-DEBUG: Sample 45: 0.007001

PTB-DEBUG: Sample 46: 0.006935

PTB-DEBUG: Sample 47: 0.006895

PTB-DEBUG: Sample 48: 0.007001

PTB-DEBUG: Sample 49: 0.006935

PTB-DEBUG: Sample 50: 0.006945

PTB-DEBUG: End of calibration data for this run...

 

 

 

PTB-INFO: OpenGL-Renderer is NVIDIA Corporation :: GeForce GTX 1070/PCIe/SSE2 :: 4.6.0 NVIDIA 388.13

PTB-INFO: VBL startline = 1080 , VBL Endline = 1079

PTB-INFO: Measured monitor refresh interval from beamposition = 6.944293 ms [144.003143 Hz].

PTB-INFO: Will use beamposition query for accurate Flip time stamping.

PTB-INFO: Measured monitor refresh interval from VBLsync = 6.944370 ms [144.001544 Hz]. (50 valid samples taken, stddev=0.024952 ms.)

PTB-INFO: Reported monitor refresh interval from operating system = 6.944444 ms [144.000000 Hz].

PTB-INFO: Small deviations between reported values are normal and no reason to worry.

PTB-INFO: Support for fast OffscreenWindows enabled.

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): WM_PAINT!

PTB-DEBUG: Swaprequest too close to last swap vbl (0.000064 secs) or between forbidden scanline 1 and 50. Delaying...

PTB-DEBUG: Swaprequest too close to last swap vbl (0.000064 secs) or between forbidden scanline 1 and 50. Delaying...

PTB-DEBUG: Swaprequest too close to last swap vbl (0.001394 secs) or between forbidden scanline 1 and 50. Delaying...

PTB-DEBUG: Swaprequest too close to last swap vbl (0.000227 secs) or between forbidden scanline 1 and 50. Delaying...

PTB-DEBUG: Swaprequest too close to last swap vbl (0.000555 secs) or between forbidden scanline 1 and 50. Delaying...

The refresh interval reported by the operating system is 6.94444 ms.

PTB-DEBUG: Allocated unicode string: 77.000000 101.000000 97.000000 115.000000 117.000000 114.000000 105.000000 110.000000 103.000000 32.000000 109.000000 111.000000 110.000000 105.000000 116.000000 111.000000 114.000000 32.000000 114.000000 101.000000 102.000000 114.000000 101.000000 115.000000 104.000000 32.000000 105.000000 110.000000 116.000000 101.000000 114.000000 118.000000 97.000000 108.000000 46.000000 46.000000 46.000000 32.000000 84.000000 104.000000 105.000000 115.000000 32.000000 99.000000 97.000000 110.000000 32.000000 116.000000 97.000000 107.000000 101.000000 32.000000 117.000000 112.000000 32.000000 116.000000 111.000000 32.000000 50.000000 48.000000 32.000000 115.000000 101.000000 99.000000 111.000000 110.000000 100.000000 115.000000 46.000000 46.000000 46.000000

PTB-DEBUG: DrawText: Trying to load external text renderer plugin from following file: [ C:\toolbox\Psychtoolbox\PsychBasic\PsychPlugins\libptbdrawtext_ftgl64.dll ]

Measured refresh interval, as reported by "GetFlipInterval" is 6.94437 ms. (nsamples = 0, stddev = 0.00000 ms)

 

ans =

 

     1

 

PTB-DEBUG: In ScreenCloseAllWindows(): Destroying window 0

PTB-DEBUG: In CleanupDrawtextGDI: Releasing GDI ressources for DrawTextGDI.

PTB-DEBUG: In PsychCleanupTextRenderer: Releasing text renderer plugin completely.

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

PTB-DEBUG: WndProc(): Called!

Hookchain 'CloseOnscreenWindowPostGLShutdown' : Slot 0: Id='Shutdown window callback into PsychJavaSwingCleanup().' : Runtime-Function : Evalstring= PsychJavaSwingCleanup;

PTB missed 1 out of 600 stimulus presentation deadlines.

One missed deadline is ok and an artifact of the measurement.

PTB completed 0 stimulus presentations before the requested target time.

Have a look at the plots for more details...



> I'll paste the output at the end of this post.
I cannot spot anything obviously wrong at a first glance. But I’m not a Windows expert at all. So maybe you can ask Mario or the other forum experts for ideas.

My first guess would be problems with power management on CPU or GPU. Please first check whether you can find any power management options in the Nvidia driver settings. Switch to high performance instead of adaptive or power saving mode. Power management as potential source of the problem could be plausible as there are no obvious problems during OpenWindow testing or VBLSyncTest asking for high performance. Also your first trial shows no delay. The card is mostly sleeping later on during your trials and possible switches into some lower power mode. This would also be well compatible with your observation that there are less problems when you re-draw and flip for every frame. I would, however, still not consider this as a proper workaround.

Also search the forum for issues with power management. This topic came up several times during the last years, iirc.

> I did try a Windows 10, 60 Hz computer, the problem more or less disappears there. Unfortunately, I don't think I'll be able to change the monitor in the EEG lab room, unless I can convince everyone that it's absolutely vital.
I suggested to switch the refresh rate (also somewhere in the driver settings) not the monitor. The monitor should also run well at 60 Hz. Did you already try that?

Best,
Andreas
Good that the problem is resolved.

If timing is accurate (VBLTimestamp - when < ifi for all flips with when timestamp) I would expect the reported missed flips to be false positives. Please change the 0.8 * ifi term back to 0.5 * ifi for all flips. This was for testing only and might well result in false positives (this is only 1.3 ms after the expected time of the preceding flip; minimal jitter or inaccuracies might result in a reported missed flip). There are additional output arguments for Screen('Flip'). With the „Missed“ output argument you can try to identify the flips considered as missed and have a closer look at them. Also have a look on the help text of Screen('Flip?') for more information on false positives.

Best,
Andreas

> Am 15.06.2018 um 16:42 schrieb mate.gyurkovics@... [PSYCHTOOLBOX] <PSYCHTOOLBOX@yahoogroups.com>:
>
>
>
> Hello,
>
> thanks for all the suggestions. First of all: changing the graphics card setting from Optimal power management to Maximum power dramatically reduced the issue. The 30 ms jitter around the 150 ms SOA disappeared completely. Timing looks very accurate based on time stamps. Thank you very much for this piece of advice.
>
> I do however still get missed flips, quite a lot of them (2-5%). Interestingly when I run the sample code Andreas suggested I only get missed flips (20 out of 30!) on the first run of the code, with no timing issues (calculated durations are fine). Then if I run it again, there are no more missed flips.
>
> In my actual code, I can never get below 2.5%, although there are flips in there without a deadline. (However, the duratoins calculated based on Flip timestamps are okay for those stimuli too that are presented without deadlines.)
>
> VBLSyncTest(10,22) returns no error message s, and 0 missed flips. This bit, however, is always there when I run a code, although I assumed this is not a huge problem:
>
> PTB-INFO: The detected endline of the vertical blank interval is equal or lower than the startline. This indicates
> PTB-INFO: that i couldn't detect the duration of the vertical blank interval and won't be able to correct timestamps
> PTB-INFO: for it. This will introduce a very small and constant offset (typically << 1 msec). Read 'help BeampositionQueries'
> PTB-INFO: for how to correct this, should you really require that last few microseconds of precision.
> PTB-INFO: Btw. this can also mean that your systems beamposition queries are slightly broken. It may help timing precision to
> PTB-INFO: enable the beamposition workaround, as explained in 'help ConserveVRAMSettings', section 'kPsychUseBeampositionQueryWorkaround'.
>
> I did try setting Priority higher by putting Priority(1) or Priority(2) at the top of the script, but a) I think Priority is at 1 to begin with as if I query priority with Priority() I get 1; and 2) if I set it to 2, Priority seems to remain at 1...
>
> Thanks,
> Mate
>
>
>
<widmann@...> wrote :

Good that the problem is resolved.

If timing is accurate (VBLTimestamp - when < ifi for all flips with when timestamp) I would expect the reported missed flips to be false positives. Please change the 0.8 * ifi term back to 0.5 * ifi for all flips. This was for testing only and might well result in false positives (this is only 1.3 ms after the expected time of the preceding flip; minimal jitter or inaccuracies might result in a reported missed flip).

-> While 0.5 * ifi is a more safe value than 0.8 * ifi on operating systems with primitive graphics drivers like macOS and Windows - or if one insists using Linux with the proprietary NVidia graphics driver, it shouldn't cause false positives, as PTB uses the 'when' value just as guidance to calculate when a flip should truly complete, similar to what PrecictVisualOnsetForTime() does. 0.8 does provide less security margin than 0.5 on noisy operating systems, so your recommendation is right, just not completely for reason i think you stated, or maybe i just misunderstood.

-> Another question is if the "- 0.5 *ifi" makes actual sense if SOA and blankDur are specified in absolute time instead of frame counts. If one expresses time in video refresh cycle counts like waitFrames = 22, then the typical sample code when = t0 + waitFrames * ifi - 0.5 * ifi; just translates that into absolute target times. For absolute values, when = t0 + SOA; would be equally valid, and depending on the refresh rate chosen for the display and how well SOA fits into multiple refresh duration, when = t0 + SOA would be more appropriate or robust against running on setups with different monitor video refresh rates, e.g., always showing the stim at SOA or slightly later, which is something one has to deal with anyway as delays are always possible, not sometimes *earlier* than SOA due to unlucky combos of SOA and video refresh duration and general execution timing.

It all depends on your approach, and in general using an OS with advanced builtin visual onset scheduling like Linux with the open-source graphics drivers should make sure that theory and practice agree better in practice, which is less taxing on the experimenters brain.

-mario

There are additional output arguments for Screen('Flip'). With the „Missed“ output argument you can try to identify the flips considered as missed and have a closer look at them. Also have a look on the help text of Screen('Flip?') for more information on false positives.

Best,
Andreas

> Am 15.06.2018 um 16:42 schrieb mate.gyurkovics@... [PSYCHTOOLBOX] <PSYCHTOOLBOX@yahoogroups.com>:
>
>
>
> Hello,
>
> thanks for all the suggestions. First of all: changing the graphics card setting from Optimal power management to Maximum power dramatically reduced the issue. The 30 ms jitter around the 150 ms SOA disappeared completely. Timing looks very accurate based on time stamps. Thank you very much for this piece of advice.
>
> I do however still get missed flips, quite a lot of them (2-5%). Interestingly when I run the sample code Andreas suggested I only get missed flips (20 out of 30!) on the first run of the code, with no timing issues (calculated durations are fine). Then if I run it again, there are no more missed flips.
>
> In my actual code, I can never get below 2.5%, although there are flips in there without a deadline. (However, the duratoins calculated based on Flip timestamps are okay for those stimuli too that are presented without deadlines.)
>
> VBLSyncTest(10,22) returns no error message s, and 0 missed flips. This bit, however, is always there when I run a code, although I assumed this is not a huge problem:
>
> PTB-INFO: The detected endline of the vertical blank interval is equal or lower than the startline. This indicates
> PTB-INFO: that i couldn't detect the duration of the vertical blank interval and won't be able to correct timestamps
> PTB-INFO: for it. This will introduce a very small and constant offset (typically << 1 msec). Read 'help BeampositionQueries'
> PTB-INFO: for how to correct this, should you really require that last few microseconds of precision.
> PTB-INFO: Btw. this can also mean that your systems beamposition queries are slightly broken. It may help timing precision to
> PTB-INFO: enable the beamposition workaround, as explained in 'help ConserveVRAMSettings', section 'kPsychUseBeampositionQueryWorkaround'.
>
> I did try setting Priority higher by putting Priority(1) or Priority(2) at the top of the script, but a) I think Priority is at 1 to begin with as if I query priority with Priority() I get 1; and 2) if I set it to 2, Priority seems to remain at 1...
>
> Thanks,
> Mate
>
>
>

Sorry for the delay in responding, I was testing different versions of my task, and also trying to implement some of your suggestions.


Thank you very much for every comment and idea – I am definitely a LOT more confident about my setup now.


I used the Missed argument of the flip command to check which flips exactly were missed. After setting every fixed duration to a multiple of the refresh rate, the number of missed flips went down to about 3%. When I check which flips were missed, it seems most or all of them are flips that do not have a when argument specified because they cannot be timed to a previous flip. Since I see no way to improve this and timing of those flips is not of huge importance, I think I can live with this. Stimulus presentation seems very accurate based on the flip time stamps.


An interesting thing I noticed is that the number of reported missed flips is increased when I run my task for the first time after opening Matlab. If I run anything before running the actual task script, e.g., even just a VBLSyncTest, the percentage of missed flips for the task will be lower. I’m not sure why this is.


Once again, thank you for all your help.

> An interesting thing I noticed is that the number of reported missed flips is increased when I run my task for the first time after opening Matlab. If I run anything before running the actual task script, e.g., even just a VBLSyncTest, the percentage of missed flips for the task will be lower. I’m not sure why this is.
See here for a possible explanation (also check other parts of the page):
https://github.com/Psychtoolbox-3/Psychtoolbox-3/wiki/FAQ%3A-Performance-Tuning#preloading-of-functions

Best,
Andreas

> Am 23.06.2018 um 20:32 schrieb mate.gyurkovics@... [PSYCHTOOLBOX] <PSYCHTOOLBOX@yahoogroups.com>:
>
>
>
> Sorry for the delay in responding, I was testing different versions of my task, and also trying to implement some of your suggestions.
>
>
>
> Thank you very much for every comment and idea – I am definitely a LOT more confident about my setup now.
>
>
>
> I used the Missed argument of the flip command to check which flips exactly were missed. After setting every fixed duration to a multiple of the refresh rate, the number of missed flips went down to about 3%. When I check which flips were missed, it seems most or all of them are flips that do not have a when argument specified because they cannot be timed to a previous flip. Since I see no way to improve this and timing of those flips is not of huge importance, I think I can live with this. Stimulus presentation seems very accurate based on the flip time stamps.
>
>
>
> An interesting thing I noticed is that the number of reported missed flips is increased when I run my task for the first time after opening Matlab. If I run anything before running the actual task script, e.g., even just a VBLSyncTest, the percentage of missed flips for the task will be lower. I’m not sure why this is.
>
>
>
> Once again, thank you for all your help.
>
>
>
>
O, thank you, one again, very useful links!

Another tangentially related question (sorry, I don't want to spam the message boards with lots of different topics):

I would like to present the target for a fixed amount of time within a longer response window. I want to give participants 3000 ms to respond, but the target would only be on screen for the first 200 ms. I tried to figure out how to do it, and this was the only solution that seemed to work. Is this okay? Seems to give accurate timing.

%flipping the target on screen, starting the trial
timeStart = Screen('Flip', mainwin, tDist + SOA - 0.5 * ifi); tic
    keypressed = 0; rt = 0; rt2 = 0; correct = 0; br = 0;
    while (GetSecs - timeStart) < 3 %response window is 3 s
        if br == 0
         if (GetSecs - timeStart) > (targetDuration - 0.5 * ifi) %didn't work with ==
           Screen('Flip', mainwin); tocs(iP) = toc;
           br = 1; %the reason I'm doing this is because without this, it would be flipping the empty screen on screen on every run of the while loop after target duration has been reached, right? and that would inflate the number of flips per trial
         end
        end
        [keyIsDown, secs, keyCode] = KbCheck;
        if keyIsDown
            nKeys = sum(keyCode);
            if nKeys==1
                if keyCode(Left)||keyCode(Right)||keyCode(Down)||keyCode(Up)
                    rt = 1000.*(secs-timeStart);
                    keypressed=find(keyCode);
                    break;
                elseif keyCode(escKey)
                    ShowCursor; fclose(outfile);  Screen('CloseAll'); return
                end
            end
        else
            keypressed = 0; %the resp column in output is 0 if no button is pressed
        end
    end