Find large number of consecutive values fulfilling condition in a numpy array

后端 未结 8 1570
悲&欢浪女
悲&欢浪女 2020-12-05 00:53

I have some audio data loaded in a numpy array and I wish to segment the data by finding silent parts, i.e. parts where the audio amplitude is below a certain threshold over

8条回答
  •  猫巷女王i
    2020-12-05 01:45

    I haven't tested this but you it should be close to what you are looking for. Slightly more lines of code but should be more efficient, readable, and it doesn't abuse regular expressions :-)

    def find_silent(samples):
        num_silent = 0
        start = 0
        for index in range(0, len(samples)):
            if abs(samples[index]) < SILENCE_THRESHOLD:
                if num_silent == 0:
                    start = index
                num_silent += 1
            else:
                if num_silent > MIN_SILENCE:
                    yield samples[start:index]
                num_silent = 0
        if num_silent > MIN_SILENCE:
            yield samples[start:]
    
    for match in find_silent(samples):
        # code goes here
    

提交回复
热议问题