annotate constant-q-cpp/misc/yeti/test_frequency.yeti @ 372:af71cbdab621 tip

Update bqvec code
author Chris Cannam
date Tue, 19 Nov 2019 10:13:32 +0000
parents 5d0a2ebb4d17
children
rev   line source
Chris@366 1 /*
Chris@366 2 Constant-Q library
Chris@366 3 Copyright (c) 2013-2014 Queen Mary, University of London
Chris@366 4
Chris@366 5 Permission is hereby granted, free of charge, to any person
Chris@366 6 obtaining a copy of this software and associated documentation
Chris@366 7 files (the "Software"), to deal in the Software without
Chris@366 8 restriction, including without limitation the rights to use, copy,
Chris@366 9 modify, merge, publish, distribute, sublicense, and/or sell copies
Chris@366 10 of the Software, and to permit persons to whom the Software is
Chris@366 11 furnished to do so, subject to the following conditions:
Chris@366 12
Chris@366 13 The above copyright notice and this permission notice shall be
Chris@366 14 included in all copies or substantial portions of the Software.
Chris@366 15
Chris@366 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Chris@366 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Chris@366 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Chris@366 19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
Chris@366 20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Chris@366 21 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Chris@366 22 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Chris@366 23
Chris@366 24 Except as contained in this notice, the names of the Centre for
Chris@366 25 Digital Music; Queen Mary, University of London; and Chris Cannam
Chris@366 26 shall not be used in advertising or otherwise to promote the sale,
Chris@366 27 use or other dealings in this Software without prior written
Chris@366 28 authorization.
Chris@366 29 */
Chris@366 30
Chris@366 31 module test_frequency;
Chris@366 32
Chris@366 33 mat = load may.matrix;
Chris@366 34 vec = load may.vector;
Chris@366 35 win = load may.signal.window;
Chris@366 36 mm = load may.mathmisc;
Chris@366 37 cm = load may.matrix.complex;
Chris@366 38 syn = load may.stream.syntheticstream;
Chris@366 39 plot = load may.plot;
Chris@366 40
Chris@366 41 { compare } = load may.test;
Chris@366 42
Chris@366 43 { cqt } = load cqt;
Chris@366 44
Chris@366 45 // Test with a single windowed sinusoid, repeating at various frequencies
Chris@366 46
Chris@366 47 sinTestStream sampleRate duration signalFreq = // duration is in samples
Chris@366 48 (sin = syn.sinusoid sampleRate signalFreq;
Chris@366 49 chunk = mat.getRow 0 (sin.read duration);
Chris@366 50 syn.precalculatedMono sampleRate (win.windowed win.hann chunk));
Chris@366 51
Chris@366 52 // We want to make a CQ transform spanning more than one octave, but
Chris@366 53 // not going all the way to fs/2 so we can test it also with
Chris@366 54 // frequencies above and below its extents
Chris@366 55
Chris@366 56 sampleRate = 100;
Chris@366 57
Chris@366 58 // fs/2 = 50 so 10->40 gives us 2 octaves
Chris@366 59 cqmin = 10;
Chris@366 60 cqmax = 40;
Chris@366 61 bpo = 4; // fairly arbitrary
Chris@366 62
Chris@366 63 testFreqs = map (* 5) [ 0..10 ];
Chris@366 64 duration = sampleRate * 2;
Chris@366 65
Chris@366 66 threshold = 0.08;
Chris@366 67
Chris@366 68 streamBuilder = sinTestStream sampleRate duration;
Chris@366 69
Chris@366 70 binForFreq f =
Chris@366 71 mm.round (bpo * mm.log2 (f / cqmin)) - 1;
Chris@366 72
Chris@366 73 report message matrix =
Chris@366 74 (eprintln message;
Chris@366 75 eprintln "matrix is:";
Chris@366 76 mat.eprint matrix);
Chris@366 77 // chart = plot.plot [Grid matrix];
Chris@366 78 // sleep 100;
Chris@366 79 // chart#dispose());
Chris@366 80
Chris@366 81 tests = mapIntoHash
Chris@366 82 do f: "freq_\(f)" done
Chris@366 83 do f: \(
Chris@366 84 str = streamBuilder f;
Chris@366 85 cq = cqt { maxFreq = cqmax, minFreq = cqmin, binsPerOctave = bpo } str;
Chris@366 86 spec = cq.cqSpectrogram;
Chris@366 87 rightSize = all id
Chris@366 88 (map do s:
Chris@366 89 compare (mat.size s) {
Chris@366 90 rows = cq.kernel.binsPerOctave * cq.octaves,
Chris@366 91 columns = cq.kernel.atomsPerFrame * mm.pow 2 (cq.octaves - 1)
Chris@366 92 }
Chris@366 93 done spec);
Chris@366 94 m = mat.concatHorizontal spec;
Chris@366 95 // println "binFrequencies = \(cq.kernel.binFrequencies)";
Chris@366 96 // println "binForFreq \(f) = \(binForFreq f)";
Chris@366 97 var colno = 0;
Chris@366 98 success = all id
Chris@366 99 (rightSize :: map do c:
Chris@366 100 // The test passes for this column if:
Chris@366 101 //
Chris@366 102 // * the max bin is the expected one, or
Chris@366 103 //
Chris@366 104 // * the expected max is out of range entirely (but
Chris@366 105 // we need to test _something_ in this case --
Chris@366 106 // what?), or
Chris@366 107 //
Chris@366 108 // * all bins are below a threshold, or
Chris@366 109 //
Chris@366 110 // * this is an odd column and the expected max is in
Chris@366 111 // the lower octave
Chris@366 112 //
Chris@366 113 // We should also check that all values in the lower
Chris@366 114 // octave are zero for odd columns.
Chris@366 115 //
Chris@366 116 expected = binForFreq f;
Chris@366 117 good =
Chris@366 118 (expected < 0 or expected >= vec.length c) or
Chris@366 119 ((colno % 2 == 1) and expected < (vec.length c / 2)) or
Chris@366 120 (vec.max c < threshold) or
Chris@366 121 (vec.maxindex c == binForFreq f);
Chris@366 122 if (not good) then
Chris@366 123 report " * bad! maxindex \(vec.maxindex c) != expected \(binForFreq f) for freq \(f) in column \(colno) of \(mat.width m): column is \(vec.list c)" m;
Chris@366 124 fi;
Chris@366 125 colno := colno + 1;
Chris@366 126 good;
Chris@366 127 done (mat.asColumns m));
Chris@366 128 success;
Chris@366 129 ) done
Chris@366 130 testFreqs;
Chris@366 131
Chris@366 132 tests is hash<string, () -> boolean>