def fade_out_in(function, fall_stop, rise_start, rise_length, out):
    """Helper function for generating a signal, that first fades out and then fades in."""
    if fall_stop == rise_length:
        fade = function(fall_stop + rise_length)
        fade_out = fade[fall_stop:]
        fade_in = fade[0:rise_length]
    else:
        fade_out = function(2 * fall_stop)[fall_stop:]
        fade_in = function(2 * rise_length)[0:rise_length]
    if fall_stop <= rise_start:
        out[0:fall_stop] = fade_out
        out[fall_stop:rise_start] = 0.0
        out[rise_start:] = fade_in
    else:
        rise_stop = rise_start + rise_length
        out[0:rise_start] = fade_out[0:rise_start]
        if rise_stop >= len(out):
            overlap_length = fall_stop - rise_start
            out[rise_start:fall_stop] = fade_out[rise_start:fall_stop
                ] + fade_in[0:overlap_length]
            out[fall_stop:] = fade_in[overlap_length:]
        else:
            out[rise_start:rise_stop] = fade_out[rise_start:rise_stop
                ] + fade_in
            out[rise_stop:] = fade_out[rise_stop:] + 1.0
