-----
    Brief
    -----
    Function for removing ectopic beats.

    -----------
    Description
    -----------
    Ectopic beats are beats that are originated in cells that do not correspond to the expected pacemaker cells. These
    beats are identifiable in ECG signals by abnormal rhythms.

    This function allows to remove the ectopic beats by defining time thresholds that consecutive heartbeats should
    comply with.

    ----------
    Parameters
    ----------
    tachogram_data : list
        Y Axis of tachogram.

    tachogram_time : list
        X Axis of tachogram.

    Returns
    -------
    out : list, list
        List of tachogram samples. List of instants where each cardiac cycle ends.

    Source
    ------
    "Comparison of methods for removal of ectopy in measurement of heart rate variability" by
    N. Lippman, K. M. Stein and B. B. Lerman.

def remove_ectopy(tachogram_data, tachogram_time):
    """
    -----
    Brief
    -----
    Function for removing ectopic beats.

    -----------
    Description
    -----------
    Ectopic beats are beats that are originated in cells that do not correspond to the expected pacemaker cells. These
    beats are identifiable in ECG signals by abnormal rhythms.

    This function allows to remove the ectopic beats by defining time thresholds that consecutive heartbeats should
    comply with.

    ----------
    Parameters
    ----------
    tachogram_data : list
        Y Axis of tachogram.

    tachogram_time : list
        X Axis of tachogram.

    Returns
    -------
    out : list, list
        List of tachogram samples. List of instants where each cardiac cycle ends.

    Source
    ------
    "Comparison of methods for removal of ectopy in measurement of heart rate variability" by
    N. Lippman, K. M. Stein and B. B. Lerman.
    """

    # If the i RR interval differs from i-1 by more than 20 % then it will be removed from analysis.
    remove_margin = 0.20
    finish_ectopy_remove = False

    signal = list(tachogram_data)
    time = list(tachogram_time)

    # Sample by sample analysis.
    beat = 1
    while finish_ectopy_remove is False:
        max_thresh = signal[beat - 1] + remove_margin * signal[beat - 1]
        min_thresh = signal[beat - 1] - remove_margin * signal[beat - 1]
        if signal[beat] > max_thresh or signal[beat] < min_thresh:
            signal.pop(beat)
            signal.pop(beat)
            time.pop(beat)
            time.pop(beat)
            # To remove the influence of the ectopic beat we need to exclude the RR
            # intervals "before" and "after" the ectopic beat.
            # [NB <RRi> NB <RRi+1> EB <RRi+2> NB <RRi+3> NB...] -->
            # --> [NB <RRi> NB cut NB <RRi+3> NB...]

            # Advance "Pointer".
            beat += 1
        else:
            # Advance "Pointer".
            beat += 1

        # Verification if the cycle should or not end.
        if beat >= len(signal):
            finish_ectopy_remove = True

    return signal, time