annotate src/CQInverse.cpp @ 162:7c444fea4338

Added tag v1.0 for changeset 361b4f8b7b2d
author Chris Cannam <c.cannam@qmul.ac.uk>
date Thu, 07 Aug 2014 19:19:21 +0100
parents 242bf7bc38ce
children 1081c73fbbe3
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@145 79 CQInverse::getBinFrequency(double bin) const
c@116 80 {
c@145 81 // our bins are returned in high->low order
c@145 82 bin = (getBinsPerOctave() * getOctaves()) - bin - 1;
c@145 83 return getMinFrequency() * pow(2, (bin / getBinsPerOctave()));
c@116 84 }
c@116 85
c@116 86 void
c@116 87 CQInverse::initialise()
c@116 88 {
c@116 89 m_octaves = int(ceil(log2(m_maxFrequency / m_minFrequency)));
c@150 90
c@150 91 if (m_octaves < 1) {
c@150 92 m_kernel = 0; // incidentally causing isValid() to return false
c@150 93 return;
c@150 94 }
c@150 95
c@127 96 m_kernel = new CQKernel(m_inparams);
c@116 97 m_p = m_kernel->getProperties();
c@116 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_upsamplers.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 / factor, sourceRate, 50, 0.05);
c@116 118
c@116 119 #ifdef DEBUG_CQ
c@116 120 cerr << "inverse: octave " << i << ": resample from " << sourceRate/factor << " to " << sourceRate << endl;
c@116 121 #endif
c@116 122
c@116 123 // See ConstantQ.cpp for discussion on latency -- output
c@116 124 // latency here is at target rate which, this way around, is
c@116 125 // what we want
c@116 126
c@116 127 latencies.push_back(r->getLatency());
c@116 128 m_upsamplers.push_back(r);
c@116 129 }
c@116 130
c@116 131 // additionally we will have fftHop latency at individual octave
c@116 132 // rate (before upsampling) for the overlap-add in each octave
c@116 133 for (int i = 0; i < m_octaves; ++i) {
c@116 134 latencies[i] += m_p.fftHop * pow(2, i);
c@116 135 }
c@116 136
c@116 137 // Now reverse the drop adjustment made in ConstantQ to align the
c@116 138 // atom centres across different octaves (but this time at output
c@116 139 // sample rate)
c@116 140
c@116 141 int emptyHops = m_p.firstCentre / m_p.atomSpacing;
c@116 142
c@116 143 vector<int> pushes;
c@116 144 for (int i = 0; i < m_octaves; ++i) {
c@116 145 int factor = pow(2, i);
c@116 146 int pushHops = emptyHops * pow(2, m_octaves - i - 1) - emptyHops;
c@116 147 int push = ((pushHops * m_p.fftHop) * factor) / m_p.atomsPerFrame;
c@116 148 pushes.push_back(push);
c@116 149 }
c@116 150
c@116 151 int maxLatLessPush = 0;
c@116 152 for (int i = 0; i < m_octaves; ++i) {
c@116 153 int latLessPush = latencies[i] - pushes[i];
c@116 154 if (latLessPush > maxLatLessPush) maxLatLessPush = latLessPush;
c@116 155 }
c@116 156
c@116 157 int totalLatency = maxLatLessPush + 10;
c@116 158 if (totalLatency < 0) totalLatency = 0;
c@116 159
c@116 160 m_outputLatency = totalLatency + m_p.firstCentre * pow(2, m_octaves-1);
c@116 161
c@116 162 #ifdef DEBUG_CQ
c@116 163 cerr << "totalLatency = " << totalLatency << ", m_outputLatency = " << m_outputLatency << endl;
c@116 164 #endif
c@116 165
c@116 166 for (int i = 0; i < m_octaves; ++i) {
c@116 167
c@116 168 // Calculate the difference between the total latency applied
c@116 169 // across all octaves, and the existing latency due to the
c@116 170 // upsampler for this octave.
c@116 171
c@116 172 int latencyPadding = totalLatency - latencies[i] + pushes[i];
c@116 173
c@116 174 #ifdef DEBUG_CQ
c@116 175 cerr << "octave " << i << ": push " << pushes[i] << ", resampler latency inc overlap space " << latencies[i] << ", latencyPadding = " << latencyPadding << " (/factor = " << latencyPadding / pow(2, i) << ")" << endl;
c@116 176 #endif
c@116 177
c@116 178 m_buffers.push_back(RealSequence(latencyPadding, 0.0));
c@116 179 }
c@116 180
c@116 181 for (int i = 0; i < m_octaves; ++i) {
c@116 182 // Fixed-size buffer for IFFT overlap-add
c@116 183 m_olaBufs.push_back(RealSequence(m_p.fftSize, 0.0));
c@116 184 }
c@116 185
c@116 186 m_fft = new FFTReal(m_p.fftSize);
c@116 187 }
c@116 188
c@116 189 CQInverse::RealSequence
c@116 190 CQInverse::process(const ComplexBlock &block)
c@116 191 {
c@116 192 // The input data is of the form produced by ConstantQ::process --
c@116 193 // an unknown number N of columns of varying height. We assert
c@116 194 // that N is a multiple of atomsPerFrame * 2^(octaves-1), as must
c@116 195 // be the case for data that came directly from our ConstantQ
c@116 196 // implementation.
c@116 197
c@116 198 int widthProvided = block.size();
c@116 199
c@116 200 if (widthProvided == 0) {
c@116 201 return drawFromBuffers();
c@116 202 }
c@116 203
c@116 204 int blockWidth = m_p.atomsPerFrame * int(pow(2, m_octaves - 1));
c@116 205
c@116 206 if (widthProvided % blockWidth != 0) {
c@116 207 cerr << "ERROR: CQInverse::process: Input block size ("
c@116 208 << widthProvided
c@116 209 << ") must be a multiple of processing block width "
c@116 210 << "(atoms-per-frame * 2^(octaves-1) = "
c@116 211 << m_p.atomsPerFrame << " * 2^(" << m_octaves << "-1) = "
c@116 212 << blockWidth << ")" << endl;
c@116 213 throw std::invalid_argument
c@116 214 ("Input block size must be a multiple of processing block width");
c@116 215 }
c@116 216
c@116 217 // Procedure:
c@116 218 //
c@116 219 // 1. Slice the list of columns into a set of lists of columns,
c@116 220 // one per octave, each of width N / (2^octave-1) and height
c@116 221 // binsPerOctave, containing the values present in that octave
c@116 222 //
c@116 223 // 2. Group each octave list by atomsPerFrame columns at a time,
c@116 224 // and stack these so as to achieve a list, for each octave, of
c@116 225 // taller columns of height binsPerOctave * atomsPerFrame
c@116 226 //
c@116 227 // 3. For each taller column, take the product with the inverse CQ
c@116 228 // kernel (which is the conjugate of the forward kernel) and
c@116 229 // perform an inverse FFT
c@116 230 //
c@116 231 // 4. Overlap-add each octave's resynthesised blocks (unwindowed)
c@116 232 //
c@116 233 // 5. Resample each octave's overlap-add stream to the original
c@116 234 // rate
c@116 235 //
c@116 236 // 6. Sum the resampled streams and return
c@116 237
c@116 238 for (int i = 0; i < m_octaves; ++i) {
c@116 239
c@116 240 // Step 1
c@116 241
c@116 242 ComplexBlock oct;
c@116 243
c@116 244 for (int j = 0; j < widthProvided; ++j) {
c@116 245 int h = block[j].size();
c@116 246 if (h < m_binsPerOctave * (i+1)) {
c@116 247 continue;
c@116 248 }
c@116 249 ComplexColumn col(block[j].begin() + m_binsPerOctave * i,
c@116 250 block[j].begin() + m_binsPerOctave * (i+1));
c@116 251 oct.push_back(col);
c@116 252 }
c@116 253
c@116 254 // Steps 2, 3, 4, 5
c@116 255 processOctave(i, oct);
c@116 256 }
c@116 257
c@116 258 // Step 6
c@116 259 return drawFromBuffers();
c@116 260 }
c@116 261
c@116 262 CQInverse::RealSequence
c@116 263 CQInverse::drawFromBuffers()
c@116 264 {
c@116 265 // 6. Sum the resampled streams and return
c@116 266
c@116 267 int available = 0;
c@116 268
c@116 269 for (int i = 0; i < m_octaves; ++i) {
c@116 270 if (i == 0 || int(m_buffers[i].size()) < available) {
c@116 271 available = m_buffers[i].size();
c@116 272 }
c@116 273 }
c@116 274
c@116 275 RealSequence result(available, 0);
c@116 276
c@116 277 if (available == 0) {
c@116 278 return result;
c@116 279 }
c@116 280
c@116 281 for (int i = 0; i < m_octaves; ++i) {
c@116 282 for (int j = 0; j < available; ++j) {
c@116 283 result[j] += m_buffers[i][j];
c@116 284 }
c@116 285 m_buffers[i] = RealSequence(m_buffers[i].begin() + available,
c@116 286 m_buffers[i].end());
c@116 287 }
c@116 288
c@116 289 return result;
c@116 290 }
c@116 291
c@116 292 CQInverse::RealSequence
c@116 293 CQInverse::getRemainingOutput()
c@116 294 {
c@116 295 for (int j = 0; j < m_octaves; ++j) {
c@116 296 int factor = pow(2, j);
c@116 297 int latency = (j > 0 ? m_upsamplers[j]->getLatency() : 0) / factor;
c@116 298 for (int i = 0; i < (latency + m_p.fftSize) / m_p.fftHop; ++i) {
c@116 299 overlapAddAndResample(j, RealSequence(m_olaBufs[j].size(), 0));
c@116 300 }
c@116 301 }
c@116 302
c@116 303 return drawFromBuffers();
c@116 304 }
c@116 305
c@116 306 void
c@116 307 CQInverse::processOctave(int octave, const ComplexBlock &columns)
c@116 308 {
c@116 309 // 2. Group each octave list by atomsPerFrame columns at a time,
c@116 310 // and stack these so as to achieve a list, for each octave, of
c@116 311 // taller columns of height binsPerOctave * atomsPerFrame
c@116 312
c@116 313 int ncols = columns.size();
c@116 314
c@116 315 if (ncols % m_p.atomsPerFrame != 0) {
c@116 316 cerr << "ERROR: CQInverse::process: Number of columns ("
c@116 317 << ncols
c@116 318 << ") in octave " << octave
c@116 319 << " must be a multiple of atoms-per-frame ("
c@116 320 << m_p.atomsPerFrame << ")" << endl;
c@116 321 throw std::invalid_argument
c@116 322 ("Columns in octave must be a multiple of atoms per frame");
c@116 323 }
c@116 324
c@116 325 for (int i = 0; i < ncols; i += m_p.atomsPerFrame) {
c@116 326
c@116 327 ComplexColumn tallcol;
c@116 328 for (int b = 0; b < m_binsPerOctave; ++b) {
c@116 329 for (int a = 0; a < m_p.atomsPerFrame; ++a) {
c@116 330 tallcol.push_back(columns[i + a][m_binsPerOctave - b - 1]);
c@116 331 }
c@116 332 }
c@116 333
c@116 334 processOctaveColumn(octave, tallcol);
c@116 335 }
c@116 336 }
c@116 337
c@116 338 void
c@116 339 CQInverse::processOctaveColumn(int octave, const ComplexColumn &column)
c@116 340 {
c@116 341 // 3. For each taller column, take the product with the inverse CQ
c@116 342 // kernel (which is the conjugate of the forward kernel) and
c@116 343 // perform an inverse FFT
c@116 344
c@116 345 if ((int)column.size() != m_p.atomsPerFrame * m_binsPerOctave) {
c@116 346 cerr << "ERROR: CQInverse::processOctaveColumn: Height of column ("
c@116 347 << column.size() << ") in octave " << octave
c@116 348 << " must be atoms-per-frame * bins-per-octave ("
c@116 349 << m_p.atomsPerFrame << " * " << m_binsPerOctave << " = "
c@116 350 << m_p.atomsPerFrame * m_binsPerOctave << ")" << endl;
c@116 351 throw std::invalid_argument
c@116 352 ("Column height must match atoms-per-frame * bins-per-octave");
c@116 353 }
c@116 354
c@116 355 ComplexSequence transformed = m_kernel->processInverse(column);
c@116 356
c@116 357 int halfLen = m_p.fftSize/2 + 1;
c@116 358
c@116 359 RealSequence ri(halfLen, 0);
c@116 360 RealSequence ii(halfLen, 0);
c@116 361
c@116 362 for (int i = 0; i < halfLen; ++i) {
c@116 363 ri[i] = transformed[i].real();
c@116 364 ii[i] = transformed[i].imag();
c@116 365 }
c@116 366
c@116 367 RealSequence timeDomain(m_p.fftSize, 0);
c@116 368
c@116 369 m_fft->inverse(ri.data(), ii.data(), timeDomain.data());
c@116 370
c@116 371 overlapAddAndResample(octave, timeDomain);
c@116 372 }
c@116 373
c@116 374 void
c@116 375 CQInverse::overlapAddAndResample(int octave, const RealSequence &seq)
c@116 376 {
c@116 377 // 4. Overlap-add each octave's resynthesised blocks (unwindowed)
c@116 378 //
c@116 379 // and
c@116 380 //
c@116 381 // 5. Resample each octave's overlap-add stream to the original
c@116 382 // rate
c@116 383
c@116 384 if (seq.size() != m_olaBufs[octave].size()) {
c@116 385 cerr << "ERROR: CQInverse::overlapAdd: input sequence length ("
c@116 386 << seq.size() << ") is expected to match OLA buffer size ("
c@116 387 << m_olaBufs[octave].size() << ")" << endl;
c@116 388 throw std::invalid_argument
c@116 389 ("Input sequence length should match OLA buffer size");
c@116 390 }
c@116 391
c@116 392 RealSequence toResample(m_olaBufs[octave].begin(),
c@116 393 m_olaBufs[octave].begin() + m_p.fftHop);
c@116 394
c@116 395 RealSequence resampled =
c@116 396 octave > 0 ?
c@116 397 m_upsamplers[octave]->process(toResample.data(), toResample.size()) :
c@116 398 toResample;
c@116 399
c@116 400 m_buffers[octave].insert(m_buffers[octave].end(),
c@116 401 resampled.begin(),
c@116 402 resampled.end());
c@116 403
c@116 404 m_olaBufs[octave] = RealSequence(m_olaBufs[octave].begin() + m_p.fftHop,
c@116 405 m_olaBufs[octave].end());
c@116 406
c@116 407 RealSequence pad(m_p.fftHop, 0);
c@116 408
c@116 409 m_olaBufs[octave].insert(m_olaBufs[octave].end(),
c@116 410 pad.begin(),
c@116 411 pad.end());
c@116 412
c@116 413 for (int i = 0; i < m_p.fftSize; ++i) {
c@116 414 m_olaBufs[octave][i] += seq[i];
c@116 415 }
c@116 416 }
c@116 417