annotate src/Finder.cpp @ 172:30d59e1e4232 structure

Minor tidy
author Chris Cannam
date Fri, 06 Feb 2015 18:09:18 +0000
parents bb4507f24dc9
children eeed3498fe96
rev   line source
cannam@0 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@0 2
cannam@0 3 /*
cannam@0 4 Vamp feature extraction plugin using the MATCH audio alignment
cannam@0 5 algorithm.
cannam@0 6
cannam@0 7 Centre for Digital Music, Queen Mary, University of London.
cannam@0 8 This file copyright 2007 Simon Dixon, Chris Cannam and QMUL.
cannam@0 9
cannam@0 10 This program is free software; you can redistribute it and/or
cannam@0 11 modify it under the terms of the GNU General Public License as
cannam@0 12 published by the Free Software Foundation; either version 2 of the
cannam@0 13 License, or (at your option) any later version. See the file
cannam@0 14 COPYING included with this distribution for more information.
cannam@0 15 */
cannam@0 16
cannam@0 17 #include "Finder.h"
cannam@0 18
Chris@30 19 #include "Path.h"
Chris@30 20
Chris@30 21 #include <algorithm>
Chris@92 22 #include <iomanip>
Chris@30 23
Chris@72 24 using namespace std;
cannam@0 25
Chris@140 26 //#define DEBUG_FINDER 1
Chris@140 27 //#define PERFORM_ERROR_CHECKS 1
Chris@140 28
Chris@72 29 Finder::Finder(Matcher *pm)
cannam@0 30 {
Chris@72 31 m_m = pm;
Chris@72 32 m_duration1 = -1;
Chris@72 33 m_duration2 = -1;
cannam@0 34 } // constructor
cannam@0 35
cannam@0 36 Finder::~Finder()
cannam@0 37 {
cannam@0 38 }
cannam@0 39
Chris@60 40 void
Chris@154 41 Finder::setMatcher(Matcher *pm)
Chris@154 42 {
Chris@155 43 cerr << "Finder::setMatcher: finder " << this << ", matcher " << pm << endl;
Chris@154 44 m_m = pm;
Chris@154 45 }
Chris@154 46
Chris@154 47 void
Chris@60 48 Finder::setDurations(int d1, int d2)
Chris@60 49 {
Chris@140 50 #ifdef DEBUG_FINDER
Chris@140 51 cerr << "*** setDurations: " << d1 << ", " << d2 << endl;
Chris@140 52 #endif
Chris@72 53 m_duration1 = d1;
Chris@72 54 m_duration2 = d2;
Chris@60 55 }
Chris@60 56
Chris@154 57 bool
Chris@154 58 Finder::getBestRowCost(int row, int &bestCol, double &min)
Chris@154 59 {
Chris@172 60 if (!m_m->isRowAvailable(row)) return false;
Chris@154 61 pair<int, int> colRange = m_m->getColRange(row);
Chris@172 62 if (colRange.first >= colRange.second) return false;
Chris@154 63 for (int index = colRange.first; index < colRange.second; index++) {
Chris@154 64 double tmp = m_m->getNormalisedPathCost(row, index);
Chris@154 65 if (index == colRange.first || tmp < min) {
Chris@154 66 min = tmp;
Chris@154 67 bestCol = index;
Chris@154 68 }
Chris@154 69 }
Chris@154 70 return true;
Chris@154 71 }
Chris@154 72
Chris@154 73 bool
Chris@154 74 Finder::getBestColCost(int col, int &bestRow, double &min)
Chris@154 75 {
Chris@154 76 if (!m_m->isColAvailable(col)) return false;
Chris@154 77 pair<int, int> rowRange = m_m->getRowRange(col);
Chris@154 78 if (rowRange.first >= rowRange.second) return false;
Chris@154 79 for (int index = rowRange.first; index < rowRange.second; index++) {
Chris@154 80 double tmp = m_m->getNormalisedPathCost(index, col);
Chris@154 81 if (index == rowRange.first || tmp < min) {
Chris@154 82 min = tmp;
Chris@154 83 bestRow = index;
Chris@154 84 }
Chris@154 85 }
Chris@154 86 return true;
Chris@154 87 }
Chris@154 88
Chris@147 89 void
Chris@147 90 Finder::getBestEdgeCost(int row, int col,
Chris@147 91 int &bestRow, int &bestCol,
Chris@147 92 double &min)
cannam@0 93 {
Chris@147 94 min = m_m->getPathCost(row, col);
Chris@72 95
Chris@147 96 bestRow = row;
Chris@147 97 bestCol = col;
Chris@72 98
Chris@72 99 pair<int, int> rowRange = m_m->getRowRange(col);
Chris@72 100 if (rowRange.second > row+1) {
Chris@72 101 rowRange.second = row+1; // don't cheat by looking at future :)
Chris@72 102 }
Chris@72 103 for (int index = rowRange.first; index < rowRange.second; index++) {
Chris@135 104 double tmp = m_m->getNormalisedPathCost(index, col);
cannam@0 105 if (tmp < min) {
cannam@0 106 min = tmp;
cannam@0 107 bestRow = index;
cannam@0 108 }
cannam@0 109 }
Chris@72 110
Chris@72 111 pair<int, int> colRange = m_m->getColRange(row);
Chris@72 112 if (colRange.second > col+1) {
Chris@72 113 colRange.second = col+1; // don't cheat by looking at future :)
Chris@72 114 }
Chris@72 115 for (int index = colRange.first; index < colRange.second; index++) {
Chris@135 116 double tmp = m_m->getNormalisedPathCost(row, index);
cannam@0 117 if (tmp < min) {
cannam@0 118 min = tmp;
cannam@0 119 bestCol = index;
cannam@0 120 bestRow = row;
cannam@0 121 }
cannam@0 122 }
Chris@147 123 }
Chris@72 124
Chris@147 125 Matcher::Advance
Chris@171 126 Finder::getExpandDirection()
Chris@171 127 {
Chris@171 128 return getExpandDirection(m_m->getFrameCount() - 1,
Chris@171 129 m_m->getOtherFrameCount() - 1);
Chris@171 130 }
Chris@171 131
Chris@171 132 Matcher::Advance
Chris@147 133 Finder::getExpandDirection(int row, int col)
Chris@147 134 {
Chris@147 135 // To determine which direction to expand the search area in, we
Chris@147 136 // look at the path costs along the leading edges of the search
Chris@147 137 // area (the final row and column within the area). We find the
Chris@147 138 // lowest path cost within the final row, and the lowest within
Chris@147 139 // the final column, and we compare them. If the row is cheaper
Chris@147 140 // then we expand by adding another row next to it; if the column
Chris@147 141 // is cheaper then we expand by adding another column next to
Chris@147 142 // it. (The overall lowest path cost across the row and column
Chris@147 143 // represents the best alignment we have within the entire search
Chris@147 144 // area given the data available and the assumption that the piece
Chris@147 145 // is not ending yet.)
Chris@147 146
Chris@147 147 int bestRow = row;
Chris@147 148 int bestCol = col;
Chris@147 149 double bestCost = -1;
Chris@147 150
Chris@155 151 // cerr << "Finder " << this << "::getExpandDirection: ";
Chris@155 152
Chris@147 153 getBestEdgeCost(row, col, bestRow, bestCol, bestCost);
Chris@147 154
Chris@147 155 // cerr << "at [" << row << "," << col << "] (cost " << m_m->getPathCost(row, col) << ") blocksize = " << m_m->getBlockSize() << " best is [" << bestRow << "," << bestCol << "] (cost " << bestCost << ")" << endl;
Chris@135 156
Chris@45 157 if (bestRow == row) {
Chris@45 158 if (bestCol == col) {
Chris@45 159 return Matcher::AdvanceBoth;
Chris@45 160 } else {
Chris@45 161 return Matcher::AdvanceThis;
Chris@45 162 }
Chris@45 163 } else if (bestCol == col) {
Chris@45 164 return Matcher::AdvanceOther;
Chris@45 165 } else {
Chris@46 166 return Matcher::AdvanceNone;
Chris@45 167 }
Chris@73 168 }
cannam@0 169
cannam@0 170 void
cannam@0 171 Finder::recalculatePathCostMatrix(int r1, int c1, int r2, int c2)
cannam@0 172 {
Chris@72 173 int prevRowStart = 0, prevRowStop = 0;
Chris@72 174
Chris@83 175 float diagonalWeight = m_m->getDiagonalWeight();
Chris@83 176
Chris@72 177 for (int r = r1; r <= r2; r++) {
Chris@72 178
Chris@72 179 pair<int, int> colRange = m_m->getColRange(r);
Chris@72 180
Chris@72 181 int rowStart = max(c1, colRange.first);
Chris@72 182 int rowStop = min(c2 + 1, colRange.second);
Chris@72 183
Chris@72 184 for (int c = rowStart; c < rowStop; c++) {
Chris@72 185
Chris@72 186 float newCost = m_m->getDistance(r, c);
Chris@72 187 Matcher::Advance dir = Matcher::AdvanceNone;
Chris@72 188
Chris@72 189 if (r > r1) { // not first row
Chris@72 190 double min = -1;
Chris@72 191 if ((c > prevRowStart) && (c <= prevRowStop)) {
Chris@72 192 // diagonal from (r-1,c-1)
Chris@83 193 min = m_m->getPathCost(r-1, c-1) + newCost * diagonalWeight;
Chris@72 194 dir = Matcher::AdvanceBoth;
Chris@72 195 }
Chris@72 196 if ((c >= prevRowStart) && (c < prevRowStop)) {
Chris@72 197 // vertical from (r-1,c)
Chris@72 198 double cost = m_m->getPathCost(r-1, c) + newCost;
Chris@72 199 if ((min < 0) || (cost < min)) {
Chris@72 200 min = cost;
Chris@72 201 dir = Matcher::AdvanceThis;
Chris@72 202 }
Chris@72 203 }
Chris@72 204 if (c > rowStart) {
Chris@72 205 // horizontal from (r,c-1)
Chris@72 206 double cost = m_m->getPathCost(r, c-1) + newCost;
Chris@72 207 if ((min < 0) || (cost < min)) {
Chris@72 208 min = cost;
Chris@72 209 dir = Matcher::AdvanceOther;
Chris@72 210 }
Chris@72 211 }
Chris@72 212
Chris@72 213 m_m->setPathCost(r, c, dir, min);
Chris@72 214
Chris@72 215 } else if (c > rowStart) { // first row
Chris@72 216 // horizontal from (r,c-1)
Chris@72 217 m_m->setPathCost(r, c, Matcher::AdvanceOther,
Chris@72 218 m_m->getPathCost(r, c-1) + newCost);
Chris@72 219 }
Chris@72 220 }
Chris@72 221
Chris@72 222 prevRowStart = rowStart;
Chris@72 223 prevRowStop = rowStop;
cannam@0 224 }
Chris@72 225 }
Chris@30 226
Chris@82 227 #ifdef PERFORM_ERROR_CHECKS
Chris@81 228 Finder::ErrorPosition
Chris@81 229 Finder::checkPathCostMatrix()
Chris@81 230 {
Chris@81 231 ErrorPosition err;
Chris@81 232
Chris@81 233 int r1 = 0;
Chris@81 234 int c1 = 0;
Chris@81 235 int r2 = m_m->getFrameCount() - 1;
Chris@81 236 int c2 = m_m->getOtherFrameCount() - 1;
Chris@81 237
Chris@81 238 if (r2 < r1 || c2 < c1) {
Chris@81 239 return err;
Chris@81 240 }
Chris@81 241
Chris@81 242 int prevRowStart = 0, prevRowStop = 0;
Chris@81 243
Chris@83 244 float diagonalWeight = m_m->getDiagonalWeight();
Chris@83 245
Chris@81 246 for (int r = r1; r <= r2; r++) {
Chris@81 247
Chris@81 248 pair<int, int> colRange = m_m->getColRange(r);
Chris@81 249
Chris@81 250 int rowStart = max(c1, colRange.first);
Chris@81 251 int rowStop = min(c2 + 1, colRange.second);
Chris@81 252
Chris@81 253 for (int c = rowStart; c < rowStop; c++) {
Chris@81 254
Chris@81 255 float newCost = m_m->getDistance(r, c);
Chris@81 256 double updateTo = -1.0;
Chris@81 257 Matcher::Advance dir = Matcher::AdvanceNone;
Chris@81 258
Chris@95 259 if (r > r1) { // not first row
Chris@81 260 double min = -1;
Chris@81 261 if ((c > prevRowStart) && (c <= prevRowStop)) {
Chris@81 262 // diagonal from (r-1,c-1)
Chris@83 263 min = m_m->getPathCost(r-1, c-1) + newCost * diagonalWeight;
Chris@81 264 err.prevCost = m_m->getPathCost(r-1, c-1);
Chris@83 265 err.distance = newCost * diagonalWeight;
Chris@81 266 dir = Matcher::AdvanceBoth;
Chris@81 267 }
Chris@81 268 if ((c >= prevRowStart) && (c < prevRowStop)) {
Chris@81 269 // vertical from (r-1,c)
Chris@81 270 double cost = m_m->getPathCost(r-1, c) + newCost;
Chris@81 271 if ((min < 0) || (cost < min)) {
Chris@81 272 min = cost;
Chris@81 273 err.prevCost = m_m->getPathCost(r-1, c);
Chris@81 274 err.distance = newCost;
Chris@81 275 dir = Matcher::AdvanceThis;
Chris@81 276 }
Chris@81 277 }
Chris@81 278 if (c > rowStart) {
Chris@81 279 // horizontal from (r,c-1)
Chris@81 280 double cost = m_m->getPathCost(r, c-1) + newCost;
Chris@81 281 if ((min < 0) || (cost < min)) {
Chris@81 282 min = cost;
Chris@81 283 err.prevCost = m_m->getPathCost(r, c-1);
Chris@81 284 err.distance = newCost;
Chris@81 285 dir = Matcher::AdvanceOther;
Chris@81 286 }
Chris@81 287 }
Chris@81 288
Chris@81 289 updateTo = min;
Chris@81 290
Chris@82 291 } else { // first row
Chris@82 292
Chris@82 293 if (c > rowStart) {
Chris@82 294 // horizontal from (r,c-1)
Chris@83 295 updateTo = m_m->getPathCost(r, c-1) + newCost;
Chris@83 296 err.prevCost = m_m->getPathCost(r, c-1);
Chris@83 297 err.distance = newCost;
Chris@82 298 dir = Matcher::AdvanceOther;
Chris@82 299 }
Chris@81 300 }
Chris@81 301
Chris@82 302 if (dir != Matcher::AdvanceNone) {
Chris@86 303 if (m_m->getAdvance(r, c) != dir) {
Chris@86 304 err.type = ErrorPosition::WrongAdvance;
Chris@86 305 err.r = r;
Chris@86 306 err.c = c;
Chris@86 307 err.costWas = m_m->getPathCost(r, c);
Chris@86 308 err.costShouldBe = updateTo;
Chris@86 309 err.advanceWas = m_m->getAdvance(r, c);
Chris@86 310 err.advanceShouldBe = dir;
Chris@86 311 return err;
Chris@86 312 }
Chris@84 313 if (m_m->getPathCost(r, c) != updateTo) {
Chris@84 314 err.type = ErrorPosition::WrongCost;
Chris@84 315 err.r = r;
Chris@84 316 err.c = c;
Chris@84 317 err.costWas = m_m->getPathCost(r, c);
Chris@84 318 err.costShouldBe = updateTo;
Chris@84 319 err.advanceWas = m_m->getAdvance(r, c);
Chris@84 320 err.advanceShouldBe = dir;
Chris@82 321 return err;
Chris@82 322 }
Chris@82 323 } else {
Chris@82 324 // AdvanceNone should occur only at r = r1, c = c1
Chris@82 325 if (r != r1 || c != c1) {
Chris@82 326 err.type = ErrorPosition::NoAdvance;
Chris@82 327 err.r = r;
Chris@82 328 err.c = c;
Chris@82 329 err.costWas = m_m->getPathCost(r, c);
Chris@82 330 err.costShouldBe = updateTo;
Chris@84 331 err.advanceWas = m_m->getAdvance(r, c);
Chris@84 332 err.advanceShouldBe = dir;
Chris@82 333 return err;
Chris@82 334 }
Chris@81 335 }
Chris@81 336 }
Chris@81 337
Chris@81 338 prevRowStart = rowStart;
Chris@81 339 prevRowStop = rowStop;
Chris@81 340 }
Chris@81 341
Chris@81 342 return err;
Chris@82 343 }
Chris@81 344
Chris@92 345 void
Chris@92 346 Finder::checkAndReport()
Chris@30 347 {
Chris@92 348 cerr << "Finder: Checking path-cost matrix..." << endl;
Chris@82 349 ErrorPosition err = checkPathCostMatrix();
Chris@92 350 if (err.type == ErrorPosition::NoError) {
Chris@92 351 cerr << "No errors found" << endl;
Chris@92 352 } else {
Chris@82 353 cerr << "\nWARNING: Checking path-cost matrix returned mismatch:" << endl;
Chris@92 354 cerr << "Type: " << err.type << ": ";
Chris@92 355 switch (err.type) {
Chris@92 356 case ErrorPosition::NoError: break;
Chris@92 357 case ErrorPosition::WrongCost: cerr << "WrongCost"; break;
Chris@92 358 case ErrorPosition::WrongAdvance: cerr << "WrongAdvance"; break;
Chris@92 359 case ErrorPosition::NoAdvance: cerr << "NoAdvance"; break;
Chris@92 360 }
Chris@92 361 cerr << endl;
Chris@84 362 cerr << "At row " << err.r << ", column " << err.c
Chris@84 363 << "\nShould be advancing "
Chris@84 364 << Matcher::advanceToString(err.advanceShouldBe)
Chris@84 365 << ", advance in matrix is "
Chris@84 366 << Matcher::advanceToString(err.advanceWas)
Chris@83 367 << "\nPrev cost " << err.prevCost
Chris@82 368 << " plus distance " << err.distance << " gives "
Chris@84 369 << err.costShouldBe << ", matrix contains " << err.costWas
Chris@83 370 << endl;
Chris@83 371 cerr << "Note: diagonal weight = " << m_m->getDiagonalWeight() << endl;
Chris@83 372 cerr << endl;
Chris@92 373
Chris@95 374 int w(4);
Chris@95 375 int ww(15);
Chris@92 376
Chris@92 377 cerr << "Distance matrix leading up to this point:" << endl;
Chris@95 378 cerr << setprecision(12) << setw(w) << "";
Chris@92 379 for (int i = -4; i <= 0; ++i) {
Chris@95 380 cerr << setw(ww) << i;
Chris@92 381 }
Chris@92 382 cerr << endl;
Chris@92 383 for (int j = -4; j <= 0; ++j) {
Chris@92 384 cerr << setw(w) << j;
Chris@92 385 for (int i = -4; i <= 0; ++i) {
Chris@95 386 cerr << setw(ww) << m_m->getDistance(err.r + j, err.c + i);
Chris@92 387 }
Chris@92 388 cerr << endl;
Chris@92 389 }
Chris@92 390 cerr << endl;
Chris@92 391
Chris@92 392 cerr << "Cost matrix leading up to this point:" << endl;
Chris@92 393 cerr << setw(w) << "";
Chris@92 394 for (int i = -4; i <= 0; ++i) {
Chris@95 395 cerr << setw(ww) << i;
Chris@92 396 }
Chris@92 397 cerr << endl;
Chris@92 398 for (int j = -4; j <= 0; ++j) {
Chris@92 399 cerr << setw(w) << j;
Chris@92 400 for (int i = -4; i <= 0; ++i) {
Chris@95 401 cerr << setw(ww) << m_m->getPathCost(err.r + j, err.c + i);
Chris@92 402 }
Chris@92 403 cerr << endl;
Chris@92 404 }
Chris@92 405 cerr << endl;
Chris@82 406 }
Chris@92 407 }
Chris@92 408 #endif
Chris@92 409
Chris@92 410 int
Chris@92 411 Finder::retrievePath(bool smooth, vector<int> &pathx, vector<int> &pathy)
Chris@92 412 {
Chris@92 413 pathx.clear();
Chris@92 414 pathy.clear();
Chris@92 415
Chris@92 416 #ifdef PERFORM_ERROR_CHECKS
Chris@92 417 checkAndReport();
Chris@82 418 #endif
Chris@82 419
Chris@72 420 int ex = m_m->getOtherFrameCount() - 1;
Chris@72 421 int ey = m_m->getFrameCount() - 1;
Chris@69 422
Chris@69 423 if (ex < 0 || ey < 0) {
Chris@69 424 return 0;
Chris@69 425 }
Chris@66 426
Chris@66 427 int x = ex;
Chris@66 428 int y = ey;
Chris@30 429
Chris@140 430 #ifdef DEBUG_FINDER
Chris@140 431 cerr << "*** retrievePath: smooth = " << smooth << endl;
Chris@140 432 cerr << "*** retrievePath: before: x = " << x << ", y = " << y << endl;
Chris@140 433 #endif
Chris@140 434
Chris@72 435 if (m_duration2 > 0 && m_duration2 < m_m->getOtherFrameCount()) {
Chris@72 436 x = m_duration2 - 1;
Chris@60 437 }
Chris@72 438 if (m_duration1 > 0 && m_duration1 < m_m->getFrameCount()) {
Chris@72 439 y = m_duration1 - 1;
Chris@60 440 }
Chris@60 441
Chris@72 442 if (!m_m->isAvailable(y, x)) {
Chris@66 443 // Path did not pass through the expected end point --
Chris@66 444 // probably means the pieces are substantially different in
Chris@66 445 // the later bits. Reset the expected end point to the end of
Chris@66 446 // both files including any trailing silence.
Chris@66 447 cerr << "NOTE: Path did not pass through expected end point, inputs are probably significantly different" << endl;
Chris@66 448 x = ex;
Chris@66 449 y = ey;
Chris@66 450 }
Chris@66 451
Chris@55 452 recalculatePathCostMatrix(0, 0, y, x);
Chris@55 453
Chris@140 454 #ifdef DEBUG_FINDER
Chris@140 455 cerr << "*** retrievePath: start: x = " << x << ", y = " << y << endl;
Chris@140 456 #endif
Chris@66 457
Chris@72 458 while (m_m->isAvailable(y, x) && (x > 0 || y > 0)) {
Chris@30 459
Chris@33 460 // cerr << "x = " << x << ", y = " << y;
Chris@33 461
Chris@30 462 pathx.push_back(x);
Chris@30 463 pathy.push_back(y);
Chris@30 464
Chris@72 465 switch (m_m->getAdvance(y, x)) {
Chris@45 466 case Matcher::AdvanceThis:
Chris@70 467 // cerr << ", going down (dist = " << getDistance() << ")" << endl;
Chris@33 468 y--;
Chris@33 469 break;
Chris@45 470 case Matcher::AdvanceOther:
Chris@70 471 // cerr << ", going left (dist = " << getDistance() << ")" << endl;
Chris@33 472 x--;
Chris@33 473 break;
Chris@45 474 case Matcher::AdvanceBoth:
Chris@70 475 // cerr << ", going diag (dist = " << getDistance() << ")" << endl;
Chris@33 476 x--;
Chris@33 477 y--;
Chris@33 478 break;
Chris@45 479 case Matcher::AdvanceNone: // this would indicate a bug, but we wouldn't want to hang
Chris@69 480 cerr << "WARNING: Neither matcher advanced in path backtrack at (" << x << "," << y << ")" << endl;
Chris@33 481 if (x > y) {
Chris@33 482 x--;
Chris@33 483 } else {
Chris@33 484 y--;
Chris@33 485 }
Chris@33 486 break;
Chris@30 487 }
Chris@30 488 }
Chris@30 489
Chris@72 490 if (x > 0 || y > 0) {
Chris@72 491 cerr << "WARNING: Ran out of available path at (" << y << "," << x
Chris@72 492 << ")!" << endl;
Chris@72 493 }
Chris@72 494
Chris@72 495 reverse(pathx.begin(), pathx.end());
Chris@72 496 reverse(pathy.begin(), pathy.end());
Chris@30 497
Chris@31 498 if (smooth) {
Chris@31 499 int smoothedLen = Path().smooth(pathx, pathy, pathx.size());
Chris@31 500 return smoothedLen;
Chris@31 501 } else {
Chris@31 502 return pathx.size();
Chris@31 503 }
Chris@30 504 }
Chris@30 505
Chris@30 506