annotate signal_processing.py @ 3:4be70944d21b skeleton_v2

Add some basic signal processing methods we need
author Chris Cannam
date Thu, 04 Oct 2012 17:14:05 +0100
parents
children ea2387fd1b90
rev   line source
Chris@3 1
Chris@3 2 import numpy as np
Chris@3 3
Chris@3 4 def rms(samples):
Chris@3 5 """Return the root mean square of the given samples"""
Chris@3 6 return np.sqrt(np.mean(np.array(samples)**2))
Chris@3 7
Chris@3 8 def autocorrelation(samples):
Chris@3 9 """Return the autocorrelation of the given samples, as an array with zero lag at the start"""
Chris@3 10 acf = np.correlate(samples, samples, mode='full')
Chris@3 11 return acf[len(acf)/2:]
Chris@3 12
Chris@3 13 def bpm_to_lag(bpm, hops_per_sec):
Chris@3 14 """Return the autocorrelation lag corresponding to the duration of a beat at the given tempo in bpm"""
Chris@3 15 return int((60.0 / bpm) * hops_per_sec)
Chris@3 16
Chris@3 17 def lag_to_bpm(lag, hops_per_sec):
Chris@3 18 """Return the tempo in bpm whose beat duration corresponds to the given autocorrelation lag"""
Chris@3 19 return (60.0 * hops_per_sec) / lag
Chris@3 20