annotate maths/CosineDistance.cpp @ 298:255e431ae3d4

* Key detector: when returning key strengths, use the peak value of the three underlying chromagram correlations (from 36-bin chromagram) corresponding to each key, instead of the mean. Rationale: This is the same method as used when returning the key value, and it's nice to have the same results in both returned value and plot. The peak performed better than the sum with a simple test set of triads, so it seems reasonable to change the plot to match the key output rather than the other way around. * FFT: kiss_fftr returns only the non-conjugate bins, synthesise the rest rather than leaving them (perhaps dangerously) undefined. Fixes an uninitialised data error in chromagram that could cause garbage results from key detector. * Constant Q: remove precalculated values again, I reckon they're not proving such a good tradeoff.
author Chris Cannam <c.cannam@qmul.ac.uk>
date Fri, 05 Jun 2009 15:12:39 +0000
parents 43943a4382ef
children 769da847732b
rev   line source
c@256 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@256 2
c@256 3 /*
c@256 4 QM DSP Library
c@256 5
c@256 6 Centre for Digital Music, Queen Mary, University of London.
c@256 7 This file copyright 2008 Kurt Jacobson.
c@256 8 All rights reserved.
c@256 9 */
c@256 10
c@256 11 #include "CosineDistance.h"
c@256 12
c@256 13 #include <iostream>
c@256 14 #include <limits>
c@256 15
c@256 16 using std::cerr;
c@256 17
c@256 18 double CosineDistance::distance(const vector<double> &v1,
c@256 19 const vector<double> &v2)
c@256 20 {
c@256 21 dist = 1.0; dDenTot = 0; dDen1 = 0; dDen2 = 0; dSum1 =0;
c@256 22
c@256 23 //check if v1, v2 same size
c@256 24 if (v1.size() != v2.size())
c@256 25 {
c@256 26 cerr << "CosineDistance::distance: ERROR: vectors not the same size\n";
c@256 27 return 1.0;
c@256 28 }
c@256 29 else
c@256 30 {
c@256 31 for(int i=0; i<v1.size(); i++)
c@256 32 {
c@256 33 dSum1 += v1[i]*v2[i];
c@256 34 dDen1 += v1[i]*v1[i];
c@256 35 dDen2 += v2[i]*v2[i];
c@256 36 }
c@256 37 dDenTot = sqrt(fabs(dDen1*dDen2));
c@256 38 if(dDenTot == 0)
c@256 39 {
c@256 40 cerr << "CosineDistance::distance: WARNING: dividing by zero in cosine dist\n";
c@256 41 return 1.0;
c@256 42 }
c@256 43
c@256 44 dist = 1-((dSum1)/dDenTot);
c@256 45 return dist;
c@256 46 }
c@256 47 }