# Sending EEG triggers right at the time of stimulus presentation

**URL:** <https://psychtoolbox.discourse.group/t/sending-eeg-triggers-right-at-the-time-of-stimulus-presentation/132>\
**Category:** Uncategorized\
**Created:** [June 5, 2018, 10:03am UTC](https://psychtoolbox.discourse.group/t/sending-eeg-triggers-right-at-the-time-of-stimulus-presentation/132 "2018-06-05T10:03:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mate.gyurkovics](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@mate.gyurkovics](https://psychtoolbox.discourse.group/u/mate.gyurkovics)\
**Post date:** [June 5, 2018, 10:03am UTC](https://psychtoolbox.discourse.group/t/sending-eeg-triggers-right-at-the-time-of-stimulus-presentation/132/1 "2018-06-05T10:03:21Z")

</div>

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);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Screen('DrawingFinished', mainwin);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vbl = Screen('Flip', mainwin);

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

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

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Screen('DrawingFinished', mainwin);

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

&nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp; &nbsp; &nbsp; Screen('DrawTexture', mainwin, Distractor);

&nbsp; &nbsp; &nbsp; &nbsp; Screen('DrawingFinished', mainwin);

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

&nbsp; &nbsp; &nbsp; &nbsp; fwrite(s1, dist\_trig);&nbsp; %%%%%%% this is the EEG trigger&nbsp;%%%%%%%&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; for frames2 = 1:distFrames-1

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Screen('DrawTexture', mainwin, Distractor);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Screen('DrawingFinished', mainwin);

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

&nbsp; &nbsp; &nbsp; &nbsp; end

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

&nbsp; &nbsp; &nbsp; &nbsp; flips = 1;

&nbsp; &nbsp; &nbsp; &nbsp; while (GetSecs - timeStart) \< 3&nbsp; %%%participants have 3 seconds to respond%%%

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Screen('DrawTexture', mainwin, Target);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Screen('DrawingFinished', mainwin);

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

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if flips == 1

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

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flips = flips + 1;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [keyIsDown, secs, keyCode] = KbCheck;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FlushEvents('keyDown');

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if keyIsDown

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nKeys = sum(keyCode);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if nKeys==1

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if keyCode(Left)||keyCode(Right)||keyCode(Down)||keyCode(Up)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rt = 1000.\*(secs-timeStart);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keypressed=find(keyCode);

%&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fwrite(s1, trigger2);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif keyCode(escKey)

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

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keyIsDown=0; keyCode=0;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else

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

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp; &nbsp; &nbsp; end

---

<div class="post-metadata">

**Author:** ![mate.gyurkovics](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@mate.gyurkovics](https://psychtoolbox.discourse.group/u/mate.gyurkovics)\
**Post date:** [June 5, 2018, 10:22am UTC](https://psychtoolbox.discourse.group/t/sending-eeg-triggers-right-at-the-time-of-stimulus-presentation/132/2 "2018-06-05T10:22:00Z")

</div>

(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.)

---

<div class="post-metadata">

**Author:** ![andreaswidmann](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@andreaswidmann](https://psychtoolbox.discourse.group/u/andreaswidmann)\
**Post date:** [June 6, 2018, 6:18pm UTC](https://psychtoolbox.discourse.group/t/sending-eeg-triggers-right-at-the-time-of-stimulus-presentation/132/3 "2018-06-06T18:18:40Z")

</div>

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@...](mailto:mate.gyurkovics@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto: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  
> \>   
> \>   
> \>   
> \>   
> \>   
> \>

---

<div class="post-metadata">

**Author:** ![mate.gyurkovics](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@mate.gyurkovics](https://psychtoolbox.discourse.group/u/mate.gyurkovics)\
**Post date:** [June 8, 2018, 3:10pm UTC](https://psychtoolbox.discourse.group/t/sending-eeg-triggers-right-at-the-time-of-stimulus-presentation/132/4 "2018-06-08T15:10:17Z")

</div>

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.&nbsp;

  

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

&nbsp; &nbsp; &nbsp; &nbsp; t0 = Screen('Flip', mainwin);

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; % Distractor for 150 ms

&nbsp; &nbsp; &nbsp; &nbsp; Screen('DrawTexture', mainwin, Distractor);

&nbsp; &nbsp; &nbsp; &nbsp; tDist = Screen('Flip', mainwin, t0 + blankDur - 0.5 \* ifi);

%&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fwrite(s1, dist\_trig); %%%%%%% this is the EEG trigger %%%%%%%

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

% Target for max 3 s

&nbsp; &nbsp; &nbsp; &nbsp; Screen('DrawTexture', mainwin, Target);

&nbsp; &nbsp; &nbsp; &nbsp; timeStart = Screen('Flip', mainwin, tDist + SOA - 0.5 \* ifi);

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

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

&nbsp; &nbsp; &nbsp; &nbsp; keypressed = 0; rt = 0; rt2 = 0; correct = 0;

&nbsp; &nbsp; &nbsp; &nbsp; while (GetSecs - timeStart) \< 3 %%% I switched back to KbCheck and while loop for the reasons stated above

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [keyIsDown, secs, keyCode] = KbCheck;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FlushEvents('keyDown');

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if keyIsDown

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nKeys = sum(keyCode);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if nKeys==1

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if keyCode(Left)||keyCode(Right)||keyCode(Down)||keyCode(Up)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rt = 1000.\*(secs-timeStart);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keypressed=find(keyCode);

%&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fwrite(s1, trigger2);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif keyCode(escKey)

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

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keyIsDown=0; keyCode=0;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else

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

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp; &nbsp; &nbsp; 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

  

&nbsp; &nbsp; &nbsp; &nbsp; if keypressed == 0 %indicates timeout errors

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DrawFormattedText(mainwin, 'TOO SLOW', 'center', 'center', errorcol);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Screen('DrawingFinished', mainwin);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ITIstart = Screen('Flip', mainwin, Fbstart + fbDur - 0.5 \* ifi);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ITIsec = ITIs(z) / 1000; %%%%%%%%% randomly drawn from a pool of ITIs

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Screen('Flip', mainwin, ITIstart + ITIsec - 0.5 \* ifi);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; else

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if keyCode(Left) && (Target == lll || Target == rlr || Target == dld || Target == ulu)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; correct = 1;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ITIsec = ITIs(z) / 1000; full\_dur = fbDur + ITIsec;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int\_start = Screen('Flip', mainwin);&nbsp;%%%%%%%%% once again, no when argument can be specified for this

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Screen('Flip', mainwin, int\_start + full\_dur - 0.5 \* ifi);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif keyCode(Right) && (Target == lrl || Target == rrr || Target == drd || Target == uru)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; correct = 1;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ITIsec = ITIs(z) / 1000; full\_dur = fbDur + ITIsec;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int\_start = Screen('Flip', mainwin);&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Screen('Flip', mainwin, int\_start + full\_dur - 0.5 \* ifi);&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif keyCode(Down) && (Target == ldl || Target == rdr || Target == ddd || Target == udu)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; correct = 1;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ITIsec = ITIs(z) / 1000; full\_dur = fbDur + ITIsec;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int\_start = Screen('Flip', mainwin);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Screen('Flip', mainwin, int\_start + full\_dur - 0.5 \* ifi);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif keyCode(Up) && (Target == lul || Target == rur || Target == dud || Target == uuu)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; correct = 1;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ITIsec = ITIs(z) / 1000; full\_dur = fbDur + ITIsec;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int\_start = Screen('Flip', mainwin);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Screen('Flip', mainwin, int\_start + full\_dur - 0.5 \* ifi);&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; corrs(z) = 0;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DrawFormattedText(mainwin, 'ERROR', 'center', 'center', errorcol);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Fbstart = Screen('Flip', mainwin);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ITIstart = Screen('Flip', mainwin, Fbstart + fbDur - 0.5 \* ifi); tic

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ITIsec = ITIs(z) / 1000;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Screen('Flip', mainwin, ITIstart + ITIsec - 0.5 \* ifi); tocs(tc) = toc; tc = tc + 1;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp; &nbsp; &nbsp; end

  

  

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

---

<div class="post-metadata">

**Author:** ![andreaswidmann](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@andreaswidmann](https://psychtoolbox.discourse.group/u/andreaswidmann)\
**Post date:** [June 9, 2018, 11:12am UTC](https://psychtoolbox.discourse.group/t/sending-eeg-triggers-right-at-the-time-of-stimulus-presentation/132/5 "2018-06-09T11:12:27Z")

</div>

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-](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](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?  
> \>   
> \>   
> \>

---

<div class="post-metadata">

**Author:** ![mate.gyurkovics](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@mate.gyurkovics](https://psychtoolbox.discourse.group/u/mate.gyurkovics)\
**Post date:** [June 9, 2018, 6:34pm UTC](https://psychtoolbox.discourse.group/t/sending-eeg-triggers-right-at-the-time-of-stimulus-presentation/132/6 "2018-06-09T18:34:06Z")

</div>

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é

---

<div class="post-metadata">

**Author:** ![andreaswidmann](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@andreaswidmann](https://psychtoolbox.discourse.group/u/andreaswidmann)\
**Post date:** [June 11, 2018, 9:59am UTC](https://psychtoolbox.discourse.group/t/sending-eeg-triggers-right-at-the-time-of-stimulus-presentation/132/7 "2018-06-11T09:59:24Z")

</div>

> \> 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@...](mailto:mate.gyurkovics@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto: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é  
> \>   
> \>   
> \>   
> \>

---

<div class="post-metadata">

**Author:** ![mate.gyurkovics](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@mate.gyurkovics](https://psychtoolbox.discourse.group/u/mate.gyurkovics)\
**Post date:** [June 12, 2018, 2:49pm UTC](https://psychtoolbox.discourse.group/t/sending-eeg-triggers-right-at-the-time-of-stimulus-presentation/132/8 "2018-06-12T14:49:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![andreaswidmann](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@andreaswidmann](https://psychtoolbox.discourse.group/u/andreaswidmann)\
**Post date:** [June 12, 2018, 4:03pm UTC](https://psychtoolbox.discourse.group/t/sending-eeg-triggers-right-at-the-time-of-stimulus-presentation/132/9 "2018-06-12T16:03:38Z")

</div>

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@...](mailto:mate.gyurkovics@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto: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  
> \>   
> \>   
> \>

---

<div class="post-metadata">

**Author:** ![mate.gyurkovics](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@mate.gyurkovics](https://psychtoolbox.discourse.group/u/mate.gyurkovics)\
**Post date:** [June 13, 2018, 10:26am UTC](https://psychtoolbox.discourse.group/t/sending-eeg-triggers-right-at-the-time-of-stimulus-presentation/132/10 "2018-06-13T10:26:50Z")

</div>

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

&nbsp; &nbsp; t0 = Screen('Flip', mainwin);

&nbsp; &nbsp; DrawFormattedText(mainwin, 'Distractor', 'center', 'center', textcolor);

&nbsp; &nbsp; tDist = Screen('Flip', mainwin, t0 + blankDur - 0.5 \* ifi);

&nbsp; &nbsp; %&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fwrite(s1, dist\_trig);

&nbsp; &nbsp; DrawFormattedText(mainwin, 'Target', 'center', 'center', textcolor);

&nbsp; &nbsp; timeStart = Screen('Flip', mainwin, tDist + SOA - 0.5 \* ifi); %%%this is the problematic flip

&nbsp; &nbsp; %&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fwrite(s1, trigger);

&nbsp; &nbsp; keypressed = 0; rt = 0; rt2 = 0; correct = 0;

&nbsp; &nbsp; while (GetSecs - timeStart) \< 1

&nbsp; &nbsp; &nbsp; &nbsp; [keyIsDown, secs, keyCode] = KbCheck;

&nbsp; &nbsp; &nbsp; &nbsp; FlushEvents('keyDown'); %still haven't removed this, sorry

&nbsp; &nbsp; &nbsp; &nbsp; if keyIsDown

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nKeys = sum(keyCode);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if nKeys==1

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp; &nbsp; &nbsp; else

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keypressed = 0;

&nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp; end

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; actual\_blank(i) = tDist - t0;&nbsp;

&nbsp; &nbsp; dist\_flip\_delay(i) = tDist - (t0 + blankDur - 0.5 \* ifi);&nbsp;

&nbsp; &nbsp; actual\_distractor(i) = timeStart - tDist;&nbsp;

&nbsp; &nbsp; targ\_flip\_delay(i) = timeStart - (tDist + SOA - 0.5 \* ifi);&nbsp;

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.

---

<div class="post-metadata">

**Author:** ![andreaswidmann](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@andreaswidmann](https://psychtoolbox.discourse.group/u/andreaswidmann)\
**Post date:** [June 13, 2018, 11:49am UTC](https://psychtoolbox.discourse.group/t/sending-eeg-triggers-right-at-the-time-of-stimulus-presentation/132/11 "2018-06-13T11:49:18Z")

</div>

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@...](mailto:mate.gyurkovics@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto:PSYCHTOOLBOX@yahoogroups.com)\>:  
> \>   
> \> [Attachment(s) from [mate.gyurkovics@...](mailto: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.  
> \>   
> \>   
> \>   
> \>   
> \>   
> \>

---

<div class="post-metadata">

**Author:** ![mate.gyurkovics](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@mate.gyurkovics](https://psychtoolbox.discourse.group/u/mate.gyurkovics)\
**Post date:** [June 13, 2018, 1:07pm UTC](https://psychtoolbox.discourse.group/t/sending-eeg-triggers-right-at-the-time-of-stimulus-presentation/132/12 "2018-06-13T13:07:47Z")

</div>

While I try to figure out how to share the figures with you, here's the output of that code:
  

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

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

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

timeStart – timeStartReq: 0.0056&nbsp; &nbsp;&nbsp;0.0404&nbsp;&nbsp;&nbsp; 0.0334&nbsp;&nbsp;&nbsp; 0.0265&nbsp;&nbsp;&nbsp; 0.0265&nbsp;&nbsp;&nbsp; 0.0403&nbsp;&nbsp;&nbsp; 0.0403&nbsp;&nbsp;&nbsp; 0.0334&nbsp;&nbsp;&nbsp; 0.0334&nbsp;&nbsp;&nbsp; 0.0334

tTarDrawn – tDist: 0.0077&nbsp;&nbsp;&nbsp; 0.0046&nbsp;&nbsp;&nbsp; 0.0051&nbsp;&nbsp;&nbsp; 0.0049&nbsp;&nbsp;&nbsp; 0.0055&nbsp;&nbsp;&nbsp; 0.0055&nbsp;&nbsp;&nbsp; 0.0055&nbsp;&nbsp;&nbsp; 0.0058&nbsp;&nbsp;&nbsp; 0.0057&nbsp;&nbsp;&nbsp; 0.0058

timeStart – tDist: 0.1528&nbsp;&nbsp;&nbsp; 0.1875&nbsp;&nbsp;&nbsp; 0.1806&nbsp;&nbsp;&nbsp; 0.1736&nbsp;&nbsp;&nbsp; 0.1736&nbsp;&nbsp;&nbsp; 0.1875&nbsp;&nbsp;&nbsp; 0.1875&nbsp;&nbsp;&nbsp; 0.1805&nbsp;&nbsp;&nbsp; 0.1805&nbsp;&nbsp;&nbsp; 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?&nbsp;

---

<div class="post-metadata">

**Author:** ![andreaswidmann](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@andreaswidmann](https://psychtoolbox.discourse.group/u/andreaswidmann)\
**Post date:** [June 13, 2018, 1:51pm UTC](https://psychtoolbox.discourse.group/t/sending-eeg-triggers-right-at-the-time-of-stimulus-presentation/132/13 "2018-06-13T13:51:08Z")

</div>

> \> 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@...](mailto:mate.gyurkovics@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto: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?   
> \>   
> \>   
> \>   
> \>

---

<div class="post-metadata">

**Author:** ![mate.gyurkovics](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@mate.gyurkovics](https://psychtoolbox.discourse.group/u/mate.gyurkovics)\
**Post date:** [June 13, 2018, 2:31pm UTC](https://psychtoolbox.discourse.group/t/sending-eeg-triggers-right-at-the-time-of-stimulus-presentation/132/14 "2018-06-13T14:31:59Z")

</div>

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&nbsp; 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.

&nbsp;

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

PTB-DEBUG: Checking for DWM desktop compositor support...&nbsp; ... 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'

&nbsp;

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

&nbsp;-\> Ok

# CREATOR: GIMP PNM Filter Version 1.1

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

&nbsp;

&nbsp;

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

&nbsp;

&nbsp;

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

&nbsp;

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.

&nbsp;

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...

&nbsp;

&nbsp;

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...

&nbsp;

&nbsp;

&nbsp;

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)

&nbsp;

ans =

&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp; 1

&nbsp;

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...

---

<div class="post-metadata">

**Author:** ![andreaswidmann](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@andreaswidmann](https://psychtoolbox.discourse.group/u/andreaswidmann)\
**Post date:** [June 13, 2018, 6:40pm UTC](https://psychtoolbox.discourse.group/t/sending-eeg-triggers-right-at-the-time-of-stimulus-presentation/132/15 "2018-06-13T18:40:03Z")

</div>

> \> 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

---

<div class="post-metadata">

**Author:** ![andreaswidmann](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@andreaswidmann](https://psychtoolbox.discourse.group/u/andreaswidmann)\
**Post date:** [June 17, 2018, 8:28am UTC](https://psychtoolbox.discourse.group/t/sending-eeg-triggers-right-at-the-time-of-stimulus-presentation/132/16 "2018-06-17T08:28:54Z")

</div>

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@...](mailto:mate.gyurkovics@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto: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  
> \>   
> \>   
> \>

---

<div class="post-metadata">

**Author:** ![mariokleiner](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@mariokleiner](https://psychtoolbox.discourse.group/u/mariokleiner)\
**Post date:** [June 17, 2018, 4:31pm UTC](https://psychtoolbox.discourse.group/t/sending-eeg-triggers-right-at-the-time-of-stimulus-presentation/132/17 "2018-06-17T16:31:11Z")

</div>

\<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@...](mailto:mate.gyurkovics@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto: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  
> > \>   
> > \>   
> > \>

---

<div class="post-metadata">

**Author:** ![mate.gyurkovics](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@mate.gyurkovics](https://psychtoolbox.discourse.group/u/mate.gyurkovics)\
**Post date:** [June 23, 2018, 6:32pm UTC](https://psychtoolbox.discourse.group/t/sending-eeg-triggers-right-at-the-time-of-stimulus-presentation/132/18 "2018-06-23T18:32:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![andreaswidmann](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@andreaswidmann](https://psychtoolbox.discourse.group/u/andreaswidmann)\
**Post date:** [June 25, 2018, 10:11am UTC](https://psychtoolbox.discourse.group/t/sending-eeg-triggers-right-at-the-time-of-stimulus-presentation/132/19 "2018-06-25T10:11:29Z")

</div>

> \> 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](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@...](mailto:mate.gyurkovics@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto: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.  
> \>   
> \>   
> \>   
> \>

---

<div class="post-metadata">

**Author:** ![mate.gyurkovics](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@mate.gyurkovics](https://psychtoolbox.discourse.group/u/mate.gyurkovics)\
**Post date:** [June 26, 2018, 3:50pm UTC](https://psychtoolbox.discourse.group/t/sending-eeg-triggers-right-at-the-time-of-stimulus-presentation/132/20 "2018-06-26T15:50:55Z")

</div>

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

&nbsp; &nbsp; keypressed = 0; rt = 0; rt2 = 0; correct = 0; br = 0;

&nbsp; &nbsp; while (GetSecs - timeStart) \< 3 %response window is 3 s

&nbsp; &nbsp; &nbsp; &nbsp; if br == 0

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (GetSecs - timeStart) \> (targetDuration - 0.5 \* ifi) %didn't work with ==

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Screen('Flip', mainwin); tocs(iP) = toc;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;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

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end

&nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp; &nbsp; &nbsp; [keyIsDown, secs, keyCode] = KbCheck;

&nbsp; &nbsp; &nbsp; &nbsp; if keyIsDown

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nKeys = sum(keyCode);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if nKeys==1

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if keyCode(Left)||keyCode(Right)||keyCode(Down)||keyCode(Up)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rt = 1000.\*(secs-timeStart);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keypressed=find(keyCode);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif keyCode(escKey)

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

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp; &nbsp; &nbsp; else

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

&nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp; end
