# Calculate phase lag between two signals with perturbed frequencies

This type of question has been asked quite a few times on this forum and others now, but I still haven't found a satisfactory answer to my problem.

Given an input signal: $$x_1(t)=\cos\big(2\pi ft\big)$$ A process derives an output signal: $$x_2(t)=\cos\big(2\pi (f+b)t+\phi\big)$$ My objective is to determine the phase lag $$\phi$$.

$$b$$ is an unknown perturbation to the input frequency very close to 0 due to the mechanical nature of the process. For example, a human trying to track this signal.

If the perturbation is exactly zero, I'm able to use cross spectral density to determine the phase lag. However, if the perturbation is even 0.1% of the original frequency, then my answer is totally erroneous.

For example, in Python:

import numpy as np

Fs = 1000
t = np.arange(0,1,1/Fs)
x1 = np.cos(2*np.pi*100*t)
x2 = np.cos(2*np.pi*100.1*t + np.pi/4)
dft1 = np.fft.fft(x1); dft1 = dft1[0:int(n/2)+1]/Fs;
dft2 = np.fft.fft(x2); dft2 = dft2[0:int(n/2)+1]/Fs;
n = len(t)
f = np.multiply(range(0,int(n/2)+1), Fs/n)
csd = np.conj(dft1)*dft2


Expect 45 but get 62.9 instead.

I've tried Hilbert transform and straight fft. Any idea?

• Well, you gotta problem because you can think of $x_2(t)$ as: \begin{align} x_2(t) &= \cos\big(2\pi (f+b)t+\phi\big) \\ &= \cos\big(2\pi ft+ (2\pi bt + \phi) \big) \\ \end{align} and your actual phase displacement is $2\pi bt + \phi$ which is time-dependent. so do you want your phase displacement around the time $t=0$? – robert bristow-johnson Jul 3 at 19:50
• @robertbristow-johnson Very good point. It's no longer a constant phase for x2. I'll use stft to extract the time-dependent phase. – Jimmy Jul 3 at 20:43