title: Find Closest Minima to Peak
body: <p>I am using <a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.find_peaks.html" rel="nofollow noreferrer"><code>scipy.singal.find_peaks</code></a> to find peaks and minimas using:</p>
  
  <pre><code>import numpy as np
  from scipy.signal import find_peaks
  
  x=np.array([9.8,57.,53.,37.,24.,19.,16.,15.,13.,13.,12.,12.,11.,11.,11.,11.,11.,11.,10.,13.,13.,13.,15.,13.,12.,14.,15.,14.,51.,34.,24.,20.,24.,22.,18.,57.,63.,38.,27.,28.,31.,33.,94.,71.,48.,40.,43.,39.,31.,27.,22.,21.,20.,19.,18.,18.,19.,20.,20.,49.,62.,48.,43.,34.,33.,28.,26.,26.,24.,23.,23.,26.,27.,70.,97.,57.,46.,68.,82.,59.,49.,37.,40.,45.,36.,33.,28.,22.,23.,284.,524.,169.,111.,148.,98.,68.,50.,38.,30.,28.])
  peaks, _ = find_peaks(x)
  mins, _ =find_peaks(x*-1)
  </code></pre>
  
  <p>Which looks like:</p>
  
  <p><a href="https://i.stack.imgur.com/AL5bE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AL5bE.png" alt="enter image description here"></a></p>
  
  <p>Now I am interested to find the closest minima to each peak. So I can take the difference between them.</p>
  
  <p>After looking through the <code>find_peaks</code> documentation, the argument <a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.peak_prominences.html#scipy.signal.peak_prominences" rel="nofollow noreferrer"><code>peak_prominece</code></a> seems like what I am looking for. </p>
  
  <pre><code>prominences = peak_prominences(x, peaks)[0]
  contour_heights = x[peaks] - prominences
  </code></pre>
  
  <p>Which then looks like:</p>
  
  <p><a href="https://i.stack.imgur.com/iffy8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iffy8.png" alt="enter image description here"></a></p>
  
  <p>After inspection, <code>peak_prominences</code> has found the minimum preceding the peak. For my application, I want the closest peak, regardless if it was preceding or following.</p>
  
  <p>How could I to use <code>mins</code> to define the parameter <code>wlen</code> for the <code>peak_prominence</code> calculation.?</p>
  
  <p>Since <code>mins</code> consists of the indices of the minima, how can I use it to define <code>wlen</code>?  I would basically have to find the indices in min that bound each peak (i.e. <code>peaks[i]</code>).  </p>
  
  <p>Is there a better way to achieve this just using <code>mins</code> and <code>peaks</code>?</p>