Does it make sense to use cross-correlation on timestamp arrays?

I want to find the offset between two arrays of timestamps. They can represent, say, the beginning of sound signals on two sound tracks.

Note Any track may have additional or missing sets.

I found some information on cross-correlation (e.g. https://dsp.stackexchange.com/questions/736/how-do-i-implement-cross-correlation-to-prove-two-audio-files-are-similar ) which looked promising.

I assumed that each sound track lasts 10 seconds, and represents sound signals in the form of "square wave" peaks with a sampling frequency of 44.1 kHz:

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() 

This approach is not particularly fast, but I was able to get pretty consistent results even at fairly low frequencies. However ... I have no idea if this is a good idea! Is there a better way to find this bias than cross-correlation?

Edit : Added note about missing and additional onsets.

+8
python fft waveform cross-correlation
source share
2 answers

If track_1 and track_2 are timestamps of audio signals, and both of them pick up all audio signals, then there is no need to create a waveform and perform cross-correlation. Just find the average delay between two arrays of audio signals:

 import numpy as np frequency = 44100 num_samples = 10 * frequency actual_time_delay = 0.020 timestamp_noise = 0.001 # Assumes track_1 and track_2 are timestamps of beeps, with a delay and noise added track_1 = np.array([1,2,10000]) track_2 = np.array([1,2,10000]) + actual_time_delay*frequency + frequency*np.random.uniform(-timestamp_noise,timestamp_noise,len(track_1)) calculated_time_delay = np.mean(track_2 - track_1) / frequency print('Calculated time delay (s):',calculated_time_delay) 

This gives approximately 0.020 , depending on the random errors entered, and about as fast as it gets.

EDIT: If you need to process extra or missing timestamps, you can change the code as follows. Essentially, if the error of an accidental randomness of time is limited, then perform a statistical β€œmode” function with delays between all values. Everything related to the assessment of randomness of randomness is grouped together and identified, then the initial identified delays are then averaged.

 import numpy as np from scipy.stats import mode frequency = 44100 num_samples = 10 * frequency actual_time_delay = 0.020 # Assumes track_1 and track_2 are timestamps of beeps, with a delay, noise added, # and extra/missing beeps timestamp_noise = 0.001 timestamp_noise_threshold = 0.002 track_1 = np.array([1,2,5,10000,100000,200000]) track_2 = np.array([1,2,44,10000,30000]) + actual_time_delay*frequency track_2 = track_2 + frequency*np.random.uniform(-timestamp_noise,timestamp_noise,len(track_2)) deltas = [] for t1 in track_1: for t2 in track_2: deltas.append( t2 - t1) sorted_deltas = np.sort(deltas)/frequency truncated_deltas = np.array(sorted_deltas/(timestamp_noise_threshold)+0.5, dtype=int)*timestamp_noise_threshold truncated_time_delay = min(mode(truncated_deltas).mode,key=abs) calculated_time_delay = np.mean(sorted_deltas[truncated_deltas == truncated_time_delay]) print('Calculated time delay (s):',calculated_time_delay) 

Obviously, if too many audio signals are missing or additional audio signals exist, then the code starts to fail at some point, but in general it should work well and be faster than trying to generate an entire waveform and perform correlation.

+3
source share

Yes, that makes sense. This is usually done in Matlab. Here is a link to a similar application:

http://www.mathworks.com/help/signal/ug/cross-correlation-of-delayed-signal-in-noise.html

Few considerations

Cross-correlation is usually used for the time when this signal has too much noise. If you have no noise, to worry about the fact that I would use a different method.

+3
source share

All Articles