""" Searches for peaks in data
    History:
         -nov 2015: Janko Slavic, update
         -mar 2013: janko.slavic@fs.uni-lj.si
"""

import numpy as np

def findpeaks(data, spacing=10, limit=None):
    """Finds peaks in `data` which are of `spacing` width and >=`limit`.
    :param data: values
    :param spacing: minimum width for single peak
    :param limit: peaks should have value greater or equal
    :return:
    """
    x = np.zeros(len(data)+2*spacing)
    x[:spacing] = data[0]
    x[-spacing:] = data[-1]
    x[spacing:-spacing] = data
    peak_candidate = np.zeros(x.size - 2*spacing - 2)
    peak_candidate[:] = True
    for s in range(spacing):
        h_b = x[s:-2 * spacing + s - 2]  # before
        h_c = x[spacing: -spacing - 2]  # central
        h_a = x[spacing + s + 1: -spacing + s - 1]  # after
        peak_candidate = np.logical_and(peak_candidate, np.logical_and(h_c > h_b, h_c > h_a))

    ind = np.argwhere(peak_candidate)
    ind = ind.reshape(ind.size)
    if limit is not None:
        ind = ind[data[ind] > limit]
    return ind


if __name__ == '__main__':
    import matplotlib.pyplot as plt

    n = 1000
    m = 20
    t = np.linspace(0., 1, n)
    x = np.zeros(n)
    np.random.seed(0)
    phase = 2 * np.pi * np.random.random(m)
    for i in range(m):
        x += np.sin(phase[i] + 2 * np.pi * t * i)

    peaks = findpeaks(x, spacing=100, limit=4.)
    plt.plot(t, x)
    plt.axhline(4, color='r')
    plt.plot(t[peaks], x[peaks], 'ro')
    plt.title('Peaks: minimum value 4., minimum width 100 points')
    plt.show()

@@ -149,16 +149,17 @@
  as np%0A%0A
+%0A
 def find
@@ -179,17 +179,16 @@
 pacing=1
-0
 , limit=
@@ -323,29 +323,54 @@
 mum 
-width for single peak
+spacing to the next peak (should be 1 or more)
 %0A   
@@ -446,16 +446,36 @@
     %22%22%22%0A
+    len = data.size%0A
     x = 
@@ -490,14 +490,8 @@
 (len
-(data)
 +2*s
@@ -523,16 +523,22 @@
  data%5B0%5D
+-1.e-6
 %0A    x%5B-
@@ -557,16 +557,22 @@
 data%5B-1%5D
+-1.e-6
 %0A    x%5Bs
@@ -578,24 +578,27 @@
 spacing:
--
 spacing
++len
 %5D = data
@@ -632,30 +632,11 @@
 ros(
-x.size - 2*spacing - 2
+len
 )%0A  
@@ -703,38 +703,67 @@
     
-h_b = x%5Bs:-2 * spacing + s - 2
+start = spacing - s - 1%0A        h_b = x%5Bstart : start + len
 %5D  #
@@ -782,37 +782,59 @@
     
-h_c
+start
  = 
-x%5B
 spacing
-: -spacing - 2
+%0A        h_c = x%5Bstart : start + len
 %5D  #
@@ -854,16 +854,16 @@
     
-h_a
+start
  = 
-x%5B
 spac
@@ -877,26 +877,44 @@
  + 1
-: -spacing + s - 1
+%0A        h_a = x%5Bstart : start + len
 %5D  #
@@ -1237,19 +1237,17 @@
     n = 
-100
+8
 0%0A    m 
@@ -1251,16 +1251,46 @@
  m = 20%0A
+    limit = 0%0A    spacing = 3%0A
     t = 
@@ -1508,19 +1508,23 @@
 ing=
-100
+spacing
 , limit=
 4.)%0A
@@ -1519,18 +1519,21 @@
 , limit=
-4.
+limit
 )%0A    pl
@@ -1561,17 +1561,21 @@
 axhline(
-4
+limit
 , color=
@@ -1658,10 +1658,15 @@
 lue 
-4.
+%7Blimit%7D
 , mi
@@ -1675,25 +1675,80 @@
 mum 
-width 100 points'
+spacing %7Bspacing%7D points'.format(**%7B'limit': limit, 'spacing': spacing%7D)
 )%0A  

Update Janko Slavic findpeaks