# Determine Phase Difference and Time Delay Between Two Pulsed Sinusoidal Signals

I am working on a code that generates two signals: A pulsed sinusoidal signal ($1\textrm{ MHz}$) beginning at $0$ phase, and lasts for say $5\ \mu\text{s}$, then trails zeros. The other signal has the same pulse width and frequency ($5\ \mu\text{s}, 1\textrm{ MHz}$), but begins at a different time ($1\ \mu\text{s}$ later for example), and with a different phase ( $\pi$ radians).

If either the phase or time delay were consistent between the two, then I would be able to solve this problem, but how do you go about solving this when both variables differ between the signals? Any feedback on this is greatly appreciated.

• Could you clarify your question. The title says "extract", which to me implies that you have the signals and you are trying to get information out of them. But the body of the post says you are trying to "generate" the signals, which implies that you do do not have the signals, but you already know some information about them, the problem being just how to produce the signal. Can you clarify what is the question? Jan 10, 2017 at 18:09
• Thanks for the reply. So I'm starting by actually generating both signals in code. Then I want to work backwards and try to determine both the phase and time differences with only knowledge of the pulse width and frequency. Hopefully this clarifies your concern Jan 10, 2017 at 18:52
• Ok. If I understand you correctly, then I would say that it is impossible, in the general sense, to automatically determine both the delay and the phase simultaneously. You can use a cross-correlation, but that will give you just the sum of the two. You might be able to manually (i.e. by eye), line up either the delay or the phase, and then cross-correlation could give you the other. Jan 11, 2017 at 1:47

Pulse width and frequency alone are insufficient information to determine phase (relative to a fixed point in time or to each other) and delay.

The main difference between two equal length sinusoids with different starting phases is in the difference between their starting and ending transients. So after estimating the time difference between the two pulse envelopes, you would have to look at how each pulse envelope starts and ends (which requires having a lot more bandwidth than narrow band frequency estimation alone needs).

Provided the two signals have exactly the same known frequency (which you have indicated to be your case), you can estimate the delay and phase difference using the following steps:

1. Estimate the relative time delay between the envelopes of the two pulses;
2. Estimate the phase difference between each pulse and a reference (infinite length) tone at the specified frequency at an arbitrary time instant;
3. Compute the relative phase of the delayed pulse based on the above estimated delay and phase differences.

For reference we can write the two pulses as: \begin{align} y_1\left(t\right) &= \sin\left(2\pi f t\right) \cdot \Pi\left(\frac{t}{T}\right) \\ y_2\left(t\right) &= \sin\left(2\pi f (t-t_0) + \phi\right) \cdot \Pi\left(\frac{t-t_0}{T}\right) \\ \end{align} where $\Pi\left(t\right)$ is a rectangular pulse defined as: $$\Pi\left(t\right) = \begin{cases} 1 &, 0\leq t\leq 1 \\ 0 &, \mbox{otherwise} \end{cases}$$

1. Estimating the relative time delay between the envelopes of the two pulses

To do this, we must first estimate the envelope of the pulses $y_1\left(t\right)$ and $y_2\left(t\right)$. This can be achieved by using the complex analytical signals $\hat y_1\left(t\right)$ and $\hat y_2\left(t\right)$ given by: \begin{align} \hat y_1\left(t\right) &= y_1\left(t\right) + j \mathcal{H}\left\{y_1\left(t\right)\right\}\\ \hat y_2\left(t\right) &= y_2\left(t\right) + j \mathcal{H}\left\{y_2\left(t\right)\right\} \end{align} where $\mathcal{H}\left\{f\left(t\right)\right\}$ is the Hilbert transform of $f\left(t\right)$. The pulse envelopes estimated are then obtained from $\left|\hat y_1\left(t\right)\right|$ and $\left|\hat y_2\left(t\right)\right|$. Finally, the time delay between the two pulses can be obtained from the peak of the cross-correlation between those two envelope estimates.

2. Estimating the phase difference between each pulse and a reference tone

Given the two complex analytical signals $\hat y_1\left(t\right)$ and $\hat y_2\left(t\right)$ obtained earlier, computing the complex baseband signals \begin{align} z_1\left(t\right) &= \hat y_1\left(t\right) \cdot e^{-j 2\pi f t} \\ z_2\left(t\right) &= \hat y_2\left(t\right) \cdot e^{-j 2\pi f t} \end{align} readily gives us the phase differences between the pulses and the (complex) reference tone $e^{j 2\pi f t}$ as $\angle z_1\left(t\right)$ and $\angle z_2\left(t\right)$ respectively. Note that to reduce the estimation error it is also possible to use the angle of the average complex baseband signal over the pulse duration.

3. Computing the relative phase of the delayed pulse

Since both phase differences are expressed relative to the same complex reference tone, and looking at the expression for the phase of the original pulses $y_1\left(t\right)$ and $y_2\left(t\right)$, the phase difference between the two pulses can be seen to be: \begin{align} \angle z_2\left(t\right) - \angle z_1\left(t\right) &= - 2\pi f t_0 + \phi + 2\pi k \end{align} from which we derive that \begin{align} \phi &= \angle z_2\left(t\right) - \angle z_1\left(t\right) + 2\pi f t_0 + 2\pi k' \end{align}

Demonstration

This can be demonstrated using the following Matlab script:

clear all;
close all;

% Simulation parameters
K   = 40;
f   = 1e6;       % tone at 1MHz
fs  = K*f;       % sampling rate
T   = 5e-6;      % 5 microsecond pulse
d   = 1e-6;      % second pulse's delay (wrt first pulse)
phi = 2*pi*rand; % second pulse's phase difference (wrt first pulse)

% Generate pulses
t = [0:1/fs:T];
N = length(t);
M = floor(1.5*(T+d)*fs);
y1 = zeros(1,M);
y1(1:N) = sin(2*pi*f*t);
y2 = zeros(1,M);
offset = floor(d*fs);
y2([1:N]+offset) = sin(2*pi*f*t + phi);

tt = [0:M-1]/fs;

% 1. Estimating the relative time delay between the envelopes of the two pulses
y1hat = hilbert(y1); % complex analytical envelope of y1
y2hat = hilbert(y2); % complex analytical envelope of y2
env1 = abs(y1hat); % estimated envelope of y1
env2 = abs(y2hat); % estimated envelope of y2
[r,lag]=xcorr(env2,env1);
[rmax,idxmax]=max(r);
lagmax = lag(idxmax);
delay = lagmax / fs;

% 2. Estimating the phase difference between each pulse and a reference tone
z1 = y1hat .* exp(-2*pi*f*tt*i);
z2 = y2hat .* exp(-2*pi*f*tt*i);

% Integrate the signal (averaging) to reduce estimation error
w1 = zeros(size(z1));
w2 = zeros(size(z2));
w1(1) = z1(1);
w2(1) = z2(1);
for i=2:length(z1)
w1(i) = w1(i-1) + z1(i);
w2(i) = w2(i-1) + z2(i);
end

%3. Computing the relative phase of the delayed pulse

% Sample the result near the end of the pulses
Ns1 = floor(0.95*N) + 1;
Ns2 = Ns1 + lagmax;
phasediff = mod(angle(w2(Ns2))-angle(w1(Ns1))+2*pi*f*delay, 2*pi);

% Show results
printf("Actual parameters\n");
printf("  phase difference : %f\n", phi);
printf("  time delay       : %e\n", d);
printf("Estimated parameters\n");
printf("  phase difference : %f\n", phasediff);
printf("  time delay       : %e\n", delay);