Mercurial > hg > python-tutorial-skeleton
comparison 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 |
comparison
equal
deleted
inserted
replaced
2:32bf6cb28341 | 3:4be70944d21b |
---|---|
1 | |
2 import numpy as np | |
3 | |
4 def rms(samples): | |
5 """Return the root mean square of the given samples""" | |
6 return np.sqrt(np.mean(np.array(samples)**2)) | |
7 | |
8 def autocorrelation(samples): | |
9 """Return the autocorrelation of the given samples, as an array with zero lag at the start""" | |
10 acf = np.correlate(samples, samples, mode='full') | |
11 return acf[len(acf)/2:] | |
12 | |
13 def bpm_to_lag(bpm, hops_per_sec): | |
14 """Return the autocorrelation lag corresponding to the duration of a beat at the given tempo in bpm""" | |
15 return int((60.0 / bpm) * hops_per_sec) | |
16 | |
17 def lag_to_bpm(lag, hops_per_sec): | |
18 """Return the tempo in bpm whose beat duration corresponds to the given autocorrelation lag""" | |
19 return (60.0 * hops_per_sec) / lag | |
20 |