对4个subarray的检测结果设置阈值,做判断,保留其中至少2个subarray能够检测到的结果。思路是对于1来说,其余三个结果合并做差小于阈值,即满足条件;然而还有2和3或者4,同时排出和1,这时候先做判断,排除掉1和2已经append的结果,然后做2和3、4;以此类推,3就只和4
def voting_scheme(arr1, arr2, arr3, arr4, thre=10):
valid_values = []
#-----------arr1 with arr2 or arr3 or arr4 ar all of them------------#
for i, num in enumerate(arr1):
other = np.hstack((arr2, arr3, arr4))
other = np.sort(other)
diff = abs(num - other)
# f_data = np.where(np.abs(f_data)<np.abs(thre_amp), f_data, thre_amp)
if np.any(diff <= thre):
valid_values.append(num)
#-----------arr2 with arr3 or arr4 or all of them------------#
arr2_ = arr2.copy()
J = []
for j, num in enumerate(arr2):
diff_arr1 = abs(num - arr1)
if np.any(diff_arr1 <=thre):
J.append(j)
arr2_ = np.delete(arr2_, J)
for j, num in enumerate(arr2_):
other = np.hstack((arr3, arr4))
other = np.sort(other)
diff = abs(num - other)
if np.any(diff <= thre):
valid_values.append(num)
#-----------arr3 with arr4------------#
arr3_ = arr3.copy()
K = []
for k, num in enumerate(arr3):
diff_arr12 = abs(num - np.hstack((arr1, arr2)))
if np.any(diff_arr12 <=thre):
K.append(k)
arr3_ = np.delete(arr3_, K)
for k, num in enumerate(arr3_):
other = arr4
diff = abs(num - other)
if np.any(diff <= thre):
valid_values.append(num)
return valid_values
def drop_elements(arr, threshold):
# Initialize a new empty list to store the indices of elements to keep
keep_indices = [0]
# Iterate through the array, starting from the second element
for i in range(1, len(arr)):
# If the difference between the current element and the last kept element is greater than the threshold,
# add the index of the current element to the list of indices to keep
if arr[i] - arr[keep_indices[-1]] > threshold:
keep_indices.append(i)
# Use the numpy indexing syntax to return the elements at the selected indices
return arr[keep_indices]