I have an array of signal traces, which are essentially captures of the power consumption of the same operation. However, they are time-shifted and I need to align them before searching for differences.
I am using numpy.correlate to estimate the time shift:
# Use cross-correlation to estimate the shift between arrays
# Returns the estimated shift
def get_time_shift(reference_array, lagging_array):
corr = np.correlate(reference_array, lagging_array, 'full')
max_corr = np.max(corr) # The maximum correlation coef.
argmax_corr = np.argmax(corr) # The position of the correlation coef.
shift = argmax_corr - (len(reference_array)-1)
return shift
Then I am using another function to shift the array:
# https://stackoverflow.com/questions/30399534/shift-elements-in-a-numpy-array
# preallocate empty array and assign slice by chrisaycock
def shift5(arr, num, fill_value=np.nan):
result = np.empty_like(arr)
if num > 0:
result[:num] = fill_value
result[num:] = arr[:-num]
elif num < 0:
result[num:] = fill_value
result[:num] = arr[-num:]
else:
result[:] = arr
return result
The final results are not what I am expecting. Here's an example reference array plot: reference array, and here's a time-shifted array: lagging array. I thought the largest correlation point should occur at the peaks of the waveform, and hence the two traces will be aligned so these peaks would match. The result is here: result; the orange trace is the shifted array.
I need the traces to align at the waveform peaks. Can you suggest a procedure?