changeset 34:3fb9c657d86b

Expand hypothesis tests
author Chris Cannam
date Fri, 13 Jul 2012 22:48:02 +0100
parents 5f2a57b1a75a
children 2f5b169e4a3b
files NoteHypothesis.cpp NoteHypothesis.h test/TestNoteHypothesis.h
diffstat 3 files changed, 90 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/NoteHypothesis.cpp	Fri Jul 13 22:14:01 2012 +0100
+++ b/NoteHypothesis.cpp	Fri Jul 13 22:48:02 2012 +0100
@@ -134,6 +134,7 @@
 NoteHypothesis::getMeanFrequency() const
 {
     double acc = 0.0;
+    if (m_pending.empty()) return acc;
     for (int i = 0; i < (int)m_pending.size(); ++i) {
         acc += m_pending[i].freq;
     }
--- a/NoteHypothesis.h	Fri Jul 13 22:14:01 2012 +0100
+++ b/NoteHypothesis.h	Fri Jul 13 22:48:02 2012 +0100
@@ -52,6 +52,9 @@
         Estimate() : freq(0), time(), confidence(0) { }
         Estimate(double _f, RealTime _t, double _c) :
             freq(_f), time(_t), confidence(_c) { }
+        bool operator==(const Estimate &e) const {
+            return e.freq == freq && e.time == time && e.confidence == confidence;
+        }
 	double freq;
 	RealTime time;
 	double confidence;
@@ -82,12 +85,20 @@
         Note() : freq(0), time(), duration() { }
         Note(double _f, RealTime _t, RealTime _d) :
             freq(_f), time(_t), duration(_d) { }
+        bool operator==(const Note &e) const {
+            return e.freq == freq && e.time == time && e.duration == duration;
+        }
 	double freq;
 	RealTime time;
 	RealTime duration;
     };
     
     /**
+     * Return the mean frequency of the accepted estimates
+     */
+    double getMeanFrequency() const;
+
+    /**
      * Return a single note roughly matching this hypothesis
      */
     Note getAveragedNote() const;
@@ -96,7 +107,6 @@
     bool isWithinTolerance(Estimate) const;
     bool isOutOfDateFor(Estimate) const;
     bool isSatisfied() const;
-    double getMeanFrequency() const;
     
     State m_state;
     Estimates m_pending;
--- a/test/TestNoteHypothesis.h	Fri Jul 13 22:14:01 2012 +0100
+++ b/test/TestNoteHypothesis.h	Fri Jul 13 22:48:02 2012 +0100
@@ -78,11 +78,11 @@
     void weakSatisfy() {
 	NoteHypothesis h;
 	NoteHypothesis::Estimate e1(500, RealTime::fromMilliseconds(0), 0.5);
-	NoteHypothesis::Estimate e2(500, RealTime::fromMilliseconds(10), 0.5);
-	NoteHypothesis::Estimate e3(500, RealTime::fromMilliseconds(20), 0.5);
-	NoteHypothesis::Estimate e4(500, RealTime::fromMilliseconds(30), 0.5);
-	NoteHypothesis::Estimate e5(500, RealTime::fromMilliseconds(40), 0.5);
-	NoteHypothesis::Estimate e6(500, RealTime::fromMilliseconds(90), 0.5);
+	NoteHypothesis::Estimate e2(502, RealTime::fromMilliseconds(10), 0.5);
+	NoteHypothesis::Estimate e3(504, RealTime::fromMilliseconds(20), 0.5);
+	NoteHypothesis::Estimate e4(506, RealTime::fromMilliseconds(30), 0.5);
+	NoteHypothesis::Estimate e5(508, RealTime::fromMilliseconds(40), 0.5);
+	NoteHypothesis::Estimate e6(510, RealTime::fromMilliseconds(90), 0.5);
 	QCOMPARE(h.getState(), NoteHypothesis::New);
 	QVERIFY(h.accept(e1));
 	QCOMPARE(h.getState(), NoteHypothesis::Provisional);
@@ -98,6 +98,79 @@
 	QCOMPARE(h.getState(), NoteHypothesis::Expired);
     }
 	
+    void frequencyRange() {
+	NoteHypothesis h;
+	NoteHypothesis::Estimate e1(440, RealTime::fromMilliseconds(0), 1);
+	NoteHypothesis::Estimate e2(448, RealTime::fromMilliseconds(10), 1);
+	NoteHypothesis::Estimate e3(444, RealTime::fromMilliseconds(20), 1);
+	NoteHypothesis::Estimate e4(470, RealTime::fromMilliseconds(30), 1);
+	QCOMPARE(h.getState(), NoteHypothesis::New);
+	QVERIFY(h.accept(e1));
+	QCOMPARE(h.getState(), NoteHypothesis::Provisional);
+	QVERIFY(h.accept(e2));
+	QCOMPARE(h.getState(), NoteHypothesis::Provisional);
+	QVERIFY(h.accept(e3));
+	QCOMPARE(h.getState(), NoteHypothesis::Satisfied);
+	QVERIFY(!h.accept(e4));
+	QCOMPARE(h.getState(), NoteHypothesis::Satisfied);
+    }
+
+    void acceptedEstimates() {
+	NoteHypothesis h;
+	NoteHypothesis::Estimate e1(440, RealTime::fromMilliseconds(0), 1);
+	NoteHypothesis::Estimate e2(448, RealTime::fromMilliseconds(10), 1);
+	NoteHypothesis::Estimate e3(444, RealTime::fromMilliseconds(20), 1);
+	NoteHypothesis::Estimate e4(470, RealTime::fromMilliseconds(30), 1);
+	NoteHypothesis::Estimate e5(444, RealTime::fromMilliseconds(90), 1);
+	NoteHypothesis::Estimates es;
+	es.push_back(e1);
+	es.push_back(e2);
+	es.push_back(e3);
+	QCOMPARE(h.getState(), NoteHypothesis::New);
+	QCOMPARE(h.getAcceptedEstimates(), NoteHypothesis::Estimates());
+	QVERIFY(h.accept(e1));
+	QCOMPARE(h.getState(), NoteHypothesis::Provisional);
+	QCOMPARE(h.getAcceptedEstimates(), NoteHypothesis::Estimates());
+	QVERIFY(h.accept(e2));
+	QCOMPARE(h.getState(), NoteHypothesis::Provisional);
+	QCOMPARE(h.getAcceptedEstimates(), NoteHypothesis::Estimates());
+	QVERIFY(h.accept(e3));
+	QCOMPARE(h.getState(), NoteHypothesis::Satisfied);
+	QCOMPARE(h.getAcceptedEstimates(), es);
+	QVERIFY(!h.accept(e4));
+	QCOMPARE(h.getState(), NoteHypothesis::Satisfied);
+	QCOMPARE(h.getAcceptedEstimates(), es);
+	QVERIFY(!h.accept(e5));
+	QCOMPARE(h.getState(), NoteHypothesis::Expired);
+	QCOMPARE(h.getAcceptedEstimates(), es);
+    }
+	
+    void meanFrequency() {
+	NoteHypothesis h;
+	NoteHypothesis::Estimate e1(440, RealTime::fromMilliseconds(0), 1);
+	NoteHypothesis::Estimate e2(448, RealTime::fromMilliseconds(10), 1);
+	NoteHypothesis::Estimate e3(444, RealTime::fromMilliseconds(20), 1);
+	QVERIFY(h.accept(e1));
+	QVERIFY(h.accept(e2));
+	QVERIFY(h.accept(e3));
+	QCOMPARE(h.getMeanFrequency(), 444.0);
+    }
+
+    void averagedNote() {
+	NoteHypothesis h;
+	NoteHypothesis::Estimate e1(440, RealTime::fromMilliseconds(10), 1);
+	NoteHypothesis::Estimate e2(448, RealTime::fromMilliseconds(20), 1);
+	NoteHypothesis::Estimate e3(444, RealTime::fromMilliseconds(30), 1);
+	QVERIFY(h.accept(e1));
+	QVERIFY(h.accept(e2));
+	QVERIFY(h.accept(e3));
+	QCOMPARE(h.getAveragedNote(), NoteHypothesis::Note
+		 (444,
+		  RealTime::fromMilliseconds(10),
+		  RealTime::fromMilliseconds(20)));
+    }
+
+	
     
 };