# Delay measurements for sampled signals

## Introduction

The algorithms we will present here are used to determine the time shift between two sampled signals. In this analysis, we will consider the incoming signals to be the same. This do not mean that the samples are the same. They correspond to the values of the same signal but taken at different moment.

This approximation can be used for our application. Indeed, we plan to measure the impulse response of a filter wich is, theoretically, the same over the time.

## Cross correlation

The easiest way to measure the phase shift between two signals is by
doing the cross-correlation between

them. The delay between them is the distance between the maximum of the
correlation and the center of the correlation array. The center of this
array corresponds to the correlation without any phase-shift between the
two signals, without any lag.

Unfortunately, this method do not give a better resolution than the sampling frequency. Nevertheless, it is possible to make some interpolation to get higher resolution.

## Interpolation

According to the signal theory, it is possible to reconstruct any sampled signals without error. This signal must be sampled respecting some conditions such as the Nyquist theorem. To reconstruct the signal, we realise the convolution between the samples and the sinc function (cardinal sine).

Why not reconstruct the signal with a resolution of 1ps and then making the correlation again in order to be able to measure the delay with a picosecond resolution ?

By doing this, we would need a large amount of memory and the number of calculations would be gigantic. As the program will be implemented inside embedded system, we cannot provide these memory and computation power.

## The local interpolation algorithm

The python script ... is used to compute the delay between two sine
waves. These signals have a very thin

bandwidth and the calculations are easier.

The first thing done in the script is the signal creation and sampling.
The user can set the sines frequency,

the Signal Noise Ratio (SNR) and the delay between the two sines. He is
also able to set the ADC parameters

such as the sampling frequency or the Effective Number Of Bits (ENOB).
The number of samples taken for

the computations can also be
set.

*Explanation of the interpolation algorithm*

In order to avoid the memory to be overloaded, we compute the correlation only with a limited number of sampling points. At each iteration, we use the sinc interpolation to compute new sampling frame as if the sine was time shifted.

Then, the correlation is done between the first signal samples and the second signal new samples. The delay between the two sines is the delay giving the best correlation without time shifting between the two samples frames. This corresponds to compute the multiplication between the time corresponding samples and then summing all these results.

This calculation can be done for different values of time shifting. The delay giving us the best correlation at lag 0 is the value of the measured delay between the incoming signals.

This maximum can be determined by an optimization method such as the parabolic interpolation method. We decided to use the Brent's method. This method uses more points to determine the top of the function but is more stable.

In our code, we use the `optimise.brent()`

function. This function gives
an apporximation of the minimum of the function. Looking for the minimum
of a function f(x) is the same than looking for the maximum of the
function -f(x). That is the reason the function `MinusCorrLag0`

returns
`-corr_at_lag0`

.

### The use of windowing

Before doing the correlation, the two signals are windowed. Not doing this would lead us to some errors on the measurement. Indeed, the sines are not infinite and are time limited between the first and the last sample. This phenomena will generate a spectral leakage that can be seen in the frequency domain.

*FFT of the sine without zero padding, we cannot see any spectral
leakage*

To be able to see this phenomena while doing the FFT (Fast Fourier Transform), it may be necessary to compute it with a higher resolution. This higher resolution can be obtained by computing the FFT with more points. common technique to do it is by adding new samples at the end of the signal. As the value of these samples is zero, the name of this technique is "zero-padding".

*FFT of the sine with zero padding, the resolution allows us to see the
spectral leakage*

Theoretically, a sine in the frequency domain would give a pulse at the considered frequency. If this sine is time limited, we can observe the spectral leakage phenomenon. The resulting spectrum is the convolution between the sample and a sinc function corresponding to the Fourier Transform of a square window.

As a reminder, a multiplication in the time domain corresponds to a
convolution in the frequency domain and *vice versa*. Our time limited
sine is actually a sine multiplied by a square window. This explains
this spectral leakage.

To avoid it, we use the "windowing" technique. We multiply the time limited signal by another window with "smoother" shapes. We use for example a window with the shape of the Gaussian function. Theoretically, the Fourier transform of this function still being a Gaussian function. As this function is also time limited during our process, we still get some side lobes. fortunately, these side lobes are much more reduced.

*Effect of the windowing. The spectrum in blue corresponds to the signal
without windowing. A Hanning windowing function has been applied on the
red spectrum. The side lobes are reduced when thanks to the windowing
function*

The convolution of our sine and the resulting function better reflects the sine Fourier Transform.

### Results

The results of this algorithm are represented in the figure below.

*Results of the interpolation algorithm simulation*

To get these results, we set the parameters as presented here :

- Frequency of the sines : 50 MHz,
- Sampling frequency : 125 MHz,
- Number of samples taken into account : 64
- SNR : 60 dB
- ADC ENOB : 12 bits
- Final ENOB : around 10 bits

We did 16 measurements for the same delay between the incoming sines. This has been done for a couple of different delays. Each time, we measure the error on the measurements. As we can see, the precision is around 1 ps.

Unfortunately, this method is slow. We need around 0.5 s to get the result. In addition, this method is hard to implement in a FPGA due to the sinc interpolation.

## Fitting

Another way to measure the delay between two signals is to compare them with a model which is known with a big resolution.

This method is based on the same principle than the previous one.

*Fitting algorithm explanations*

The first step is to measure the time shift between the samples and one model. To do it, we compute the difference between the samples and the value of the model at that times. The sum of all these errors is saved. The samples are then delayed and the same calculation is done.

Instead of searching for the maximum of the function `error = f(delay)`

,
we search for its minimum. This can be done again by the Brent's method.

Once the delay between one sine and the other has been computed, we can do the same for the other sine. When both the delays are determined, the delay between the two real sines can be computed by making the difference between the results.

### Results

The results of the simulations are given in the figure below.

*Results of the fitting algorithm simulations*

These simulations were done with the same parameters than previously. This time, each experiment has been done 32 times. As we have added a new step in the method (the comparison with the model), we also add a source of error.

Nevertheless, this method could be easier to implement in a FPGA. Instead of computing each time the value of the signal at a point using, we can save the value of all the model points in the memory. When we need to evaluate the function at one point, we just have to search for its value in the memory.

For example, if the incoming signal is a 50 MHz sine wave and we want a picosecond resolution, the model would only be composed of 20000 points (the sine is a periodic function).

We do not have this advantage with the sinc function used for the interpolation method.

It should be said that we plan to use an incoming signal with the shape of a damped sine wave. If the damping factor and the number of samples taken into account are low enough, the signal can be approximated as coming from a sine wave.