c@116
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
c@116
|
2 /*
|
c@116
|
3 Constant-Q library
|
c@116
|
4 Copyright (c) 2013-2014 Queen Mary, University of London
|
c@116
|
5
|
c@116
|
6 Permission is hereby granted, free of charge, to any person
|
c@116
|
7 obtaining a copy of this software and associated documentation
|
c@116
|
8 files (the "Software"), to deal in the Software without
|
c@116
|
9 restriction, including without limitation the rights to use, copy,
|
c@116
|
10 modify, merge, publish, distribute, sublicense, and/or sell copies
|
c@116
|
11 of the Software, and to permit persons to whom the Software is
|
c@116
|
12 furnished to do so, subject to the following conditions:
|
c@116
|
13
|
c@116
|
14 The above copyright notice and this permission notice shall be
|
c@116
|
15 included in all copies or substantial portions of the Software.
|
c@116
|
16
|
c@116
|
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
c@116
|
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
c@116
|
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
c@116
|
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
c@116
|
21 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
c@116
|
22 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
c@116
|
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
c@116
|
24
|
c@116
|
25 Except as contained in this notice, the names of the Centre for
|
c@116
|
26 Digital Music; Queen Mary, University of London; and Chris Cannam
|
c@116
|
27 shall not be used in advertising or otherwise to promote the sale,
|
c@116
|
28 use or other dealings in this Software without prior written
|
c@116
|
29 authorization.
|
c@116
|
30 */
|
c@116
|
31
|
c@116
|
32 #include "ConstantQ.h"
|
c@116
|
33
|
c@116
|
34 #include "CQKernel.h"
|
c@116
|
35
|
c@121
|
36 #include "dsp/Resampler.h"
|
c@121
|
37 #include "dsp/MathUtilities.h"
|
c@121
|
38 #include "dsp/FFT.h"
|
c@116
|
39
|
c@116
|
40 #include <algorithm>
|
c@116
|
41 #include <iostream>
|
c@116
|
42 #include <stdexcept>
|
c@116
|
43
|
c@116
|
44 using std::vector;
|
c@116
|
45 using std::cerr;
|
c@116
|
46 using std::endl;
|
c@116
|
47
|
c@116
|
48 //#define DEBUG_CQ 1
|
c@116
|
49
|
c@127
|
50 ConstantQ::ConstantQ(CQParameters params) :
|
c@127
|
51 m_inparams(params),
|
c@127
|
52 m_sampleRate(params.sampleRate),
|
c@127
|
53 m_maxFrequency(params.maxFrequency),
|
c@127
|
54 m_minFrequency(params.minFrequency),
|
c@127
|
55 m_binsPerOctave(params.binsPerOctave),
|
c@116
|
56 m_fft(0)
|
c@116
|
57 {
|
c@127
|
58 if (m_minFrequency <= 0.0 || m_maxFrequency <= 0.0) {
|
c@116
|
59 throw std::invalid_argument("Frequency extents must be positive");
|
c@116
|
60 }
|
c@116
|
61
|
c@116
|
62 initialise();
|
c@116
|
63 }
|
c@116
|
64
|
c@116
|
65 ConstantQ::~ConstantQ()
|
c@116
|
66 {
|
c@116
|
67 delete m_fft;
|
c@116
|
68 for (int i = 0; i < (int)m_decimators.size(); ++i) {
|
c@116
|
69 delete m_decimators[i];
|
c@116
|
70 }
|
c@116
|
71 delete m_kernel;
|
c@116
|
72 }
|
c@116
|
73
|
c@116
|
74 double
|
c@116
|
75 ConstantQ::getMinFrequency() const
|
c@116
|
76 {
|
c@116
|
77 return m_p.minFrequency / pow(2.0, m_octaves - 1);
|
c@116
|
78 }
|
c@116
|
79
|
c@116
|
80 double
|
c@145
|
81 ConstantQ::getBinFrequency(double bin) const
|
c@116
|
82 {
|
c@137
|
83 // our bins are returned in high->low order
|
c@137
|
84 bin = (getBinsPerOctave() * getOctaves()) - bin - 1;
|
c@145
|
85 return getMinFrequency() * pow(2, (bin / getBinsPerOctave()));
|
c@116
|
86 }
|
c@116
|
87
|
c@116
|
88 void
|
c@116
|
89 ConstantQ::initialise()
|
c@116
|
90 {
|
c@116
|
91 m_octaves = int(ceil(log2(m_maxFrequency / m_minFrequency)));
|
c@127
|
92 m_kernel = new CQKernel(m_inparams);
|
c@116
|
93 m_p = m_kernel->getProperties();
|
c@116
|
94
|
c@147
|
95 if (!m_kernel->isValid()) {
|
c@147
|
96 return;
|
c@147
|
97 }
|
c@147
|
98
|
c@116
|
99 // Use exact powers of two for resampling rates. They don't have
|
c@116
|
100 // to be related to our actual samplerate: the resampler only
|
c@116
|
101 // cares about the ratio, but it only accepts integer source and
|
c@116
|
102 // target rates, and if we start from the actual samplerate we
|
c@116
|
103 // risk getting non-integer rates for lower octaves
|
c@116
|
104
|
c@116
|
105 int sourceRate = pow(2, m_octaves);
|
c@116
|
106 vector<int> latencies;
|
c@116
|
107
|
c@116
|
108 // top octave, no resampling
|
c@116
|
109 latencies.push_back(0);
|
c@116
|
110 m_decimators.push_back(0);
|
c@116
|
111
|
c@116
|
112 for (int i = 1; i < m_octaves; ++i) {
|
c@116
|
113
|
c@116
|
114 int factor = pow(2, i);
|
c@116
|
115
|
c@116
|
116 Resampler *r = new Resampler
|
c@116
|
117 (sourceRate, sourceRate / factor, 50, 0.05);
|
c@116
|
118
|
c@116
|
119 #ifdef DEBUG_CQ
|
c@116
|
120 cerr << "forward: octave " << i << ": resample from " << sourceRate << " to " << sourceRate / factor << endl;
|
c@116
|
121 #endif
|
c@116
|
122
|
c@116
|
123 // We need to adapt the latencies so as to get the first input
|
c@116
|
124 // sample to be aligned, in time, at the decimator output
|
c@116
|
125 // across all octaves.
|
c@116
|
126 //
|
c@116
|
127 // Our decimator uses a linear phase filter, but being causal
|
c@116
|
128 // it is not zero phase: it has a latency that depends on the
|
c@116
|
129 // decimation factor. Those latencies have been calculated
|
c@116
|
130 // per-octave and are available to us in the latencies
|
c@116
|
131 // array. Left to its own devices, the first input sample will
|
c@116
|
132 // appear at output sample 0 in the highest octave (where no
|
c@116
|
133 // decimation is needed), sample number latencies[1] in the
|
c@116
|
134 // next octave down, latencies[2] in the next one, etc. We get
|
c@116
|
135 // to apply some artificial per-octave latency after the
|
c@116
|
136 // decimator in the processing chain, in order to compensate
|
c@116
|
137 // for the differing latencies associated with different
|
c@116
|
138 // decimation factors. How much should we insert?
|
c@116
|
139 //
|
c@116
|
140 // The outputs of the decimators are at different rates (in
|
c@116
|
141 // terms of the relation between clock time and samples) and
|
c@116
|
142 // we want them aligned in terms of time. So, for example, a
|
c@116
|
143 // latency of 10 samples with a decimation factor of 2 is
|
c@116
|
144 // equivalent to a latency of 20 with no decimation -- they
|
c@116
|
145 // both result in the first output sample happening at the
|
c@116
|
146 // same equivalent time in milliseconds.
|
c@116
|
147 //
|
c@116
|
148 // So here we record the latency added by the decimator, in
|
c@116
|
149 // terms of the sample rate of the undecimated signal. Then we
|
c@116
|
150 // use that to compensate in a moment, when we've discovered
|
c@116
|
151 // what the longest latency across all octaves is.
|
c@116
|
152
|
c@116
|
153 latencies.push_back(r->getLatency() * factor);
|
c@116
|
154 m_decimators.push_back(r);
|
c@116
|
155 }
|
c@116
|
156
|
c@116
|
157 m_bigBlockSize = m_p.fftSize * pow(2, m_octaves - 1);
|
c@116
|
158
|
c@116
|
159 // Now add in the extra padding and compensate for hops that must
|
c@116
|
160 // be dropped in order to align the atom centres across
|
c@116
|
161 // octaves. Again this is a bit trickier because we are doing it
|
c@116
|
162 // at input rather than output and so must work in per-octave
|
c@116
|
163 // sample rates rather than output blocks
|
c@116
|
164
|
c@116
|
165 int emptyHops = m_p.firstCentre / m_p.atomSpacing;
|
c@116
|
166
|
c@116
|
167 vector<int> drops;
|
c@116
|
168 for (int i = 0; i < m_octaves; ++i) {
|
c@116
|
169 int factor = pow(2, i);
|
c@116
|
170 int dropHops = emptyHops * pow(2, m_octaves - i - 1) - emptyHops;
|
c@116
|
171 int drop = ((dropHops * m_p.fftHop) * factor) / m_p.atomsPerFrame;
|
c@116
|
172 drops.push_back(drop);
|
c@116
|
173 }
|
c@116
|
174
|
c@116
|
175 int maxLatPlusDrop = 0;
|
c@116
|
176 for (int i = 0; i < m_octaves; ++i) {
|
c@116
|
177 int latPlusDrop = latencies[i] + drops[i];
|
c@116
|
178 if (latPlusDrop > maxLatPlusDrop) maxLatPlusDrop = latPlusDrop;
|
c@116
|
179 }
|
c@116
|
180
|
c@116
|
181 int totalLatency = maxLatPlusDrop;
|
c@116
|
182
|
c@116
|
183 int lat0 = totalLatency - latencies[0] - drops[0];
|
c@116
|
184 totalLatency = ceil(double(lat0 / m_p.fftHop) * m_p.fftHop)
|
c@116
|
185 + latencies[0] + drops[0];
|
c@116
|
186
|
c@116
|
187 // We want (totalLatency - latencies[i]) to be a multiple of 2^i
|
c@116
|
188 // for each octave i, so that we do not end up with fractional
|
c@116
|
189 // octave latencies below. In theory this is hard, in practice if
|
c@116
|
190 // we ensure it for the last octave we should be OK.
|
c@116
|
191 double finalOctLat = latencies[m_octaves-1];
|
c@116
|
192 double finalOctFact = pow(2, m_octaves-1);
|
c@116
|
193 totalLatency =
|
c@116
|
194 int(round(finalOctLat +
|
c@116
|
195 finalOctFact *
|
c@116
|
196 ceil((totalLatency - finalOctLat) / finalOctFact)));
|
c@116
|
197
|
c@116
|
198 #ifdef DEBUG_CQ
|
c@116
|
199 cerr << "total latency = " << totalLatency << endl;
|
c@116
|
200 #endif
|
c@116
|
201
|
c@116
|
202 // Padding as in the reference (will be introduced with the
|
c@116
|
203 // latency compensation in the loop below)
|
c@116
|
204 m_outputLatency = totalLatency + m_bigBlockSize
|
c@116
|
205 - m_p.firstCentre * pow(2, m_octaves-1);
|
c@116
|
206
|
c@116
|
207 #ifdef DEBUG_CQ
|
c@116
|
208 cerr << "m_bigBlockSize = " << m_bigBlockSize << ", firstCentre = "
|
c@116
|
209 << m_p.firstCentre << ", m_octaves = " << m_octaves
|
c@116
|
210 << ", so m_outputLatency = " << m_outputLatency << endl;
|
c@116
|
211 #endif
|
c@116
|
212
|
c@116
|
213 for (int i = 0; i < m_octaves; ++i) {
|
c@116
|
214
|
c@116
|
215 double factor = pow(2, i);
|
c@116
|
216
|
c@116
|
217 // Calculate the difference between the total latency applied
|
c@116
|
218 // across all octaves, and the existing latency due to the
|
c@116
|
219 // decimator for this octave, and then convert it back into
|
c@116
|
220 // the sample rate appropriate for the output latency of this
|
c@116
|
221 // decimator -- including one additional big block of padding
|
c@116
|
222 // (as in the reference).
|
c@116
|
223
|
c@116
|
224 double octaveLatency =
|
c@116
|
225 double(totalLatency - latencies[i] - drops[i]
|
c@116
|
226 + m_bigBlockSize) / factor;
|
c@116
|
227
|
c@116
|
228 #ifdef DEBUG_CQ
|
c@116
|
229 cerr << "octave " << i << ": resampler latency = " << latencies[i]
|
c@116
|
230 << ", drop " << drops[i] << " (/factor = " << drops[i]/factor
|
c@116
|
231 << "), octaveLatency = " << octaveLatency << " -> "
|
c@116
|
232 << int(round(octaveLatency)) << " (diff * factor = "
|
c@116
|
233 << (octaveLatency - round(octaveLatency)) << " * "
|
c@116
|
234 << factor << " = "
|
c@116
|
235 << (octaveLatency - round(octaveLatency)) * factor << ")" << endl;
|
c@116
|
236
|
c@116
|
237 cerr << "double(" << totalLatency << " - "
|
c@116
|
238 << latencies[i] << " - " << drops[i] << " + "
|
c@116
|
239 << m_bigBlockSize << ") / " << factor << " = "
|
c@116
|
240 << octaveLatency << endl;
|
c@116
|
241 #endif
|
c@116
|
242
|
c@116
|
243 m_buffers.push_back
|
c@116
|
244 (RealSequence(int(round(octaveLatency)), 0.0));
|
c@116
|
245 }
|
c@116
|
246
|
c@116
|
247 m_fft = new FFTReal(m_p.fftSize);
|
c@116
|
248 }
|
c@116
|
249
|
c@116
|
250 ConstantQ::ComplexBlock
|
c@116
|
251 ConstantQ::process(const RealSequence &td)
|
c@116
|
252 {
|
c@116
|
253 m_buffers[0].insert(m_buffers[0].end(), td.begin(), td.end());
|
c@116
|
254
|
c@116
|
255 for (int i = 1; i < m_octaves; ++i) {
|
c@116
|
256 RealSequence dec = m_decimators[i]->process(td.data(), td.size());
|
c@116
|
257 m_buffers[i].insert(m_buffers[i].end(), dec.begin(), dec.end());
|
c@116
|
258 }
|
c@116
|
259
|
c@116
|
260 ComplexBlock out;
|
c@116
|
261
|
c@116
|
262 while (true) {
|
c@116
|
263
|
c@116
|
264 // We could have quite different remaining sample counts in
|
c@116
|
265 // different octaves, because (apart from the predictable
|
c@116
|
266 // added counts for decimator output on each block) we also
|
c@116
|
267 // have variable additional latency per octave
|
c@116
|
268 bool enough = true;
|
c@116
|
269 for (int i = 0; i < m_octaves; ++i) {
|
c@116
|
270 int required = m_p.fftSize * pow(2, m_octaves - i - 1);
|
c@116
|
271 if ((int)m_buffers[i].size() < required) {
|
c@116
|
272 enough = false;
|
c@116
|
273 }
|
c@116
|
274 }
|
c@116
|
275 if (!enough) break;
|
c@116
|
276
|
c@116
|
277 int base = out.size();
|
c@116
|
278 int totalColumns = pow(2, m_octaves - 1) * m_p.atomsPerFrame;
|
c@116
|
279 for (int i = 0; i < totalColumns; ++i) {
|
c@116
|
280 out.push_back(ComplexColumn());
|
c@116
|
281 }
|
c@116
|
282
|
c@116
|
283 for (int octave = 0; octave < m_octaves; ++octave) {
|
c@116
|
284
|
c@116
|
285 int blocksThisOctave = pow(2, (m_octaves - octave - 1));
|
c@116
|
286
|
c@116
|
287 for (int b = 0; b < blocksThisOctave; ++b) {
|
c@116
|
288 ComplexBlock block = processOctaveBlock(octave);
|
c@116
|
289
|
c@116
|
290 for (int j = 0; j < m_p.atomsPerFrame; ++j) {
|
c@116
|
291
|
c@116
|
292 int target = base +
|
c@116
|
293 (b * (totalColumns / blocksThisOctave) +
|
c@116
|
294 (j * ((totalColumns / blocksThisOctave) /
|
c@116
|
295 m_p.atomsPerFrame)));
|
c@116
|
296
|
c@116
|
297 while (int(out[target].size()) <
|
c@116
|
298 m_p.binsPerOctave * (octave + 1)) {
|
c@116
|
299 out[target].push_back(Complex());
|
c@116
|
300 }
|
c@116
|
301
|
c@116
|
302 for (int i = 0; i < m_p.binsPerOctave; ++i) {
|
c@116
|
303 out[target][m_p.binsPerOctave * octave + i] =
|
c@116
|
304 block[j][m_p.binsPerOctave - i - 1];
|
c@116
|
305 }
|
c@116
|
306 }
|
c@116
|
307 }
|
c@116
|
308 }
|
c@116
|
309 }
|
c@116
|
310
|
c@116
|
311 return out;
|
c@116
|
312 }
|
c@116
|
313
|
c@116
|
314 ConstantQ::ComplexBlock
|
c@116
|
315 ConstantQ::getRemainingOutput()
|
c@116
|
316 {
|
c@116
|
317 // Same as padding added at start, though rounded up
|
c@116
|
318 int pad = ceil(double(m_outputLatency) / m_bigBlockSize) * m_bigBlockSize;
|
c@116
|
319 RealSequence zeros(pad, 0.0);
|
c@116
|
320 return process(zeros);
|
c@116
|
321 }
|
c@116
|
322
|
c@116
|
323 ConstantQ::ComplexBlock
|
c@116
|
324 ConstantQ::processOctaveBlock(int octave)
|
c@116
|
325 {
|
c@116
|
326 RealSequence ro(m_p.fftSize, 0.0);
|
c@116
|
327 RealSequence io(m_p.fftSize, 0.0);
|
c@116
|
328
|
c@116
|
329 m_fft->forward(m_buffers[octave].data(), ro.data(), io.data());
|
c@116
|
330
|
c@116
|
331 RealSequence shifted;
|
c@116
|
332 shifted.insert(shifted.end(),
|
c@116
|
333 m_buffers[octave].begin() + m_p.fftHop,
|
c@116
|
334 m_buffers[octave].end());
|
c@116
|
335 m_buffers[octave] = shifted;
|
c@116
|
336
|
c@116
|
337 ComplexSequence cv;
|
c@116
|
338 for (int i = 0; i < m_p.fftSize; ++i) {
|
c@116
|
339 cv.push_back(Complex(ro[i], io[i]));
|
c@116
|
340 }
|
c@116
|
341
|
c@116
|
342 ComplexSequence cqrowvec = m_kernel->processForward(cv);
|
c@116
|
343
|
c@116
|
344 // Reform into a column matrix
|
c@116
|
345 ComplexBlock cqblock;
|
c@116
|
346 for (int j = 0; j < m_p.atomsPerFrame; ++j) {
|
c@116
|
347 cqblock.push_back(ComplexColumn());
|
c@116
|
348 for (int i = 0; i < m_p.binsPerOctave; ++i) {
|
c@116
|
349 cqblock[j].push_back(cqrowvec[i * m_p.atomsPerFrame + j]);
|
c@116
|
350 }
|
c@116
|
351 }
|
c@116
|
352
|
c@116
|
353 return cqblock;
|
c@116
|
354 }
|
c@116
|
355
|
c@116
|
356
|