def beatExtraction(stFeatures, winSize, PLOT=False): """ This function extracts an estimate of the beat rate for a musical signal. ARGUMENTS: - stFeatures: a numpy array (numOfFeatures x numOfShortTermWindows) - winSize: window size in seconds RETURNS: - BPM: estimates of beats per minute - Ratio: a confidence measure """ # Features that are related to the beat tracking task: toWatch = [0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18] maxBeatTime = int(round(2.0 / winSize)) HistAll = numpy.zeros((maxBeatTime,)) for ii, i in enumerate(toWatch): # for each feature DifThres = 2.0 * ( numpy.abs(stFeatures[i, 0:-1] - stFeatures[i, 1::])).mean() # dif threshold (3 x Mean of Difs) if DifThres <= 0: DifThres = 0.0000000000000001 [pos1, _] = utilities.peakdet(stFeatures[i, :], DifThres) # detect local maxima posDifs = [] # compute histograms of local maxima changes for j in range(len(pos1) - 1): posDifs.append(pos1[j + 1] - pos1[j]) [HistTimes, HistEdges] = numpy.histogram(posDifs, numpy.arange(0.5, maxBeatTime + 1.5)) HistCenters = (HistEdges[0:-1] + HistEdges[1::]) / 2.0 HistTimes = HistTimes.astype(float) / stFeatures.shape[1] HistAll += HistTimes if PLOT: plt.subplot(9, 2, ii + 1) plt.plot(stFeatures[i, :], 'k') for k in pos1: plt.plot(k, stFeatures[i, k], 'k*') f1 = plt.gca() f1.axes.get_xaxis().set_ticks([]) f1.axes.get_yaxis().set_ticks([]) if PLOT: plt.show(block=False) plt.figure() # Get beat as the argmax of the agregated histogram: I = numpy.argmax(HistAll) BPMs = 60 / (HistCenters * winSize) BPM = BPMs[I] # ... and the beat ratio: Ratio = HistAll[I] / HistAll.sum() if PLOT: # filter out >500 beats from plotting: HistAll = HistAll[BPMs < 500] BPMs = BPMs[BPMs < 500] plt.plot(BPMs, HistAll, 'k') plt.xlabel('Beats per minute') plt.ylabel('Freq Count') plt.show(block=True) return BPM, Ratio
def beatExtraction(stFeatures, winSize, PLOT=False): """ This function extracts an estimate of the beat rate for a musical signal. ARGUMENTS: - stFeatures: a numpy array (numOfFeatures x numOfShortTermWindows) - winSize: window size in seconds RETURNS: - BPM: estimates of beats per minute - Ratio: a confidence measure """ # Features that are related to the beat tracking task: toWatch = [0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18] maxBeatTime = int(round(2.0 / winSize)) HistAll = numpy.zeros((maxBeatTime,)) for ii, i in enumerate(toWatch): # for each feature DifThres = 2.0 * (numpy.abs(stFeatures[i, 0:-1] - stFeatures[i, 1::])).mean() # dif threshold (3 x Mean of Difs) if DifThres<=0: DifThres = 0.0000000000000001 [pos1, _] = utilities.peakdet(stFeatures[i, :], DifThres) # detect local maxima posDifs = [] # compute histograms of local maxima changes for j in range(len(pos1)-1): posDifs.append(pos1[j+1]-pos1[j]) [HistTimes, HistEdges] = numpy.histogram(posDifs, numpy.arange(0.5, maxBeatTime + 1.5)) HistCenters = (HistEdges[0:-1] + HistEdges[1::]) / 2.0 HistTimes = HistTimes.astype(float) / stFeatures.shape[1] HistAll += HistTimes if PLOT: plt.subplot(9, 2, ii + 1) plt.plot(stFeatures[i, :], 'k') for k in pos1: plt.plot(k, stFeatures[i, k], 'k*') f1 = plt.gca() f1.axes.get_xaxis().set_ticks([]) f1.axes.get_yaxis().set_ticks([]) if PLOT: plt.show(block=False) plt.figure() # Get beat as the argmax of the agregated histogram: I = numpy.argmax(HistAll) BPMs = 60 / (HistCenters * winSize) BPM = BPMs[I] # ... and the beat ratio: Ratio = HistAll[I] / HistAll.sum() if PLOT: # filter out >500 beats from plotting: HistAll = HistAll[BPMs < 500] BPMs = BPMs[BPMs < 500] plt.plot(BPMs, HistAll, 'k') plt.xlabel('Beats per minute') plt.ylabel('Freq Count') plt.show(block=True) return BPM, Ratio
def beatExtraction(stFeatures, winSize, PLOT=False): """ This function extracts an estimate of the beat rate for a musical signal. ARGUMENTS: - stFeatures: a numpy array (numOfFeatures x numOfShortTermWindows) - winSize: window size in seconds RETURNS: - BPM: estimates of beats per minute - Ratio: a confidence measure """ # Features that are related to the beat tracking task: toWatch = [0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18] maxBeatTime = int(round(2.0 / winSize)) print maxBeatTime HistAll = numpy.zeros((maxBeatTime, )) for ii, i in enumerate(toWatch): # for each feature DifThres = 2.0 * (numpy.abs(stFeatures[i, 0:-1] - stFeatures[i, 1::]) ).mean() # dif threshold (3 x Mean of Difs) if DifThres <= 0: DifThres = 0.0000000000000001 [pos1, _] = utilities.peakdet(stFeatures[i, :], DifThres) # detect local maxima posDifs = [] # compute histograms of local maxima changes for j in range(len(pos1) - 1): posDifs.append(pos1[j + 1] - pos1[j]) [HistTimes, HistEdges] = numpy.histogram(posDifs, numpy.arange(0.5, maxBeatTime + 1.5)) HistCenters = (HistEdges[0:-1] + HistEdges[1::]) / 2.0 HistTimes = HistTimes.astype(float) / stFeatures.shape[1] HistAll += HistTimes # Get beat as the argmax of the agregated histogram: I = numpy.argmax(HistAll) BPMs = 60 / (HistCenters * winSize) BPM = BPMs[I] # ... and the beat ratio: Ratio = HistAll[I] / HistAll.sum() return BPM, Ratio
def beat_extraction(short_features, window_size, plot=False): """ This function extracts an estimate of the beat rate for a musical signal. ARGUMENTS: - short_features: a np array (n_feats x numOfShortTermWindows) - window_size: window size in seconds RETURNS: - bpm: estimates of beats per minute - ratio: a confidence measure """ # Features that are related to the beat tracking task: selected_features = [ 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 ] max_beat_time = int(round(2.0 / window_size)) hist_all = np.zeros((max_beat_time, )) # for each feature for ii, i in enumerate(selected_features): # dif threshold (3 x Mean of Difs) dif_threshold = 2.0 * (np.abs(short_features[i, 0:-1] - short_features[i, 1::])).mean() if dif_threshold <= 0: dif_threshold = 0.0000000000000001 # detect local maxima [pos1, _] = utilities.peakdet(short_features[i, :], dif_threshold) position_diffs = [] # compute histograms of local maxima changes for j in range(len(pos1) - 1): position_diffs.append(pos1[j + 1] - pos1[j]) histogram_times, histogram_edges = \ np.histogram(position_diffs, np.arange(0.5, max_beat_time + 1.5)) hist_centers = (histogram_edges[0:-1] + histogram_edges[1::]) / 2.0 histogram_times = \ histogram_times.astype(float) / short_features.shape[1] hist_all += histogram_times if plot: plt.subplot(9, 2, ii + 1) plt.plot(short_features[i, :], 'k') for k in pos1: plt.plot(k, short_features[i, k], 'k*') f1 = plt.gca() f1.axes.get_xaxis().set_ticks([]) f1.axes.get_yaxis().set_ticks([]) if plot: plt.show(block=False) plt.figure() # Get beat as the argmax of the agregated histogram: max_indices = np.argmax(hist_all) bpms = 60 / (hist_centers * window_size) bpm = bpms[max_indices] # ... and the beat ratio: ratio = hist_all[max_indices] / (hist_all.sum() + eps) if plot: # filter out >500 beats from plotting: hist_all = hist_all[bpms < 500] bpms = bpms[bpms < 500] plt.plot(bpms, hist_all, 'k') plt.xlabel('Beats per minute') plt.ylabel('Freq Count') plt.show(block=True) return bpm, ratio