annotate yeti/test_frequency.yeti @ 89:25947630486b

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