annotate yeti/cqtkernel.yeti @ 20:dad5d8a06a5d

Return only the actual results (i.e. space with zeros rather than duplicates)
author Chris Cannam <c.cannam@qmul.ac.uk>
date Wed, 30 Oct 2013 17:29:14 +0000
parents b54bd1d0dc1e
children 4f2f28d5df58
rev   line source
c@1 1
c@1 2 module cqtkernel;
c@1 3
c@3 4 vec = load may.vector;
c@3 5 bf = load may.vector.blockfuncs;
c@3 6 complex = load may.complex;
c@3 7 window = load may.signal.window;
c@3 8 fft = load may.transform.fft;
c@6 9 cm = load may.matrix.complex;
c@3 10
c@2 11 { pow, round, floor, ceil, nextPowerOfTwo } = load may.mathmisc;
c@1 12
c@9 13 makeKernel { sampleRate, maxFreq, binsPerOctave } =
c@9 14 (q = 1;
c@9 15 atomHopFactor = 0.25;
c@9 16 thresh = 0.0005;
c@9 17 minFreq = (maxFreq/2) * (pow 2 (1/binsPerOctave));
c@9 18 bigQ = q / ((pow 2 (1/binsPerOctave)) - 1);
c@1 19
c@9 20 maxNK = round(bigQ * sampleRate / minFreq);
c@9 21 minNK = round(bigQ * sampleRate /
c@9 22 (minFreq * (pow 2 ((binsPerOctave-1) / binsPerOctave))));
c@1 23
c@9 24 atomHop = round(minNK * atomHopFactor);
c@9 25
c@9 26 firstCentre = atomHop * (ceil ((ceil (maxNK/2)) / atomHop));
c@9 27
c@9 28 fftSize = nextPowerOfTwo (firstCentre + ceil (maxNK/2));
c@9 29
c@16 30 eprintln "sampleRate = \(sampleRate), maxFreq = \(maxFreq), binsPerOctave = \(binsPerOctave), q = \(q), atomHopFactor = \(atomHopFactor), thresh = \(thresh)";
c@16 31 eprintln "minFreq = \(minFreq), bigQ = \(bigQ), maxNK = \(maxNK), minNK = \(minNK), atomHop = \(atomHop), firstCentre = \(firstCentre), fftSize = \(fftSize)";
c@9 32
c@9 33 winNr = floor((fftSize - ceil(maxNK/2) - firstCentre) / atomHop) + 1;
c@9 34
c@9 35 lastCentre = firstCentre + (winNr - 1) * atomHop;
c@9 36
c@9 37 fftHop = (lastCentre + atomHop) - firstCentre;
c@9 38
c@16 39 eprintln "winNr = \(winNr), lastCentre = \(lastCentre), fftHop = \(fftHop)";
c@9 40
c@9 41 fftFunc = fft.forward fftSize;
c@9 42
c@9 43 // Note the MATLAB uses exp(2*pi*1i*x) for a complex generating
c@9 44 // function. We can't do that here; we need to generate real and imag
c@9 45 // parts separately as real = cos(2*pi*x), imag = sin(2*pi*x).
c@9 46
c@9 47 kernels = map do k:
c@9 48
c@9 49 nk = round(bigQ * sampleRate / (minFreq * (pow 2 ((k-1)/binsPerOctave))));
c@9 50
c@9 51 // the cq MATLAB toolbox uses a symmetric window for
c@9 52 // blackmanharris -- which is odd because it uses a periodic one
c@9 53 // for other types. Oh well
c@9 54 win = bf.divideBy nk
c@9 55 (bf.sqrt
c@9 56 (window.windowFunction (BlackmanHarris ()) [Symmetric true] nk));
c@9 57
c@9 58 fk = minFreq * (pow 2 ((k-1)/binsPerOctave));
c@9 59
c@9 60 genKernel f = bf.multiply win
c@9 61 (vec.fromList
c@9 62 (map do i: f (2 * pi * fk * i / sampleRate) done [0..nk-1]));
c@9 63
c@9 64 reals = genKernel cos;
c@9 65 imags = genKernel sin;
c@9 66
c@9 67 atomOffset = firstCentre - ceil(nk/2);
c@9 68
c@9 69 map do i:
c@9 70
c@9 71 shift = vec.zeros (atomOffset + ((i-1) * atomHop));
c@9 72
c@9 73 specKernel = fftFunc
c@9 74 (complex.complexArray
c@9 75 (vec.concat [shift, reals])
c@9 76 (vec.concat [shift, imags]));
c@9 77
c@9 78 map do c:
c@9 79 if complex.magnitude c <= thresh then complex.zero else c fi
c@9 80 done specKernel;
c@9 81
c@9 82 done [1..winNr];
c@9 83
c@9 84 done [1..binsPerOctave];
c@9 85
c@9 86 kmat = cm.toSparse
c@9 87 (cm.scaled (1/fftSize)
c@9 88 (cm.newComplexMatrix (RowMajor()) (concat kernels)));
c@9 89
c@16 90 eprintln "density = \(cm.density kmat)";
c@9 91
c@9 92 // Normalisation
c@9 93
c@9 94 wx1 = bf.maxindex (complex.magnitudes (cm.getRow 0 kmat));
c@9 95 wx2 = bf.maxindex (complex.magnitudes (cm.getRow (cm.height kmat - 1) kmat));
c@9 96
c@9 97 subset = cm.columnSlice kmat wx1 (wx2+1);
c@9 98 square = cm.product (cm.conjugateTransposed subset) subset;
c@9 99 diag = complex.magnitudes (cm.getDiagonal 0 square);
c@9 100 wK = vec.slice diag (round(1/q)) (vec.length diag - round(1/q) - 2);
c@9 101
c@9 102 weight = (fftHop / fftSize) / (bf.mean (bf.abs wK));
c@9 103 weight = sqrt(weight);
c@1 104
c@9 105 {
c@9 106 kernel = cm.scaled weight kmat,
c@9 107 fftSize,
c@9 108 fftHop,
c@9 109 binsPerOctave,
c@12 110 atomsPerFrame = winNr,
c@12 111 atomSpacing = atomHop,
c@13 112 firstCentre,
c@9 113 maxFreq,
c@9 114 minFreq,
c@9 115 bigQ
c@9 116 });
c@1 117
c@9 118 {
c@9 119 makeKernel
c@9 120 }
c@1 121