annotate base/test/TestMovingMedian.h @ 1573:f04038819c26 spectrogramparam

Introduce & make use of faster MovingMedian class (now with resize capability)
author Chris Cannam
date Thu, 08 Nov 2018 15:02:30 +0000
parents
children cfcfec216c21
rev   line source
Chris@1573 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1573 2
Chris@1573 3 /*
Chris@1573 4 Sonic Visualiser
Chris@1573 5 An audio file viewer and annotation editor.
Chris@1573 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1573 7
Chris@1573 8 This program is free software; you can redistribute it and/or
Chris@1573 9 modify it under the terms of the GNU General Public License as
Chris@1573 10 published by the Free Software Foundation; either version 2 of the
Chris@1573 11 License, or (at your option) any later version. See the file
Chris@1573 12 COPYING included with this distribution for more information.
Chris@1573 13 */
Chris@1573 14
Chris@1573 15 #ifndef TEST_MOVING_MEDIAN_H
Chris@1573 16 #define TEST_MOVING_MEDIAN_H
Chris@1573 17
Chris@1573 18 #include "../MovingMedian.h"
Chris@1573 19
Chris@1573 20 #include <QObject>
Chris@1573 21 #include <QtTest>
Chris@1573 22 #include <QDir>
Chris@1573 23
Chris@1573 24 #include <iostream>
Chris@1573 25
Chris@1573 26 using namespace std;
Chris@1573 27
Chris@1573 28 class TestMovingMedian : public QObject
Chris@1573 29 {
Chris@1573 30 Q_OBJECT
Chris@1573 31
Chris@1573 32 template <typename T>
Chris@1573 33 void checkExpected(const vector<T> &output,
Chris@1573 34 const vector<T> &expected) {
Chris@1573 35 if (output.size() != expected.size()) {
Chris@1573 36 cerr << "ERROR: output array size " << output.size()
Chris@1573 37 << " differs from expected size " << expected.size() << endl;
Chris@1573 38 }
Chris@1573 39 for (int i = 0; i < int(output.size()); ++i) {
Chris@1573 40 if (output[i] != expected[i]) {
Chris@1573 41 cerr << "ERROR: Value at index " << i
Chris@1573 42 << " in output array differs from expected" << endl;
Chris@1573 43 cerr << "Output: ";
Chris@1573 44 for (auto v: output) cerr << v << " ";
Chris@1573 45 cerr << "\nExpected: ";
Chris@1573 46 for (auto v: expected) cerr << v << " ";
Chris@1573 47 cerr << endl;
Chris@1573 48 break;
Chris@1573 49 }
Chris@1573 50 }
Chris@1573 51 QCOMPARE(output, expected);
Chris@1573 52 }
Chris@1573 53
Chris@1573 54 template <typename T>
Chris@1573 55 void testFixed(int n,
Chris@1573 56 const vector<T> &input,
Chris@1573 57 const vector<T> &expected,
Chris@1573 58 double percentile = 50.0) {
Chris@1573 59 vector<T> output;
Chris@1573 60 MovingMedian<T> mm(n, percentile);
Chris@1573 61 for (auto v: input) {
Chris@1573 62 mm.push(v);
Chris@1573 63 mm.checkIntegrity();
Chris@1573 64 output.push_back(mm.get());
Chris@1573 65 }
Chris@1573 66 mm.checkIntegrity();
Chris@1573 67 checkExpected<T>(output, expected);
Chris@1573 68 }
Chris@1573 69
Chris@1573 70 private slots:
Chris@1573 71
Chris@1573 72 void empty() {
Chris@1573 73 MovingMedian<double> mm(3);
Chris@1573 74 QCOMPARE(mm.get(), 0.0);
Chris@1573 75 }
Chris@1573 76
Chris@1573 77 void zeros() {
Chris@1573 78 vector<double> input { 0.0, 0.0, 0.0, 0.0, 0.0 };
Chris@1573 79 vector<double> expected { 0.0, 0.0, 0.0, 0.0, 0.0 };
Chris@1573 80 testFixed<double>(3, input, expected);
Chris@1573 81 }
Chris@1573 82
Chris@1573 83 void ascending() {
Chris@1573 84 vector<double> input { 1.0, 2.0, 3.0, 4.0, 5.0 };
Chris@1573 85 vector<double> expected { 0.0, 1.0, 2.0, 3.0, 4.0 };
Chris@1573 86 testFixed<double>(3, input, expected);
Chris@1573 87 }
Chris@1573 88
Chris@1573 89 void ascendingInt() {
Chris@1573 90 vector<int> input { 1, 2, 3, 4, 5 };
Chris@1573 91 vector<int> expected { 0, 1, 2, 3, 4 };
Chris@1573 92 testFixed<int>(3, input, expected);
Chris@1573 93 }
Chris@1573 94
Chris@1573 95 void descending() {
Chris@1573 96 vector<double> input { 5.0, 4.0, 3.0, 2.0, 1.0 };
Chris@1573 97 vector<double> expected { 0.0, 4.0, 4.0, 3.0, 2.0 };
Chris@1573 98 testFixed<double>(3, input, expected);
Chris@1573 99 }
Chris@1573 100
Chris@1573 101 void descendingInt() {
Chris@1573 102 vector<int> input { 5, 4, 3, 2, 1 };
Chris@1573 103 vector<int> expected { 0, 4, 4, 3, 2 };
Chris@1573 104 testFixed<int>(3, input, expected);
Chris@1573 105 }
Chris@1573 106
Chris@1573 107 void duplicates() {
Chris@1573 108 vector<double> input { 2.0, 2.0, 3.0, 4.0, 3.0 };
Chris@1573 109 vector<double> expected { 0.0, 2.0, 2.0, 3.0, 3.0 };
Chris@1573 110 testFixed<double>(3, input, expected);
Chris@1573 111 }
Chris@1573 112
Chris@1573 113 void percentile10() {
Chris@1573 114 vector<double> input { 1.0, 2.0, 3.0, 4.0, 5.0 };
Chris@1573 115 vector<double> expected { 0.0, 0.0, 1.0, 2.0, 3.0 };
Chris@1573 116 testFixed<double>(3, input, expected, 10);
Chris@1573 117 }
Chris@1573 118
Chris@1573 119 void percentile90() {
Chris@1573 120 vector<double> input { 1.0, 2.0, 3.0, 4.0, 5.0 };
Chris@1573 121 vector<double> expected { 1.0, 2.0, 3.0, 4.0, 5.0 };
Chris@1573 122 testFixed<double>(3, input, expected, 90);
Chris@1573 123 }
Chris@1573 124
Chris@1573 125 void even() {
Chris@1573 126 vector<double> input { 5.0, 4.0, 3.0, 2.0, 1.0 };
Chris@1573 127 vector<double> expected { 0.0, 4.0, 4.0, 4.0, 3.0 };
Chris@1573 128 testFixed<double>(4, input, expected);
Chris@1573 129 }
Chris@1573 130
Chris@1573 131 void growing() {
Chris@1573 132 vector<double> input { 2.0, 4.0, 3.0, 2.5, 2.5, 3.0, 1.0, 2.0, 1.0, 0.0 };
Chris@1573 133 vector<double> expected { 2.0, 4.0, 4.0, 3.0, 2.5, 2.5, 2.5, 2.5, 2.0, 1.0 };
Chris@1573 134 vector<double> output;
Chris@1573 135 MovingMedian<double> mm(1);
Chris@1573 136 for (int i = 0; i < int(input.size()); ++i) {
Chris@1573 137 // sizes 1, 1, 2, 2, 3, 3, 4, 4, 5, 5
Chris@1573 138 int sz = i/2 + 1;
Chris@1573 139 mm.resize(sz);
Chris@1573 140 QCOMPARE(mm.size(), sz);
Chris@1573 141 mm.push(input[i]);
Chris@1573 142 mm.checkIntegrity();
Chris@1573 143 output.push_back(mm.get());
Chris@1573 144 }
Chris@1573 145 mm.checkIntegrity();
Chris@1573 146 checkExpected<double>(output, expected);
Chris@1573 147 }
Chris@1573 148
Chris@1573 149 void shrinking() {
Chris@1573 150 vector<double> input { 2.0, 4.0, 3.0, 2.5, 2.5, 3.0, 1.0, 2.0, 1.0, 0.0 };
Chris@1573 151 vector<double> expected { 0.0, 0.0, 3.0, 3.0, 2.5, 2.5, 3.0, 2.0, 1.0, 0.0 };
Chris@1573 152 vector<double> output;
Chris@1573 153 MovingMedian<double> mm(99);
Chris@1573 154 for (int i = 0; i < int(input.size()); ++i) {
Chris@1573 155 // sizes 5, 5, 4, 4, 3, 3, 2, 2, 1, 1
Chris@1573 156 int sz = 5 - i/2;
Chris@1573 157 mm.resize(sz);
Chris@1573 158 QCOMPARE(mm.size(), sz);
Chris@1573 159 mm.push(input[i]);
Chris@1573 160 mm.checkIntegrity();
Chris@1573 161 output.push_back(mm.get());
Chris@1573 162 }
Chris@1573 163 mm.checkIntegrity();
Chris@1573 164 checkExpected<double>(output, expected);
Chris@1573 165 }
Chris@1573 166 };
Chris@1573 167
Chris@1573 168 #endif