#NEWLINE# def estimate_beat_start( self , candidates = 10 , tolerance = 0.025 ) : #NEWLINE# #INDENT# 'Estimate the location of the first beat based on which of the first\n        few onsets results in the best correlation with the onset spike train.\n\n        Parameters\n        ----------\n        candidates : int\n            Number of candidate onsets to try\n        tolerance : float\n            The tolerance in seconds around which onsets will be used to\n            treat a beat as correct\n\n        Returns\n        -------\n        beat_start : float\n            The offset which is chosen as the beat start location\n        ' #NEWLINE# note_list = [ n for i in self . instruments for n in i . notes ] #NEWLINE# note_list . sort ( key = ( lambda note : note . start ) ) #NEWLINE# beat_candidates = [ ] #NEWLINE# start_times = [ ] #NEWLINE# onset_index = 0 #NEWLINE# while ( ( len ( beat_candidates ) <= candidates ) and ( len ( beat_candidates ) <= len ( note_list ) ) and ( onset_index < len ( note_list ) ) ) : #NEWLINE# #INDENT# if ( ( onset_index == 0 ) or ( np . abs ( ( note_list [ ( onset_index - 1 ) ] . start - note_list [ onset_index ] . start ) ) > 0.001 ) ) : #NEWLINE# #INDENT# beat_candidates . append ( self . get_beats ( note_list [ onset_index ] . start ) ) #NEWLINE# start_times . append ( note_list [ onset_index ] . start ) #NEWLINE# #UNINDENT# onset_index += 1 #NEWLINE# #UNINDENT# onset_scores = np . zeros ( len ( beat_candidates ) ) #NEWLINE# fs = 1000 #NEWLINE# onset_signal = np . zeros ( int ( ( fs * ( self . get_end_time ( ) + 1 ) ) ) ) #NEWLINE# for note in note_list : #NEWLINE# #INDENT# onset_signal [ int ( ( note . start * fs ) ) ] += note . velocity #NEWLINE# #UNINDENT# for ( n , beats ) in enumerate ( beat_candidates ) : #NEWLINE# #INDENT# beat_signal = np . zeros ( int ( ( fs * ( self . get_end_time ( ) + 1 ) ) ) ) #NEWLINE# for beat in np . append ( 0 , beats ) : #NEWLINE# #INDENT# if ( ( beat - tolerance ) < 0 ) : #NEWLINE# #INDENT# beat_window = np . ones ( int ( ( ( ( fs * 2 ) * tolerance ) + ( ( beat - tolerance ) * fs ) ) ) ) #NEWLINE# beat_signal [ : int ( ( ( beat + tolerance ) * fs ) ) ] = beat_window #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# beat_start = int ( ( ( beat - tolerance ) * fs ) ) #NEWLINE# beat_end = ( beat_start + int ( ( ( fs * tolerance ) * 2 ) ) ) #NEWLINE# beat_window = np . ones ( int ( ( ( fs * tolerance ) * 2 ) ) ) #NEWLINE# beat_signal [ beat : beat_end ] = beat_window #NEWLINE# #UNINDENT# #UNINDENT# onset_scores [ n ] = ( np . dot ( beat_signal , onset_signal ) / beats . shape [ 0 ] ) #NEWLINE# #UNINDENT# return start_times [ np . argmax ( onset_scores ) ]