visionsim.emulate.itof package

Submodules

visionsim.emulate.itof.coding module

Waveform generation and coding schemes for iToF.

visionsim.emulate.itof.coding.unambiguous_range(scheme: Literal['convSin', 'deltaSin', 'convSquare', 'singleRamp', 'doubleRamp', 'deltaHilbertDimOne', 'deltaHilbertDimTwo', 'deltaHilbertDimThree', 'multFreqSin'], freq: float) → float[source]

Return the unambiguous depth range of a coding scheme, in metres.

Most schemes recover depth over a single code period, i.e. c / (2 f). The ramp codes (singleRamp, doubleRamp) put two periods in one code array, so their correlation function is only monotonic over c / (4 f); depths beyond that fold back into the reported range (see visionsim.emulate.itof.decoding.decode_single_ramp()).

Parameters:
  • scheme – Coding scheme identifier.

  • freq – Modulation frequency in Hz.

Returns:

Unambiguous depth range in metres.

visionsim.emulate.itof.coding.make_conv_sinusoidal_codes(n_captures: int, n_depths: int) → tuple[npt.NDArray, npt.NDArray][source]

Build conventional sinusoidal modulation and reference codes.

The emitter is modulated with 0.5 * (1 + cos(2*pi*t/T)) and every capture mixes the returning light with the same waveform shifted by 2*pi*k/n_captures, which is the classic continuous-wave iToF waveform pair. The modulation code is normalized so that its sum over one period is one (unit emitted energy), while the reference code spans [0, 1].

Parameters:
  • n_captures – Number of captures (phase shifts).

  • n_depths – Number of time bins per period.

Returns:

Tuple of (modulation_codes, reference_codes), each of shape (n_captures, n_depths).

See also

visionsim.emulate.itof.decoding.decode() with scheme="convSin".

visionsim.emulate.itof.coding.make_delta_sinusoidal_codes(n_captures: int, n_depths: int) → tuple[npt.NDArray, npt.NDArray][source]

Build delta (impulse) sinusoidal modulation and reference codes.

Instead of emitting a sinusoid, each capture emits a single short pulse and keeps the sinusoidal reference waveform of make_conv_sinusoidal_codes(). Concentrating the emitted energy in time maximizes the signal that reaches the sensor, at the cost of a much weaker suppression of ambient light.

Parameters:
  • n_captures – Number of captures (phase shifts).

  • n_depths – Number of time bins per period.

Returns:

Tuple of (modulation_codes, reference_codes), each of shape (n_captures, n_depths).

See also

visionsim.emulate.itof.decoding.decode() with scheme="deltaSin".

visionsim.emulate.itof.coding.make_conv_square_codes(n_captures: int, n_depths: int) → tuple[npt.NDArray, npt.NDArray][source]

Build conventional square-wave modulation and reference codes.

The sinusoidal waveforms of make_conv_sinusoidal_codes() thresholded at their mid-level: square waves are cheap to generate and to demodulate in hardware, at the cost of harmonics that make the correlation piecewise linear rather than sinusoidal.

Parameters:
  • n_captures – Number of captures (phase shifts).

  • n_depths – Number of time bins per period.

Returns:

Tuple of (modulation_codes, reference_codes), each of shape (n_captures, n_depths).

See also

visionsim.emulate.itof.decoding.decode() with scheme="convSquare".

visionsim.emulate.itof.coding.make_single_ramp_codes(n_depths: int) → tuple[npt.NDArray, npt.NDArray][source]

Build single-ramp modulation and reference codes (n_captures=3).

Three captures are combined: a square-wave illumination with a matching reference, a capture with uniform illumination and an all-ones reference (the ambient/offset reference), and a dark capture that measures ambient light only. The recovered depth is only unambiguous over c / (4 * freq), half of what the sinusoidal schemes cover at the same frequency, because the ramp spans two correlation periods.

Parameters:

n_depths – Base number of depth bins; the actual code length is 2*n_depths - 1.

Returns:

Tuple of (modulation_codes, reference_codes), each of shape (3, 2*n_depths - 1).

See also

unambiguous_range() and visionsim.emulate.itof.decoding.decode() with scheme="singleRamp".

visionsim.emulate.itof.coding.make_double_ramp_codes(n_depths: int) → tuple[npt.NDArray, npt.NDArray][source]

Build double-ramp modulation and reference codes (n_captures=3).

The double-ramp variant of make_single_ramp_codes(): the second capture uses a reference that is shifted by half a period, so the two ramps together cover the depth range in both directions. Its usable range is likewise c / (4 * freq).

Parameters:

n_depths – Base number of depth bins; the actual code length is 2*n_depths - 1.

Returns:

Tuple of (modulation_codes, reference_codes), each of shape (3, 2*n_depths - 1).

See also

unambiguous_range() and visionsim.emulate.itof.decoding.decode() with scheme="doubleRamp".

visionsim.emulate.itof.coding.make_multi_freq_sinusoidal_codes(freq_vec: npt.NDArray, shifts_vec: npt.NDArray, n_depths: int) → tuple[npt.NDArray, npt.NDArray][source]

Build multi-frequency sinusoidal modulation and reference codes.

Every capture uses its own frequency multiplier and phase shift, so a single capture set probes the scene at several modulation frequencies. Multi- frequency coding is what extends the unambiguous range beyond c / (2*f) of a single frequency: each frequency yields a wrapped phase and unwrapping them coarse-to-fine recovers the depth over the largest of their periods.

The tap layout expected by the decoder is n_captures == 4 (one low-frequency tap plus three uniformly shifted taps of one frequency) or n_captures >= 5 (three shifts on the first frequency and two per further frequency).

Parameters:
  • freq_vec – Frequency multipliers for each capture, shape (n_captures,).

  • shifts_vec – Phase shifts (radians) for each capture, shape (n_captures,).

  • n_depths – Number of time bins per period.

Returns:

Tuple of (modulation_codes, reference_codes), each of shape (n_captures, n_depths).

See also

visionsim.emulate.itof.decoding.decode() with scheme="multFreqSin".

visionsim.emulate.itof.coding.make_gray_codes(n_bits: int) → npt.NDArray[source]

Generate the standard n_bits-bit reflected Gray code table.

Consecutive rows differ in exactly one bit, which is what makes a Gray-coded capture set usable as a continuous-time code: while the scene moves between two code words, only one capture channel changes at a time.

Parameters:

n_bits – Number of bits; the returned table has 2**n_bits rows.

Returns:

Boolean integer array of shape (2**n_bits, n_bits).

visionsim.emulate.itof.coding.make_max_min_run_length_gray_codes() → npt.NDArray[source]

Build the 5-capture max-min run-length Gray code table.

A Hamiltonian cycle over the 32 code words of five captures that keeps the runs of constant illumination as short as possible, which spreads the emitted energy evenly over the depth range. It is the table used by make_tof_gray_codes() for n_captures == 5.

Returns:

Float array of shape (32, 5) containing the code words.

visionsim.emulate.itof.coding.make_gray_codes_reduced(n_bits: int) → npt.NDArray[source]

Build a reduced Gray code table ordered as a Hamiltonian cycle.

The reflected Gray table with the all-zero and all-one code words removed: those two words are the only ones that would keep every capture channel constant, so dropping them leaves a table in which every code word has at least one channel transitioning. For 4 and 6 captures no Hamiltonian cycle exists over the reduced table, so the cycle of the next smaller dimension is duplicated with a constant leading bit instead.

Parameters:

n_bits – Number of sensor captures; must be in {3, 4, 5, 6}.

Returns:

Float array of shape (n_intervals, n_bits) containing the reduced, Hamiltonian-ordered Gray codes.

Raises:

ValueError – If n_bits is not in the supported set.

visionsim.emulate.itof.coding.make_tof_gray_codes(n_captures: int, n_depths: int) → npt.NDArray[source]

Build ToF reference codes based on the max-min run-length Gray code.

Each code word is emitted over a run of consecutive time bins, and the runs are concatenated so that the reference code interpolates linearly between adjacent code words: a depth falling inside a run maps to a point on the line connecting its two words, which is what the interval-wise decoder inverts.

Parameters:
  • n_captures – Number of captures.

  • n_depths – Requested number of time bins; the returned length is n_segments * ceil(n_depths / n_segments), which can exceed n_depths.

Returns:

Reference code array of shape (n_captures, n_bins).

Note

The max-min run-length table is only defined for five captures, so it is used for n_captures == 5 and the reduced Gray table otherwise. visionsim.emulate.itof.decoding.decode_hilbert() applies the same rule, which keeps the encoder and decoder consistent for every capture count.

See also

visionsim.emulate.itof.decoding.decode() with scheme="deltaHilbertDimOne".

visionsim.emulate.itof.coding.make_tof_hilbert_codes(n_captures: int, dim: int, hilbert_order: int, hilbert_delta: float, n_depths: int) → npt.NDArray[source]

Build ToF reference codes by sampling along a Hilbert curve.

A Hilbert curve fills the dim-dimensional space of code words, so walking along it visits neighbouring code words and therefore changes one capture channel at a time. Each code word is emitted over a run of time bins whose length follows the arc length of the corresponding curve segment, and the result is normalized to [hilbert_delta, 1 - hilbert_delta] so that no channel saturates. dim=1 degenerates to the Gray-coded codes of make_tof_gray_codes().

Parameters:
  • n_captures – Number of captures; (4, 2), (5, 2) and (5, 3) have code tables, where the pair is (n_captures, dim).

  • dim – Hilbert curve dimensionality (1, 2, or 3).

  • hilbert_order – Hilbert curve recursion order; higher orders resolve the code words more finely.

  • hilbert_delta – Hilbert curve normalization margin in [0, 0.5).

  • n_depths – Requested number of time bins; the returned length is n_segments * ceil(n_depths / n_segments).

Returns:

Reference code array of shape (n_captures, n_bins).

Raises:

ValueError – If no code table exists for the requested (n_captures, dim).

See also

visionsim.emulate.itof.decoding.decode() with scheme="deltaHilbertDimTwo" or scheme="deltaHilbertDimThree".

visionsim.emulate.itof.coding.make_delta_hilbert_codes(n_captures: int, dim: int, hilbert_order: int, hilbert_delta: float, n_depths: int) → tuple[npt.NDArray, npt.NDArray][source]

Build delta modulation and Hilbert-based reference codes.

Combines the impulse (delta) illumination of make_delta_sinusoidal_codes() with the Hilbert curve reference codes of make_tof_hilbert_codes(): the emitted energy is concentrated in a single bin while the reference code walks along the curve, which gives continuous-time codes with a large light budget. The reference codes are normalized so that every channel reaches one.

Parameters:
  • n_captures – Number of captures.

  • dim – Hilbert curve dimensionality (1, 2, or 3).

  • hilbert_order – Hilbert curve recursion order.

  • hilbert_delta – Hilbert curve normalization margin in [0, 0.5).

  • n_depths – Requested number of time bins.

Returns:

Tuple of (modulation_codes, reference_codes), each of shape (n_captures, n_bins).

See also

visionsim.emulate.itof.decoding.decode() with scheme="deltaHilbertDimOne", scheme="deltaHilbertDimTwo" or scheme="deltaHilbertDimThree".

visionsim.emulate.itof.coding.CodingScheme

Identifier of a supported coding scheme, used as the scheme argument of make_coding_functions().

alias of Literal[‘convSin’, ‘deltaSin’, ‘convSquare’, ‘singleRamp’, ‘doubleRamp’, ‘deltaHilbertDimOne’, ‘deltaHilbertDimTwo’, ‘deltaHilbertDimThree’, ‘multFreqSin’]

visionsim.emulate.itof.coding.make_coding_functions(scheme: CodingScheme, n_captures: int, n_depths: int, *, hilbert_order: int = 1, hilbert_delta: float = 0.25, freq_vec: npt.NDArray | None = None, shifts_vec: npt.NDArray | None = None) → tuple[npt.NDArray, npt.NDArray][source]

Return modulation and demodulation codes for the named coding scheme.

Parameters:
  • scheme – Coding scheme identifier; must be one of the CodingScheme literals.

  • n_captures – Number of captures (phase shifts / code words).

  • n_depths – Requested number of depth (time) bins per code period. The returned length is the smallest multiple of the scheme’s segment count that is at least n_depths, so it can exceed it: 2*n_depths - 1 for the ramp schemes and n_segments * ceil(n_depths / n_segments) for the Hilbert schemes; every other scheme returns exactly n_depths.

  • hilbert_order – Hilbert curve recursion order (for deltaHilbertDim codes).

  • hilbert_delta – Hilbert curve normalization margin in [0, 0.5) (for deltaHilbertDim codes).

  • freq_vec – Frequency multipliers required by "multFreqSin"; its length determines n_captures.

  • shifts_vec – Phase shifts (radians) required by "multFreqSin".

Returns:

Tuple of (modulation_codes, reference_codes), each of shape (n_captures, n_bins), where n_bins is as described for n_depths above.

Raises:

ValueError – If scheme is not a recognized coding scheme, if a scheme-specific constraint is violated (n_captures != 3 for the ramp schemes, missing or mismatched freq_vec/shifts_vec for "multFreqSin"), or if no code table exists for the requested n_captures (or (n_captures, dim)) combination.

See also

unambiguous_range() for the depth range each scheme can cover, and the per-scheme builders such as make_conv_sinusoidal_codes().

visionsim.emulate.itof.decoding module

Decoding functions for iToF depth recovery.

visionsim.emulate.itof.decoding.decode_sinusoid(measurements: npt.NDArray, freq: float) → npt.NDArray[source]

Decode sinusoidal (conventional or delta) iToF measurements.

The offset and the two quadrature components are recovered with a linear least-squares fit of the taps onto the basis [1, cos(phi_k), sin(phi_k)]. The phase is the angle of the two quadrature coefficients, and it is turned into depth with d = c * phi / (4 * pi * freq).

Parameters:
  • measurements – Raw measurement array of shape (n_captures, n_pixels).

  • freq – Modulation frequency in Hz.

Returns:

Recovered depth map of shape (n_pixels,) in metres.

See also

decode() for the scheme-dispatching entry point, and visionsim.emulate.itof.coding.make_conv_sinusoidal_codes() or visionsim.emulate.itof.coding.make_delta_sinusoidal_codes() for the matching codes.

visionsim.emulate.itof.decoding.decode_square(measurements: npt.NDArray, freq: float) → npt.NDArray[source]

Decode square-wave iToF measurements.

Square waves correlate into a piecewise-linear response, so the depth range is split into intervals whose ordering is fixed by the capture count; the interval is selected from the relative ordering of the taps and the depth is then interpolated inside it.

Parameters:
  • measurements – Raw measurement array of shape (n_captures, n_pixels).

  • freq – Modulation frequency in Hz.

Returns:

Recovered depth map of shape (n_pixels,) in metres.

See also

decode() for the scheme-dispatching entry point and visionsim.emulate.itof.coding.make_conv_square_codes() for the matching codes.

visionsim.emulate.itof.decoding.decode_single_ramp(measurements: npt.NDArray, freq: float) → npt.NDArray[source]

Decode single-ramp iToF measurements (n_captures=3).

Parameters:
  • measurements – Raw measurement array of shape (3, n_pixels).

  • freq – Modulation frequency in Hz.

Returns:

Recovered depth map of shape (n_pixels,) in metres.

Note

The single-ramp code spans two correlation periods, so the recovered depth is only unambiguous over c / (4 * freq) – half of the c / (2 * freq) covered by the other schemes (see visionsim.emulate.itof.coding.unambiguous_range()). Scene points deeper than that fold back into the reported range.

See also

decode() for the scheme-dispatching entry point and visionsim.emulate.itof.coding.make_single_ramp_codes() for the matching codes.

visionsim.emulate.itof.decoding.decode_double_ramp(measurements: npt.NDArray, freq: float) → npt.NDArray[source]

Decode double-ramp iToF measurements (n_captures=3).

Parameters:
  • measurements – Raw measurement array of shape (3, n_pixels).

  • freq – Modulation frequency in Hz.

Returns:

Recovered depth map of shape (n_pixels,) in metres.

Note

Like decode_single_ramp(), the recovered depth is only unambiguous over c / (4 * freq); deeper scene points fold back into that range.

See also

decode() for the scheme-dispatching entry point and visionsim.emulate.itof.coding.make_double_ramp_codes() for the matching codes.

visionsim.emulate.itof.decoding.decode_hilbert(measurements: npt.NDArray, freq: float, dim: int = 1, hilbert_order: int = 1, hilbert_delta: float = 0.25) → tuple[npt.NDArray, npt.NDArray][source]

Decode Hilbert-coded iToF measurements for any supported dimensionality.

Dispatches to a Gray-code interval classifier for dim=1 and to a Hilbert-curve segment-distance classifier for dim=2 or dim=3. The final least-squares depth-recovery step is shared across all paths.

Parameters:
  • measurements – Raw measurement array of shape (n_captures, n_pixels).

  • freq – Modulation frequency in Hz.

  • dim – Hilbert curve dimensionality. 1 uses the Gray-code path; 2 or 3 use the Hilbert-curve path. Defaults to 1.

  • hilbert_order – Hilbert curve recursion order (ignored for dim=1). Defaults to 1.

  • hilbert_delta – Hilbert curve normalization margin in [0, 0.5) (ignored for dim=1). Defaults to 0.25.

Returns:

Tuple of (interval_indices, depths) where interval_indices has shape (n_pixels,) (integer bin index) and depths has shape (n_pixels,) in metres.

Note

The dim >= 2 classifier compares each pixel’s per-channel normalized measurements against segment endpoints that live on the coarse Hilbert grid, while the encoder expands the curve along its arc length. Pixels whose channels nearly vanish (they sit close to a segment junction) can therefore be misassigned, which shows up as isolated large depth errors for the n_captures=5 codes. The dim == 1 and n_captures=4 paths are unaffected.

Raises:

ValueError – If the (n_captures, dim) combination is not supported.

See also

decode() for the scheme-dispatching entry point that returns only the depths, and visionsim.emulate.itof.coding.make_tof_hilbert_codes() for the matching reference codes.

visionsim.emulate.itof.decoding.decode_mult_freq_sinusoid(measurements: npt.NDArray, freq: float, freq_vec: npt.NDArray, shifts_vec: npt.NDArray) → npt.NDArray[source]

Decode multi-frequency sinusoidal iToF measurements.

Frequencies are unwrapped coarse-to-fine: the phase of a group of uniformly shifted taps recovers a wrapped depth, and every finer (higher) frequency then selects the branch of its shorter period closest to that estimate. The result is unambiguous over the largest effective range of any group, c / (2 * freq * min(freq_vec)).

Note

The tap layout is n_captures == 4 with three uniformly shifted taps on one frequency plus a single low-frequency tap, or n_captures >= 5 with three shifts on the first frequency and two per further frequency. For n_captures == 4 the three-shift frequency must cover the full range (freq_vec[1] == 1 unit): if the single low-frequency tap would be needed to widen it, NotImplementedError is raised rather than returning aliased depths.

Parameters:
  • measurements – Raw measurement array of shape (n_captures, n_pixels).

  • freq – Base modulation frequency in Hz. freq_vec are multipliers of it, so a tap with multiplier f has period c / (2 * freq * f).

  • freq_vec – Frequency multiplier per tap, shape (n_captures,).

  • shifts_vec – Phase shift (radians) per tap, shape (n_captures,).

Returns:

Recovered depth map of shape (n_pixels,) in metres.

Raises:

ValueError – If the tap vectors do not match the measurements, or if the tap layout cannot be decoded.

See also

decode() for the scheme-dispatching entry point and visionsim.emulate.itof.coding.make_multi_freq_sinusoidal_codes() for the matching codes.

visionsim.emulate.itof.decoding.decode(scheme: CodingScheme, measurements: npt.NDArray, freq: float, *, hilbert_order: int = 1, hilbert_delta: float = 0.25, freq_vec: npt.NDArray | None = None, shifts_vec: npt.NDArray | None = None) → npt.NDArray[source]

Decode iToF measurements using the specified coding scheme.

This is the primary entry point for depth recovery. It dispatches the raw measurement data to the appropriate decoding algorithm based on the scheme of the coding scheme.

Parameters:
  • scheme – Identifier of the coding scheme used for acquisition.

  • measurements – Raw measurement array of shape (n_captures, n_pixels).

  • freq – Modulation frequency in Hz. Note that the ramp schemes recover depth over c / (4 * freq) rather than c / (2 * freq) (see visionsim.emulate.itof.coding.unambiguous_range()).

  • hilbert_order – Hilbert curve recursion order (only for Hilbert schemes). Defaults to 1.

  • hilbert_delta – Hilbert curve normalization margin in [0, 0.5) (only for Hilbert schemes). Defaults to 0.25.

  • freq_vec – Frequency multipliers per tap, required by "multFreqSin".

  • shifts_vec – Phase shifts (radians) per tap, required by "multFreqSin".

Returns:

Recovered depth map of shape (n_pixels,) in metres. Pixels with no modulation signal are not flagged by any decoder: they decode to an artefact of the per-scheme fit rather than a physical depth, so callers must check the recovered amplitude themselves.

Raises:
  • NotImplementedError – If the coding scheme does not have a decoder.

  • ValueError – If measurements is not a 2-D array, if its capture count is not supported by the scheme (e.g. n_captures != 3 for the ramp schemes), or if a required argument such as freq_vec is missing.

See also

The per-scheme decoders this function dispatches to: decode_sinusoid() (convSin, deltaSin), decode_square() (convSquare), decode_single_ramp() (singleRamp), decode_double_ramp() (doubleRamp), decode_hilbert() (deltaHilbertDimOne, deltaHilbertDimTwo, deltaHilbertDimThree) and decode_mult_freq_sinusoid() (multFreqSin).

visionsim.emulate.itof.simulation module

Image simulation and projection pipeline for iToF.

visionsim.emulate.itof.simulation.compute_correlation_function(modulation_code: npt.NDArray, reference_code: npt.NDArray, time_resolution: float, *, n_depths: int | None = None) → npt.NDArray[source]

Compute the correlation function between one modulation/reference pair.

Parameters:
  • modulation_code – 1-D modulation code of length n_bins.

  • reference_code – 1-D reference code of length n_bins.

  • time_resolution – Time resolution (seconds per bin).

  • n_depths – Number of depth bins to return. Defaults to len(modulation_code). The correlation function is periodic with the code period, so requesting more bins than that repeats it: len(modulation_code) + 1 bins give the value at exactly max_depth, which equals the value at depth 0.

Returns:

Correlation values sampled at n_depths evenly spaced depth bins, shape (n_depths,).

Raises:

ValueError – If modulation_code and reference_code differ in length, or if n_depths is not positive.

visionsim.emulate.itof.simulation.simulate_measurements(depths: npt.NDArray, albedos: npt.NDArray, modulation_codes: npt.NDArray, reference_codes: npt.NDArray, period: float, *, exposure_time: float = 1.0, ambient_power: float = 0.0, light_power: float = 1.0) → npt.NDArray[source]

Directly simulate iToF measurements from depths and albedos.

This implements a physically accurate direct model including $1/d^2$ radiometric falloff and albedo scaling [1].

Parameters:
  • depths – Depth map in metres, arbitrary shape (*spatial,). Non-finite entries (e.g. a renderer’s sky/miss sentinel) describe no surface and are treated as no return: their active-light term is zero, leaving only ambient light.

  • albedos – Reflectance values (0-1), same shape as depths.

  • modulation_codes – Modulation codes, shape (n_captures, n_bins).

  • reference_codes – Reference (demodulation) codes, same shape as modulation_codes.

  • period – Duration of one code period in seconds, i.e. 1 / f. The correlation function is periodic over it, so n_bins bins span the round-trip distance c * period / 2 and depths wrap back over it.

  • exposure_time – Camera exposure time in seconds. Defaults to 1.0.

  • ambient_power – Average ambient irradiance. Defaults to 0.0.

  • light_power – Peak active light intensity. Defaults to 1.0.

Returns:

Simulated measurement array of shape (n_captures, *spatial). Finite whenever depths and albedos are: inf/nan sentinel depths resolve to the ambient term instead of leaking NaN into the measurements.

Raises:

ValueError – If reference_codes does not have one row per modulation capture, or if albedos does not have the same shape as depths.

References

Module contents

iToF (indirect time-of-flight) sensor emulation.