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

后端 未结 8 1569
悲&欢浪女
悲&欢浪女 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:28

    another way to do this quickly and concisely:

    import pylab as pl
    
    v=[0,0,1,1,0,0,1,1,1,1,1,0,1,0,1,1,0,0,0,0,0,1,0,0]
    vd = pl.diff(v)
    #vd[i]==1 for 0->1 crossing; vd[i]==-1 for 1->0 crossing
    #need to add +1 to indexes as pl.diff shifts to left by 1
    
    i1=pl.array([i for i in xrange(len(vd)) if vd[i]==1])+1
    i2=pl.array([i for i in xrange(len(vd)) if vd[i]==-1])+1
    
    #corner cases for the first and the last element
    if v[0]==1:
      i1=pl.hstack((0,i1))
    if v[-1]==1:
      i2=pl.hstack((i2,len(v)))
    

    now i1 contains the beginning index and i2 the end index of 1,...,1 areas

提交回复
热议问题