def _correlate_onsets_and_offsets(onsets, offsets, gradients):
<EOL>for freq_index, (ons, offs) in enumerate(zip(onsets[:, :], offsets[:, :])):<EOL><INDENT>indexes_of_all_ones = np.reshape(np.where(ons == <NUM_LIT:1>), (-<NUM_LIT:1>,))<EOL>last_idx = indexes_of_all_ones[<NUM_LIT:0>]<EOL>offs[<NUM_LIT:0>:last_idx + <NUM_LIT:1>] = <NUM_LIT:0><EOL>if len(indexes_of_all_ones > <NUM_LIT:1>):<EOL><INDENT>for next_idx in indexes_of_all_ones[<NUM_LIT:1>:]:<EOL><INDENT>offset_choices = offs[last_idx:next_idx]<EOL>offset_choice_indexes = np.where(offset_choices == <NUM_LIT:1>)<EOL>if not np.any(offset_choices):<EOL><INDENT>continue<EOL><DEDENT>assert np.any(offset_choices), "<STR_LIT>".format(last_idx, next_idx)<EOL>offset_choice_indexes = np.reshape(last_idx + offset_choice_indexes, (-<NUM_LIT:1>,))<EOL>assert np.all(offsets[freq_index, offset_choice_indexes])<EOL>gradient_values = gradients[freq_index, offset_choice_indexes]<EOL>index_of_largest_from_gradient_values = np.where(gradient_values == np.min(gradient_values))[<NUM_LIT:0>]<EOL>index_of_largest_offset_choice = offset_choice_indexes[index_of_largest_from_gradient_values]<EOL>assert offsets[freq_index, index_of_largest_offset_choice] == <NUM_LIT:1><EOL>offsets[freq_index, offset_choice_indexes] = <NUM_LIT:0><EOL>offsets[freq_index, index_of_largest_offset_choice] = <NUM_LIT:1><EOL>last_idx = next_idx<EOL><DEDENT><DEDENT>else:<EOL><INDENT>offsets[freq_index, :] = <NUM_LIT:0><EOL>offsets[freq_index, -<NUM_LIT:1>] = <NUM_LIT:1><EOL><DEDENT><DEDENT>return offsets<EOL>
Takes an array of onsets and an array of offsets, of the shape [nfrequencies, nsamples], where
each item in these arrays is either a 0 (not an on/offset) or a 1 (a possible on/offset).

This function returns a new offsets array, where there is a one-to-one correlation between
onsets and offsets, such that each onset has exactly one offset that occurs after it in
the time domain (the second dimension of the array).

The gradients array is used to decide which offset to use in the case of multiple possibilities.