# Syncing Psychtoolbox timing to ni-daq time

**URL:** <https://psychtoolbox.discourse.group/t/syncing-psychtoolbox-timing-to-ni-daq-time/5253>\
**Category:** Programming Help\
**Tags:** windows\
**Created:** [February 8, 2024, 7:30pm UTC](https://psychtoolbox.discourse.group/t/syncing-psychtoolbox-timing-to-ni-daq-time/5253 "2024-02-08T19:30:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lt532](https://avatars.discourse-cdn.com/v4/letter/l/a9a28c/32.png) [@lt532](https://psychtoolbox.discourse.group/u/lt532)\
**Post date:** [February 8, 2024, 7:30pm UTC](https://psychtoolbox.discourse.group/t/syncing-psychtoolbox-timing-to-ni-daq-time/5253/1 "2024-02-08T19:30:05Z")

</div>

Hi Psychtoolbox community,

I am having some issues getting the timing offset between psychtoolbox stimulus timestamps and my daq. I am using a Windows 10 machine running MatLab 2022a and Psychtoolbox 3 (3.0.18 - Flavor: beta - Corresponds to SVN Revision 12968).

For my experiment, I am using a ni-daq to trigger psychtoolbox screen flips while acquiring videos and other signals. I am using the screen(“flip”) function in psychtoolbox to flip my patterns and saving the VBL timestand (and stimulus onsettime). From my understanding, this time is the seconds since the system’s last bootup time. However, adding the timestamps to the system’s last bootup time gives me a large time difference (~10+seconds) from the daq timestamp when the screen flip should have happened. From acquired experimental data, the neuron response/behavioral lag from when the ni-daq triggered the flip is on the order of ~100 ms, not 10+ seconds. The 10s time difference is not consistent and will depend on how long the computer has been running for.

As a sanity check, I tried the following:

```auto
[sys_bootTime] = getSystemBootTime([]);
sys_bootTime_conv = datetime(sys_bootTime.year,sys_bootTime.month,sys_bootTime.day,sys_bootTime.hour,...
sys_bootTime.min,floor(sys_bootTime.second),mod(sys_bootTime.second,1).*1000);
timeoffset = (sys_bootTime_conv+seconds(GetSecs))-datetime('now');
milliseconds(timeoffset)

```

timeoffset is on the same timescale as the difference seen in the experiments. Restarting the computer results in a small timeoffset. However, this timeoffset increases the longer the computer has been left running.

In the above section of code, the function getSystemBootTime() first calls a powershell script that gets the last boot time and then parses it into the year, month, day, hour, min, and seconds/milliseconds. Please see the following attached gist links for the getSystemBootTime.m and getSystemBootTime.ps1 files.

> <https://gist.github.com/ltao31/1856b49b38f6c53d1445f22f960a6997>

> <https://gist.github.com/ltao31/cac25da7a49c072a611838c415970e44>

Since GetSecs is a compiled mex file, I do not have access to the backend of the function to debug. So, I’m not sure if I was supposed to use a different time than the system bootup time, or if the bootup time is wrong, or if I should be doing something else.

Any help will be appreciated!  
-Liangyu

---

<div class="post-metadata">

**Author:** ![Ian-Max-Andolina](https://yyz2.discourse-cdn.com/free1/user_avatar/psychtoolbox.discourse.group/ian-max-andolina/32/4_2.png) [@Ian-Max-Andolina](https://psychtoolbox.discourse.group/u/Ian-Max-Andolina)\
**Post date:** [February 9, 2024, 12:40pm UTC](https://psychtoolbox.discourse.group/t/syncing-psychtoolbox-timing-to-ni-daq-time/5253/2 "2024-02-09T12:40:34Z")

</div>

You should read:

[http://psychtoolbox.org/docs/GetSecs](http://psychtoolbox.org/docs/GetSecs)

> GetSecs returns the time in seconds (with high precision). GetSecs uses  
> the highest precision realtime clock on each operating system. The zero  
> point (point in time where GetSecs would report a value of zero) is  
> operating system dependent and **nothing to be relied on**.

Using NI-DAQ to trigger PTB is not the optimal way to ensure precise stimulus timing in general. I don’t quite understand what your experiment paradigm is but you should be sending triggers based on the flip, not the other way round. This also needs to be confirmed with a photodiode test, as many monitors introduce other delays (unless you use a VPixx or Display++ that guarantee precise response time). You should look at the docs that come with PTB regarding timing to get a better idea of the best experimental approach.

> [@lt532](#):
>
> Since GetSecs is a compiled mex file

It is open source, just read the C code: [Psychtoolbox-3/PsychSourceGL/Source/Common/GetSecs at master · Psychtoolbox-3/Psychtoolbox-3 · GitHub](https://github.com/Psychtoolbox-3/Psychtoolbox-3/tree/master/PsychSourceGL/Source/Common/GetSecs)

---

<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:** [February 10, 2024, 3:21pm UTC](https://psychtoolbox.discourse.group/t/syncing-psychtoolbox-timing-to-ni-daq-time/5253/3 "2024-02-10T15:21:07Z")

</div>

Also, `help GetSecs` for the simple way.
