我有一个奇数长度的窗口和一个数组,我需要找到窗口居中的数组的索引,并且匹配更多。到目前为止,我用下面的代码来做。有可能加快这些计算吗?
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
字符串
1条答案
按热度按时间n6lpvg4x1#
在我的测试中,这比以前快了10倍。
字符串
更快,使用更少的内存:
型