annotate yeti/cqtkernel.yeti @ 80:872fc9dc0321

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