-1

I have data like this: [1,3,3,....1] ~ 500 numbers.

Each 1 ms, i've got a new one, but with some shift(5-10 points)+small noise.

E.g:
 [1 2 3 4 3 21 4 5...]
 [0 4 3 1 2  3 4 19 7 5 ...] 

In this case shift is 3.

I want to know this shift. How can I get it?

Fourier way is to slow.. Because I have many lines per second. find time shift between two similar waveforms

May be there some fast ways? Or May be I should use Fourier for only part of my data (because they are shifting approximately like whole). Many thanks.

Community
  • 1
  • 1
Mister_ll
  • 1
  • 1

1 Answers1

0

If you know that the lag is only a few samples 5-10, then you could use autocorrelation to find it. It should be fast if you only calculate it for this limited small range of lags. If you calculate it for all possible lags, then it will likely be very slow.

Here is an example where a modified AMDF algorithm has been used:

import numpy as np

def modified_amdf(x1, x2, steps):
    N = min(len(x1), len(x2))
    res = []
    for step in steps:
        sm = 0
        for n in range(0, N - step):
            sm += np.abs(x1[n] - x2[n + step])
        sm = sm * (1.0 / (N - step - 1))
        res.append(sm)
    return res

#x1 = [1, 2, 3, 4, 3, 21, 4, 5]
#x2 = [0, 4, 3, 1, 2, 3, 4, 19, 7, 5]
x1 = np.sin(np.linspace(0, 10*np.pi, 500))
x2 = np.r_[[0,0], x1]  # add two lag entries
penalties = modified_amdf(x1, x2, range(5))
print "Found lag:", np.argmin(penalties)

You can improve the speed a bit if you change the calculations to used only numpy, but this should take long with only 500 samples for a few lags.

J. P. Petersen
  • 4,871
  • 4
  • 33
  • 33