# HG changeset patch # User Chris Cannam # Date 1349707327 -3600 # Node ID cb43d088e36923727f5cdf5a35030456a0cb66b2 # Parent 7fc62809da94dac81c318025eb2dcfe77a78fb56 Implement and test detection function diff -r 7fc62809da94 -r cb43d088e369 tempo_estimator.py --- a/tempo_estimator.py Mon Oct 08 15:34:50 2012 +0100 +++ b/tempo_estimator.py Mon Oct 08 15:42:07 2012 +0100 @@ -1,9 +1,23 @@ import audiofile as af +import numpy as np +import framer as fr +import signal_processing as sp + +def detection_function(samples, hop): + """Given some samples and a hop size, return a function corresponding to the interest level of the samples at intervals of the hop size.""" + n_frames = fr.get_frame_count(len(samples), hop) + df = np.zeros(n_frames) + for n in range(0, n_frames): + frame = fr.get_frame(samples, hop, n) + value = sp.rms(frame) + df[n] = value + return df def estimate_tempo_of_samples(samples, samplerate): """Given some audio samples and their samplerate, return their estimated tempo in bpm.""" - df = detection_function(samples) + hop = 512 + df = detection_function(samples, hop) acf = autocorrelation(df) tempo = find_tempo_from_autocorrelation(acf) return tempo diff -r 7fc62809da94 -r cb43d088e369 test_tempo_estimator.py --- a/test_tempo_estimator.py Mon Oct 08 15:34:50 2012 +0100 +++ b/test_tempo_estimator.py Mon Oct 08 15:42:07 2012 +0100 @@ -1,5 +1,13 @@ import tempo_estimator as est +import numpy as np + +def test_detection_function(): + samples = np.array([1,0, 0,0, 0,0, 1,0, 0,0]) + df = est.detection_function(samples, 2) + assert len(df) == 5 + assert df[3] > df[2] + assert df[0] > df[2] def test_tempo_120bpm(): tempo = est.estimate_tempo_of_file('testfiles/120bpm.wav')