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

后端 未结 8 1567
悲&欢浪女
悲&欢浪女 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条回答
  •  误落风尘
    2020-12-05 01:29

    Slightly sloppy, but simple and fast-ish, if you don't mind using scipy:

    from scipy.ndimage import gaussian_filter
    sigma = 3
    threshold = 1
    above_threshold = gaussian_filter(data, sigma=sigma) > threshold
    

    The idea is that quiet portions of the data will smooth down to low amplitude, and loud regions won't. Tune 'sigma' to affect how long a 'quiet' region must be; tune 'threshold' to affect how quiet it must be. This slows down for large sigma, at which point using FFT-based smoothing might be faster.

    This has the added benefit that single 'hot pixels' won't disrupt your silence-finding, so you're a little less sensitive to certain types of noise.

提交回复
热议问题