changeset 10:7fc62809da94

Test and implement framer
author Chris Cannam
date Mon, 08 Oct 2012 15:34:50 +0100
parents d43d1adecea6
children cb43d088e369
files framer.py test_framer.py
diffstat 2 files changed, 30 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/framer.py	Mon Oct 08 15:34:50 2012 +0100
@@ -0,0 +1,14 @@
+
+import numpy as np
+
+def get_frame_count(nsamples, hop):
+    """Given the number of samples, return the number of non-overlapping frames of length hop we can extract from them."""
+    return int(np.ceil(nsamples / float(hop)))
+
+def get_frame(samples, hop, n):
+    """Given samples and hop, return the n'th non-overlapping frame of length hop."""
+    frame = samples[n * hop : (n+1) * hop]
+    if len(frame) < hop:
+        frame = np.append(frame, np.zeros(hop - len(frame)))
+    return frame
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test_framer.py	Mon Oct 08 15:34:50 2012 +0100
@@ -0,0 +1,16 @@
+
+import framer as fr
+import numpy as np
+
+def test_get_frame_count():
+    assert fr.get_frame_count(253929, 512) == 496
+    assert fr.get_frame_count(0, 59) == 0
+    assert fr.get_frame_count(4, 2) == 2
+    assert fr.get_frame_count(5, 2) == 3
+    assert fr.get_frame_count(6, 1) == 6
+
+def test_get_frame():
+    assert (fr.get_frame(np.array([1,2,3,4]), 2, 0) == np.array([1,2])).all()
+    assert (fr.get_frame(np.array([1,2,3,4,5]), 2, 2) == np.array([5,0])).all()
+    assert (fr.get_frame(np.array([1,2,3,4,5,6]), 1, 3) == np.array([4])).all()
+