# C203 第 1 课：时钟、偏移与漂移

> C203 时间同步课程的第一课：“同步”的两种含义，一台采集设备能承受多大的时间误差，两个时钟为什么会漂开、会漂开多远，以及一帧的时间戳实际标记的是哪个瞬间，附算例。

Page: https://egocam.ai/zh-cn/university/c203/lesson-1-clocks-offset-and-drift/

采集流程中每一个下游步骤都假定自己知道每个采样是什么时候取得的。双目深度把左帧与右帧匹配，视觉惯性跟踪在帧与帧之间对 IMU 积分，手部三角化把一只正在运动的手的多个视角组合起来。这一课讲清时间同步的词汇和算术，这样后面的课才能说明每种方法有多好。完整的论述见[为什么同步很重要](https://egocam.ai/zh-cn/sync/why-sync-matters/)。

## 本课用到的术语

这些术语也都在[术语表的同步一节](https://egocam.ai/zh-cn/reference/glossary/)中。

| 术语 | 含义 |
|---|---|
| 时钟（Clock） | 由振荡器（通常是石英晶体）驱动的计数器。每台相机、每个 IMU 和每台计算机都有自己的时钟 |
| 时钟偏移（Clock offset） | 两个时钟在同一时刻的读数之差 |
| 时钟漂移（Clock drift, skew） | 两个时钟走速之间的差异；有漂移时，偏移会随时间变化 |
| ppm | 百万分之一（parts per million）。50 ppm 的速率误差意味着一个时钟每秒快或慢 50 µs |
| 时间戳（Timestamp） | 系统附加在一个采样上的时间；它标记的是哪个瞬间因设备而异 |
| 曝光中点（Exposure midpoint） | 传感器收集光的那段时间的中间时刻；通常是代表一帧的最佳单一瞬间 |
| 读出时间（Readout time） | 对卷帘快门传感器，读出一帧中每一行所用的时间 |
| 延迟（Latency） | 某件事发生的时刻与它被打上时间戳的时刻之间的延时；恒定的延迟就是一个偏移 |
| 抖动（Jitter） | 这个延时在一个采样与下一个采样之间的随机变化 |

## “同步”的两种含义

人们说*相机是同步的*，指的是两件不同的事情之一（[“同步”的两种含义](https://egocam.ai/zh-cn/sync/why-sync-matters/)）：

- **曝光同步**：各相机在同一瞬间拍摄。这需要共用的触发信号或帧定时信号。
- **时间戳同步**：每个采样都带有来自同一个时钟的时间，或者来自相互关系已知的多个时钟的时间。各帧可以在不同时刻拍摄，但你确切知道是什么时候。

这两者相互独立，有了第二种并不等于有了第一种。Meta 为多设备 Aria Gen 2 录制写的文档把每副眼镜都放到共同的时间基准上，随后指出 "the host and client devices' RGB cameras are NOT trigger aligned by nature"（意为：主机设备和客户端设备的 RGB 相机本来就不是触发对齐的）（[Aria Gen 2 时间对齐教程](https://github.com/facebookresearch/projectaria_tools/blob/main/examples/Gen2/python_notebooks/Tutorial_4_timestamp_alignment.ipynb)）。RocSync 的作者给出了这对基于时钟的方法意味着什么："residual misalignment of up to half a frame can remain even when pairing the closest timecodes between two video streams"（意为：即使把两路视频流中最接近的时间码配对，仍可能残留最多半帧的错位）（[Meyer et al., 2025](https://arxiv.org/abs/2511.14948)）。

> **Diagram:** Two cameras at 30 frames per second whose frames start at different moments. A hand hits the table at 72 ms; camera A first shows it in the frame starting at 66.7 ms and camera B in the frame starting at 81.7 ms. Matching each event to the nearest frame leaves an error of up to one frame, 33 ms at 30 fps, unless the exposures themselves are synchronised.

## 误差预算

一台采集设备能容忍多大的时间误差，取决于图像中物体运动得多快。距离等于速度乘以时间，所以算术很简单。实用指南的[算例](https://egocam.ai/zh-cn/sync/why-sync-matters/)使用以 1 m/s 运动的手和以 100 °/s 转动的头部，两者都是作为示例值给出的，不是实测的典型值：

| 时间误差 | 以 1 m/s 运动的手移动了 | 头部以 100 °/s 转动，图像宽 1280 px，视场 90°：图像中心附近的像移 |
|---|---|---|
| 1 ms | 1 mm | 约 1.1 px |
| 10 ms | 10 mm | 约 11 px |
| 16.7 ms（30 fps 下的半帧） | 16.7 mm | 约 19 px |
| 33.3 ms（30 fps 下的一帧） | 33.3 mm | 约 37 px |

用你自己任务的速度和你自己的镜头把这张表重算一遍。已发表的结果显示了这种大小的误差在下游会造成什么。[Ling et al. (2018)](https://arxiv.org/abs/1810.05456) 在 EuRoC 基准上用人为加入的偏移做测试，报告说 "OKVIS fails to track in sequence 2 ∼ 5 when the time offset is set to be 60 ms."（意为：当时间偏移设为 60 ms 时，OKVIS 在序列 2 至 5 上跟踪失败。）对于多视角下的手，RocSync 发现插值到共同的瞬间 "reduced the mean Euclidean distance between poses from 26.07 mm without interpolation to 7.52 mm with interpolation"（意为：把位姿之间的平均欧氏距离从不插值时的 26.07 mm 降到了插值时的 7.52 mm）（[Meyer et al., 2025](https://arxiv.org/abs/2511.14948)）。

## 偏移和漂移

每台设备都用自己的振荡器计时，没有两个振荡器的走速完全相同。在一次录制的时间内，两个时钟之间的关系可以很好地近似为一条直线（[时钟偏移与时钟漂移](https://egocam.ai/zh-cn/sync/why-sync-matters/)）：

时钟 B 上的时间 = (1 + 漂移) × 时钟 A 上的时间 + 偏移

测量一次偏移能消除偏移，但消除不了漂移。漂移通常以 ppm 给出，它累积得很快：

| 速率差 | 每小时的漂移 | 30 fps 下的帧数 |
|---|---|---|
| 1 ppm | 3.6 ms | 0.11 |
| 20 ppm | 72 ms | 2.2 |
| 50 ppm | 180 ms | 5.4 |
| 100 ppm | 360 ms | 10.8 |

两个时钟的误差会相加。Fora Soft 的指南指出："Consumer-grade crystals are typically rated at ±20 to ±50 ppm, so two ordinary devices can differ from each other by as much as 100 ppm in the worst case"（意为：消费级晶体的标称精度通常为 ±20 至 ±50 ppm，所以两台普通设备在最坏情况下彼此可相差多达 100 ppm）（[Fora Soft](https://www.forasoft.com/learn/audio-for-video/articles-audio/drift-correction-resampling-frame-drop)）。在 50 ppm 下，两台 30 fps 的相机大约 5.6 分钟就会漂开半帧。设备内部的传感器可能比它的主时钟差得多：GoPro 的文档说 "Camera internal sensors or data via a connected Bluetooth device, will have timing errors in the range ±1%"（意为：相机内部传感器或经由已连接蓝牙设备传来的数据，会有 ±1% 范围内的时序误差）（[gpmf-parser](https://github.com/gopro/gpmf-parser)）。从手机到眼动仪再到头显，真实设备上实测的漂移列在[真实设备上实测的漂移](https://egocam.ai/zh-cn/sync/why-sync-matters/)的表中。

### 一个同步点不够

在一次采集开头做的单次同步会逐渐失效。GoPro 自己的[精确时间页面](https://gopro.github.io/labs/control/precisiontime/)说："As the camera’s internal clock will drift slowly over time, use this QR Code just before your multi-camera shoot for the best synchronization."（意为：由于相机的内部时钟会随时间缓慢漂移，请在多相机拍摄即将开始前使用这个二维码，以获得最佳同步。）解决办法是两个同步点，一个在开头，一个在结尾，并在两者之间连一条直线。有了两个点，你就同时得到偏移和漂移。

> **Diagram:** Clock drift between two cameras over an hour. Sync readings at the start give an offset of about 1240 ms and readings at the end, sixty minutes later, about 1312 ms. A straight line through both gives the offset for every frame in between; 72 ms of drift in an hour means the clocks run 20 parts per million apart.

**算例**（来自[实用指南](https://egocam.ai/zh-cn/sync/why-sync-matters/)）。在一次录制的开头，相机 B 的读数比相机 A 快 1240 ms，60 分钟后快 1312 ms。漂移是 (1312 − 1240) ms ÷ 3,600,000 ms = 20 ppm。在 30 分钟处，偏移是 1240 + 72 × 0.5 = 1276 ms。如果只用开头的偏移，结尾附近的帧会差出 72 ms，在 30 fps 下超过两帧。

这条直线假定走速保持稳定。实际上并不完全如此：[chrony FAQ](https://chrony-project.org/faq.html) 指出，时钟的稳定性 "mainly depends on the temperature sensitivity of the crystal oscillator and the maximum rate of the temperature change"（意为：主要取决于晶体振荡器的温度敏感性和温度变化的最大速率）。戴在人头上会逐渐变热的采集设备，用多于两个同步点或用连续同步会更好。

## 时间戳标记的是哪个瞬间

一帧不是在一个瞬间拍下的：传感器在曝光时间内收集光，然后图像被读出、处理和发送。时间戳可以标记这些时刻中的任何一个（[时间戳标记的是哪个瞬间](https://egocam.ai/zh-cn/sync/why-sync-matters/)）。Linux 的视频 API 给其中两个起了名字：一个缓冲区标志表示 "End Of Frame"（意为：帧结束），另一个表示 "Start Of Exposure. The buffer timestamp has been taken when the exposure of the frame has begun"（意为：曝光开始。缓冲区时间戳取自该帧曝光开始之时）（[V4L2 buffers](https://docs.kernel.org/userspace-api/media/v4l/buffer.html)）。

对于运动，最佳的单一瞬间是曝光的中点。Kalibr 的论文指出 "The middle of the exposure time constitutes the ideal point to timestamp an image"（意为：曝光时间的中点是给图像打时间戳的理想时刻）（[Furgale, Rehder and Siegwart, 2013](https://furgalep.github.io/bib/furgale_iros13.pdf)）。原因是：像素在整个曝光期间累加光，所以一个运动的点会留下一道拖影，在匀速运动时，拖影的中心就是这个点在曝光进行到一半时所在的位置（[曝光中点](https://egocam.ai/zh-cn/sync/why-sync-matters/)）。两台相机在同一瞬间被触发，一台曝光 1 ms，另一台曝光 10 ms，即使触发是完美的，它们的中点也相差 4.5 ms。

> **Diagram:** Two cameras triggered at the same instant but exposing for 1 ms and 10 ms. Their exposure midpoints, the instants the frames best represent, are 4.5 ms apart (top). Starting the longer exposure half its length earlier, as exposure-compensated triggering does, puts both midpoints at the same instant (bottom).

在卷帘快门传感器上，每一行还有它自己的时间：最后一行比第一行晚一个读出时间被采集。一种常用的近似是把这一帧当作在它的中间一行拍下的（[卷帘快门的行时序](https://egocam.ai/zh-cn/sync/why-sync-matters/)）。

## 延迟与抖动

在一个采样被取得的时刻和它被打上时间戳的时刻之间，有一段延时（[延迟与抖动](https://egocam.ai/zh-cn/sync/why-sync-matters/)）：

- **延迟**，即恒定的那部分，看起来与时钟偏移完全一样，消除的方法也一样，就是测量它一次。
- **抖动**，即随机的那部分，无法通过标定去掉；可以通过在更靠近传感器的地方打时间戳来减小它，或者通过对许多采样拟合一个模型来平滑它。
- **漂移**两者都不是。Fora Soft 的判断方法："A slow, monotonic slide is always a clock mismatch, never network jitter."（意为：缓慢、单调的滑移一定是时钟不匹配，绝不是网络抖动。）

由计算机在数据到达时打时间戳，抖动最严重。Faizullin et al. 测量了一个数据包每 1328 µs 到达一次的传感器：由 ROS 在到达时打时间戳，周期变化的标准差为 82.64 µs，而由微控制器打时间戳时为 1.35 µs（[Faizullin et al., 2021](https://arxiv.org/abs/2107.02625)）。

## 自测

1. 你的头戴设备和一台外部相机都连在一台网络时间服务器上，它们的时钟彼此相差远小于一毫秒。它们的帧是一起曝光的吗？
2. 两台 30 fps 的相机使用消费级晶体，彼此相差 50 ppm。大约多久之后它们会相差半帧？
3. 你只在一小时采集的开头用二维码同步了两台相机。你无法测量什么？你要加上什么？
4. 一台相机在曝光开始时给帧打时间戳，并使用自动曝光。这对把它与另一台相机配对为什么有影响？
5. 两台设备之间的偏移在帧与帧之间跳动几毫秒，但没有斜率。这是漂移吗？

### 答案

1. 不一定。时钟同步给出的是时间戳同步，不是曝光同步；自由运行的相机仍然可能相差最多半帧。
2. 大约 5.6 分钟：30 fps 下的半帧是 16.7 ms，而 50 ppm 是每秒 50 µs。
3. 漂移。在结尾加上第二个同步事件，并过这两个点拟合一条直线。
4. 代表这一帧的瞬间是曝光中点，在时间戳之后半个曝光时间，而使用自动曝光时这个间隔每帧都在变。
5. 不是。那是抖动；漂移是朝一个方向缓慢而稳定的滑移。

## 参考来源

- 实用指南：[为什么同步很重要](https://egocam.ai/zh-cn/sync/why-sync-matters/)、[术语表](https://egocam.ai/zh-cn/reference/glossary/)。
- Meta Project Aria: [Gen 2 time alignment tutorial](https://github.com/facebookresearch/projectaria_tools/blob/main/examples/Gen2/python_notebooks/Tutorial_4_timestamp_alignment.ipynb).
- Meyer, J. et al. (2025). [RocSync: Millisecond-accurate temporal synchronization for heterogeneous camera systems](https://arxiv.org/abs/2511.14948). arXiv.
- Ling, Y. et al. (2018). [Modeling varying camera-IMU time offset in optimization-based visual-inertial odometry](https://arxiv.org/abs/1810.05456). *ECCV*.
- Fora Soft. [Drift correction: resampling, frame drop, frame insertion](https://www.forasoft.com/learn/audio-for-video/articles-audio/drift-correction-resampling-frame-drop).
- GoPro. [gpmf-parser](https://github.com/gopro/gpmf-parser); GoPro Labs, [precision date and time](https://gopro.github.io/labs/control/precisiontime/).
- chrony. [Frequently asked questions](https://chrony-project.org/faq.html).
- Linux kernel documentation. [V4L2 buffers](https://docs.kernel.org/userspace-api/media/v4l/buffer.html).
- Furgale, P., Rehder, J. and Siegwart, R. (2013). [Unified temporal and spatial calibration for multi-sensor systems](https://furgalep.github.io/bib/furgale_iros13.pdf). *IEEE/RSJ IROS*.
- Faizullin, M., Kornilova, A. and Ferrer, G. (2021). [Open-source LiDAR time synchronization system by mimicking GNSS-clock](https://arxiv.org/abs/2107.02625). arXiv.
