changeset 23:1633c56b9d87

Fix up framer tests, get framer to pass them
author Chris Cannam
date Thu, 20 Dec 2012 15:08:14 +0000
parents 5d130537fcc2
children ad5149048604
files block.yeti framer.yeti fvector.yeti syntheticstream.yeti tests.yeti
diffstat 5 files changed, 124 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/block.yeti	Thu Dec 20 12:21:59 2012 +0000
+++ b/block.yeti	Thu Dec 20 15:08:14 2012 +0000
@@ -11,17 +11,25 @@
 unblock b is (Block ~double[]) -> ~double[] =
     case b of Block a: a esac;
 
+fromList l =
+    block (vec.vector l);
+
 list' b = vec.list (unblock b);
 length' b = vec.length (unblock b);
 
+equal b1 b2 =
+    list' b1 == list' b2;
+
 copyOf b = Block (vec.copyOf (unblock b));
-subset b start len = Block (vec.subset (unblock b) start len);
+range b start len = Block (vec.range (unblock b) start len);
 
 {
 zeros, ones,
 block, unblock,
+fromList,
 length = length',
 list = list',
-copyOf, subset,
+equal,
+copyOf, range,
 }
 
--- a/framer.yeti	Thu Dec 20 12:21:59 2012 +0000
+++ b/framer.yeti	Thu Dec 20 15:08:14 2012 +0000
@@ -12,34 +12,44 @@
         stream.readMono blocksize :. \(blockList blocksize stream);
     fi;
 
-overlappingBlockList blocksize hopsize stream buffer
-    is number -> number -> 'a -> ~double[] -> 'b =
-    if stream.finished? then
-       (stream.close (); [] ); //!!! wrong! need unit tests! need to read from any stream, not only a file, to facilitate that
+overlappingBlockList bs hop stream valid buffer
+    is number -> number -> 'a -> number -> ~double[] -> 'b =
+   (b = stream.readMono hop;
+    blen = block.length b;
+    samples = block.unblock b;
+
+    // Shift existing buffer down by hop
+    for [hop..bs-1] do i: buffer[i-hop] := buffer[i] done;
+
+    // Copy hop new elements in from samples (and extra zeros if needed)
+    for [0..blen-1] do i: buffer[bs-hop+i] := samples[i] done;
+    for [blen..hop-1] do i: buffer[bs-hop+i] := 0.0 done;
+
+    // Number of "valid" elements (not tail-end zero-padding) left in buffer
+    remaining = valid - (hop - blen);
+
+    if remaining <= 0 then
+        stream.close ();
+        [];
     else
-        b = stream.readMono hopsize;
-        samples = block.unblock b;
-        blen = block.length b;
-        for [0..blen-1] do i: buffer[blocksize - hopsize + i] := samples[i] done;
-        for [blen..hopsize-1] do i: buffer[blocksize - hopsize + i] := 0.0 done;
-        v = Block (vec.subset buffer 0 blocksize);
-        for [hopsize..blocksize-1] do i: buffer[i - hopsize] := buffer[i] done;
-        v :. \(overlappingBlockList blocksize hopsize stream buffer);
-    fi;
-
-overlappingFrames blocksize hopsize stream =
-    overlappingBlockList blocksize hopsize stream (vec.zeros blocksize);
+        // Copy buffer to return
+        v = block.block (vec.copyOf buffer);
+        v :. \(overlappingBlockList bs hop stream remaining buffer);
+    fi);
 
 frames blocksize stream =
     blockList blocksize stream;
 
-overlappingFramesOfFile blocksize hopsize filename =
-    overlappingBlockList blocksize hopsize (af.open filename)
-        (vec.zeros blocksize);
-
 framesOfFile blocksize filename =
     blockList blocksize (af.open filename);
 
+overlappingFrames { blocksize, hop } stream =
+    overlappingBlockList blocksize hop stream blocksize (vec.zeros blocksize);
+
+overlappingFramesOfFile { blocksize, hop } filename =
+    overlappingBlockList blocksize hop (af.open filename)
+        blocksize (vec.zeros blocksize);
+
 { 
     frames, overlappingFrames,
     framesOfFile, overlappingFramesOfFile,
--- a/fvector.yeti	Thu Dec 20 12:21:59 2012 +0000
+++ b/fvector.yeti	Thu Dec 20 15:08:14 2012 +0000
@@ -27,10 +27,13 @@
 length' =
     length . list';
 
+equal v1 v2 =
+    list' v1 == list' v2;
+
 copyOf v is ~double[] -> ~double[] =
     Arrays#copyOf(v, list' v |> length);
 
-subset v start len is ~double[] -> number -> number -> ~double[] =
+range v start len is ~double[] -> number -> number -> ~double[] =
     Arrays#copyOfRange(v, start, start + len);
 
 {
@@ -38,6 +41,7 @@
 vector,
 length = length',
 list = list',
-copyOf, subset,
+equal,
+copyOf, range,
 }
 
--- a/syntheticstream.yeti	Thu Dec 20 12:21:59 2012 +0000
+++ b/syntheticstream.yeti	Thu Dec 20 15:08:14 2012 +0000
@@ -35,7 +35,7 @@
         rate,
         read count = 
            (rc = min count (len - position);
-            result = vec.subset data position rc;
+            result = vec.range data position rc;
             position := position + rc;
             block.block result),
         close = \(),
--- a/tests.yeti	Thu Dec 20 12:21:59 2012 +0000
+++ b/tests.yeti	Thu Dec 20 15:08:14 2012 +0000
@@ -4,24 +4,95 @@
 ss = load syntheticstream;
 vec = load fvector;
 fr = load framer;
+block = load block;
 
 testStream n is number -> 'a  = ss.precalculated 1000 (vec.vector [1..n]);
 
+compare obtained expected =
+    if obtained == expected then
+        true;
+    else
+        println "** expected: \(expected)\n   obtained: \(obtained)";
+        false;
+    fi;
+
 tests = [
 
-"framer-2x2": \( 
+"framecount-2x2": \( 
     fr = fr.frames 2 (testStream 2);
-    length fr == 1
+    compare (length fr) 1
 ),
 
-"framer-2x3": \( 
+"framecount-2x3": \( 
     fr = fr.frames 2 (testStream 3);
-    length fr == 2
+    compare (length fr) 2
 ),
 
-"framer-2x4": \( 
+"framecount-2x4": \( 
     fr = fr.frames 2 (testStream 4);
-    length fr == 2
+    compare (length fr) 2
+),
+
+"framecount-2.1x0": \( 
+    fr = fr.overlappingFrames { blocksize = 2, hop = 1 } (testStream 0);
+    compare (length fr) 1
+),
+
+"framecount-2.1x1": \( 
+    fr = fr.overlappingFrames { blocksize = 2, hop = 1 } (testStream 1);
+    compare (length fr) 2
+),
+
+"framecount-2.1x2": \( 
+    fr = fr.overlappingFrames { blocksize = 2, hop = 1 } (testStream 2);
+    compare (length fr) 3
+),
+
+"framecount-2.1x3": \( 
+    fr = fr.overlappingFrames { blocksize = 2, hop = 1 } (testStream 3);
+    compare (length fr) 4
+),
+
+"framecount-4.1x4": \( 
+    fr = fr.overlappingFrames { blocksize = 4, hop = 1 } (testStream 4);
+    compare (length fr) 7
+),
+
+"framecount-4.3x4": \( 
+    fr = fr.overlappingFrames { blocksize = 4, hop = 3 } (testStream 4);
+    compare (length fr) 2 
+),
+
+"framecount-4.4x4": \( 
+    fr = fr.overlappingFrames { blocksize = 4, hop = 4 } (testStream 4);
+    compare (length fr) 1
+),
+
+"framecount-3.2x4": \(
+    fr = fr.overlappingFrames { blocksize = 3, hop = 2 } (testStream 4);
+    compare (length fr) 3
+),
+
+"frames-4.3x4": \( 
+    fr = fr.overlappingFrames { blocksize = 4, hop = 3 } (testStream 4);
+    expected = array [ [0,1,2,3], [3,4,0,0] ];
+    obtained = array (map block.list fr);
+    compare obtained expected;
+),
+
+"frames-3.2x4": \(
+    fr = fr.overlappingFrames { blocksize = 3, hop = 2 } (testStream 4);
+    expected = array [ [0,1,2], [2,3,4], [4,0,0] ];
+    obtained = array (map block.list fr);
+    compare obtained expected;
+),
+
+"frames-3.1x6": \(
+    fr = fr.overlappingFrames { blocksize = 3, hop = 1 } (testStream 6);
+    expected = array [ [0,0,1], [0,1,2], [1,2,3], [2,3,4],
+                       [3,4,5], [4,5,6], [5,6,0], [6,0,0] ];
+    obtained = array (map block.list fr);
+    compare obtained expected;
 ),
 
 ];