# Timing between laser and stimulus onset

**URL:** <https://mworks.discourse.group/t/timing-between-laser-and-stimulus-onset/1082>\
**Category:** Support\
**Created:** [March 24, 2025, 5:46pm UTC](https://mworks.discourse.group/t/timing-between-laser-and-stimulus-onset/1082 "2025-03-24T17:46:55Z")\
**Posts on this page:** 13\
**Page:** 2

<div class="post-metadata">

**Author:** ![taylor.xie](https://avatars.discourse-cdn.com/v4/letter/t/9fc348/32.png) [@taylor.xie](https://mworks.discourse.group/u/taylor.xie)\
**Post date:** [April 29, 2025, 2:53pm UTC](https://mworks.discourse.group/t/timing-between-laser-and-stimulus-onset/1082/21 "2025-04-29T14:53:02Z")

</div>

Hi Chris,

I tried to upload on the website but new users can’t upload files. So I’ve attached a pdf version of the file below. Let me know if this works.

Best,  
Taylor

> **(attachments)**
>
> [timing.pdf](https://mworks.discourse.group/uploads/short-url/iI7rQhZwyzkIUP0SxeAsJGPJlkV.pdf) (21 KB)

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [April 29, 2025, 3:36pm UTC](https://mworks.discourse.group/t/timing-between-laser-and-stimulus-onset/1082/22 "2025-04-29T15:36:41Z")

</div>

Hi Taylor,

> I tried to upload on the website but new users can’t upload files.

Sorry about that. I changed your permissions to “basic user”, so I think you’ll be able to upload files in the future.

> I’ve attached a pdf version of the file below. Let me know if this works.

Yes, I received it. Thanks! I will take a look and get back to you.

Chris

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [April 29, 2025, 4:00pm UTC](https://mworks.discourse.group/t/timing-between-laser-and-stimulus-onset/1082/23 "2025-04-29T16:00:54Z")

</div>

Hi Taylor,

Are these times in milliseconds?

The numbers look a little suspect to me. Specifically: In rows 2-9, the value of `actual_predicted_display_update_time` is identical to the value of `extrapolated_predicted_display_update_time` from the previous row. That can’t be right.

Also, where in your experiment are you calling `next_frame_time()` and storing its value?

Thanks,  
Chris

---

<div class="post-metadata">

**Author:** ![taylor.xie](https://avatars.discourse-cdn.com/v4/letter/t/9fc348/32.png) [@taylor.xie](https://mworks.discourse.group/u/taylor.xie)\
**Post date:** [April 29, 2025, 5:02pm UTC](https://mworks.discourse.group/t/timing-between-laser-and-stimulus-onset/1082/24 "2025-04-29T17:02:56Z")

</div>

Hi Chris,

I am reporting all three times at the end of the “Stimulus-On” state. And yes, they are in milliseconds.

I just tried to put them right after I cued the delay\_stim\_onset and the attachment is what I got. I was indeed unsure about the placement. Where should I put them for the right report?

Best,  
Taylor

> **(attachments)**
>
> [timing2.txt](https://mworks.discourse.group/uploads/short-url/vL3L2bXmR3i4pOsafixcfsA8ukj.txt) (17 KB)

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [April 29, 2025, 9:03pm UTC](https://mworks.discourse.group/t/timing-between-laser-and-stimulus-onset/1082/25 "2025-04-29T21:03:10Z")

</div>

Hi Taylor,

You need to wait until `actual_predicted_display_update_time` is set, which doesn’t happen until the stimuli are revealed.

Probably the best approach would be to record the prediction error inside the body of `delay_stim_onset`. First, define a new variable:

```auto
var extrapolated_predicted_display_update_time = 0
var actual_predicted_display_update_time = 0
var predicted_display_update_time_error = 0 // This is new

```

then set it right after setting `actual_predicted_display_update_time`:

```auto
if (refreshes_until_stim_onset == 0) {
    // Record the actual predicted display update time for stimulus
    // onset
    actual_predicted_display_update_time = next_frame_time()

    // The next line is new
    predicted_display_update_time_error = extrapolated_predicted_display_update_time - actual_predicted_display_update_time

    // Reveal the stimulus
    stimulus_alpha_multiplier = 1
    distractor_alpha_multiplier = 1
}

```

There’s no need to record the value of `next_frame_time()` separately, since it’s already assigned to `actual_predicted_display_update_time`.

Sorry for not being clearer about this previously.

Chris

---

<div class="post-metadata">

**Author:** ![taylor.xie](https://avatars.discourse-cdn.com/v4/letter/t/9fc348/32.png) [@taylor.xie](https://mworks.discourse.group/u/taylor.xie)\
**Post date:** [April 30, 2025, 2:25pm UTC](https://mworks.discourse.group/t/timing-between-laser-and-stimulus-onset/1082/26 "2025-04-30T14:25:09Z")

</div>

Hi Chris,

Thank you and I just recorded a file. The prediction error started at -0.144 and increased to 0.856, then 1.856, and stabilized at 2.856. I’ve attached a .txt of the console if that helps.

Best,  
Taylor  
[timing3.txt](https://mworks.discourse.group/uploads/short-url/Au47y7eFS9l5cZZed0qu9NvzoN2.txt) (14.0 KB)

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [May 1, 2025, 2:56pm UTC](https://mworks.discourse.group/t/timing-between-laser-and-stimulus-onset/1082/27 "2025-05-01T14:56:54Z")

</div>

Hi Taylor,

Did you report `predicted_display_update_time_error` in milliseconds, or are the logged values in the raw microseconds?

Either way, I don’t think the prediction error explains any of the increased latency you observed in the time between laser onset and photodiode onset. Since the error eventually settles at a positive value (2.856), the extrapolated display update time is _after_ the actual display update time. This will cause the experiment to wait too long before turning on the laser, meaning the time between laser onset and photodiode onset will be _less_ than it would be if the error were zero. (This assumes you calculate the error as extrapolated minus actual, as I suggested. Is that correct?)

Since the prediction error stabilizes over time, you could try using it to improve the accuracy of your waits. Specifically, you could subtract the prediction error from the previous trial when determining how long to wait before turning on the laser in the current trial. In code:

```auto
wait ((extrapolated_predicted_display_update_time - predicted_display_update_time_error - target_laser_padding_time) - now())
vpixx_dout_laser_gate = true

```

But if what I said above is correct, this is only going to further increase the observed latency, which obviously doesn’t help with the problem you’re having.

Looking back through the discussion, I don’t think you answered my question about the photodiode location. Is it in the upper-left corner of the display, or somewhere else?

Thanks,  
Chris

---

<div class="post-metadata">

**Author:** ![taylor.xie](https://avatars.discourse-cdn.com/v4/letter/t/9fc348/32.png) [@taylor.xie](https://mworks.discourse.group/u/taylor.xie)\
**Post date:** [May 1, 2025, 4:14pm UTC](https://mworks.discourse.group/t/timing-between-laser-and-stimulus-onset/1082/28 "2025-05-01T16:14:30Z")

</div>

Hi Chris,

Thank you for the explanation.

I didn’t convert the time into milliseconds. And yes, I calculated it as following:  
predicted\_display\_update\_time\_error = extrapolated\_predicted\_display\_update\_time - actual\_predicted\_display\_update\_time  
report('predicted\_display\_update\_time\_error = $predicted\_display\_update\_time\_error’)

Sorry that I missed the question but yes, the photodiode is in the upper-left corner of the display.

Best,  
Taylor

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [May 1, 2025, 8:49pm UTC](https://mworks.discourse.group/t/timing-between-laser-and-stimulus-onset/1082/29 "2025-05-01T20:49:09Z")

</div>

Hi Taylor,

> I didn’t convert the time into milliseconds.

Oh, wow. So the extrapolated time matches the actual one to within a few _microseconds_? That’s a little hard to believe, but fantastic if true. Also, it confirms much more strongly that extrapolation error is not the source of the extra latency you’re seeing.

> Sorry that I missed the question but yes, the photodiode is in the upper-left corner of the display.

OK, good. No worries about that, then.

Another question I should have asked sooner: When you’re talking about the laser onset time, what specific event are you measuring? I’ve been assuming that you mean the time of the `e_laser_on` event as recorded by the Blackrock. Is that correct? If not, are you looking at a signal from the laser itself (e.g. from one of its “Light Sensor” BNC outputs, presumably also connected to the Blackrock)? If the latter, then it’s possible that the changing latency is due to the laser hardware, rather than anything MWorks is doing.

Chris

---

<div class="post-metadata">

**Author:** ![taylor.xie](https://avatars.discourse-cdn.com/v4/letter/t/9fc348/32.png) [@taylor.xie](https://mworks.discourse.group/u/taylor.xie)\
**Post date:** [May 2, 2025, 2:51pm UTC](https://mworks.discourse.group/t/timing-between-laser-and-stimulus-onset/1082/30 "2025-05-02T14:51:18Z")

</div>

Hi Chris,

I measure the laser onset in both ways: using event code and the light sensor. The difference between the event code and the actual light signal is very small though - on average about 0.7ms. So I don’t think using one or the other would vary the latency that much?

Best,  
Taylor

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [May 13, 2025, 2:34pm UTC](https://mworks.discourse.group/t/timing-between-laser-and-stimulus-onset/1082/31 "2025-05-13T14:34:13Z")

</div>

Hi Taylor,

Sorry for not responding sooner.

> The difference between the event code and the actual light signal is very small though - on average about 0.7ms. So I don’t think using one or the other would vary the latency that much?

I agree. Also, if the laser were adding extra latency, it would _decrease_ the time between laser onset and photodiode onset, not increase it. So even if the difference were substantial (which it is not), it couldn’t be causing the issue you’re seeing.

At this point, I don’t know what to suggest, other than that the OS’s predicted display update time is deviating from the actual display update time by up to 10 milliseconds. That isn’t something I’ve seen before – 2-3ms, yes, but not 10ms. That said, I’ve never worked with a VPixx display. As a workaround, I guess you could decrease the target laser padding time by 5-10ms, although that isn’t a very satisfying solution.

Do you have any other ideas about what might be going on?

Chris

---

<div class="post-metadata">

**Author:** ![taylor.xie](https://avatars.discourse-cdn.com/v4/letter/t/9fc348/32.png) [@taylor.xie](https://mworks.discourse.group/u/taylor.xie)\
**Post date:** [June 4, 2025, 2:22pm UTC](https://mworks.discourse.group/t/timing-between-laser-and-stimulus-onset/1082/32 "2025-06-04T14:22:50Z")

</div>

Hi Chris,

Sorry for the late response but I was sick for a while.

I am also not sure what could be added at this point. I think the current version is good enough for now and I will just decrease the target laser padding time a little as you suggested. Thank you!

Best,  
Taylor

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [August 5, 2025, 2:30pm UTC](https://mworks.discourse.group/t/timing-between-laser-and-stimulus-onset/1082/33 "2025-08-05T14:30:44Z")

</div>



[Previous page](https://mworks.discourse.group/t/timing-between-laser-and-stimulus-onset/1082.md?page=1)
