Does it make sense to use cross-correlation on arrays of timestamps?
<p>I want to find the offset between two arrays of timestamps. They could represent, let's say, the onset of beeps in two audio tracks.</p>

<p><strong>Note</strong>: There may be extra or missing onsets in either track.</p>

<p>I found some information about cross-correlation (e.g. <a href="https://dsp.stackexchange.com/questions/736/how-do-i-implement-cross-correlation-to-prove-two-audio-files-are-similar">https://dsp.stackexchange.com/questions/736/how-do-i-implement-cross-correlation-to-prove-two-audio-files-are-similar</a>) which looked promising.</p>

<p>I assumed that each audio track is 10 seconds in duration, and represented the beep onsets as the peaks of a "square wave" with a sample rate of 44.1 kHz:</p>

<pre><code>import numpy as np

rfft = np.fft.rfft
irfft = np.fft.irfft

track_1 = np.array([..., 5.2, 5.5, 7.0, ...])
# The onset in track_2 at 8.0 is "extra," it has no
# corresponding onset in track_1
track_2 = np.array([..., 7.2, 7.45, 8.0, 9.0, ...])
frequency = 44100
num_samples = 10 * frequency
wave_1 = np.zeros(num_samples)
wave_1[(track_1 * frequency).astype(int)] = 1
wave_2 = np.zeros(num_samples)
wave_2[(track_2 * frequency).astype(int)] = 1
xcor = irfft(rfft(wave_1) * np.conj(rfft(wave_2)))
offset = xcor.argmax()
</code></pre>

<p>This approach isn't particularly fast, but I was able to get fairly consistent results even with quite low frequencies. However... I have no idea if this is a good idea! Is there a better way to find this offset than cross-correlation?</p>

<p><strong>Edit</strong>: added note about missing and extra onsets.</p>

<p>Yes it does make sense. This is commonly done in matlab. Here is a link to a similar application:</p>

<p><a href="http://www.mathworks.com/help/signal/ug/cross-correlation-of-delayed-signal-in-noise.html" rel="nofollow">http://www.mathworks.com/help/signal/ug/cross-correlation-of-delayed-signal-in-noise.html</a></p>

<p>Several considerations</p>

<p>Cross-correlation is commonly used for times when the signal in question has too much noise. If you don't have any noise to worry about I would use a different method however. </p>

python fft waveform cross-correlation