annotate yeti/cqtkernel.yeti @ 69:27007f8302f4

Copyrights, licence
author Chris Cannam <c.cannam@qmul.ac.uk>
date Wed, 12 Mar 2014 08:53:45 +0000
parents df6d89381f49
children 642df7b3346f
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@25 92 genKernel f = vec.multiply win
c@9 93 (vec.fromList
c@9 94 (map do i: f (2 * pi * fk * i / sampleRate) done [0..nk-1]));
c@9 95
c@9 96 reals = genKernel cos;
c@9 97 imags = genKernel sin;
c@9 98
c@9 99 atomOffset = firstCentre - ceil(nk/2);
c@9 100
c@9 101 map do i:
c@9 102
c@9 103 shift = vec.zeros (atomOffset + ((i-1) * atomHop));
c@9 104
c@9 105 specKernel = fftFunc
c@9 106 (complex.complexArray
c@9 107 (vec.concat [shift, reals])
c@9 108 (vec.concat [shift, imags]));
c@23 109
c@9 110 map do c:
c@9 111 if complex.magnitude c <= thresh then complex.zero else c fi
c@9 112 done specKernel;
c@23 113
c@9 114 done [1..winNr];
c@9 115
c@9 116 done [1..binsPerOctave];
c@9 117
c@43 118 kmat = cm.toSparse (cm.scaled (1/fftSize) (cm.fromRows (concat kernels)));
c@9 119
c@64 120 // eprintln "density = \(cm.density kmat) (\(cm.nonZeroValues kmat) of \(cm.width kmat * cm.height kmat))";
c@9 121
c@9 122 // Normalisation
c@9 123
c@25 124 wx1 = vec.maxindex (complex.magnitudes (cm.getRow 0 kmat));
c@25 125 wx2 = vec.maxindex (complex.magnitudes (cm.getRow (cm.height kmat - 1) kmat));
c@27 126
c@9 127 subset = cm.columnSlice kmat wx1 (wx2+1);
c@9 128 square = cm.product (cm.conjugateTransposed subset) subset;
c@27 129
c@9 130 diag = complex.magnitudes (cm.getDiagonal 0 square);
c@9 131 wK = vec.slice diag (round(1/q)) (vec.length diag - round(1/q) - 2);
c@27 132
c@25 133 weight = (fftHop / fftSize) / (vec.mean (vec.abs wK));
c@9 134 weight = sqrt(weight);
c@1 135
c@64 136 // eprintln "weight = \(weight)";
c@23 137
c@9 138 {
c@9 139 kernel = cm.scaled weight kmat,
c@9 140 fftSize,
c@9 141 fftHop,
c@9 142 binsPerOctave,
c@12 143 atomsPerFrame = winNr,
c@12 144 atomSpacing = atomHop,
c@13 145 firstCentre,
c@40 146 maxFrequency = maxFreq,
c@40 147 minFrequency = minFreq,
c@40 148 binFrequencies,
c@9 149 bigQ
c@9 150 });
c@1 151
c@9 152 {
c@9 153 makeKernel
c@9 154 }
c@1 155