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