When two sensors record the same event on a robot test bench, an engineer may use cross-correlation to estimate how far apart the traces are. The position of the correlation maximum in an output array and the signed lag at that position are different quantities. Treating the position as a delay can give a plausible number with the wrong magnitude or sign.

Use a known delay before interpreting a test run

Take a short reference trace with one pulse at sample index 2 and an observed trace with one pulse at index 5. Assume the two traces share a clock origin and sample interval. The event is three samples later in the observed trace. The observed trace can be longer than the reference. Full linear cross-correlation covers positive and negative displacements, and zero lag need not be in the center of the output array when the input lengths differ.

For SciPy, pass the observation first when using the sign convention in this example. Use the same operand lengths in the same order when building the lag coordinates, and use the same mode for both operations.

import numpy as np

from scipy.signal import correlate, correlation_lags

reference = np.zeros(7)

observed = np.zeros(9)

reference[2] = 1

observed[5] = 1

def peak_lag(first, second):

values = correlate(first, second, mode="full", method="direct")

lags = correlation_lags(len(first), len(second), mode="full")

peak_index = int(np.argmax(values))

return peak_index, int(lags[peak_index])

assert peak_lag(observed, reference) == (9, 3)

assert peak_lag(reference, observed) == (5, -3)

In the first call, the lag sequence begins at -6 because the second input has seven samples. A peak at position 9 corresponds to lag 3. In the reverse call, the lag sequence begins at -8. The same two pulses then yield a peak at position 5 and lag -3. Reporting a positive value for a late observation is a convention established here by a fixture with known ordering.

Keep the sample clock in the report

A lag from this computation has units of samples. On a shared 2,000 Hz sample grid, three samples correspond to 1.5 milliseconds. The conversion assumes a known interval between samples. Record the grid rate, window start and channel identities, particularly when traces come from different log files. Independent start times, uneven timestamps and missing rows require checking how the traces were placed on the grid before applying that conversion.

The single-pulse fixture checks indexing and operand order. Real traces can have offsets, repetitive patterns or multiple events, any of which can create competing peaks. Validate the chosen lag over a window containing the event of interest and compare it with the available timestamps. Keep the lag search range and any preprocessing with the result so that the same decision can be reproduced. An apparent delay outside the expected range can be a sign of a mismatched window.

SciPy's correlate reference defines the lag range for full one-dimensional correlation. Its correlation_lags reference describes the companion displacement array. Using the pair in the fixture keeps the output index separate from the delay that an engineer reports.

Sources

SciPy correlate reference: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.correlate.html

SciPy correlation_lags reference: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.correlation_lags.html