# How to calculate the phase shift AND time delay of non-periodic signals

Say I have two non-periodic signals, $f_1(t)$ and $f_2(t)$, with Fourier transforms $F_1(\omega)$ and $F_2(\omega)$. Basically, I need to line up $f_1(t)$ and $f_2(t)$ as close as possible, and I am allowed to shift the signals in time and multiply their Fourier transforms by a constant phase.

What is the most efficient method to find the values of the phase shift $\Delta \phi$ and the time delay $\Delta t$ such that, when the Fourier transform of $f_2$ is modified into

$$F_2(\omega) e^{i (\Delta \phi + \omega \Delta t)},$$

the modified $f_2$ best "lines up" with $f_1$ according to their cross-correlation (or some other measurement)?

I am interested in simple and computationally efficient methods, even if they are not necessarily perfect.

A pure time delay could be determined by looking for a peak in the cross correlation. But in your case $f2$ might also have an overall phase offset.

You could try to compute two cross correlations:

\begin{align} x &= cross(f1,f2) \\ y &= cross(f1,hilbert(f2)) \\ \end{align}

where $hilbert(f2)$ refers to an overall 90° phase shifted version of $f2$. If you combine those two like this

$$z = \sqrt{x^2 + y^2}$$

you should get something that is independent of the phase shift and shows you a peak at the correct time delay $\Delta t$. The "phase" at that peak, $atan2(y,x)$ should give you the phase offset $\Delta\phi$.

I don't know if such a problem is usually solved this way and I have not tried it myself. But it might work.

• I used your method on some test cases and while it gives approximately correct answers, it wasn't perfect. – ursus432 Oct 4 '17 at 11:54
• Cool! This​ could be a starting point to a nonlinear optimization. It's really a nonlinear least squares problem if you want to minimize the sum of squared errors. – sellibitze Oct 4 '17 at 11:57
• However, inspired by your approach, I did the following and it worked perfectly: Define the complex-valued signal $\tilde{f}_i = f_i + i \text{hilbert}(f_i)$, and then compute the (complex-valued) cross correlation of the two complex signals: $\text{cross}(\tilde{f}_1, \tilde{f}_2)$. The position of the peak of the amplitude of this complex vector identifies $\Delta t$, and the phase of the value at the peak is $\Delta \phi$. – ursus432 Oct 4 '17 at 11:58
• Yeah, I was scared of getting into nonlinear optimization because like I said, the method needed to be quick and simple. Fortunately the trick with the complex signals worked fine. – ursus432 Oct 4 '17 at 12:02
• Isn't your "updated" approach basically the same? I see no difference at the moment. – sellibitze Oct 4 '17 at 12:04

Am I missing something obvious here?

Restating the problem:

Suppose that

$$H(x) = G(x) e^{i(a + bx)}$$

where $H,G$ are known complex valued functions for a set of $x$ values and $a,b,x$ are real values. Find $a,b$.

$$i( a + bx ) = \ln( H(x) / G(x) )$$

$$i( a + bx ) = \ln( \| H(x) / G(x) \| ) + i \arg( H(x) / G(x) )$$

The real part should be zero. Otherwise the best fit assumption is inaccurate. Disregard any non-zero values. If they are large, the fit is not good.

$$a + bx = \arg( H(x) / G(x) )$$

Where:

$$\arg(q) = \operatorname{atan2}( Im[q],Re[q] )$$

The problem is now reduced to a linear regression application. For best results, select values of x where $\| G(x) \|$ (and thus $\| H(x) \|$) are not near zero.

From the comments, it seems the OP found a solution, but I think this one would be a lot more efficient.

I am still puzzling about what $\phi$ means for a non-periodic function. For a pure complex tone, or even a mix of pure complex tones, it makes sense. Of course, for a pure complex tone a phase shift is indistinguishable from a time shift.

Ced