annotate src/CQKernel.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 1060a19e2334
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 "CQKernel.h"
c@116 33
c@121 34 #include "dsp/MathUtilities.h"
c@121 35 #include "dsp/FFT.h"
c@121 36 #include "dsp/Window.h"
c@116 37
c@116 38 #include <cmath>
c@116 39 #include <cassert>
c@116 40 #include <vector>
c@116 41 #include <iostream>
c@116 42 #include <algorithm>
c@116 43
c@116 44 using std::vector;
c@116 45 using std::complex;
c@116 46 using std::cerr;
c@116 47 using std::endl;
c@116 48
c@116 49 typedef std::complex<double> C;
c@116 50
c@138 51 //#define DEBUG_CQ_KERNEL 1
c@138 52
c@127 53 CQKernel::CQKernel(CQParameters params) :
c@127 54 m_inparams(params),
c@147 55 m_valid(false),
c@116 56 m_fft(0)
c@116 57 {
c@127 58 m_p.sampleRate = params.sampleRate;
c@127 59 m_p.maxFrequency = params.maxFrequency;
c@127 60 m_p.binsPerOctave = params.binsPerOctave;
c@147 61 m_valid = generateKernel();
c@116 62 }
c@116 63
c@116 64 CQKernel::~CQKernel()
c@116 65 {
c@116 66 delete m_fft;
c@116 67 }
c@116 68
c@127 69 vector<double>
c@127 70 CQKernel::makeWindow(int len) const
c@127 71 {
c@127 72 // The MATLAB version uses a symmetric window, but our windows
c@127 73 // are periodic. A symmetric window of size N is a periodic
c@127 74 // one of size N-1 with the first element stuck on the end.
c@127 75
c@127 76 WindowType wt(BlackmanHarrisWindow);
c@127 77
c@127 78 switch (m_inparams.window) {
c@127 79 case CQParameters::SqrtBlackmanHarris:
c@127 80 case CQParameters::BlackmanHarris:
c@127 81 wt = BlackmanHarrisWindow;
c@127 82 break;
c@127 83 case CQParameters::SqrtBlackman:
c@127 84 case CQParameters::Blackman:
c@127 85 wt = BlackmanWindow;
c@127 86 break;
c@127 87 case CQParameters::SqrtHann:
c@127 88 case CQParameters::Hann:
c@127 89 wt = HanningWindow;
c@127 90 break;
c@127 91 }
c@127 92
c@127 93 Window<double> w(wt, len-1);
c@127 94 vector<double> win = w.getWindowData();
c@127 95 win.push_back(win[0]);
c@127 96
c@127 97 switch (m_inparams.window) {
c@127 98 case CQParameters::SqrtBlackmanHarris:
c@127 99 case CQParameters::SqrtBlackman:
c@127 100 case CQParameters::SqrtHann:
c@127 101 for (int i = 0; i < (int)win.size(); ++i) {
c@127 102 win[i] = sqrt(win[i]) / len;
c@127 103 }
c@127 104 break;
c@127 105 case CQParameters::BlackmanHarris:
c@127 106 case CQParameters::Blackman:
c@127 107 case CQParameters::Hann:
c@127 108 for (int i = 0; i < (int)win.size(); ++i) {
c@127 109 win[i] = win[i] / len;
c@127 110 }
c@127 111 break;
c@127 112 }
c@127 113
c@127 114 return win;
c@127 115 }
c@127 116
c@147 117 bool
c@116 118 CQKernel::generateKernel()
c@116 119 {
c@127 120 double q = m_inparams.q;
c@127 121 double atomHopFactor = m_inparams.atomHopFactor;
c@127 122 double thresh = m_inparams.threshold;
c@116 123
c@116 124 double bpo = m_p.binsPerOctave;
c@116 125
c@116 126 m_p.minFrequency = (m_p.maxFrequency / 2) * pow(2, 1.0/bpo);
c@116 127 m_p.Q = q / (pow(2, 1.0/bpo) - 1.0);
c@116 128
c@116 129 double maxNK = round(m_p.Q * m_p.sampleRate / m_p.minFrequency);
c@116 130 double minNK = round
c@116 131 (m_p.Q * m_p.sampleRate /
c@116 132 (m_p.minFrequency * pow(2, (bpo - 1.0) / bpo)));
c@116 133
c@116 134 if (minNK == 0 || maxNK == 0) {
c@116 135 // most likely pathological parameters of some sort
c@116 136 cerr << "WARNING: CQKernel::generateKernel: minNK or maxNK is zero (minNK == " << minNK << ", maxNK == " << maxNK << "), not generating a kernel" << endl;
c@116 137 m_p.atomSpacing = 0;
c@116 138 m_p.firstCentre = 0;
c@116 139 m_p.fftSize = 0;
c@116 140 m_p.atomsPerFrame = 0;
c@116 141 m_p.lastCentre = 0;
c@116 142 m_p.fftHop = 0;
c@147 143 return false;
c@116 144 }
c@116 145
c@116 146 m_p.atomSpacing = round(minNK * atomHopFactor);
c@116 147 m_p.firstCentre = m_p.atomSpacing * ceil(ceil(maxNK / 2.0) / m_p.atomSpacing);
c@116 148 m_p.fftSize = MathUtilities::nextPowerOfTwo
c@116 149 (m_p.firstCentre + ceil(maxNK / 2.0));
c@116 150
c@116 151 m_p.atomsPerFrame = floor
c@116 152 (1.0 + (m_p.fftSize - ceil(maxNK / 2.0) - m_p.firstCentre) / m_p.atomSpacing);
c@116 153
c@138 154 #ifdef DEBUG_CQ_KERNEL
c@127 155 cerr << "atomsPerFrame = " << m_p.atomsPerFrame << " (q = " << q << ", Q = " << m_p.Q << ", atomHopFactor = " << atomHopFactor << ", atomSpacing = " << m_p.atomSpacing << ", fftSize = " << m_p.fftSize << ", maxNK = " << maxNK << ", firstCentre = " << m_p.firstCentre << ")" << endl;
c@138 156 #endif
c@116 157
c@116 158 m_p.lastCentre = m_p.firstCentre + (m_p.atomsPerFrame - 1) * m_p.atomSpacing;
c@116 159
c@116 160 m_p.fftHop = (m_p.lastCentre + m_p.atomSpacing) - m_p.firstCentre;
c@116 161
c@138 162 #ifdef DEBUG_CQ_KERNEL
c@116 163 cerr << "fftHop = " << m_p.fftHop << endl;
c@138 164 #endif
c@116 165
c@116 166 m_fft = new FFT(m_p.fftSize);
c@116 167
c@116 168 for (int k = 1; k <= m_p.binsPerOctave; ++k) {
c@116 169
c@116 170 int nk = round(m_p.Q * m_p.sampleRate /
c@116 171 (m_p.minFrequency * pow(2, ((k-1.0) / bpo))));
c@116 172
c@127 173 vector<double> win = makeWindow(nk);
c@116 174
c@116 175 double fk = m_p.minFrequency * pow(2, ((k-1.0) / bpo));
c@116 176
c@116 177 vector<double> reals, imags;
c@116 178
c@116 179 for (int i = 0; i < nk; ++i) {
c@116 180 double arg = (2.0 * M_PI * fk * i) / m_p.sampleRate;
c@116 181 reals.push_back(win[i] * cos(arg));
c@116 182 imags.push_back(win[i] * sin(arg));
c@116 183 }
c@116 184
c@116 185 int atomOffset = m_p.firstCentre - int(ceil(nk/2.0));
c@116 186
c@116 187 for (int i = 0; i < m_p.atomsPerFrame; ++i) {
c@116 188
c@116 189 int shift = atomOffset + (i * m_p.atomSpacing);
c@116 190
c@116 191 vector<double> rin(m_p.fftSize, 0.0);
c@116 192 vector<double> iin(m_p.fftSize, 0.0);
c@116 193
c@116 194 for (int j = 0; j < nk; ++j) {
c@116 195 rin[j + shift] = reals[j];
c@116 196 iin[j + shift] = imags[j];
c@116 197 }
c@116 198
c@116 199 vector<double> rout(m_p.fftSize, 0.0);
c@116 200 vector<double> iout(m_p.fftSize, 0.0);
c@116 201
c@116 202 m_fft->process(false,
c@116 203 rin.data(), iin.data(),
c@116 204 rout.data(), iout.data());
c@116 205
c@116 206 // Keep this dense for the moment (until after
c@116 207 // normalisation calculations)
c@116 208
c@116 209 vector<C> row;
c@116 210
c@116 211 for (int j = 0; j < m_p.fftSize; ++j) {
c@116 212 if (sqrt(rout[j] * rout[j] + iout[j] * iout[j]) < thresh) {
c@116 213 row.push_back(C(0, 0));
c@116 214 } else {
c@116 215 row.push_back(C(rout[j] / m_p.fftSize,
c@116 216 iout[j] / m_p.fftSize));
c@116 217 }
c@116 218 }
c@116 219
c@116 220 m_kernel.origin.push_back(0);
c@116 221 m_kernel.data.push_back(row);
c@116 222 }
c@116 223 }
c@116 224
c@116 225 assert((int)m_kernel.data.size() == m_p.binsPerOctave * m_p.atomsPerFrame);
c@116 226
c@116 227 // print density as diagnostic
c@116 228
c@116 229 int nnz = 0;
c@116 230 for (int i = 0; i < (int)m_kernel.data.size(); ++i) {
c@116 231 for (int j = 0; j < (int)m_kernel.data[i].size(); ++j) {
c@116 232 if (m_kernel.data[i][j] != C(0, 0)) {
c@116 233 ++nnz;
c@116 234 }
c@116 235 }
c@116 236 }
c@116 237
c@138 238 #ifdef DEBUG_CQ_KERNEL
c@116 239 cerr << "size = " << m_kernel.data.size() << "*" << m_kernel.data[0].size() << " (fft size = " << m_p.fftSize << ")" << endl;
c@138 240 #endif
c@116 241
c@116 242 assert((int)m_kernel.data.size() == m_p.binsPerOctave * m_p.atomsPerFrame);
c@116 243 assert((int)m_kernel.data[0].size() == m_p.fftSize);
c@116 244
c@138 245 #ifdef DEBUG_CQ_KERNEL
c@116 246 cerr << "density = " << double(nnz) / double(m_p.binsPerOctave * m_p.atomsPerFrame * m_p.fftSize) << " (" << nnz << " of " << m_p.binsPerOctave * m_p.atomsPerFrame * m_p.fftSize << ")" << endl;
c@138 247 #endif
c@116 248
c@116 249 finaliseKernel();
c@147 250 return true;
c@116 251 }
c@116 252
c@116 253 static bool ccomparator(C &c1, C &c2)
c@116 254 {
c@116 255 return abs(c1) < abs(c2);
c@116 256 }
c@116 257
c@116 258 static int maxidx(vector<C> &v)
c@116 259 {
c@116 260 return std::max_element(v.begin(), v.end(), ccomparator) - v.begin();
c@116 261 }
c@116 262
c@116 263 void
c@116 264 CQKernel::finaliseKernel()
c@116 265 {
c@116 266 // calculate weight for normalisation
c@116 267
c@116 268 int wx1 = maxidx(m_kernel.data[0]);
c@116 269 int wx2 = maxidx(m_kernel.data[m_kernel.data.size()-1]);
c@116 270
c@116 271 vector<vector<C> > subset(m_kernel.data.size());
c@116 272 for (int j = wx1; j <= wx2; ++j) {
c@116 273 for (int i = 0; i < (int)m_kernel.data.size(); ++i) {
c@116 274 subset[i].push_back(m_kernel.data[i][j]);
c@116 275 }
c@116 276 }
c@116 277
c@116 278 int nrows = subset.size();
c@116 279 int ncols = subset[0].size();
c@116 280 vector<vector<C> > square(ncols); // conjugate transpose of subset * subset
c@116 281
c@116 282 for (int i = 0; i < nrows; ++i) {
c@116 283 assert((int)subset[i].size() == ncols);
c@116 284 }
c@116 285
c@116 286 for (int j = 0; j < ncols; ++j) {
c@116 287 for (int i = 0; i < ncols; ++i) {
c@116 288 C v(0, 0);
c@116 289 for (int k = 0; k < nrows; ++k) {
c@116 290 v += subset[k][i] * conj(subset[k][j]);
c@116 291 }
c@116 292 square[i].push_back(v);
c@116 293 }
c@116 294 }
c@116 295
c@116 296 vector<double> wK;
c@127 297 double q = m_inparams.q;
c@116 298 for (int i = round(1.0/q); i < ncols - round(1.0/q) - 2; ++i) {
c@116 299 wK.push_back(abs(square[i][i]));
c@116 300 }
c@116 301
c@116 302 double weight = double(m_p.fftHop) / m_p.fftSize;
c@147 303 if (!wK.empty()) {
c@147 304 weight /= MathUtilities::mean(wK.data(), wK.size());
c@147 305 }
c@116 306 weight = sqrt(weight);
c@138 307
c@138 308 #ifdef DEBUG_CQ_KERNEL
c@147 309 cerr << "weight = " << weight << " (from " << wK.size() << " elements in wK, ncols = " << ncols << ", q = " << q << ")" << endl;
c@138 310 #endif
c@116 311
c@116 312 // apply normalisation weight, make sparse, and store conjugate
c@116 313 // (we use the adjoint or conjugate transpose of the kernel matrix
c@116 314 // for the forward transform, the plain kernel for the inverse
c@116 315 // which we expect to be less common)
c@116 316
c@116 317 KernelMatrix sk;
c@116 318
c@116 319 for (int i = 0; i < (int)m_kernel.data.size(); ++i) {
c@116 320
c@116 321 sk.origin.push_back(0);
c@116 322 sk.data.push_back(vector<C>());
c@116 323
c@116 324 int lastNZ = 0;
c@116 325 for (int j = (int)m_kernel.data[i].size()-1; j >= 0; --j) {
c@116 326 if (abs(m_kernel.data[i][j]) != 0.0) {
c@116 327 lastNZ = j;
c@116 328 break;
c@116 329 }
c@116 330 }
c@116 331
c@116 332 bool haveNZ = false;
c@116 333 for (int j = 0; j <= lastNZ; ++j) {
c@116 334 if (haveNZ || abs(m_kernel.data[i][j]) != 0.0) {
c@116 335 if (!haveNZ) sk.origin[i] = j;
c@116 336 haveNZ = true;
c@116 337 sk.data[i].push_back(conj(m_kernel.data[i][j]) * weight);
c@116 338 }
c@116 339 }
c@116 340 }
c@116 341
c@116 342 m_kernel = sk;
c@116 343 }
c@116 344
c@116 345 vector<C>
c@116 346 CQKernel::processForward(const vector<C> &cv)
c@116 347 {
c@116 348 // straightforward matrix multiply (taking into account m_kernel's
c@116 349 // slightly-sparse representation)
c@116 350
c@116 351 if (m_kernel.data.empty()) return vector<C>();
c@116 352
c@116 353 int nrows = m_p.binsPerOctave * m_p.atomsPerFrame;
c@116 354
c@116 355 vector<C> rv(nrows, C());
c@116 356
c@116 357 for (int i = 0; i < nrows; ++i) {
c@116 358 int len = m_kernel.data[i].size();
c@116 359 for (int j = 0; j < len; ++j) {
c@116 360 rv[i] += cv[j + m_kernel.origin[i]] * m_kernel.data[i][j];
c@116 361 }
c@116 362 }
c@116 363
c@116 364 return rv;
c@116 365 }
c@116 366
c@116 367 vector<C>
c@116 368 CQKernel::processInverse(const vector<C> &cv)
c@116 369 {
c@116 370 // matrix multiply by conjugate transpose of m_kernel. This is
c@116 371 // actually the original kernel as calculated, we just stored the
c@116 372 // conjugate-transpose of the kernel because we expect to be doing
c@116 373 // more forward transforms than inverse ones.
c@116 374
c@116 375 if (m_kernel.data.empty()) return vector<C>();
c@116 376
c@116 377 int ncols = m_p.binsPerOctave * m_p.atomsPerFrame;
c@116 378 int nrows = m_p.fftSize;
c@116 379
c@116 380 vector<C> rv(nrows, C());
c@116 381
c@116 382 for (int j = 0; j < ncols; ++j) {
c@116 383 int i0 = m_kernel.origin[j];
c@116 384 int i1 = i0 + m_kernel.data[j].size();
c@116 385 for (int i = i0; i < i1; ++i) {
c@116 386 rv[i] += cv[j] * conj(m_kernel.data[j][i - i0]);
c@116 387 }
c@116 388 }
c@116 389
c@116 390 return rv;
c@116 391 }
c@116 392
c@116 393