import logging


def is_beat_valid(voltages, times, QRS_threshold):
    """
    Determines whether a heartbeat is valid by seeing if it is above the noise

    :param voltages: (list) voltage interval over which to analyze
    :param times: (list) time interval over which to analyze
    :param QRS_threshold: length to QRS region to exclude from cutoff
    :return: boolean indicating whether there is a valid heartbeat
    """
    cutoff = 0.4
    index_max_val = voltages.index(1)

    x = 0
    end_front = 0
    for i in times:
        if i <= times[index_max_val]-QRS_threshold/2:
            end_front = x
        x = x + 1

    x = 0
    start_back = 0
    for i in times:
        if i < times[index_max_val]+QRS_threshold/2:
            start_back = x
        x = x + 1

    front_voltages = voltages[0:end_front]
    if (front_voltages == []):
        front_voltages = [0]
    back_voltages = voltages[start_back:len(voltages)]
    if (back_voltages == []):
        back_voltages = [0]
    max_front = max(front_voltages)
    max_back = max(back_voltages)
    if max_front > cutoff or max_back > cutoff:
        return False
    else:
        logging.info('valid beat detected')
        return True