简体   繁体   中英

Align an array of traces in python

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 inte.net and I'm so confused since it seems like a common problem that many faced before.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM