changeset 65:bb1799a6d690

Pull out tests
author Chris Cannam <c.cannam@qmul.ac.uk>
date Fri, 14 Feb 2014 11:55:03 +0000
parents df6d89381f49
children 3d165bf186f1
files yeti/cqt.yeti yeti/test.yeti yeti/test_cqtkernel.yeti yeti/test_frequency.yeti
diffstat 4 files changed, 107 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/yeti/cqt.yeti	Fri Feb 14 11:19:52 2014 +0000
+++ b/yeti/cqt.yeti	Fri Feb 14 11:55:03 2014 +0000
@@ -16,7 +16,7 @@
 cqt { maxFreq, minFreq, binsPerOctave } str =
    (sampleRate = str.sampleRate;
     octaves = ceil (log2 (maxFreq / minFreq));
-    actualMinFreq = (maxFreq / (pow 2 octaves)) * (pow 2 (1/binsPerOctave));
+//    actualMinFreq = (maxFreq / (pow 2 octaves)) * (pow 2 (1/binsPerOctave));
 
     kdata = cqtkernel.makeKernel { sampleRate, maxFreq, binsPerOctave };
 
@@ -88,7 +88,7 @@
     // and split into single-atom columns
 
     emptyHops = kdata.firstCentre / kdata.atomSpacing; //!!! int? round?
-    maxDrop = emptyHops * (pow 2 (octaves-1)) - emptyHops;
+//    maxDrop = emptyHops * (pow 2 (octaves-1)) - emptyHops;
 //    eprintln "maxDrop = \(maxDrop)";
 
     cqblocks =
--- a/yeti/test.yeti	Fri Feb 14 11:19:52 2014 +0000
+++ b/yeti/test.yeti	Fri Feb 14 11:55:03 2014 +0000
@@ -1,6 +1,6 @@
 
 program test;
-
+/*
 af = load may.stream.audiofile;
 plot = load may.plot;
 cm = load may.matrix.complex;
@@ -10,10 +10,12 @@
 mm = load may.mathmisc;
 manipulate = load may.stream.manipulate;
 syn = load may.stream.syntheticstream;
-test = load may.test;
+*/
 
-{ makeKernel } = load cqtkernel;
-{ cqt } = load cqt;
+{ runTests } = load may.test;
+
+//{ makeKernel } = load cqtkernel;
+//{ cqt } = load cqt;
 
 // We want to test:
 // 
@@ -32,58 +34,20 @@
 //
 // Specimen output for simple test case
 
-// Test with a single windowed sinusoid, repeating at various frequencies
+tests = [
+"kernel"    : load test_cqtkernel,
+"frequency" : load test_frequency,
+];
 
-sinTestStream sampleRate duration signalFreq = // duration is in samples
-   (sin = syn.sinusoid sampleRate signalFreq;
-    chunk = mat.getRow 0 (sin.read duration);
-    syn.precalculatedMono sampleRate (win.windowed win.hann chunk));
+bad = sum (mapHash do name testHash: runTests name testHash done tests);
 
-// We want to make a CQ transform spanning more than one octave, but
-// not going all the way to fs/2 so we can test it also with
-// frequencies above and below its extents
+if (bad > 0) then
+    println "\n** \(bad) test(s) failed!";
+    threadExit 1
+else
+    ()
+fi
 
-sampleRate = 100;
-
-// fs/2 = 50 so 10->40 gives us 2 octaves
-cqmin = 10;
-cqmax = 40;
-bpo = 4; // fairly arbitrary
-
-testFreqs = map (* 5) [ 0..10 ];
-duration = sampleRate * 2;
-
-streamBuilder = sinTestStream sampleRate duration;
-
-binForFreq f =
-    mm.round (bpo * mm.log2 (f / cqmin)) - 1;
-
-for testFreqs do f:
-    str = streamBuilder f;
-    cq = cqt { maxFreq = cqmax, minFreq = cqmin, binsPerOctave = bpo } str;
-    m = mat.concatHorizontal (map cm.magnitudes cq.output);
-    println "binFrequencies = \(cq.kernel.binFrequencies)";
-    println "binForFreq \(f) = \(binForFreq f)";
-    success = all id
-       (map do c:
-            // passes test if the correct max bin, or the expected max
-            // is out of range, or if all bins are below a threshold
-            expected = binForFreq f;
-            good =
-               (expected < 0 or expected >= vec.length c) or
-               (vec.max c < 0.001) or
-               (vec.maxindex c == binForFreq f);
-            if (not good) then
-                println " * bad! maxindex \(vec.maxindex c) != expected \(binForFreq f) for freq \(f) in column: \(vec.list c)";
-//                println "matrix is:";
-//                mat.print m;
-            else 
-                print "✓"; 
-            fi;
-            good;
-            done (mat.asColumns m));
-    println " success = \(success) for freq \(f)";
-done;
 
 
 
@@ -113,5 +77,5 @@
 //\() (plot.plot [Contour bigM]);
 \() (plot.plot [Grid bigM]);
 */
-()
+//()
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/yeti/test_cqtkernel.yeti	Fri Feb 14 11:55:03 2014 +0000
@@ -0,0 +1,18 @@
+
+module test_cqtkernel;
+
+cm = load may.matrix.complex;
+
+{ makeKernel } = load cqtkernel;
+
+[
+
+"minimal": \(
+    k = makeKernel { sampleRate = 16, maxFreq = 8, binsPerOctave = 4 };
+print k;
+cm.print k.kernel;
+true;
+),
+
+] is hash<string, () -> boolean>
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/yeti/test_frequency.yeti	Fri Feb 14 11:55:03 2014 +0000
@@ -0,0 +1,69 @@
+
+module test_frequency;
+
+mat = load may.matrix;
+vec = load may.vector;
+win = load may.signal.window;
+mm = load may.mathmisc;
+cm = load may.matrix.complex;
+syn = load may.stream.syntheticstream;
+
+{ cqt } = load cqt;
+
+// Test with a single windowed sinusoid, repeating at various frequencies
+
+sinTestStream sampleRate duration signalFreq = // duration is in samples
+   (sin = syn.sinusoid sampleRate signalFreq;
+    chunk = mat.getRow 0 (sin.read duration);
+    syn.precalculatedMono sampleRate (win.windowed win.hann chunk));
+
+// We want to make a CQ transform spanning more than one octave, but
+// not going all the way to fs/2 so we can test it also with
+// frequencies above and below its extents
+
+sampleRate = 100;
+
+// fs/2 = 50 so 10->40 gives us 2 octaves
+cqmin = 10;
+cqmax = 40;
+bpo = 4; // fairly arbitrary
+
+testFreqs = map (* 5) [ 0..10 ];
+duration = sampleRate * 2;
+
+streamBuilder = sinTestStream sampleRate duration;
+
+binForFreq f =
+    mm.round (bpo * mm.log2 (f / cqmin)) - 1;
+
+tests = mapIntoHash
+    do f: "freq_\(f)" done
+    do f: \(
+        str = streamBuilder f;
+        cq = cqt { maxFreq = cqmax, minFreq = cqmin, binsPerOctave = bpo } str;
+        m = mat.concatHorizontal (map cm.magnitudes cq.output);
+//    println "binFrequencies = \(cq.kernel.binFrequencies)";
+//    println "binForFreq \(f) = \(binForFreq f)";
+        success = all id
+           (map do c:
+                // passes test if the correct max bin, or the expected max
+                // is out of range, or if all bins are below a threshold
+                expected = binForFreq f;
+                good =
+                   (expected < 0 or expected >= vec.length c) or
+                   (vec.max c < 0.001) or
+                   (vec.maxindex c == binForFreq f);
+                if (not good) then
+                    println " * bad! maxindex \(vec.maxindex c) != expected \(binForFreq f) for freq \(f) in column: \(vec.list c)";
+                println "matrix is:";
+                mat.print m;
+                else 
+                    print "✓"; 
+                fi;
+                good;
+            done (mat.asColumns m));
+        success;
+    ) done
+    testFreqs;
+
+tests is hash<string, () -> boolean>