Syncing Psychtoolbox timing to ni-daq time

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:

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

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

You should read:

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.

It is open source, just read the C code: Psychtoolbox-3/PsychSourceGL/Source/Common/GetSecs at master · Psychtoolbox-3/Psychtoolbox-3 · GitHub

Also, help GetSecs for the simple way.