<jupyter_start><jupyter_text>Let's try to find the lag of asynchrony by looking at the cross-correlation.<jupyter_code># the cross-correlation function in statsmodels does not use FFT so it is really slow
# from statsmodels.tsa.stattools import ccf
# res = ccf(ts1[1][200000:400000,1],ts2[1][200000:400000,1])<jupyter_output><empty_output><jupyter_text>Cross-correlation on the signals is a bad idea! Two many oscillations. Instead, we should get the envelope of the signal and cross-correlate.<jupyter_code># Warning - the envelope heights could be different for different cameras if there one is buffered
# Envelope based on the hilbert transform fails:
import scipy.signal.signaltools as sigtool
env = np.abs(sigtool.hilbert(ts1[1][:,1]))
#plt.plot(env)<jupyter_output><empty_output><jupyter_text>We will try to get the envelopse by taking absolute values and then passing a low-pass filter.<jupyter_code>plt.plot(np.abs(ts1[1][:,1]))
plt.title('Absolute value of the signal')
# Another unsuccessful way of getting the envelope:
# hilb = sigtool.hilbert(ts1[1][:,1])
# env = (ts1[1][:,1] ** 2 + hilb ** 2) ** 0.5
# plt.plot(env)
# Creating a Butterworth filter
b, a = signal.butter(4, 7./48000, 'low')
# filtering 
# output_signal = signal.filtfilt(b, a, 2*ts1[1][:,1]*ts1[1][:,1])
output_signal1 = signal.filtfilt(b, a, np.abs(ts1[1][:,1]))
output_signal2 = signal.filtfilt(b, a, np.abs(ts2[1][:,1]))
plt.plot(np.sqrt(output_signal1[700000:1080000]))
plt.plot(np.sqrt(output_signal2[700000:1080000]))
plt.title('Zooming on the displacement')<jupyter_output><empty_output><jupyter_text>Calculate cross-correlation with FFT:<jupyter_code>c = signal.fftconvolve(output_signal1[800000:1080000],output_signal2[800000:1080000][::-1], mode='full') 
plt.plot(c[::-1])
print(c.shape[0])
print(c.argmax())
offset = c.shape[0]  - c.argmax()- 280000
plt.plot(280000+offset,c[c.argmax()],'ro')
plt.title('Offset is ' + str (280000+offset))
plt.plot(np.sqrt(output_signal1[800000:1080000]))
plt.plot(np.sqrt(output_signal2[800000+offset:(1080000+offset)]))
plt.title('Aligned envelopes')<jupyter_output><empty_output><jupyter_text>On a large scale the envelopes seem aligned:<jupyter_code>plt.plot(np.sqrt(output_signal1[np.abs(offset):]))
plt.plot(np.sqrt(output_signal2[:]))<jupyter_output><empty_output><jupyter_text>Offset the original data:<jupyter_code>plt.plot(ts1[1][700000:1200000,1])
plt.plot(ts2[1][700000+offset:1200000+offset,1])
plt.title('Aligned signals')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1569650742673625, 'fraction_numerical': 0.0835006021678041, 'mean_word_length': 7.996389891696751, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '277568', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1084, 'n_tokens_neox': 935, 'n_words': 183, 'path': 'notebooks/Synchronization.ipynb', 'repo_name': 'thonstad/acoustical_monitoring', 'size': '2491'}