

def peakdet(v, delta, x = None):
  # https://github.com/mattijn/pynotebook/blob/16fe0f58624938b82d93cbd208b8cb871ab95ec1/
  # ipynotebooks/Python2.7/.ipynb_checkpoints/PLOTS%20SIGNAL%20PROCESSING-P1%20and%20P2-checkpoint.ipynb

  """
  Converted from MATLAB script at http://billauer.co.il/peakdet.html
  
  Returns two arrays
  
  function [maxtab, mintab]=peakdet(v, delta, x)
  %PEAKDET Detect peaks in a vector
  %    [MAXTAB, MINTAB] = PEAKDET(V, DELTA) finds the local
  %    maxima and minima ("peaks") in the vector V.
  %    MAXTAB and MINTAB consists of two columns. Column 1
  %    contains indices in V, and column 2 the found values.
  %    
  %    With [MAXTAB, MINTAB] = PEAKDET(V, DELTA, X) the indices
  %    in MAXTAB and MINTAB are replaced with the corresponding
  %    X-values.
  %
  %    A point is considered a maximum peak if it has the maximal
  %    value, and was preceded (to the left) by a value lower by
  %    DELTA.
  
  % <NAME>, 3.4.05 (Explicitly not copyrighted).
  % This function is released to the public domain; Any use is allowed.
  
  """
  maxtab = []
  mintab = []
     
  if x is None:
    x = arange(len(v))
  
  v = asarray(v)
  
  if len(v) != len(x):
    sys.exit('Input vectors v and x must have same length')
  
  if not isscalar(delta):
    sys.exit('Input argument delta must be a scalar')
  
  if delta <= 0:
    sys.exit('Input argument delta must be positive')
  
  mn, mx = Inf, -Inf
  mnpos, mxpos = NaN, NaN
  
  lookformax = True
  
  for i in arange(len(v)):
    this = v[i]
    if this > mx:
      mx = this
      mxpos = x[i]
    if this < mn:
      mn = this
      mnpos = x[i]
    
    if lookformax:
      if this < mx-delta:
        maxtab.append((mxpos, mx))
        mn = this
        mnpos = x[i]
        lookformax = False
    else:
      if this > mn+delta:
        mintab.append((mnpos, mn))
        mx = this
        mxpos = x[i]
        lookformax = True
 
  return array(maxtab), array(mintab)

def peakdetect(y_axis, x_axis = None, lookahead = 300, delta=0):
    """
    Converted from/based on a MATLAB script at: 
    http://billauer.co.il/peakdet.html
    
    function for detecting local maximas and minmias in a signal.
    Discovers peaks by searching for values which are surrounded by lower
    or larger values for maximas and minimas respectively
    
    keyword arguments:
    y_axis -- A list containg the signal over which to find peaks
    x_axis -- (optional) A x-axis whose values correspond to the y_axis list
        and is used in the return to specify the postion of the peaks. If
        omitted an index of the y_axis is used. (default: None)
    lookahead -- (optional) distance to look ahead from a peak candidate to
        determine if it is the actual peak (default: 200) 
        '(sample / period) / f' where '4 >= f >= 1.25' might be a good value
    delta -- (optional) this specifies a minimum difference between a peak and
        the following points, before a peak may be considered a peak. Useful
        to hinder the function from picking up false peaks towards to end of
        the signal. To work well delta should be set to delta >= RMSnoise * 5.
        (default: 0)
            delta function causes a 20% decrease in speed, when omitted
            Correctly used it can double the speed of the function
    
    return -- two lists [max_peaks, min_peaks] containing the positive and
        negative peaks respectively. Each cell of the lists contains a tupple
        of: (position, peak_value) 
        to get the average peak value do: np.mean(max_peaks, 0)[1] on the
        results to unpack one of the lists into x, y coordinates do: 
        x, y = zip(*tab)
    """
    max_peaks = []
    min_peaks = []
    dump = []   #Used to pop the first hit which almost always is false
       
    # check input data
    x_axis, y_axis = _datacheck_peakdetect(x_axis, y_axis)
    # store data length for later use
    length = len(y_axis)
    
    
    #perform some checks
    if lookahead < 1:
        raise ValueError ( "Lookahead must be '1' or above in value")
    if not (np.isscalar(delta) and delta >= 0):
        raise ValueError ( "delta must be a positive number" )
    
    #maxima and minima candidates are temporarily stored in
    #mx and mn respectively
    mn, mx = np.Inf, -np.Inf
    
    #Only detect peak if there is 'lookahead' amount of points after it
    for index, (x, y) in enumerate(zip(x_axis[:-lookahead], 
                                        y_axis[:-lookahead])):
        if y > mx:
            mx = y
            mxpos = x
        if y < mn:
            mn = y
            mnpos = x
        
        ####look for max####
        if y < mx-delta and mx != np.Inf:
            #Maxima peak candidate found
            #look ahead in signal to ensure that this is a peak and not jitter
            if y_axis[index:index+lookahead].max() < mx:
                max_peaks.append([mxpos, mx])
                dump.append(True)
                #set algorithm to only find minima now
                mx = np.Inf
                mn = np.Inf
                if index+lookahead >= length:
                    #end is within lookahead no more peaks can be found
                    break
                continue
            #else:  #slows shit down this does
            #    mx = ahead
            #    mxpos = x_axis[np.where(y_axis[index:index+lookahead]==mx)]
        
        ####look for min####
        if y > mn+delta and mn != -np.Inf:
            #Minima peak candidate found 
            #look ahead in signal to ensure that this is a peak and not jitter
            if y_axis[index:index+lookahead].min() > mn:
                min_peaks.append([mnpos, mn])
                dump.append(False)
                #set algorithm to only find maxima now
                mn = -np.Inf
                mx = -np.Inf
                if index+lookahead >= length:
                    #end is within lookahead no more peaks can be found
                    break
            #else:  #slows shit down this does
            #    mn = ahead
            #    mnpos = x_axis[np.where(y_axis[index:index+lookahead]==mn)]
    
    
    #Remove the false hit on the first value of the y_axis
    try:
        if dump[0]:
            max_peaks.pop(0)
        else:
            min_peaks.pop(0)
        del dump
    except IndexError:
        #no peaks were found, should the function return empty lists?
        pass
        
    return [max_peaks, min_peaks]



mport sys
from numpy import NaN, Inf, arange, isscalar, asarray, array

def peakdet(v, delta, x = None):
    #
    # https://gist.github.com/endolith/250860
    # 
    """
    Converted from MATLAB script at http://billauer.co.il/peakdet.html
    
    Returns two arrays
    
    function [maxtab, mintab]=peakdet(v, delta, x)
    %PEAKDET Detect peaks in a vector
    %        [MAXTAB, MINTAB] = PEAKDET(V, DELTA) finds the local
    %        maxima and minima ("peaks") in the vector V.
    %        MAXTAB and MINTAB consists of two columns. Column 1
    %        contains indices in V, and column 2 the found values.
    %      
    %        With [MAXTAB, MINTAB] = PEAKDET(V, DELTA, X) the indices
    %        in MAXTAB and MINTAB are replaced with the corresponding
    %        X-values.
    %
    %        A point is considered a maximum peak if it has the maximal
    %        value, and was preceded (to the left) by a value lower by
    %        DELTA.
    
    % <NAME>, 3.4.05 (Explicitly not copyrighted).
    % This function is released to the public domain; Any use is allowed.
    
    """
    maxtab = []
    mintab = []
       
    if x is None:
        x = arange(len(v))
    
    v = asarray(v)
    
    if len(v) != len(x):
        sys.exit('Input vectors v and x must have same length')
    
    if not isscalar(delta):
        sys.exit('Input argument delta must be a scalar')
    
    if delta <= 0:
        sys.exit('Input argument delta must be positive')
    
    mn, mx = Inf, -Inf
    mnpos, mxpos = NaN, NaN
    
    lookformax = True
    
    for i in arange(len(v)):
        this = v[i]
        if this > mx:
            mx = this
            mxpos = x[i]
        if this < mn:
            mn = this
            mnpos = x[i]
        
        if lookformax:
            if this < mx-delta:
                maxtab.append((mxpos, mx))
                mn = this
                mnpos = x[i]
                lookformax = False
        else:
            if this > mn+delta:
                mintab.append((mnpos, mn))
                mx = this
                mxpos = x[i]
                lookformax = True

    return array(maxtab), array(mintab)

if __name__=="__main__":
    from matplotlib.pyplot import plot, scatter, show
    series = [0,0,0,2,0,0,0,-2,0,0,0,2,0,0,0,-2,0]
    maxtab, mintab = peakdet(series,.3)
    plot(series)
    scatter(array(maxtab)[:,0], array(maxtab)[:,1], color='blue')
    scatter(array(mintab)[:,0], array(mintab)[:,1], color='red')
    show()


def peakdet(v, delta, x = None):
  # https://github.com/mattijn/pynotebook/blob/16fe0f58624938b82d93cbd208b8cb871ab95ec1/
  # ipynotebooks/Python2.7/.ipynb_checkpoints/PLOTS%20SIGNAL%20PROCESSING-P1%20and%20P2-checkpoint.ipynb

  """
  Converted from MATLAB script at http://billauer.co.il/peakdet.html
  
  Returns two arrays
  
  function [maxtab, mintab]=peakdet(v, delta, x)
  %PEAKDET Detect peaks in a vector
  %    [MAXTAB, MINTAB] = PEAKDET(V, DELTA) finds the local
  %    maxima and minima ("peaks") in the vector V.
  %    MAXTAB and MINTAB consists of two columns. Column 1
  %    contains indices in V, and column 2 the found values.
  %    
  %    With [MAXTAB, MINTAB] = PEAKDET(V, DELTA, X) the indices
  %    in MAXTAB and MINTAB are replaced with the corresponding
  %    X-values.
  %
  %    A point is considered a maximum peak if it has the maximal
  %    value, and was preceded (to the left) by a value lower by
  %    DELTA.
  
  % <NAME>, 3.4.05 (Explicitly not copyrighted).
  % This function is released to the public domain; Any use is allowed.
  
  """
  maxtab = []
  mintab = []
     
  if x is None:
    x = arange(len(v))
  
  v = asarray(v)
  
  if len(v) != len(x):
    sys.exit('Input vectors v and x must have same length')
  
  if not isscalar(delta):
    sys.exit('Input argument delta must be a scalar')
  
  if delta <= 0:
    sys.exit('Input argument delta must be positive')
  
  mn, mx = Inf, -Inf
  mnpos, mxpos = NaN, NaN
  
  lookformax = True
  
  for i in arange(len(v)):
    this = v[i]
    if this > mx:
      mx = this
      mxpos = x[i]
    if this < mn:
      mn = this
      mnpos = x[i]
    
    if lookformax:
      if this < mx-delta:
        maxtab.append((mxpos, mx))
        mn = this
        mnpos = x[i]
        lookformax = False
    else:
      if this > mn+delta:
        mintab.append((mnpos, mn))
        mx = this
        mxpos = x[i]
        lookformax = True
 
  return array(maxtab), array(mintab)

def peakdetect(y_axis, x_axis = None, lookahead = 300, delta=0):
    """
    Converted from/based on a MATLAB script at: 
    http://billauer.co.il/peakdet.html
    
    function for detecting local maximas and minmias in a signal.
    Discovers peaks by searching for values which are surrounded by lower
    or larger values for maximas and minimas respectively
    
    keyword arguments:
    y_axis -- A list containg the signal over which to find peaks
    x_axis -- (optional) A x-axis whose values correspond to the y_axis list
        and is used in the return to specify the postion of the peaks. If
        omitted an index of the y_axis is used. (default: None)
    lookahead -- (optional) distance to look ahead from a peak candidate to
        determine if it is the actual peak (default: 200) 
        '(sample / period) / f' where '4 >= f >= 1.25' might be a good value
    delta -- (optional) this specifies a minimum difference between a peak and
        the following points, before a peak may be considered a peak. Useful
        to hinder the function from picking up false peaks towards to end of
        the signal. To work well delta should be set to delta >= RMSnoise * 5.
        (default: 0)
            delta function causes a 20% decrease in speed, when omitted
            Correctly used it can double the speed of the function
    
    return -- two lists [max_peaks, min_peaks] containing the positive and
        negative peaks respectively. Each cell of the lists contains a tupple
        of: (position, peak_value) 
        to get the average peak value do: np.mean(max_peaks, 0)[1] on the
        results to unpack one of the lists into x, y coordinates do: 
        x, y = zip(*tab)
    """
    max_peaks = []
    min_peaks = []
    dump = []   #Used to pop the first hit which almost always is false
       
    # check input data
    x_axis, y_axis = _datacheck_peakdetect(x_axis, y_axis)
    # store data length for later use
    length = len(y_axis)
    
    
    #perform some checks
    if lookahead < 1:
        raise ValueError ( "Lookahead must be '1' or above in value")
    if not (np.isscalar(delta) and delta >= 0):
        raise ValueError ( "delta must be a positive number" )
    
    #maxima and minima candidates are temporarily stored in
    #mx and mn respectively
    mn, mx = np.Inf, -np.Inf
    
    #Only detect peak if there is 'lookahead' amount of points after it
    for index, (x, y) in enumerate(zip(x_axis[:-lookahead], 
                                        y_axis[:-lookahead])):
        if y > mx:
            mx = y
            mxpos = x
        if y < mn:
            mn = y
            mnpos = x
        
        ####look for max####
        if y < mx-delta and mx != np.Inf:
            #Maxima peak candidate found
            #look ahead in signal to ensure that this is a peak and not jitter
            if y_axis[index:index+lookahead].max() < mx:
                max_peaks.append([mxpos, mx])
                dump.append(True)
                #set algorithm to only find minima now
                mx = np.Inf
                mn = np.Inf
                if index+lookahead >= length:
                    #end is within lookahead no more peaks can be found
                    break
                continue
            #else:  #slows shit down this does
            #    mx = ahead
            #    mxpos = x_axis[np.where(y_axis[index:index+lookahead]==mx)]
        
        ####look for min####
        if y > mn+delta and mn != -np.Inf:
            #Minima peak candidate found 
            #look ahead in signal to ensure that this is a peak and not jitter
            if y_axis[index:index+lookahead].min() > mn:
                min_peaks.append([mnpos, mn])
                dump.append(False)
                #set algorithm to only find maxima now
                mn = -np.Inf
                mx = -np.Inf
                if index+lookahead >= length:
                    #end is within lookahead no more peaks can be found
                    break
            #else:  #slows shit down this does
            #    mn = ahead
            #    mnpos = x_axis[np.where(y_axis[index:index+lookahead]==mn)]
    
    
    #Remove the false hit on the first value of the y_axis
    try:
        if dump[0]:
            max_peaks.pop(0)
        else:
            min_peaks.pop(0)
        del dump
    except IndexError:
        #no peaks were found, should the function return empty lists?
        pass
        
    return [max_peaks, min_peaks]



mport sys
from numpy import NaN, Inf, arange, isscalar, asarray, array

def peakdet(v, delta, x = None):
    #
    # https://gist.github.com/endolith/250860
    # 
    """
    Converted from MATLAB script at http://billauer.co.il/peakdet.html
    
    Returns two arrays
    
    function [maxtab, mintab]=peakdet(v, delta, x)
    %PEAKDET Detect peaks in a vector
    %        [MAXTAB, MINTAB] = PEAKDET(V, DELTA) finds the local
    %        maxima and minima ("peaks") in the vector V.
    %        MAXTAB and MINTAB consists of two columns. Column 1
    %        contains indices in V, and column 2 the found values.
    %      
    %        With [MAXTAB, MINTAB] = PEAKDET(V, DELTA, X) the indices
    %        in MAXTAB and MINTAB are replaced with the corresponding
    %        X-values.
    %
    %        A point is considered a maximum peak if it has the maximal
    %        value, and was preceded (to the left) by a value lower by
    %        DELTA.
    
    % <NAME>, 3.4.05 (Explicitly not copyrighted).
    % This function is released to the public domain; Any use is allowed.
    
    """
    maxtab = []
    mintab = []
       
    if x is None:
        x = arange(len(v))
    
    v = asarray(v)
    
    if len(v) != len(x):
        sys.exit('Input vectors v and x must have same length')
    
    if not isscalar(delta):
        sys.exit('Input argument delta must be a scalar')
    
    if delta <= 0:
        sys.exit('Input argument delta must be positive')
    
    mn, mx = Inf, -Inf
    mnpos, mxpos = NaN, NaN
    
    lookformax = True
    
    for i in arange(len(v)):
        this = v[i]
        if this > mx:
            mx = this
            mxpos = x[i]
        if this < mn:
            mn = this
            mnpos = x[i]
        
        if lookformax:
            if this < mx-delta:
                maxtab.append((mxpos, mx))
                mn = this
                mnpos = x[i]
                lookformax = False
        else:
            if this > mn+delta:
                mintab.append((mnpos, mn))
                mx = this
                mxpos = x[i]
                lookformax = True

    return array(maxtab), array(mintab)

if __name__=="__main__":
    from matplotlib.pyplot import plot, scatter, show
    series = [0,0,0,2,0,0,0,-2,0,0,0,2,0,0,0,-2,0]
    maxtab, mintab = peakdet(series,.3)
    plot(series)
    scatter(array(maxtab)[:,0], array(maxtab)[:,1], color='blue')
    scatter(array(mintab)[:,0], array(mintab)[:,1], color='red')
    show()
# https://github.com/mattijn/pynotebook/blob/16fe0f58624938b82d93cbd208b8cb871ab95ec1/ # ipynotebooks/Python2.7/.ipynb_checkpoints/PLOTS%20SIGNAL%20PROCESSING-P1%20and%20P2-checkpoint.ipynb Converted from MATLAB script at http://billauer.co.il/peakdet.html      Returns two arrays      function [maxtab, mintab]=peakdet(v, delta, x)   %PEAKDET Detect peaks in a vector   %    [MAXTAB, MINTAB] = PEAKDET(V, DELTA) finds the local   %    maxima and minima ("peaks") in the vector V.   %    MAXTAB and MINTAB consists of two columns. Column 1   %    contains indices in V, and column 2 the found values.   %       %    With [MAXTAB, MINTAB] = PEAKDET(V, DELTA, X) the indices   %    in MAXTAB and MINTAB are replaced with the corresponding   %    X-values.   %   %    A point is considered a maximum peak if it has the maximal   %    value, and was preceded (to the left) by a value lower by   %    DELTA.      % <NAME>, 3.4.05 (Explicitly not copyrighted).   % This function is released to the public domain; Any use is allowed. Converted from/based on a MATLAB script at:      http://billauer.co.il/peakdet.html          function for detecting local maximas and minmias in a signal.     Discovers peaks by searching for values which are surrounded by lower     or larger values for maximas and minimas respectively          keyword arguments:     y_axis -- A list containg the signal over which to find peaks     x_axis -- (optional) A x-axis whose values correspond to the y_axis list         and is used in the return to specify the postion of the peaks. If         omitted an index of the y_axis is used. (default: None)     lookahead -- (optional) distance to look ahead from a peak candidate to         determine if it is the actual peak (default: 200)          '(sample / period) / f' where '4 >= f >= 1.25' might be a good value     delta -- (optional) this specifies a minimum difference between a peak and         the following points, before a peak may be considered a peak. Useful         to hinder the function from picking up false peaks towards to end of         the signal. To work well delta should be set to delta >= RMSnoise * 5.         (default: 0)             delta function causes a 20% decrease in speed, when omitted             Correctly used it can double the speed of the function          return -- two lists [max_peaks, min_peaks] containing the positive and         negative peaks respectively. Each cell of the lists contains a tupple         of: (position, peak_value)          to get the average peak value do: np.mean(max_peaks, 0)[1] on the         results to unpack one of the lists into x, y coordinates do:          x, y = zip(*tab) #Used to pop the first hit which almost always is false # check input data # store data length for later use #perform some checks #maxima and minima candidates are temporarily stored in #mx and mn respectively #Only detect peak if there is 'lookahead' amount of points after it ####look for max#### #Maxima peak candidate found #look ahead in signal to ensure that this is a peak and not jitter #set algorithm to only find minima now #end is within lookahead no more peaks can be found #else:  #slows shit down this does #    mx = ahead #    mxpos = x_axis[np.where(y_axis[index:index+lookahead]==mx)] ####look for min#### #Minima peak candidate found #look ahead in signal to ensure that this is a peak and not jitter #set algorithm to only find maxima now #end is within lookahead no more peaks can be found #else:  #slows shit down this does #    mn = ahead #    mnpos = x_axis[np.where(y_axis[index:index+lookahead]==mn)] #Remove the false hit on the first value of the y_axis #no peaks were found, should the function return empty lists? # # https://gist.github.com/endolith/250860 # Converted from MATLAB script at http://billauer.co.il/peakdet.html          Returns two arrays          function [maxtab, mintab]=peakdet(v, delta, x)     %PEAKDET Detect peaks in a vector     %        [MAXTAB, MINTAB] = PEAKDET(V, DELTA) finds the local     %        maxima and minima ("peaks") in the vector V.     %        MAXTAB and MINTAB consists of two columns. Column 1     %        contains indices in V, and column 2 the found values.     %           %        With [MAXTAB, MINTAB] = PEAKDET(V, DELTA, X) the indices     %        in MAXTAB and MINTAB are replaced with the corresponding     %        X-values.     %     %        A point is considered a maximum peak if it has the maximal     %        value, and was preceded (to the left) by a value lower by     %        DELTA.          % <NAME>, 3.4.05 (Explicitly not copyrighted).     % This function is released to the public domain; Any use is allowed.