annotate src/CQKernel.cpp @ 117:930ff4ce4018

Move matlab dir
author Chris Cannam <c.cannam@qmul.ac.uk>
date Thu, 15 May 2014 12:04:56 +0100
parents 6deec2a51d13
children 2375457f2876
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@116 34 #include "maths/MathUtilities.h"
c@116 35 #include "dsp/transforms/FFT.h"
c@116 36 #include "base/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@116 51 CQKernel::CQKernel(double sampleRate, double maxFreq, int binsPerOctave) :
c@116 52 m_fft(0)
c@116 53 {
c@116 54 m_p.sampleRate = sampleRate;
c@116 55 m_p.maxFrequency = maxFreq;
c@116 56 m_p.binsPerOctave = binsPerOctave;
c@116 57 generateKernel();
c@116 58 }
c@116 59
c@116 60 CQKernel::~CQKernel()
c@116 61 {
c@116 62 delete m_fft;
c@116 63 }
c@116 64
c@116 65 void
c@116 66 CQKernel::generateKernel()
c@116 67 {
c@116 68 double q = 1;
c@116 69 double atomHopFactor = 0.25;
c@116 70 double thresh = 0.0005;
c@116 71
c@116 72 double bpo = m_p.binsPerOctave;
c@116 73
c@116 74 m_p.minFrequency = (m_p.maxFrequency / 2) * pow(2, 1.0/bpo);
c@116 75 m_p.Q = q / (pow(2, 1.0/bpo) - 1.0);
c@116 76
c@116 77 double maxNK = round(m_p.Q * m_p.sampleRate / m_p.minFrequency);
c@116 78 double minNK = round
c@116 79 (m_p.Q * m_p.sampleRate /
c@116 80 (m_p.minFrequency * pow(2, (bpo - 1.0) / bpo)));
c@116 81
c@116 82 if (minNK == 0 || maxNK == 0) {
c@116 83 // most likely pathological parameters of some sort
c@116 84 cerr << "WARNING: CQKernel::generateKernel: minNK or maxNK is zero (minNK == " << minNK << ", maxNK == " << maxNK << "), not generating a kernel" << endl;
c@116 85 m_p.atomSpacing = 0;
c@116 86 m_p.firstCentre = 0;
c@116 87 m_p.fftSize = 0;
c@116 88 m_p.atomsPerFrame = 0;
c@116 89 m_p.lastCentre = 0;
c@116 90 m_p.fftHop = 0;
c@116 91 return;
c@116 92 }
c@116 93
c@116 94 m_p.atomSpacing = round(minNK * atomHopFactor);
c@116 95 m_p.firstCentre = m_p.atomSpacing * ceil(ceil(maxNK / 2.0) / m_p.atomSpacing);
c@116 96 m_p.fftSize = MathUtilities::nextPowerOfTwo
c@116 97 (m_p.firstCentre + ceil(maxNK / 2.0));
c@116 98
c@116 99 m_p.atomsPerFrame = floor
c@116 100 (1.0 + (m_p.fftSize - ceil(maxNK / 2.0) - m_p.firstCentre) / m_p.atomSpacing);
c@116 101
c@116 102 cerr << "atomsPerFrame = " << m_p.atomsPerFrame << " (atomHopFactor = " << atomHopFactor << ", atomSpacing = " << m_p.atomSpacing << ", fftSize = " << m_p.fftSize << ", maxNK = " << maxNK << ", firstCentre = " << m_p.firstCentre << ")" << endl;
c@116 103
c@116 104 m_p.lastCentre = m_p.firstCentre + (m_p.atomsPerFrame - 1) * m_p.atomSpacing;
c@116 105
c@116 106 m_p.fftHop = (m_p.lastCentre + m_p.atomSpacing) - m_p.firstCentre;
c@116 107
c@116 108 cerr << "fftHop = " << m_p.fftHop << endl;
c@116 109
c@116 110 m_fft = new FFT(m_p.fftSize);
c@116 111
c@116 112 for (int k = 1; k <= m_p.binsPerOctave; ++k) {
c@116 113
c@116 114 int nk = round(m_p.Q * m_p.sampleRate /
c@116 115 (m_p.minFrequency * pow(2, ((k-1.0) / bpo))));
c@116 116
c@116 117 // The MATLAB version uses a symmetric window, but our windows
c@116 118 // are periodic. A symmetric window of size N is a periodic
c@116 119 // one of size N-1 with the first element stuck on the end
c@116 120 Window<double> w(BlackmanHarrisWindow, nk-1);
c@116 121 vector<double> win = w.getWindowData();
c@116 122 win.push_back(win[0]);
c@116 123
c@116 124 for (int i = 0; i < (int)win.size(); ++i) {
c@116 125 win[i] = sqrt(win[i]) / nk;
c@116 126 }
c@116 127
c@116 128 double fk = m_p.minFrequency * pow(2, ((k-1.0) / bpo));
c@116 129
c@116 130 vector<double> reals, imags;
c@116 131
c@116 132 for (int i = 0; i < nk; ++i) {
c@116 133 double arg = (2.0 * M_PI * fk * i) / m_p.sampleRate;
c@116 134 reals.push_back(win[i] * cos(arg));
c@116 135 imags.push_back(win[i] * sin(arg));
c@116 136 }
c@116 137
c@116 138 int atomOffset = m_p.firstCentre - int(ceil(nk/2.0));
c@116 139
c@116 140 for (int i = 0; i < m_p.atomsPerFrame; ++i) {
c@116 141
c@116 142 int shift = atomOffset + (i * m_p.atomSpacing);
c@116 143
c@116 144 vector<double> rin(m_p.fftSize, 0.0);
c@116 145 vector<double> iin(m_p.fftSize, 0.0);
c@116 146
c@116 147 for (int j = 0; j < nk; ++j) {
c@116 148 rin[j + shift] = reals[j];
c@116 149 iin[j + shift] = imags[j];
c@116 150 }
c@116 151
c@116 152 vector<double> rout(m_p.fftSize, 0.0);
c@116 153 vector<double> iout(m_p.fftSize, 0.0);
c@116 154
c@116 155 m_fft->process(false,
c@116 156 rin.data(), iin.data(),
c@116 157 rout.data(), iout.data());
c@116 158
c@116 159 // Keep this dense for the moment (until after
c@116 160 // normalisation calculations)
c@116 161
c@116 162 vector<C> row;
c@116 163
c@116 164 for (int j = 0; j < m_p.fftSize; ++j) {
c@116 165 if (sqrt(rout[j] * rout[j] + iout[j] * iout[j]) < thresh) {
c@116 166 row.push_back(C(0, 0));
c@116 167 } else {
c@116 168 row.push_back(C(rout[j] / m_p.fftSize,
c@116 169 iout[j] / m_p.fftSize));
c@116 170 }
c@116 171 }
c@116 172
c@116 173 m_kernel.origin.push_back(0);
c@116 174 m_kernel.data.push_back(row);
c@116 175 }
c@116 176 }
c@116 177
c@116 178 assert((int)m_kernel.data.size() == m_p.binsPerOctave * m_p.atomsPerFrame);
c@116 179
c@116 180 // print density as diagnostic
c@116 181
c@116 182 int nnz = 0;
c@116 183 for (int i = 0; i < (int)m_kernel.data.size(); ++i) {
c@116 184 for (int j = 0; j < (int)m_kernel.data[i].size(); ++j) {
c@116 185 if (m_kernel.data[i][j] != C(0, 0)) {
c@116 186 ++nnz;
c@116 187 }
c@116 188 }
c@116 189 }
c@116 190
c@116 191 cerr << "size = " << m_kernel.data.size() << "*" << m_kernel.data[0].size() << " (fft size = " << m_p.fftSize << ")" << endl;
c@116 192
c@116 193 assert((int)m_kernel.data.size() == m_p.binsPerOctave * m_p.atomsPerFrame);
c@116 194 assert((int)m_kernel.data[0].size() == m_p.fftSize);
c@116 195
c@116 196 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@116 197
c@116 198 finaliseKernel();
c@116 199 }
c@116 200
c@116 201 static bool ccomparator(C &c1, C &c2)
c@116 202 {
c@116 203 return abs(c1) < abs(c2);
c@116 204 }
c@116 205
c@116 206 static int maxidx(vector<C> &v)
c@116 207 {
c@116 208 return std::max_element(v.begin(), v.end(), ccomparator) - v.begin();
c@116 209 }
c@116 210
c@116 211 void
c@116 212 CQKernel::finaliseKernel()
c@116 213 {
c@116 214 // calculate weight for normalisation
c@116 215
c@116 216 int wx1 = maxidx(m_kernel.data[0]);
c@116 217 int wx2 = maxidx(m_kernel.data[m_kernel.data.size()-1]);
c@116 218
c@116 219 vector<vector<C> > subset(m_kernel.data.size());
c@116 220 for (int j = wx1; j <= wx2; ++j) {
c@116 221 for (int i = 0; i < (int)m_kernel.data.size(); ++i) {
c@116 222 subset[i].push_back(m_kernel.data[i][j]);
c@116 223 }
c@116 224 }
c@116 225
c@116 226 int nrows = subset.size();
c@116 227 int ncols = subset[0].size();
c@116 228 vector<vector<C> > square(ncols); // conjugate transpose of subset * subset
c@116 229
c@116 230 for (int i = 0; i < nrows; ++i) {
c@116 231 assert((int)subset[i].size() == ncols);
c@116 232 }
c@116 233
c@116 234 for (int j = 0; j < ncols; ++j) {
c@116 235 for (int i = 0; i < ncols; ++i) {
c@116 236 C v(0, 0);
c@116 237 for (int k = 0; k < nrows; ++k) {
c@116 238 v += subset[k][i] * conj(subset[k][j]);
c@116 239 }
c@116 240 square[i].push_back(v);
c@116 241 }
c@116 242 }
c@116 243
c@116 244 vector<double> wK;
c@116 245 double q = 1; //!!! duplicated from constructor
c@116 246 for (int i = round(1.0/q); i < ncols - round(1.0/q) - 2; ++i) {
c@116 247 wK.push_back(abs(square[i][i]));
c@116 248 }
c@116 249
c@116 250 double weight = double(m_p.fftHop) / m_p.fftSize;
c@116 251 weight /= MathUtilities::mean(wK.data(), wK.size());
c@116 252 weight = sqrt(weight);
c@116 253
c@116 254 cerr << "weight = " << weight << endl;
c@116 255
c@116 256 // apply normalisation weight, make sparse, and store conjugate
c@116 257 // (we use the adjoint or conjugate transpose of the kernel matrix
c@116 258 // for the forward transform, the plain kernel for the inverse
c@116 259 // which we expect to be less common)
c@116 260
c@116 261 KernelMatrix sk;
c@116 262
c@116 263 for (int i = 0; i < (int)m_kernel.data.size(); ++i) {
c@116 264
c@116 265 sk.origin.push_back(0);
c@116 266 sk.data.push_back(vector<C>());
c@116 267
c@116 268 int lastNZ = 0;
c@116 269 for (int j = (int)m_kernel.data[i].size()-1; j >= 0; --j) {
c@116 270 if (abs(m_kernel.data[i][j]) != 0.0) {
c@116 271 lastNZ = j;
c@116 272 break;
c@116 273 }
c@116 274 }
c@116 275
c@116 276 bool haveNZ = false;
c@116 277 for (int j = 0; j <= lastNZ; ++j) {
c@116 278 if (haveNZ || abs(m_kernel.data[i][j]) != 0.0) {
c@116 279 if (!haveNZ) sk.origin[i] = j;
c@116 280 haveNZ = true;
c@116 281 sk.data[i].push_back(conj(m_kernel.data[i][j]) * weight);
c@116 282 }
c@116 283 }
c@116 284 }
c@116 285
c@116 286 m_kernel = sk;
c@116 287 }
c@116 288
c@116 289 vector<C>
c@116 290 CQKernel::processForward(const vector<C> &cv)
c@116 291 {
c@116 292 // straightforward matrix multiply (taking into account m_kernel's
c@116 293 // slightly-sparse representation)
c@116 294
c@116 295 if (m_kernel.data.empty()) return vector<C>();
c@116 296
c@116 297 int nrows = m_p.binsPerOctave * m_p.atomsPerFrame;
c@116 298
c@116 299 vector<C> rv(nrows, C());
c@116 300
c@116 301 for (int i = 0; i < nrows; ++i) {
c@116 302 int len = m_kernel.data[i].size();
c@116 303 for (int j = 0; j < len; ++j) {
c@116 304 rv[i] += cv[j + m_kernel.origin[i]] * m_kernel.data[i][j];
c@116 305 }
c@116 306 }
c@116 307
c@116 308 return rv;
c@116 309 }
c@116 310
c@116 311 vector<C>
c@116 312 CQKernel::processInverse(const vector<C> &cv)
c@116 313 {
c@116 314 // matrix multiply by conjugate transpose of m_kernel. This is
c@116 315 // actually the original kernel as calculated, we just stored the
c@116 316 // conjugate-transpose of the kernel because we expect to be doing
c@116 317 // more forward transforms than inverse ones.
c@116 318
c@116 319 if (m_kernel.data.empty()) return vector<C>();
c@116 320
c@116 321 int ncols = m_p.binsPerOctave * m_p.atomsPerFrame;
c@116 322 int nrows = m_p.fftSize;
c@116 323
c@116 324 vector<C> rv(nrows, C());
c@116 325
c@116 326 for (int j = 0; j < ncols; ++j) {
c@116 327 int i0 = m_kernel.origin[j];
c@116 328 int i1 = i0 + m_kernel.data[j].size();
c@116 329 for (int i = i0; i < i1; ++i) {
c@116 330 rv[i] += cv[j] * conj(m_kernel.data[j][i - i0]);
c@116 331 }
c@116 332 }
c@116 333
c@116 334 return rv;
c@116 335 }
c@116 336
c@116 337