annotate yeti/cqt.yeti @ 41:ae7d2e558ed1

More debug testing
author Chris Cannam <c.cannam@qmul.ac.uk>
date Tue, 19 Nov 2013 19:27:45 +0000
parents 031386846e3c
children 53d1e0d59ac5
rev   line source
c@10 1
c@37 2 module cqt;
c@10 3
c@10 4 cqtkernel = load cqtkernel;
c@10 5 resample = load may.stream.resample;
c@10 6 manipulate = load may.stream.manipulate;
c@10 7 cm = load may.matrix.complex;
c@10 8 framer = load may.stream.framer;
c@10 9 cplx = load may.complex;
c@10 10 fft = load may.transform.fft;
c@10 11 vec = load may.vector;
c@10 12
c@10 13 { pow, round, floor, ceil, log2, nextPowerOfTwo } = load may.mathmisc;
c@10 14
c@37 15 cqt { maxFreq, minFreq, binsPerOctave } str =
c@10 16 (sampleRate = str.sampleRate;
c@10 17 octaves = ceil (log2 (maxFreq / minFreq));
c@10 18 actualMinFreq = (maxFreq / (pow 2 octaves)) * (pow 2 (1/binsPerOctave));
c@10 19
c@41 20 kdata = cqtkernel.makeKernel { sampleRate, maxFreq, binsPerOctave };
c@10 21
c@41 22 eprintln "sampleRate = \(sampleRate), maxFreq = \(maxFreq), minFreq = \(minFreq), actualMinFreq = \(actualMinFreq), octaves = \(octaves), binsPerOctave = \(binsPerOctave), fftSize = \(kdata.fftSize), hop = \(kdata.fftHop)";
c@10 23
c@16 24 eprintln "atomsPerFrame = \(kdata.atomsPerFrame)";
c@11 25
c@41 26 padding = (kdata.fftSize * (pow 2 (octaves-1)));
c@40 27
c@40 28 eprintln "padding = \(padding)";
c@40 29
c@40 30 str = manipulate.paddedBy padding str;
c@40 31
c@10 32 streams = manipulate.duplicated octaves str;
c@10 33
c@10 34 //!!! can't be right!
c@10 35 kernel = cm.transposed (cm.conjugateTransposed kdata.kernel);
c@10 36
c@16 37 eprintln "have kernel";
c@10 38
c@10 39 fftFunc = fft.forward kdata.fftSize;
c@10 40
c@10 41 cqblocks =
c@10 42 map do octave:
c@10 43 frames = framer.monoFrames //!!! mono for now
c@10 44 { framesize = kdata.fftSize, hop = kdata.fftHop }
c@10 45 (resample.decimated (pow 2 octave) streams[octave]);
c@10 46 map do frame:
c@10 47 freq = fftFunc (cplx.complexArray frame (vec.zeros kdata.fftSize));
c@41 48 println "octave = \(octave), frame = \(vec.list frame)";
c@41 49 println "octave = \(octave), freq = \(freq)";
c@10 50 cm.product kernel (cm.newComplexColumnVector freq);
c@10 51 done frames;
c@10 52 done [0..octaves-1];
c@10 53
c@13 54 // The cqblocks list is a list<list<matrix>>. Each top-level list
c@11 55 // corresponds to an octave, from highest to lowest, each having
c@11 56 // twice as many elements in its list as the next octave. The
c@11 57 // sub-lists are sampled in time with an effective spacing of
c@11 58 // fftSize * 2^(octave-1) audio frames, and the matrices are row
c@11 59 // vectors with atomsPerFrame * binsPerOctave complex elements.
c@13 60 //
c@13 61 // ***
c@13 62 //
c@13 63 // In a typical constant-Q structure, each (2^(octaves-1) *
c@13 64 // fftHop) input frames gives us an output structure conceptually
c@13 65 // like this:
c@10 66 //
c@10 67 // [][][][][][][][] <- fftHop frames per highest-octave output value
c@10 68 // [][][][][][][][] layered as many times as binsPerOctave (here 2)
c@10 69 // [--][--][--][--] <- fftHop*2 frames for the next lower octave
c@10 70 // [--][--][--][--] etc
c@10 71 // [------][------]
c@10 72 // [------][------]
c@10 73 // [--------------]
c@10 74 // [--------------]
c@10 75 //
c@13 76 // ***
c@13 77 //
c@13 78 // But the kernel we're using here has more than one temporally
c@13 79 // spaced atom; each individual cell is a row vector with
c@13 80 // atomsPerFrame * binsPerOctave elements, but that actually
c@13 81 // represents a rectangular matrix of result cells with width
c@13 82 // atomsPerFrame and height binsPerOctave. The columns of this
c@13 83 // matrix (the atoms) then need to be spaced by 2^(octave-1)
c@13 84 // relative to those from the highest octave.
c@10 85
c@15 86 // Reshape each row vector into the appropriate rectangular matrix
c@21 87 // and split into single-atom columns
c@19 88
c@21 89 emptyHops = kdata.firstCentre / kdata.atomSpacing;
c@21 90 maxDrop = emptyHops * (pow 2 (octaves-1)) - emptyHops;
c@21 91 eprintln "maxDrop = \(maxDrop)";
c@21 92
c@21 93 cqblocks = map do octlist:
c@21 94 concat
c@21 95 (map do rv:
c@21 96 cm.asColumns
c@21 97 (cm.generate do row col:
c@21 98 cm.at rv ((row * kdata.atomsPerFrame) + col) 0
c@21 99 done {
c@21 100 rows = kdata.binsPerOctave,
c@21 101 columns = kdata.atomsPerFrame
c@21 102 })
c@21 103 done octlist)
c@21 104 done cqblocks;
c@21 105
c@21 106 cqblocks = array (map2 do octlist octave:
c@21 107 d = emptyHops * (pow 2 (octaves-octave)) - emptyHops;
c@22 108
c@41 109 // d = 0; //!!!
c@22 110
c@21 111 eprintln "dropping \(d)";
c@21 112 drop d octlist;
c@21 113 done cqblocks [1..octaves]);
c@14 114
c@17 115 assembleBlock bits =
c@19 116 (eprintln "assembleBlock: structure of bits is:";
c@19 117 eprintln (map length bits);
c@19 118
c@19 119 rows = octaves * kdata.binsPerOctave;
c@19 120 columns = (pow 2 (octaves - 1)) * kdata.atomsPerFrame;
c@19 121
c@18 122 cm.generate do row col:
c@19 123
c@19 124 // bits structure: [1,2,4,8,...]
c@19 125
c@19 126 // each elt of bits is a list of the chunks that should
c@19 127 // make up this block in that octave (lowest octave first)
c@19 128
c@19 129 // each chunk has atomsPerFrame * binsPerOctave elts in it
c@19 130
c@19 131 // row is disposed with 0 at the top, highest octave (in
c@19 132 // both pitch and index into bits structure)
c@19 133
c@18 134 oct = int (row / binsPerOctave);
c@19 135 binNo = row % kdata.binsPerOctave;
c@21 136
c@19 137 chunks = pow 2 oct;
c@21 138 colsPerAtom = int (columns / (chunks * kdata.atomsPerFrame));
c@21 139 atomNo = int (col / colsPerAtom);
c@21 140 atomOffset = col % colsPerAtom;
c@18 141
c@40 142 if atomOffset == 0 and atomNo < length bits[oct] then
c@21 143 bits[oct][atomNo][binNo];
c@20 144 else
c@20 145 cplx.zero
c@20 146 fi;
c@19 147
c@19 148 done { rows, columns };
c@19 149 );
c@15 150
c@17 151 processOctaveLists octs =
c@17 152 case octs[0] of
c@17 153 block::rest:
c@19 154 (toAssemble = array
c@19 155 (map do oct:
c@21 156 n = kdata.atomsPerFrame * pow 2 oct;
c@17 157 if not empty? octs[oct] then
c@19 158 forBlock = array (take n octs[oct]);
c@17 159 octs[oct] := drop n octs[oct];
c@17 160 forBlock
c@17 161 else
c@19 162 array []
c@17 163 fi
c@19 164 done (keys octs));
c@17 165 assembleBlock toAssemble :. \(processOctaveLists octs));
c@17 166 _: []
c@15 167 esac;
c@15 168
c@19 169 eprintln "cqblocks has \(length cqblocks) entries";
c@15 170
c@17 171 octaveLists = [:];
c@19 172
c@19 173 cqblocks = array cqblocks;
c@17 174 for [1..octaves] do oct:
c@17 175 octaveLists[octaves - oct] := cqblocks[oct-1];
c@17 176 done;
c@17 177 /*
c@17 178 \() (map2 do octlist octave:
c@17 179 println "oct \(octaves) - \(octave) = \(octaves - octave)";
c@17 180 octaveLists[octaves - octave] := octlist
c@17 181 done cqblocks [1..octaves]);
c@17 182 */
c@19 183 eprintln "octaveLists keys are: \(keys octaveLists)";
c@15 184
c@40 185 {
c@40 186 kernel = kdata with {
c@40 187 binFrequencies = array (concat
c@40 188 (map do octave:
c@40 189 map do freq:
c@40 190 freq / (pow 2 octave);
c@40 191 done (reverse (list kdata.binFrequencies))
c@40 192 done [0..octaves-1]))
c@40 193 },
c@40 194 output = processOctaveLists octaveLists
c@40 195 }
c@10 196 );
c@10 197
c@37 198 { cqt }
c@10 199