我有一个奇数长度的窗口和一个数组,我需要找到窗口居中并匹配更多的数组的索引。到目前为止,我正在使用以下代码来完成此操作。是否可以加快这些计算速度?
import numpy as np
import time
def find_best_match_position(lst, window):
min_corr = np.inf
best_position = -1
for i in range(len(lst) - len(window) + 1):
window_subset = lst[i:i + len(window)]
corr = np.linalg.norm(window - window_subset)
if corr < min_corr:
min_corr = corr
best_position = i
return best_position
input_list_len = int(8E+6)
np.random.seed(2)
input_list = np.random.rand(input_list_len)
win_length = 31
np.random.seed(4)
window = np.random.rand(win_length)
gnd_index = 15
half_width = win_length // 2
start = gnd_index - half_width # Shift start by 1 to the right
end = gnd_index + half_width + 1
input_list[start:end] = window + 0.01 * np.random.rand(win_length)
t = time.time()
print(f'Computed index {find_best_match_position(input_list, window) + half_width}')
t1 = time.time()
print(f'Computation time {(t1 - t) / 60} min')
# Computed index 15
# Computation time 0.6488747239112854 min
在我的测试中,速度快了 10 倍以上。
def find_best_match_position(lst, window):
slides = np.lib.stride_tricks.sliding_window_view(lst, len(window))
norms = np.linalg.norm(window - slides, axis=1)
return norms.argmin()