annotate yeti/test_frequency.yeti @ 69:27007f8302f4

Copyrights, licence
author Chris Cannam <c.cannam@qmul.ac.uk>
date Wed, 12 Mar 2014 08:53:45 +0000
parents b75c0eaaa6dd
children 642df7b3346f
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@65 41 { cqt } = load cqt;
c@65 42
c@65 43 // Test with a single windowed sinusoid, repeating at various frequencies
c@65 44
c@65 45 sinTestStream sampleRate duration signalFreq = // duration is in samples
c@65 46 (sin = syn.sinusoid sampleRate signalFreq;
c@65 47 chunk = mat.getRow 0 (sin.read duration);
c@65 48 syn.precalculatedMono sampleRate (win.windowed win.hann chunk));
c@65 49
c@65 50 // We want to make a CQ transform spanning more than one octave, but
c@65 51 // not going all the way to fs/2 so we can test it also with
c@65 52 // frequencies above and below its extents
c@65 53
c@65 54 sampleRate = 100;
c@65 55
c@65 56 // fs/2 = 50 so 10->40 gives us 2 octaves
c@65 57 cqmin = 10;
c@65 58 cqmax = 40;
c@65 59 bpo = 4; // fairly arbitrary
c@65 60
c@65 61 testFreqs = map (* 5) [ 0..10 ];
c@65 62 duration = sampleRate * 2;
c@65 63
c@68 64 threshold = 0.08;
c@67 65
c@65 66 streamBuilder = sinTestStream sampleRate duration;
c@65 67
c@65 68 binForFreq f =
c@65 69 mm.round (bpo * mm.log2 (f / cqmin)) - 1;
c@65 70
c@68 71 report message matrix =
c@68 72 (eprintln message;
c@68 73 eprintln "matrix is:";
c@68 74 mat.eprint matrix;
c@68 75 chart = plot.plot [Grid matrix];
c@68 76 sleep 100;
c@68 77 chart#dispose());
c@68 78
c@65 79 tests = mapIntoHash
c@65 80 do f: "freq_\(f)" done
c@65 81 do f: \(
c@65 82 str = streamBuilder f;
c@65 83 cq = cqt { maxFreq = cqmax, minFreq = cqmin, binsPerOctave = bpo } str;
c@65 84 m = mat.concatHorizontal (map cm.magnitudes cq.output);
c@65 85 // println "binFrequencies = \(cq.kernel.binFrequencies)";
c@65 86 // println "binForFreq \(f) = \(binForFreq f)";
c@67 87 var colno = 0;
c@65 88 success = all id
c@65 89 (map do c:
c@68 90 // The test passes for this column if:
c@68 91 //
c@68 92 // * the max bin is the expected one, or
c@68 93 //
c@68 94 // * the expected max is out of range entirely (but
c@68 95 // we need to test _something_ in this case --
c@68 96 // what?), or
c@68 97 //
c@68 98 // * all bins are below a threshold, or
c@68 99 //
c@68 100 // * this is an odd column and the expected max is in
c@68 101 // the lower octave
c@68 102 //
c@68 103 // We should also check that all values in the lower
c@68 104 // octave are zero for odd columns.
c@68 105 //
c@65 106 expected = binForFreq f;
c@65 107 good =
c@65 108 (expected < 0 or expected >= vec.length c) or
c@68 109 ((colno % 2 == 1) and expected < (vec.length c / 2)) or
c@67 110 (vec.max c < threshold) or
c@65 111 (vec.maxindex c == binForFreq f);
c@65 112 if (not good) then
c@68 113 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 114 fi;
c@67 115 colno := colno + 1;
c@65 116 good;
c@65 117 done (mat.asColumns m));
c@65 118 success;
c@65 119 ) done
c@65 120 testFreqs;
c@65 121
c@65 122 tests is hash<string, () -> boolean>