2

In python, I have two similar signals of different lengths, and I want to find the offset between them.

I found this post: find time shift between two similar waveforms

But I don't understand the interpretation for different length signals.

For example:

a=[0,0,1,2,3]
b=[0,0,0,0,1,2,3,0]

and numpy.argmax(scipy.signal.correlate(a,b)) gives 5, which I don't understand. numpy.argmax(scipy.signal.correlate(b,a)) gives 6, which seems like the amount that b should be shifted to the right, assuming wrap-around. Can someone please explain?

aL_eX
  • 1,453
  • 2
  • 15
  • 30
user3433489
  • 911
  • 2
  • 10
  • 24

1 Answers1

0

The post "find time shift between two similar waveforms" uses the mathematical "correlate" which is only meaningful for equally sized arrays.

For your application, the following code will give the desired shift

import numpy as np
a=[0,0,1,2,3]
b=[0,0,0,0,1,2,3,0]
np.argmax(np.convolve(a[::-1],b,mode='valid'))

~~~~~~~~~~~~~~~~~~~~~ for the follow up comment question. Try

from scipy.linalg import circulant
b=[0,1,2,3,0]
a=[0,0,1,2,3]
[ i for i in range(len(b)) if not False in (a==circulant(b)[:,i])]

the problem is convolve doesn't recognize circular matrices

  • Thank you, that does work. My goal is to find how much `a` should be shifted. If `b=[0,1,2,3,0]` the above command gives 0, which is wrong. Is there a general command which will work in both cases? – user3433489 Feb 12 '18 at 19:59