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