# Synchronizing 2 time series signals at slightly different sampling frequencies

I'm working with an embedded system that has two acquisition channels and unfortunately, their clock crystals are slightly out of sync. Even though both boards are configured by software to sample at 1kHz, in reality, one of them samples at 996 Hz and the other at 1008 Hz. This was confirmed by an oscilloscope as well.

I want to synchronize these two signals so I can extract the phase lag between them. (both channels are sensing/sampling the same periodic signal with a constant phase difference between the two).

Right now, I'm just using the built-in Python scipy function to upsample both signals to 2kHz and then put them next to each other. It appears to work but I'm just not sure if upsampling like this is valid considering the ratio between the original and upsampled signal is not a whole number (2000/996 and 2000/1008 respectively).

Is there a better way to do this?

• Note: If you are dealing with linear modulated signals (QAM,PSK), I recommend you to implement Gardner algorithm. Commented Dec 1, 2021 at 17:51

If you are confident that the relationship is a ratio of integers, then resampling would be a fine approach. One would be matched to the other by upsampling by 1008 and then downsampling by 996 which when reduced by their greatest common divisors becomes upsample by 84 and downsample by 83.

To do this in scipy, use resample to interpolate to a length that is 84x longer, and then simply select every 83rd sample from that result:

x_new = sig.resample(x, len(x)*84)[::83]


If you are concerned about a residual frequency offset and wish to correct that, this can be done by using the two signals to measure the frequency error and then applying well established digital resampling techniques. Further details of this depend of certain characteristics in the signal and would not be necessary if the resampling above approach is sufficient for getting the desired delay estimate.

If the ultimate goal is to measure the delay between two copies of the same signal that have different sampling rates, this can be done more directly as I detail in this post.

• Thanks this seems much more reasonable. I'll see what effects it will have on the frequency spectrum and correct it as necessary. Commented Nov 25, 2021 at 14:46
• As one approach to measure frequency error, look at the complex conjugate product of the Hilbert Transform of both signals (assuming you are working with a real signal) with a delay of one sample between the two. The imaginary of this will proportional to frequency offset. It can give you an easy metric for which to adjust frequency offset (by multiplying the Hilbert Transform by e(−jωoffsett) and then taking the real portion of that to recover your frequency corrected signal). If that is all new to you, this will sound like a mouthful but it is actually a simple and efficient process. Commented Nov 25, 2021 at 15:03
• (Or if your periodic signal is not noisy, simply compare the zero crossings to estimate frequency error). Commented Nov 25, 2021 at 15:04
• I actually do have familiarity with Hilbert transform, it's just been a while since I've used it haha. Thanks a lot, definitely something I was looking for. Commented Nov 25, 2021 at 15:40
• Even better would be to check out the link I added at the bottom to a post where I had worked this out before. Given you already have a reference signal, there is no need to actually resample etc to then make your sampling clock the reference in which to measure delay. You can do this directly from the samples you have and that would ultimately be most efficient and robust. Check it out! Commented Nov 25, 2021 at 15:47