annotate plugins/DWT.cpp @ 178:f96ea0e4b475

Fix compiler warnings with -Wall -Wextra
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 28 Sep 2015 12:33:17 +0100
parents dcf5800f0f00
children 4697db8b91f8
rev   line source
c@178 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@178 2
c@178 3 /*
c@178 4 QM Vamp Plugin Set
c@178 5
c@178 6 Centre for Digital Music, Queen Mary, University of London.
c@178 7 This file copyright 2009 Thomas Wilmering.
c@135 8
c@135 9 This program is free software; you can redistribute it and/or
c@135 10 modify it under the terms of the GNU General Public License as
c@135 11 published by the Free Software Foundation; either version 2 of the
c@135 12 License, or (at your option) any later version. See the file
c@178 13 COPYING included with this distribution for more information.
c@178 14 */
c@178 15
c@178 16 #include "DWT.h"
c@178 17
c@178 18 #include <cmath>
c@178 19
c@178 20 using std::string;
c@178 21 using std::vector;
c@178 22 using std::cerr;
c@178 23 using std::endl;
c@178 24
c@178 25 DWT::DWT(float inputSampleRate) :
c@178 26 Plugin(inputSampleRate),
c@178 27 m_stepSize(0),
c@178 28 m_blockSize(0)
c@178 29 {
c@178 30 m_scales = 10;
c@178 31 m_flength = 0;
c@178 32 m_wavelet = Wavelet::Haar;
c@178 33 m_threshold = 0;
c@178 34 m_absolute = 0;
c@178 35 }
c@178 36
c@178 37 DWT::~DWT()
c@178 38 {
c@178 39 }
c@178 40
c@178 41 string
c@178 42 DWT::getIdentifier() const
c@178 43 {
c@178 44 return "qm-dwt";
c@178 45 }
c@178 46
c@178 47 string
c@178 48 DWT::getName() const
c@178 49 {
c@178 50 return "Discrete Wavelet Transform";
c@178 51 }
c@178 52
c@178 53 string
c@178 54 DWT::getDescription() const
c@178 55 {
c@178 56 return "Visualisation by scalogram";
c@178 57 }
c@178 58
c@178 59 string
c@178 60 DWT::getMaker() const
c@178 61 {
c@178 62 return "Queen Mary, University of London";
c@178 63 }
c@178 64
c@178 65 int
c@178 66 DWT::getPluginVersion() const
c@178 67 {
c@178 68 return 1;
c@178 69 }
c@178 70
c@178 71 string
c@178 72 DWT::getCopyright() const
c@178 73 {
c@178 74 return "Plugin by Thomas Wilmering. Copyright (c) 2009 Thomas Wilmering and QMUL - All Rights Reserved";
c@178 75 }
c@178 76
c@178 77 size_t
c@178 78 DWT::getPreferredBlockSize() const
c@178 79 {
c@178 80 size_t s = (1 << m_scales);
c@178 81 while (s < 1024) s *= 2;
c@178 82 return s;
c@178 83 }
c@178 84
c@178 85 size_t
c@178 86 DWT::getPreferredStepSize() const
c@178 87 {
c@178 88 return 0;
c@178 89 }
c@178 90
c@178 91 bool
c@178 92 DWT::initialise(size_t channels, size_t stepSize, size_t blockSize)
c@178 93 {
c@178 94 if (channels < getMinChannelCount() ||
c@178 95 channels > getMaxChannelCount()) return false;
c@178 96
c@178 97 if ((1U << m_scales) > blockSize) {
c@178 98 std::cerr << "DWT::initialise: ERROR: Block size must be at least 2^scales (specified block size " << blockSize << " < " << (1 << m_scales) << ")" << std::endl;
c@178 99 return false;
c@178 100 }
c@178 101
c@178 102 m_stepSize = stepSize;
c@178 103 m_blockSize = blockSize;
c@178 104
c@178 105 Wavelet::createDecompositionFilters(m_wavelet, m_lpd, m_hpd);
c@178 106
c@178 107 m_flength = m_lpd.size(); // or m_hpd.size()
c@178 108
c@178 109 m_samplePass.resize(m_scales); // resize buffer for samples to pass to next block
c@178 110
c@178 111 for (int i=0; i<m_scales; ++i) {
c@178 112 m_samplePass[i].resize(m_flength-2, 0.0);
c@178 113 }
c@178 114
c@178 115 return true;
c@178 116 }
c@178 117
c@178 118 void
c@178 119 DWT::reset()
c@178 120 {
c@178 121 m_samplePass.clear();
c@178 122
c@178 123 m_samplePass.resize(m_scales);
c@178 124
c@178 125 for (int i=0; i<m_scales; ++i) {
c@178 126 m_samplePass[i].resize(m_flength-2, 0.0);
c@178 127 }
c@178 128 }
c@178 129
c@178 130 DWT::OutputList
c@178 131 DWT::getOutputDescriptors() const
c@178 132 {
c@178 133 OutputList list;
c@178 134
c@178 135 OutputDescriptor sg;
c@178 136 sg.identifier = "wcoeff";
c@178 137 sg.name = "Wavelet Coefficients";
c@178 138 sg.description = "Wavelet coefficients";
c@178 139 sg.unit = "";
c@178 140 sg.hasFixedBinCount = true; // depends on block size
c@178 141 sg.binCount = m_scales; // number of scales
c@178 142 sg.hasKnownExtents = false;
c@178 143 sg.isQuantized = false;
c@178 144 sg.sampleType = OutputDescriptor::FixedSampleRate;
c@178 145 sg.sampleRate = .5 * m_inputSampleRate;
c@178 146
c@178 147 list.push_back(sg);
c@178 148
c@178 149 return list;
c@178 150 }
c@178 151
c@178 152
c@178 153 DWT::ParameterList
c@178 154 DWT::getParameterDescriptors() const
c@178 155 {
c@178 156 ParameterList list;
c@178 157
c@178 158 ParameterDescriptor d;
c@178 159 d.identifier = "scales";
c@178 160 d.name = "Scales";
c@178 161 d.description = "Scale depth";
c@178 162 d.unit = "";
c@178 163 d.minValue = 1.0f;
c@178 164 d.maxValue = 16.0f;
c@178 165 d.defaultValue = 10.0f;
c@178 166 d.isQuantized = true;
c@178 167 d.quantizeStep = 1.0f;
c@178 168 list.push_back(d);
c@178 169
c@178 170 d.identifier = "wavelet";
c@178 171 d.name = "Wavelet";
c@178 172 d.description = "Wavelet type to use";
c@178 173 d.unit = "";
c@178 174 d.minValue = 0.f;
c@178 175 d.maxValue = int(Wavelet::LastType);
c@178 176 d.defaultValue = int(Wavelet::Haar);
c@178 177 d.isQuantized = true;
c@178 178 d.quantizeStep = 1.0f;
c@178 179
c@178 180 for (int i = 0; i <= int(Wavelet::LastType); ++i) {
c@178 181 d.valueNames.push_back(Wavelet::getWaveletName(Wavelet::Type(i)));
c@178 182 }
c@178 183 list.push_back(d);
c@178 184 d.valueNames.clear();
c@178 185
c@178 186 d.identifier = "threshold";
c@178 187 d.name = "Threshold";
c@178 188 d.description = "Wavelet coefficient threshold";
c@178 189 d.unit = "";
c@178 190 d.minValue = 0.0f;
c@178 191 d.maxValue = 0.01f;
c@178 192 d.defaultValue = 0.0f;
c@178 193 d.isQuantized = false;
c@178 194 list.push_back(d);
c@178 195
c@178 196 d.identifier = "absolute";
c@178 197 d.name = "Absolute values";
c@178 198 d.description = "Return absolute values";
c@178 199 d.unit = "";
c@178 200 d.minValue = 0.0f;
c@178 201 d.maxValue = 1.00f;
c@178 202 d.defaultValue = 0.0f;
c@178 203 d.isQuantized = true;
c@178 204 d.quantizeStep = 1.0f;
c@178 205 list.push_back(d);
c@178 206
c@178 207 return list;
c@178 208 }
c@178 209
c@178 210 void DWT::setParameter(std::string paramid, float newval)
c@178 211 {
c@178 212 if (paramid == "scales") {
c@178 213 m_scales = newval;
c@178 214 }
c@178 215 else if (paramid == "wavelet") {
c@178 216 m_wavelet = (Wavelet::Type)(int(newval + 0.1));
c@178 217 }
c@178 218 else if (paramid == "threshold") {
c@178 219 m_threshold = newval;
c@178 220 }
c@178 221 else if (paramid == "absolute") {
c@178 222 m_absolute = newval;
c@178 223 }
c@178 224 }
c@178 225
c@178 226 float DWT::getParameter(std::string paramid) const
c@178 227 {
c@178 228 if (paramid == "scales") {
c@178 229 return m_scales;
c@178 230 }
c@178 231 else if (paramid == "wavelet") {
c@178 232 return int(m_wavelet);
c@178 233 }
c@178 234 else if (paramid == "threshold") {
c@178 235 return m_threshold;
c@178 236 }
c@178 237 else if (paramid == "absolute") {
c@178 238 return m_absolute;
c@178 239 }
c@178 240
c@178 241 return 0.0f;
c@178 242 }
c@178 243
c@178 244
c@178 245 DWT::FeatureSet
c@178 246 DWT::process(const float *const *inputBuffers,
c@178 247 Vamp::RealTime)
c@178 248 {
c@178 249 FeatureSet fs;
c@178 250
c@178 251 if (m_blockSize == 0) {
c@178 252 cerr << "ERROR: DWT::process: Not initialised" << endl;
c@178 253 return fs;
c@178 254 }
c@178 255
c@178 256 int s = m_scales;
c@178 257 int b = m_blockSize;
c@178 258 int b_init = b;
c@178 259
c@178 260 if ((1 << s) > b) b = 1 << s; // correct blocksize if smaller than 2^(max scale)
c@178 261
c@178 262 //--------------------------------------------------------------------------------------------------
c@178 263
c@178 264 float tempDet;
c@178 265 float aTempDet;
c@178 266 int outloc;
c@178 267 int halfblocksize = int(.5 * b);
c@178 268 int fbufloc;
c@178 269 int fbufloc2;
c@178 270
c@178 271 vector< vector<float> > wCoefficients(m_scales); // result
c@178 272 vector<float> tempAprx(halfblocksize,0.0); // approximation
c@178 273 vector<float> fbuf(b+m_flength-2,0.0); // input buffer
c@178 274
c@178 275 for (int n=m_flength-2; n<b+m_flength-2; n++) // copy input buffer to dwt input
c@178 276 fbuf[n] = inputBuffers[0][n-m_flength+2];
c@178 277
c@178 278 for (int scale=0; scale<m_scales; ++scale) // do for each scale
c@178 279 {
c@178 280 for (int n=0; n<m_flength-2; ++n) // get samples from previous block
c@178 281 fbuf[n] = m_samplePass[scale][n];
c@178 282
c@178 283
c@178 284 if ((m_flength-2)<b) // pass samples to next block
c@178 285 for (int n=0; n<m_flength-2; ++n)
c@178 286 m_samplePass[scale][n] = fbuf[b+n];
c@178 287 else {
c@178 288 for (int n=0; n<b; ++n) // if number of samples to pass > blocksize
c@178 289 m_samplePass[scale].push_back(fbuf[m_flength-2+n]);
c@178 290 m_samplePass[scale].erase (m_samplePass[scale].begin(),m_samplePass[scale].begin()+b);
c@178 291 }
c@178 292
c@178 293 for (int n=0; n<halfblocksize; ++n) { // do for every other sample of the input buffer
c@178 294 tempDet = 0;
c@178 295 fbufloc = 2*n+m_flength-1;
c@178 296 for (int m=0; m<m_flength; ++m) { // Convolve the sample with filter coefficients
c@178 297 fbufloc2 = fbufloc - m;
c@178 298 tempAprx[n] += fbuf[fbufloc2] * m_lpd[m]; // approximation
c@178 299 tempDet += fbuf[fbufloc2] * m_hpd[m]; // detail
c@178 300 }
c@178 301
c@178 302 aTempDet = fabs(tempDet);
c@178 303 if (m_absolute == 1) tempDet = aTempDet;
c@178 304
c@178 305
c@178 306 if (aTempDet < m_threshold) tempDet = 0; // simple hard thresholding, same for each scale
c@178 307 wCoefficients[scale].push_back(tempDet);
c@178 308 }
c@178 309
c@178 310 if (scale+1<m_scales) { // prepare variables for next scale
c@178 311 b = b >> 1; // the approximation in tmpfwd is stored as
c@178 312 halfblocksize = halfblocksize >> 1; // input for next level
c@178 313
c@178 314 for (int n=m_flength-2; n<b+m_flength-2; n++) // copy approximation to dwt input
c@178 315 fbuf[n] = tempAprx[n-m_flength+2];
c@178 316
c@178 317 //vector<float>(b+m_flength-2).swap(fbuf);
c@178 318 vector<float>(halfblocksize).swap(tempAprx); // set new size with zeros
c@178 319 }
c@178 320 }
c@178 321
c@178 322
c@178 323 //-----------------------------------------------------------------------------------------
c@178 324
c@178 325 halfblocksize = int(.5 * b_init);
c@178 326
c@178 327 for (int m = 0; m<halfblocksize; m++) {
c@178 328
c@178 329 Feature feature;
c@178 330 feature.hasTimestamp = false;
c@178 331
c@178 332 for (int j = 0; j < s; j++) {
c@178 333 outloc = m / (1 << j); // This one pushes a single result bin
c@178 334 // onto the top of a feature column
c@178 335 feature.values.push_back(wCoefficients[j][outloc]); // each coefficient on higher scales need
c@178 336 } // to be copied multiple times to feature columns
c@178 337 fs[0].push_back(feature);
c@178 338 }
c@178 339 return fs;
c@178 340 }
c@178 341
c@178 342
c@178 343
c@178 344 DWT::FeatureSet
c@178 345 DWT::getRemainingFeatures()
c@178 346 {
c@178 347 int s = m_scales;
c@178 348
c@178 349 FeatureSet fs;
c@178 350
c@178 351 /*
c@178 352 int b = 1;
c@178 353 while (b<((m_flength-1) * (1 << s))) { //set blocksize to tail length
c@178 354 b= (b << 1);
c@178 355 }
c@178 356 int b_init = b;
c@178 357
c@178 358 */
c@178 359 int b = m_blockSize;
c@178 360 int b_init = b;
c@178 361 int tailIterations = int(((m_flength-1) * (1 << s)) / b) + 1; // number of iterations for tail
c@178 362
c@178 363
c@178 364 for(int m=0; m<tailIterations; ++m)
c@178 365 {
c@178 366
c@178 367 b = b_init;
c@178 368
c@178 369 //-------------------------------------------------------------------------------------------
c@178 370 float tempDet;
c@178 371 float aTempDet;
c@178 372 int outloc;
c@178 373 int halfblocksize = int(.5 * b);
c@178 374 int fbufloc;
c@178 375 int fbufloc2;
c@178 376 int len = m_flength;
c@178 377
c@178 378 vector< vector<float> > wCoefficients(m_scales); // result
c@178 379 vector<float> tempAprx(halfblocksize,0.0); // approximation
c@178 380 vector<float> fbuf(b+len-2,0.0); // input buffer
c@178 381
c@178 382 //for (int n=len-2; n<b+len-2; n++) // copy input buffer to dwt input
c@178 383 // fbuf[n] = 0; //inputBuffers[0][n-len+2];
c@178 384
c@178 385 for (int scale=0; scale<m_scales; ++scale) // do for each scale
c@178 386 {
c@178 387 for (int n=0; n<len-2; ++n) // get samples from previous block
c@178 388 fbuf[n] = m_samplePass[scale][n];
c@178 389
c@178 390
c@178 391 if ((len-2)<b) // pass samples to next block
c@178 392 for (int n=0; n<len-2; ++n)
c@178 393 m_samplePass[scale][n] = fbuf[b+n];
c@178 394 else {
c@178 395 for (int n=0; n<b; ++n) // if number of samples to pass > blocksize
c@178 396 m_samplePass[scale].push_back(fbuf[len-2+n]);
c@178 397 m_samplePass[scale].erase (m_samplePass[scale].begin(),m_samplePass[scale].begin()+b);
c@178 398 }
c@178 399
c@178 400 for (int n=0; n<halfblocksize; ++n) { // do for every other sample of the input buffer
c@178 401 tempDet = 0;
c@178 402 fbufloc = 2*n+len-1;
c@178 403 for (int m=0; m<len; ++m) { // Convolve the sample with filter coefficients
c@178 404 fbufloc2 = fbufloc - m;
c@178 405 tempAprx[n] += fbuf[fbufloc2] * m_lpd[m]; // approximation
c@178 406 tempDet += fbuf[fbufloc2] * m_hpd[m]; // detail
c@178 407 }
c@178 408
c@178 409 aTempDet = fabs(tempDet);
c@178 410 if (m_absolute == 1) tempDet = aTempDet;
c@178 411 if (aTempDet < m_threshold) tempDet = 0; // simple hard thresholding, same for each scale
c@178 412 wCoefficients[scale].push_back(tempDet);
c@178 413 }
c@178 414
c@178 415 if (scale+1<m_scales) { // prepare variables for next scale
c@178 416 b = b >> 1; // the approximation in tmpfwd is stored as
c@178 417 halfblocksize = halfblocksize >> 1; // input for next level
c@178 418
c@178 419 for (int n=len-2; n<b+len-2; n++) // copy approximation to dwt input
c@178 420 fbuf[n] = tempAprx[n-len+2];
c@178 421
c@178 422 //vector<float>(b+len-2).swap(fbuf);
c@178 423 vector<float>(halfblocksize).swap(tempAprx); // set new size with zeros
c@178 424 }
c@178 425
c@178 426 }
c@178 427
c@178 428 //-----------------------------------------------------------------------------------------
c@178 429
c@178 430 halfblocksize = int(.5 * b_init + 0.1);
c@178 431
c@178 432 for (int m = 0; m<halfblocksize; m++) {
c@178 433
c@178 434 Feature feature;
c@178 435 feature.hasTimestamp = false;
c@178 436
c@178 437 for (int j = 0; j < s; j++) {
c@178 438 outloc = m / (1 << j); // This one pushes a single result bin
c@178 439 // onto the top of a feature column
c@178 440 feature.values.push_back(wCoefficients[j][outloc]); // each coefficient on higher scales need
c@178 441 } // to be copied multiple times to feature columns
c@178 442 fs[0].push_back(feature);
c@178 443 }
c@178 444 }
c@178 445 return fs;
c@178 446
c@178 447 }
c@178 448