Given an array with all the peaks of the signal ('peaks') and a
    distance value ('min_distance') and the signal, by argument, this function
    erases all the unnecessary peaks and returns an array with only the maximum
    peak for each period of the signal (the period is given by the
    min_distance).

    Parameters
    ----------
    signal: array-like
      the original signal.
    peaks: array-like
      the peaks to filter.
    min_distance: int
      the distance value to exclude the unnecessary peaks.

    Returns
    -------
    fp: array-like
      the new peaks, after filtering just the maximum peak per period.

    See also: clean_near_events()

def clean_near_peaks(signal, peaks_, min_distance):
    """ Given an array with all the peaks of the signal ('peaks') and a
    distance value ('min_distance') and the signal, by argument, this function
    erases all the unnecessary peaks and returns an array with only the maximum
    peak for each period of the signal (the period is given by the
    min_distance).

    Parameters
    ----------
    signal: array-like
      the original signal.
    peaks: array-like
      the peaks to filter.
    min_distance: int
      the distance value to exclude the unnecessary peaks.

    Returns
    -------
    fp: array-like
      the new peaks, after filtering just the maximum peak per period.

    See also: clean_near_events()
    """

    #order all peaks
    
    ars = argsort(signal[peaks_])
    
    pp = peaks(ars)

    fp = []

    #clean near peaks
    while len(pp) > 0:
        fp += [pp[-1]]
        pp = pp[abs(pp - pp[-1]) > min_distance]

    return sort(array(fp))