Mercurial > hg > constant-q-cpp
comparison yeti/test_frequency.yeti @ 65:bb1799a6d690
Pull out tests
author | Chris Cannam <c.cannam@qmul.ac.uk> |
---|---|
date | Fri, 14 Feb 2014 11:55:03 +0000 |
parents | |
children | d6dd6e1cc00e |
comparison
equal
deleted
inserted
replaced
64:df6d89381f49 | 65:bb1799a6d690 |
---|---|
1 | |
2 module test_frequency; | |
3 | |
4 mat = load may.matrix; | |
5 vec = load may.vector; | |
6 win = load may.signal.window; | |
7 mm = load may.mathmisc; | |
8 cm = load may.matrix.complex; | |
9 syn = load may.stream.syntheticstream; | |
10 | |
11 { cqt } = load cqt; | |
12 | |
13 // Test with a single windowed sinusoid, repeating at various frequencies | |
14 | |
15 sinTestStream sampleRate duration signalFreq = // duration is in samples | |
16 (sin = syn.sinusoid sampleRate signalFreq; | |
17 chunk = mat.getRow 0 (sin.read duration); | |
18 syn.precalculatedMono sampleRate (win.windowed win.hann chunk)); | |
19 | |
20 // We want to make a CQ transform spanning more than one octave, but | |
21 // not going all the way to fs/2 so we can test it also with | |
22 // frequencies above and below its extents | |
23 | |
24 sampleRate = 100; | |
25 | |
26 // fs/2 = 50 so 10->40 gives us 2 octaves | |
27 cqmin = 10; | |
28 cqmax = 40; | |
29 bpo = 4; // fairly arbitrary | |
30 | |
31 testFreqs = map (* 5) [ 0..10 ]; | |
32 duration = sampleRate * 2; | |
33 | |
34 streamBuilder = sinTestStream sampleRate duration; | |
35 | |
36 binForFreq f = | |
37 mm.round (bpo * mm.log2 (f / cqmin)) - 1; | |
38 | |
39 tests = mapIntoHash | |
40 do f: "freq_\(f)" done | |
41 do f: \( | |
42 str = streamBuilder f; | |
43 cq = cqt { maxFreq = cqmax, minFreq = cqmin, binsPerOctave = bpo } str; | |
44 m = mat.concatHorizontal (map cm.magnitudes cq.output); | |
45 // println "binFrequencies = \(cq.kernel.binFrequencies)"; | |
46 // println "binForFreq \(f) = \(binForFreq f)"; | |
47 success = all id | |
48 (map do c: | |
49 // passes test if the correct max bin, or the expected max | |
50 // is out of range, or if all bins are below a threshold | |
51 expected = binForFreq f; | |
52 good = | |
53 (expected < 0 or expected >= vec.length c) or | |
54 (vec.max c < 0.001) or | |
55 (vec.maxindex c == binForFreq f); | |
56 if (not good) then | |
57 println " * bad! maxindex \(vec.maxindex c) != expected \(binForFreq f) for freq \(f) in column: \(vec.list c)"; | |
58 println "matrix is:"; | |
59 mat.print m; | |
60 else | |
61 print "✓"; | |
62 fi; | |
63 good; | |
64 done (mat.asColumns m)); | |
65 success; | |
66 ) done | |
67 testFreqs; | |
68 | |
69 tests is hash<string, () -> boolean> |