annotate src/Finder.cpp @ 154:4159f6b71942 structure

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