gpt4 book ai didi

python - 提取值高于阈值的 numpy 数组的子数组

转载 作者:太空宇宙 更新时间:2023-11-03 14:06:24 25 4
gpt4 key购买 nike

我有一个声音信号,作为 numpy 数组导入,我想将它切割成 numpy 数组 block 。但是,我希望 block 只包含高于阈值的元素。例如:

threshold = 3
signal = [1,2,6,7,8,1,1,2,5,6,7]

应该输出两个数组

vec1 = [6,7,8]
vec2 = [5,6,7]

好的,上面是列表,但你明白我的意思了。

这是我到目前为止尝试过的方法,但这只会耗尽我的 RAM

def slice_raw_audio(audio_signal, threshold=5000):

signal_slice, chunks = [], []

for idx in range(0, audio_signal.shape[0], 1000):
while audio_signal[idx] > threshold:
signal_slice.append(audio_signal[idx])
chunks.append(signal_slice)
return chunks

最佳答案

这是一种方法-

def split_above_threshold(signal, threshold):
mask = np.concatenate(([False], signal > threshold, [False] ))
idx = np.flatnonzero(mask[1:] != mask[:-1])
return [signal[idx[i]:idx[i+1]] for i in range(0,len(idx),2)]

sample 运行-

In [48]: threshold = 3
...: signal = np.array([1,1,7,1,2,6,7,8,1,1,2,5,6,7,2,8,7,2])
...:

In [49]: split_above_threshold(signal, threshold)
Out[49]: [array([7]), array([6, 7, 8]), array([5, 6, 7]), array([8, 7])]

运行时测试

其他方法-

# @Psidom's soln
def arange_diff(signal, threshold):
above_th = signal > threshold
index, values = np.arange(signal.size)[above_th], signal[above_th]
return np.split(values, np.where(np.diff(index) > 1)[0]+1)

# @Kasramvd's soln
def split_diff_step(signal, threshold):
return np.split(signal, np.where(np.diff(signal > threshold))[0] + 1)[1::2]

时间 -

In [67]: signal = np.random.randint(0,9,(100000))

In [68]: threshold = 3

# @Kasramvd's soln
In [69]: %timeit split_diff_step(signal, threshold)
10 loops, best of 3: 39.8 ms per loop

# @Psidom's soln
In [70]: %timeit arange_diff(signal, threshold)
10 loops, best of 3: 20.5 ms per loop

In [71]: %timeit split_above_threshold(signal, threshold)
100 loops, best of 3: 8.22 ms per loop

关于python - 提取值高于阈值的 numpy 数组的子数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43258896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com