annotate misc/yeti/cqtkernel.yeti @ 196:da283326bcd3 tip master

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