Mercurial > hg > match-vamp
comparison Matcher.cpp @ 26:9f60d097f0b2
Pull out DistanceMetric into its own class
author | Chris Cannam |
---|---|
date | Fri, 31 Oct 2014 11:31:08 +0000 |
parents | 64c4c0cf80c9 |
children | cd0ffe6b1e68 |
comparison
equal
deleted
inserted
replaced
25:e6a143e4e143 | 26:9f60d097f0b2 |
---|---|
24 bool Matcher::silent = true; | 24 bool Matcher::silent = true; |
25 | 25 |
26 //#define DEBUG_MATCHER 1 | 26 //#define DEBUG_MATCHER 1 |
27 | 27 |
28 Matcher::Matcher(Parameters parameters, Matcher *p) : | 28 Matcher::Matcher(Parameters parameters, Matcher *p) : |
29 params(parameters) | 29 params(parameters), |
30 metric(parameters.distanceNorm) | |
30 { | 31 { |
31 #ifdef DEBUG_MATCHER | 32 #ifdef DEBUG_MATCHER |
32 cerr << "Matcher::Matcher(" << params.sampleRate << ", " << p << ")" << endl; | 33 cerr << "Matcher::Matcher(" << params.sampleRate << ", " << p << ")" << endl; |
33 #endif | 34 #endif |
34 | 35 |
56 initialised = false; | 57 initialised = false; |
57 } | 58 } |
58 | 59 |
59 Matcher::Matcher(Parameters parameters, Matcher *p, int featureSize) : | 60 Matcher::Matcher(Parameters parameters, Matcher *p, int featureSize) : |
60 params(parameters), | 61 params(parameters), |
61 externalFeatureSize(featureSize) | 62 externalFeatureSize(featureSize), |
63 metric(parameters.distanceNorm) | |
62 { | 64 { |
63 #ifdef DEBUG_MATCHER | 65 #ifdef DEBUG_MATCHER |
64 cerr << "Matcher::Matcher(" << params.sampleRate << ", " << p << ", " << featureSize << ")" << endl; | 66 cerr << "Matcher::Matcher(" << params.sampleRate << ", " << p << ", " << featureSize << ")" << endl; |
65 #endif | 67 #endif |
66 | 68 |
390 | 392 |
391 bool overflow = false; | 393 bool overflow = false; |
392 int mn= -1; | 394 int mn= -1; |
393 int mx= -1; | 395 int mx= -1; |
394 for ( ; index < stop; index++) { | 396 for ( ; index < stop; index++) { |
395 int dMN = calcDistance(frames[frameIndex], | 397 |
396 otherMatcher->frames[index % blockSize]); | 398 int dMN = metric.calcDistanceScaled |
399 (frames[frameIndex], | |
400 otherMatcher->frames[index % blockSize], | |
401 scale); | |
402 | |
397 if (mx<0) | 403 if (mx<0) |
398 mx = mn = dMN; | 404 mx = mn = dMN; |
399 else if (dMN > mx) | 405 else if (dMN > mx) |
400 mx = dMN; | 406 mx = dMN; |
401 else if (dMN < mn) | 407 else if (dMN < mn) |
402 mn = dMN; | 408 mn = dMN; |
403 if (dMN >= 255) { | 409 if (dMN >= 255) { |
404 overflow = true; | 410 overflow = true; |
405 dMN = 255; | 411 dMN = 255; |
406 } | 412 } |
413 | |
407 if ((frameCount == 0) && (index == 0)) // first element | 414 if ((frameCount == 0) && (index == 0)) // first element |
408 setValue(0, 0, 0, 0, dMN); | 415 setValue(0, 0, 0, 0, dMN); |
409 else if (frameCount == 0) // first row | 416 else if (frameCount == 0) // first row |
410 setValue(0, index, ADVANCE_OTHER, | 417 setValue(0, index, ADVANCE_OTHER, |
411 getValue(0, index-1, true), dMN); | 418 getValue(0, index-1, true), dMN); |
459 if (!silent) | 466 if (!silent) |
460 std::cerr << "Frame " << frameCount << ", d = " << (mx-mn) << std::endl; | 467 std::cerr << "Frame " << frameCount << ", d = " << (mx-mn) << std::endl; |
461 } | 468 } |
462 | 469 |
463 int | 470 int |
464 Matcher::calcDistance(const vector<double> &f1, const vector<double> &f2) | |
465 { | |
466 double d = 0; | |
467 double sum = 0; | |
468 for (int i = 0; i < featureSize; i++) { | |
469 d += fabs(f1[i] - f2[i]); | |
470 sum += f1[i] + f2[i]; | |
471 } | |
472 // System.err.print(" " + Format.d(d,3)); | |
473 if (sum == 0) | |
474 return 0; | |
475 if (params.distanceNorm == NormaliseDistanceToSum) | |
476 return (int)(scale * d / sum); // 0 <= d/sum <= 2 | |
477 if (params.distanceNorm != NormaliseDistanceToLogSum) | |
478 return (int)(scale * d); | |
479 | |
480 // note if this were to be restored, it would have to use | |
481 // totalEnergies vector instead of f1[freqMapSize] which used to | |
482 // store the total energy: | |
483 // double weight = (5 + Math.log(f1[freqMapSize] + f2[freqMapSize]))/10.0; | |
484 | |
485 double weight = (8 + log(sum)) / 10.0; | |
486 // if (weight < mins) { | |
487 // mins = weight; | |
488 // System.err.println(Format.d(mins,3) + " " + Format.d(maxs)); | |
489 // } | |
490 // if (weight > maxs) { | |
491 // maxs = weight; | |
492 // System.err.println(Format.d(mins,3) + " " + Format.d(maxs)); | |
493 // } | |
494 if (weight < 0) | |
495 weight = 0; | |
496 else if (weight > 1) | |
497 weight = 1; | |
498 return (int)(scale * d / sum * weight); | |
499 } // calcDistance() | |
500 | |
501 int | |
502 Matcher::getValue(int i, int j, bool firstAttempt) | 471 Matcher::getValue(int i, int j, bool firstAttempt) |
503 { | 472 { |
504 if (firstPM) | 473 if (firstPM) |
505 return bestPathCost[i][j - first[i]]; | 474 return bestPathCost[i][j - first[i]]; |
506 else | 475 else |