annotate constant-q-cpp/misc/yeti/cqtkernel.yeti @ 372:af71cbdab621 tip

Update bqvec code
author Chris Cannam
date Tue, 19 Nov 2019 10:13:32 +0000
parents 5d0a2ebb4d17
children
rev   line source
Chris@366 1 /*
Chris@366 2 Constant-Q library
Chris@366 3 Copyright (c) 2013-2014 Queen Mary, University of London
Chris@366 4
Chris@366 5 Permission is hereby granted, free of charge, to any person
Chris@366 6 obtaining a copy of this software and associated documentation
Chris@366 7 files (the "Software"), to deal in the Software without
Chris@366 8 restriction, including without limitation the rights to use, copy,
Chris@366 9 modify, merge, publish, distribute, sublicense, and/or sell copies
Chris@366 10 of the Software, and to permit persons to whom the Software is
Chris@366 11 furnished to do so, subject to the following conditions:
Chris@366 12
Chris@366 13 The above copyright notice and this permission notice shall be
Chris@366 14 included in all copies or substantial portions of the Software.
Chris@366 15
Chris@366 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Chris@366 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Chris@366 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Chris@366 19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
Chris@366 20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Chris@366 21 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Chris@366 22 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Chris@366 23
Chris@366 24 Except as contained in this notice, the names of the Centre for
Chris@366 25 Digital Music; Queen Mary, University of London; and Chris Cannam
Chris@366 26 shall not be used in advertising or otherwise to promote the sale,
Chris@366 27 use or other dealings in this Software without prior written
Chris@366 28 authorization.
Chris@366 29 */
Chris@366 30
Chris@366 31 module cqtkernel;
Chris@366 32
Chris@366 33 vec = load may.vector;
Chris@366 34 complex = load may.complex;
Chris@366 35 window = load may.signal.window;
Chris@366 36 fft = load may.transform.fft;
Chris@366 37 cm = load may.matrix.complex;
Chris@366 38
Chris@366 39 { pow, round, floor, ceil, nextPowerOfTwo } = load may.mathmisc;
Chris@366 40
Chris@366 41 makeKernel { sampleRate, maxFreq, binsPerOctave } =
Chris@366 42 (q = 1;
Chris@366 43 atomHopFactor = 0.25;
Chris@366 44 thresh = 0.0005;
Chris@366 45 minFreq = (maxFreq/2) * (pow 2 (1/binsPerOctave));
Chris@366 46 bigQ = q / ((pow 2 (1/binsPerOctave)) - 1);
Chris@366 47
Chris@366 48 maxNK = round(bigQ * sampleRate / minFreq);
Chris@366 49 minNK = round(bigQ * sampleRate /
Chris@366 50 (minFreq * (pow 2 ((binsPerOctave-1) / binsPerOctave))));
Chris@366 51
Chris@366 52 atomHop = round(minNK * atomHopFactor);
Chris@366 53
Chris@366 54 firstCentre = atomHop * (ceil ((ceil (maxNK/2)) / atomHop));
Chris@366 55
Chris@366 56 fftSize = nextPowerOfTwo (firstCentre + ceil (maxNK/2));
Chris@366 57
Chris@366 58 // eprintln "sampleRate = \(sampleRate), maxFreq = \(maxFreq), binsPerOctave = \(binsPerOctave), q = \(q), atomHopFactor = \(atomHopFactor), thresh = \(thresh)";
Chris@366 59 // eprintln "minFreq = \(minFreq), bigQ = \(bigQ), maxNK = \(maxNK), minNK = \(minNK), atomHop = \(atomHop), firstCentre = \(firstCentre), fftSize = \(fftSize)";
Chris@366 60
Chris@366 61 winNr = floor((fftSize - ceil(maxNK/2) - firstCentre) / atomHop) + 1;
Chris@366 62
Chris@366 63 lastCentre = firstCentre + (winNr - 1) * atomHop;
Chris@366 64
Chris@366 65 fftHop = (lastCentre + atomHop) - firstCentre;
Chris@366 66
Chris@366 67 // eprintln "winNr = \(winNr), lastCentre = \(lastCentre), fftHop = \(fftHop)";
Chris@366 68
Chris@366 69 fftFunc = fft.forward fftSize;
Chris@366 70
Chris@366 71 // Note the MATLAB uses exp(2*pi*1i*x) for a complex generating
Chris@366 72 // function. We can't do that here; we need to generate real and imag
Chris@366 73 // parts separately as real = cos(2*pi*x), imag = sin(2*pi*x).
Chris@366 74
Chris@366 75 binFrequencies = array [];
Chris@366 76
Chris@366 77 kernels = map do k:
Chris@366 78
Chris@366 79 nk = round(bigQ * sampleRate / (minFreq * (pow 2 ((k-1)/binsPerOctave))));
Chris@366 80
Chris@366 81 // the cq MATLAB toolbox uses a symmetric window for
Chris@366 82 // blackmanharris -- which is odd because it uses a periodic one
Chris@366 83 // for other types. Oh well
Chris@366 84 win = vec.divideBy nk
Chris@366 85 (vec.sqrt
Chris@366 86 (window.windowFunction (BlackmanHarris ()) [Symmetric true] nk));
Chris@366 87
Chris@366 88 fk = minFreq * (pow 2 ((k-1)/binsPerOctave));
Chris@366 89
Chris@366 90 push binFrequencies fk;
Chris@366 91
Chris@366 92 genKernel f = vec.multiply
Chris@366 93 [win,
Chris@366 94 vec.fromList
Chris@366 95 (map do i: f (2 * pi * fk * i / sampleRate) done [0..nk-1])];
Chris@366 96
Chris@366 97 reals = genKernel cos;
Chris@366 98 imags = genKernel sin;
Chris@366 99
Chris@366 100 atomOffset = firstCentre - ceil(nk/2);
Chris@366 101
Chris@366 102 map do i:
Chris@366 103
Chris@366 104 shift = vec.zeros (atomOffset + ((i-1) * atomHop));
Chris@366 105
Chris@366 106 specKernel = fftFunc
Chris@366 107 (complex.complexArray
Chris@366 108 (vec.concat [shift, reals])
Chris@366 109 (vec.concat [shift, imags]));
Chris@366 110
Chris@366 111 map do c:
Chris@366 112 if complex.magnitude c <= thresh then complex.zero else c fi
Chris@366 113 done specKernel;
Chris@366 114
Chris@366 115 done [1..winNr];
Chris@366 116
Chris@366 117 done [1..binsPerOctave];
Chris@366 118
Chris@366 119 kmat = cm.toSparse (cm.scaled (1/fftSize) (cm.fromRows (concat kernels)));
Chris@366 120
Chris@366 121 // eprintln "density = \(cm.density kmat) (\(cm.nonZeroValues kmat) of \(cm.width kmat * cm.height kmat))";
Chris@366 122
Chris@366 123 // Normalisation
Chris@366 124
Chris@366 125 wx1 = vec.maxindex (complex.magnitudes (cm.getRow 0 kmat));
Chris@366 126 wx2 = vec.maxindex (complex.magnitudes (cm.getRow (cm.height kmat - 1) kmat));
Chris@366 127
Chris@366 128 subset = cm.flipped (cm.columnSlice kmat wx1 (wx2+1));
Chris@366 129 square = cm.product (cm.conjugateTransposed subset) subset;
Chris@366 130
Chris@366 131 diag = complex.magnitudes (cm.getDiagonal 0 square);
Chris@366 132 wK = vec.slice diag (round(1/q)) (vec.length diag - round(1/q) - 2);
Chris@366 133
Chris@366 134 weight = (fftHop / fftSize) / (vec.mean (vec.abs wK));
Chris@366 135 weight = sqrt(weight);
Chris@366 136
Chris@366 137 kernel = cm.scaled weight kmat;
Chris@366 138
Chris@366 139 // eprintln "weight = \(weight)";
Chris@366 140
Chris@366 141 {
Chris@366 142 kernel,
Chris@366 143 fftSize,
Chris@366 144 fftHop,
Chris@366 145 binsPerOctave,
Chris@366 146 atomsPerFrame = winNr,
Chris@366 147 atomSpacing = atomHop,
Chris@366 148 firstCentre,
Chris@366 149 maxFrequency = maxFreq,
Chris@366 150 minFrequency = minFreq,
Chris@366 151 binFrequencies,
Chris@366 152 bigQ
Chris@366 153 });
Chris@366 154
Chris@366 155 {
Chris@366 156 makeKernel
Chris@366 157 }
Chris@366 158