annotate yeti/cqtkernel.yeti @ 40:031386846e3c

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