Difference between the maximum peak positions of the signals.
    
    This function returns the difference, in samples, between the peaks position 
    of the signals. If the reference signal has various peaks, the one 
    chosen is the peak which is closer to the middle of the signal, and if the 
    other signal has more than one peak also, the chosen is the one closer to
    the reference peak signal. 
    The first signal introduced is the reference signal.
     
   
    Parameters
    ----------
    refw: array-like
      the input reference signal.
    w: array-like
      the input signal.

    Returns
    -------
    al: int
      the difference between the two events position
        
    Example
    -------
    >>> peakAlign([5,7,3,20,13,5,7],[5,1,8,4,3,10,3])
    1
    
    See also: maxAlign(), minAlign(), peakNegAlign(), infMaxAlign(), infMinAlign()

def peakAlign(refw,w):
    """ Difference between the maximum peak positions of the signals.
    
    This function returns the difference, in samples, between the peaks position 
    of the signals. If the reference signal has various peaks, the one 
    chosen is the peak which is closer to the middle of the signal, and if the 
    other signal has more than one peak also, the chosen is the one closer to
    the reference peak signal. 
    The first signal introduced is the reference signal.
     
   
    Parameters
    ----------
    refw: array-like
      the input reference signal.
    w: array-like
      the input signal.

    Returns
    -------
    al: int
      the difference between the two events position
        
    Example
    -------
    >>> peakAlign([5,7,3,20,13,5,7],[5,1,8,4,3,10,3])
    1
    
    See also: maxAlign(), minAlign(), peakNegAlign(), infMaxAlign(), infMinAlign()
    """ 
    
    p_mw = array ( peaks(array(refw),min(refw)) )
    p_w = array ( peaks(array(w),min(w)) )
    
         
    if (len(p_mw)>1):      
        min_al = argmin(abs( (len(refw)/2) - p_mw))       #to choose the peak closer to the middle of the signal  
        p_mw=p_mw[min_al]  
    if (list(p_w) == [] ):
        p_w = p_mw
    elif (len(p_w)>1):        
        min_al = argmin(abs(p_w - p_mw))       #to choose the peak closer to the peak of the reference signal
        p_w=p_w[min_al]
          
    
    return int(array(p_mw-p_w))