9
$\begingroup$

I have 2 sets of data containing delays (audio / video).

data1 = Table[Sin[x + RandomReal[]/4], {x, 0, 2 Pi, Pi/10}]
data2 = Table[Sin[x + Pi/4 + RandomReal[]/4], {x, 0, 2 Pi, Pi/10}]
ListLinePlot[{data1, data2}, PlotRange -> All, Filling -> {1 -> 0, 2 -> 0}]

enter image description here

I want to correlate delays and find the synchronization offset.

  • in one instance of time
  • over a longer period
$\endgroup$
  • $\begingroup$ See this $\endgroup$ – Rojo Nov 2 '12 at 10:56
11
$\begingroup$

One method is by cross-correlating the signals. Without getting bogged down in the plurality of correlation "metrics", can start by using the built in Correlation function. Find the arg max correlation as a function of offset (measured in samples)

Table[
 k -> Correlation[data1, RotateRight[data2, k]],
 {k, 0, 5}]

(* {0 -> 0.713864, 1 -> 0.881013, 2 -> 0.969062, 3 -> 0.982054, 4 -> 0.891398, 5 -> 0.736285} *)

Arg max offset is 3. Compare the overlap between data1 and data2 using this value versus the nominal 0 offset that you plot above:

ListLinePlot[{data1, RotateRight[data2, 3]}, PlotRange -> All, 
 Filling -> {1 -> 0, 2 -> 0}]

enter image description here

This basic method doesn't handle sample interpolation and likely will be sensitive to the spectral and transient characteristics of your signals, as well as the choice of correlation as mentioned above, which is an open concept (eg, $L2$ versus $L1$ loss functionals)

$\endgroup$
6
$\begingroup$

Another method to find the offset is to find the difference between fits.

data1 = Table[{x, Sin[x + RandomReal[]/4]}, {x, 0, 2 Pi, Pi/10}];
data2 = Table[{x, Sin[x + Pi/4 + RandomReal[]/4]}, {x, 0, 2 Pi, 
    Pi/10}];

f1 = FindFit[data1, Sin[x + offset], offset, x]
(* {offset -> 0.0953108} *)
f2 = FindFit[data2, Sin[x + offset], offset, x]
(* {offset -> 0.881167} *)

The offset is equal to:

f2[[1, 2]] - f1[[1, 2]]
(* 0.785856 *)

that corresponds to $\pi/4$.

Data with fits:

Show[
 ListLinePlot[{data1, data2}, PlotRange -> All, 
  Filling -> {1 -> 0, 2 -> 0}],
 Plot[Sin[x + offset] /. f1, {x, 0, 2 Pi}, 
  PlotStyle -> {ColorData[1, 1], Thick}],
 Plot[Sin[x + offset] /. f2, {x, 0, 2 Pi}, 
  PlotStyle -> {ColorData[1, 2], Thick}]
 ]

Sine waves

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.