2
$\begingroup$

I have two noisy sinusoidal signals and I am currently investigating solutions to synchronize them using python. Here is an example I made to illustrate my goal, supposing that I generate these two sinusoidals (both with same frequency and amplitude but a phase difference):

time = np.arange(0, 60, 0.005)
amp = 2
F = 0.25
signal1 = amp*np.sin(2*np.pi*F*time + np.pi)
signal2 = amp*np.sin(2*np.pi*F*time + np.pi/3)

mu, sigma = 0.2, 0.1 # mean and standard deviation
noise1 = np.random.normal(mu, sigma, len(signal1))
noise2 = np.random.normal(mu, sigma, len(signal2))


noisy_signal1 = signal1 + noise1
noisy_signal2 = signal2 + noise2

plt.figure()
plt.plot(time, noisy_signal1)
plt.plot(time, noisy_signal2)
plt.show()

One of the possible solutions I have found was by Cross-Correlation, which would look like this:

def cross_correlation_using_fft(x, y):
    f1 = fft(x)
    f2 = fft(np.flipud(y))
    cc = np.real(ifft(f1 * f2))
    return fftshift(cc)

# shift < 0 means that y starts 'shift' time steps before x # shift > 0 means that y starts 'shift' time steps after x
def compute_shift(x, y):
    assert len(x) == len(y)
    c = cross_correlation_using_fft(x, y)
    assert len(c) == len(x)
    zero_index = int(len(x) / 2) - 1
    shift = zero_index - np.argmax(c)
    return shift

shift = compute_shift(noisy_signal1, noisy_signal2)

if shift < 0:
    noisy_signal1_sync = noisy_signal1[np.abs(shift):]
    noisy_signal2_sync = noisy_signal2[:len(noisy_signal1_sync)]
    
elif shift > 0:
    noisy_signal2_sync = noisy_signal2[np.abs(shift):]
    noisy_signal1_sync = noisy_signal1[:len(noisy_signal2_sync)]    
    

plt.figure()
plt.plot(noisy_signal1_sync)
plt.plot(noisy_signal2_sync)
plt.show() 

I also saw that one different possible solution would be via frequency domain, yet, I am still unable to achieve a solution using that approach. I checking how it would work for my given example but I haven't found a solution yet. Therefore, my questions is regarding how to perform the synchronization of two noisy sinusoidal signals (same F and A) in the frequency domain.

EDIT1: The frequency and amplitudes are previously known. I am mostly interested in achieving the same results I had by using the cross-correlation. I learned that there is a way to achieve the same result by instead of shifting the sinusoidal via cross-correlation, to shift it via FFT/IFFT in the frequency domain, which is something that I do not understand yet and what the answer should address.

$\endgroup$
5
  • 1
    $\begingroup$ I'm not reading your python code. Can you use $\LaTeX$ to express mathematically what you have and what you're trying to do? Are you certain the two sinusoids have the same frequency? Do you want a phase lock loop (PLL)? $\endgroup$ Oct 13, 2022 at 20:49
  • 1
    $\begingroup$ well, you're trying to cross-correlate two perfectly periodic signals. So, any solution would have periodic ambiguity. Is this what you mean being unable to achieve a solution? Or is it something else? Could you describe how the failure constitutes? $\endgroup$ Oct 13, 2022 at 21:20
  • $\begingroup$ Please edit your question to incorporate the above-requested information. In particular, if the two sinusoids have the same frequency, and that frequency is known, then the problem becomes vastly simpler. $\endgroup$
    – TimWescott
    Oct 13, 2022 at 22:58
  • $\begingroup$ Made some adjustments. Hopefully it is more clear now. $\endgroup$ Oct 13, 2022 at 23:48
  • $\begingroup$ Are you asking how to calculate the time-shift $\Delta t$, or how to actually shift the signal (in the frequency domain) once $\Delta t$ is known? $\endgroup$
    – roygbiv
    Oct 14, 2022 at 12:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.