Reputation: 27
I have an array of traces that are look like this :
Really small low part then a big High part and ended with low part again.
I want to be able to align all those traces ... as close as I can (so the changes from low to high and the opposite will be at the same indexes).
I tried to use cross-correlation but that gave me 0 offset ... I don't know why.
def give_offset(y,y2):
# limit your signal length to speed things up
lim = 2000
# do the actual correlation
corr = scipy.signal.correlate(y[:lim], y2[:lim], mode='full')
# The offset is the maximum of your correlation array,
# itself being offset by (lim - 1):
offset = np.argmax(corr) - (lim - 1)
return offset
I didn't find anything over the internet and I'm so confused since it seems like a common problem that many faced before.
Upvotes: 1
Views: 683
Reputation: 1
I know this is kind of an old question, but I'll answer it anyway for future people interested. The way I solved this problem was by taking a section of the signal from one array and cross-correlating the other arrays with the section. I chose the max correlation and subtracted the max index of the subarray to get the offset. Subtracting the mean of the subarray for cross-correlation is important to get the right answer. This example will confirm
a = np.concatenate((np.random.normal(size=100),np.random.normal(size=100)-10))
b = np.concatenate((np.random.normal(size=45),np.random.normal(size=155)-10))
max_ind = 150
min_ind = 50
small_sect = a[min_ind:max_ind]
cc = scipy.signal.correlate(b-np.mean(small_sect),small_sect-np.mean(small_sect))
offset = np.argmax(cc) - max_ind + 1
Edit: The more correct way to do this is by Generalized Cross-Correlation. See https://github.com/SiggiGue/gccestimating for a python implementation.
Upvotes: 0