code: def find_valley_range(vector, min_distance = 4):
      
      
      # http://users.monash.edu.au/~dengs/resource/papers/icme08.pdf
      # find min and max with mode = wrap
      mode = "wrap"
      minima = local_minima(vector,min_distance,mode)
      maxima = local_maxima(vector,min_distance,mode)
  
      if len(maxima)>len(minima):
          if vector[maxima[0]] >= vector[maxima[-1]]:
              maxima=maxima[1:]
          else:
              maxima=maxima[:-1]
          
      if len(maxima)==len(minima):
          valley_range = numpy.asarray([minima[ii+1] - minima[ii] for ii in range(len(minima)-1)] + [len(vector)-minima[-1]+minima[0]])
          if minima[0] < maxima[0]:
              minima = numpy.asarray(list(minima) + [minima[0]])
          else:
              minima = numpy.asarray(list(minima) + [minima[-1]])       
      else:
          valley_range = numpy.asarray([minima[ii+1] - minima[ii] for ii in range(len(maxima))])
      
      return maxima, minima, valley_range
docs: Internal finder peaks and valley ranges.
      Returns UNSORTED indices of maxima in input vector.
      Returns range of valleys before and after maximum