annotate src/CQInverse.cpp @ 132:c188cade44f8

Tests (not quite correct yet)
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 19 May 2014 12:03:04 +0100
parents 8996465e39fc
children ff8ae033615f
rev   line source
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 "CQInverse.h"
c@116 33
c@121 34 #include "dsp/Resampler.h"
c@121 35 #include "dsp/MathUtilities.h"
c@121 36 #include "dsp/FFT.h"
c@116 37
c@116 38 #include <algorithm>
c@116 39 #include <iostream>
c@116 40 #include <stdexcept>
c@116 41
c@116 42 using std::vector;
c@116 43 using std::cerr;
c@116 44 using std::endl;
c@116 45
c@116 46 //#define DEBUG_CQ 1
c@116 47
c@127 48 CQInverse::CQInverse(CQParameters params) :
c@127 49 m_inparams(params),
c@127 50 m_sampleRate(params.sampleRate),
c@127 51 m_maxFrequency(params.maxFrequency),
c@127 52 m_minFrequency(params.minFrequency),
c@127 53 m_binsPerOctave(params.binsPerOctave),
c@116 54 m_fft(0)
c@116 55 {
c@127 56 if (m_minFrequency <= 0.0 || m_maxFrequency <= 0.0) {
c@116 57 throw std::invalid_argument("Frequency extents must be positive");
c@116 58 }
c@116 59
c@116 60 initialise();
c@116 61 }
c@116 62
c@116 63 CQInverse::~CQInverse()
c@116 64 {
c@116 65 delete m_fft;
c@116 66 for (int i = 0; i < (int)m_upsamplers.size(); ++i) {
c@116 67 delete m_upsamplers[i];
c@116 68 }
c@116 69 delete m_kernel;
c@116 70 }
c@116 71
c@116 72 double
c@116 73 CQInverse::getMinFrequency() const
c@116 74 {
c@116 75 return m_p.minFrequency / pow(2.0, m_octaves - 1);
c@116 76 }
c@116 77
c@116 78 double
c@116 79 CQInverse::getBinFrequency(int bin) const
c@116 80 {
c@116 81 return getMinFrequency() * pow(2, (double(bin) / getBinsPerOctave()));
c@116 82 }
c@116 83
c@116 84 void
c@116 85 CQInverse::initialise()
c@116 86 {
c@116 87 m_octaves = int(ceil(log2(m_maxFrequency / m_minFrequency)));
c@127 88 m_kernel = new CQKernel(m_inparams);
c@116 89 m_p = m_kernel->getProperties();
c@116 90
c@116 91 // Use exact powers of two for resampling rates. They don't have
c@116 92 // to be related to our actual samplerate: the resampler only
c@116 93 // cares about the ratio, but it only accepts integer source and
c@116 94 // target rates, and if we start from the actual samplerate we
c@116 95 // risk getting non-integer rates for lower octaves
c@116 96
c@116 97 int sourceRate = pow(2, m_octaves);
c@116 98 vector<int> latencies;
c@116 99
c@116 100 // top octave, no resampling
c@116 101 latencies.push_back(0);
c@116 102 m_upsamplers.push_back(0);
c@116 103
c@116 104 for (int i = 1; i < m_octaves; ++i) {
c@116 105
c@116 106 int factor = pow(2, i);
c@116 107
c@116 108 Resampler *r = new Resampler
c@116 109 (sourceRate / factor, sourceRate, 50, 0.05);
c@116 110
c@116 111 #ifdef DEBUG_CQ
c@116 112 cerr << "inverse: octave " << i << ": resample from " << sourceRate/factor << " to " << sourceRate << endl;
c@116 113 #endif
c@116 114
c@116 115 // See ConstantQ.cpp for discussion on latency -- output
c@116 116 // latency here is at target rate which, this way around, is
c@116 117 // what we want
c@116 118
c@116 119 latencies.push_back(r->getLatency());
c@116 120 m_upsamplers.push_back(r);
c@116 121 }
c@116 122
c@116 123 // additionally we will have fftHop latency at individual octave
c@116 124 // rate (before upsampling) for the overlap-add in each octave
c@116 125 for (int i = 0; i < m_octaves; ++i) {
c@116 126 latencies[i] += m_p.fftHop * pow(2, i);
c@116 127 }
c@116 128
c@116 129 // Now reverse the drop adjustment made in ConstantQ to align the
c@116 130 // atom centres across different octaves (but this time at output
c@116 131 // sample rate)
c@116 132
c@116 133 int emptyHops = m_p.firstCentre / m_p.atomSpacing;
c@116 134
c@116 135 vector<int> pushes;
c@116 136 for (int i = 0; i < m_octaves; ++i) {
c@116 137 int factor = pow(2, i);
c@116 138 int pushHops = emptyHops * pow(2, m_octaves - i - 1) - emptyHops;
c@116 139 int push = ((pushHops * m_p.fftHop) * factor) / m_p.atomsPerFrame;
c@116 140 pushes.push_back(push);
c@116 141 }
c@116 142
c@116 143 int maxLatLessPush = 0;
c@116 144 for (int i = 0; i < m_octaves; ++i) {
c@116 145 int latLessPush = latencies[i] - pushes[i];
c@116 146 if (latLessPush > maxLatLessPush) maxLatLessPush = latLessPush;
c@116 147 }
c@116 148
c@116 149 int totalLatency = maxLatLessPush + 10;
c@116 150 if (totalLatency < 0) totalLatency = 0;
c@116 151
c@116 152 m_outputLatency = totalLatency + m_p.firstCentre * pow(2, m_octaves-1);
c@116 153
c@116 154 #ifdef DEBUG_CQ
c@116 155 cerr << "totalLatency = " << totalLatency << ", m_outputLatency = " << m_outputLatency << endl;
c@116 156 #endif
c@116 157
c@116 158 for (int i = 0; i < m_octaves; ++i) {
c@116 159
c@116 160 // Calculate the difference between the total latency applied
c@116 161 // across all octaves, and the existing latency due to the
c@116 162 // upsampler for this octave.
c@116 163
c@116 164 int latencyPadding = totalLatency - latencies[i] + pushes[i];
c@116 165
c@116 166 #ifdef DEBUG_CQ
c@116 167 cerr << "octave " << i << ": push " << pushes[i] << ", resampler latency inc overlap space " << latencies[i] << ", latencyPadding = " << latencyPadding << " (/factor = " << latencyPadding / pow(2, i) << ")" << endl;
c@116 168 #endif
c@116 169
c@116 170 m_buffers.push_back(RealSequence(latencyPadding, 0.0));
c@116 171 }
c@116 172
c@116 173 for (int i = 0; i < m_octaves; ++i) {
c@116 174 // Fixed-size buffer for IFFT overlap-add
c@116 175 m_olaBufs.push_back(RealSequence(m_p.fftSize, 0.0));
c@116 176 }
c@116 177
c@116 178 m_fft = new FFTReal(m_p.fftSize);
c@116 179 }
c@116 180
c@116 181 CQInverse::RealSequence
c@116 182 CQInverse::process(const ComplexBlock &block)
c@116 183 {
c@116 184 // The input data is of the form produced by ConstantQ::process --
c@116 185 // an unknown number N of columns of varying height. We assert
c@116 186 // that N is a multiple of atomsPerFrame * 2^(octaves-1), as must
c@116 187 // be the case for data that came directly from our ConstantQ
c@116 188 // implementation.
c@116 189
c@116 190 int widthProvided = block.size();
c@116 191
c@116 192 if (widthProvided == 0) {
c@116 193 return drawFromBuffers();
c@116 194 }
c@116 195
c@116 196 int blockWidth = m_p.atomsPerFrame * int(pow(2, m_octaves - 1));
c@116 197
c@116 198 if (widthProvided % blockWidth != 0) {
c@116 199 cerr << "ERROR: CQInverse::process: Input block size ("
c@116 200 << widthProvided
c@116 201 << ") must be a multiple of processing block width "
c@116 202 << "(atoms-per-frame * 2^(octaves-1) = "
c@116 203 << m_p.atomsPerFrame << " * 2^(" << m_octaves << "-1) = "
c@116 204 << blockWidth << ")" << endl;
c@116 205 throw std::invalid_argument
c@116 206 ("Input block size must be a multiple of processing block width");
c@116 207 }
c@116 208
c@116 209 // Procedure:
c@116 210 //
c@116 211 // 1. Slice the list of columns into a set of lists of columns,
c@116 212 // one per octave, each of width N / (2^octave-1) and height
c@116 213 // binsPerOctave, containing the values present in that octave
c@116 214 //
c@116 215 // 2. Group each octave list by atomsPerFrame columns at a time,
c@116 216 // and stack these so as to achieve a list, for each octave, of
c@116 217 // taller columns of height binsPerOctave * atomsPerFrame
c@116 218 //
c@116 219 // 3. For each taller column, take the product with the inverse CQ
c@116 220 // kernel (which is the conjugate of the forward kernel) and
c@116 221 // perform an inverse FFT
c@116 222 //
c@116 223 // 4. Overlap-add each octave's resynthesised blocks (unwindowed)
c@116 224 //
c@116 225 // 5. Resample each octave's overlap-add stream to the original
c@116 226 // rate
c@116 227 //
c@116 228 // 6. Sum the resampled streams and return
c@116 229
c@116 230 for (int i = 0; i < m_octaves; ++i) {
c@116 231
c@116 232 // Step 1
c@116 233
c@116 234 ComplexBlock oct;
c@116 235
c@116 236 for (int j = 0; j < widthProvided; ++j) {
c@116 237 int h = block[j].size();
c@116 238 if (h < m_binsPerOctave * (i+1)) {
c@116 239 continue;
c@116 240 }
c@116 241 ComplexColumn col(block[j].begin() + m_binsPerOctave * i,
c@116 242 block[j].begin() + m_binsPerOctave * (i+1));
c@116 243 oct.push_back(col);
c@116 244 }
c@116 245
c@116 246 // Steps 2, 3, 4, 5
c@116 247 processOctave(i, oct);
c@116 248 }
c@116 249
c@116 250 // Step 6
c@116 251 return drawFromBuffers();
c@116 252 }
c@116 253
c@116 254 CQInverse::RealSequence
c@116 255 CQInverse::drawFromBuffers()
c@116 256 {
c@116 257 // 6. Sum the resampled streams and return
c@116 258
c@116 259 int available = 0;
c@116 260
c@116 261 for (int i = 0; i < m_octaves; ++i) {
c@116 262 if (i == 0 || int(m_buffers[i].size()) < available) {
c@116 263 available = m_buffers[i].size();
c@116 264 }
c@116 265 }
c@116 266
c@116 267 RealSequence result(available, 0);
c@116 268
c@116 269 if (available == 0) {
c@116 270 return result;
c@116 271 }
c@116 272
c@116 273 for (int i = 0; i < m_octaves; ++i) {
c@116 274 for (int j = 0; j < available; ++j) {
c@116 275 result[j] += m_buffers[i][j];
c@116 276 }
c@116 277 m_buffers[i] = RealSequence(m_buffers[i].begin() + available,
c@116 278 m_buffers[i].end());
c@116 279 }
c@116 280
c@116 281 return result;
c@116 282 }
c@116 283
c@116 284 CQInverse::RealSequence
c@116 285 CQInverse::getRemainingOutput()
c@116 286 {
c@116 287 for (int j = 0; j < m_octaves; ++j) {
c@116 288 int factor = pow(2, j);
c@116 289 int latency = (j > 0 ? m_upsamplers[j]->getLatency() : 0) / factor;
c@116 290 for (int i = 0; i < (latency + m_p.fftSize) / m_p.fftHop; ++i) {
c@116 291 overlapAddAndResample(j, RealSequence(m_olaBufs[j].size(), 0));
c@116 292 }
c@116 293 }
c@116 294
c@116 295 return drawFromBuffers();
c@116 296 }
c@116 297
c@116 298 void
c@116 299 CQInverse::processOctave(int octave, const ComplexBlock &columns)
c@116 300 {
c@116 301 // 2. Group each octave list by atomsPerFrame columns at a time,
c@116 302 // and stack these so as to achieve a list, for each octave, of
c@116 303 // taller columns of height binsPerOctave * atomsPerFrame
c@116 304
c@116 305 int ncols = columns.size();
c@116 306
c@116 307 if (ncols % m_p.atomsPerFrame != 0) {
c@116 308 cerr << "ERROR: CQInverse::process: Number of columns ("
c@116 309 << ncols
c@116 310 << ") in octave " << octave
c@116 311 << " must be a multiple of atoms-per-frame ("
c@116 312 << m_p.atomsPerFrame << ")" << endl;
c@116 313 throw std::invalid_argument
c@116 314 ("Columns in octave must be a multiple of atoms per frame");
c@116 315 }
c@116 316
c@116 317 for (int i = 0; i < ncols; i += m_p.atomsPerFrame) {
c@116 318
c@116 319 ComplexColumn tallcol;
c@116 320 for (int b = 0; b < m_binsPerOctave; ++b) {
c@116 321 for (int a = 0; a < m_p.atomsPerFrame; ++a) {
c@116 322 tallcol.push_back(columns[i + a][m_binsPerOctave - b - 1]);
c@116 323 }
c@116 324 }
c@116 325
c@116 326 processOctaveColumn(octave, tallcol);
c@116 327 }
c@116 328 }
c@116 329
c@116 330 void
c@116 331 CQInverse::processOctaveColumn(int octave, const ComplexColumn &column)
c@116 332 {
c@116 333 // 3. For each taller column, take the product with the inverse CQ
c@116 334 // kernel (which is the conjugate of the forward kernel) and
c@116 335 // perform an inverse FFT
c@116 336
c@116 337 if ((int)column.size() != m_p.atomsPerFrame * m_binsPerOctave) {
c@116 338 cerr << "ERROR: CQInverse::processOctaveColumn: Height of column ("
c@116 339 << column.size() << ") in octave " << octave
c@116 340 << " must be atoms-per-frame * bins-per-octave ("
c@116 341 << m_p.atomsPerFrame << " * " << m_binsPerOctave << " = "
c@116 342 << m_p.atomsPerFrame * m_binsPerOctave << ")" << endl;
c@116 343 throw std::invalid_argument
c@116 344 ("Column height must match atoms-per-frame * bins-per-octave");
c@116 345 }
c@116 346
c@116 347 ComplexSequence transformed = m_kernel->processInverse(column);
c@116 348
c@116 349 int halfLen = m_p.fftSize/2 + 1;
c@116 350
c@116 351 RealSequence ri(halfLen, 0);
c@116 352 RealSequence ii(halfLen, 0);
c@116 353
c@116 354 for (int i = 0; i < halfLen; ++i) {
c@116 355 ri[i] = transformed[i].real();
c@116 356 ii[i] = transformed[i].imag();
c@116 357 }
c@116 358
c@116 359 RealSequence timeDomain(m_p.fftSize, 0);
c@116 360
c@116 361 m_fft->inverse(ri.data(), ii.data(), timeDomain.data());
c@116 362
c@116 363 overlapAddAndResample(octave, timeDomain);
c@116 364 }
c@116 365
c@116 366 void
c@116 367 CQInverse::overlapAddAndResample(int octave, const RealSequence &seq)
c@116 368 {
c@116 369 // 4. Overlap-add each octave's resynthesised blocks (unwindowed)
c@116 370 //
c@116 371 // and
c@116 372 //
c@116 373 // 5. Resample each octave's overlap-add stream to the original
c@116 374 // rate
c@116 375
c@116 376 if (seq.size() != m_olaBufs[octave].size()) {
c@116 377 cerr << "ERROR: CQInverse::overlapAdd: input sequence length ("
c@116 378 << seq.size() << ") is expected to match OLA buffer size ("
c@116 379 << m_olaBufs[octave].size() << ")" << endl;
c@116 380 throw std::invalid_argument
c@116 381 ("Input sequence length should match OLA buffer size");
c@116 382 }
c@116 383
c@116 384 RealSequence toResample(m_olaBufs[octave].begin(),
c@116 385 m_olaBufs[octave].begin() + m_p.fftHop);
c@116 386
c@116 387 RealSequence resampled =
c@116 388 octave > 0 ?
c@116 389 m_upsamplers[octave]->process(toResample.data(), toResample.size()) :
c@116 390 toResample;
c@116 391
c@116 392 m_buffers[octave].insert(m_buffers[octave].end(),
c@116 393 resampled.begin(),
c@116 394 resampled.end());
c@116 395
c@116 396 m_olaBufs[octave] = RealSequence(m_olaBufs[octave].begin() + m_p.fftHop,
c@116 397 m_olaBufs[octave].end());
c@116 398
c@116 399 RealSequence pad(m_p.fftHop, 0);
c@116 400
c@116 401 m_olaBufs[octave].insert(m_olaBufs[octave].end(),
c@116 402 pad.begin(),
c@116 403 pad.end());
c@116 404
c@116 405 for (int i = 0; i < m_p.fftSize; ++i) {
c@116 406 m_olaBufs[octave][i] += seq[i];
c@116 407 }
c@116 408 }
c@116 409