1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006 Martin Gasser.
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
16 #include "ChangeDetectionFunction.h"
19 #define PI (3.14159265358979232846)
24 ChangeDetectionFunction::ChangeDetectionFunction(ChangeDFConfig config) :
25 m_dFilterSigma(0.0), m_iFilterWidth(0)
27 setFilterWidth(config.smoothingWidth);
30 ChangeDetectionFunction::~ChangeDetectionFunction()
34 void ChangeDetectionFunction::setFilterWidth(const int iWidth)
36 m_iFilterWidth = iWidth*2+1;
38 // it is assumed that the gaussian is 0 outside of +/- FWHM
39 // => filter width = 2*FWHM = 2*2.3548*sigma
40 m_dFilterSigma = double(m_iFilterWidth) / double(2*2.3548);
41 m_vaGaussian.resize(m_iFilterWidth);
43 double dScale = 1.0 / (m_dFilterSigma*sqrt(2*PI));
45 for (int x = -(m_iFilterWidth-1)/2; x <= (m_iFilterWidth-1)/2; x++)
47 double w = dScale * std::exp ( -(x*x)/(2*m_dFilterSigma*m_dFilterSigma) );
48 m_vaGaussian[x + (m_iFilterWidth-1)/2] = w;
51 #ifdef DEBUG_CHANGE_DETECTION_FUNCTION
52 std::cerr << "Filter sigma: " << m_dFilterSigma << std::endl;
53 std::cerr << "Filter width: " << m_iFilterWidth << std::endl;
58 ChangeDistance ChangeDetectionFunction::process(const TCSGram& rTCSGram)
60 ChangeDistance retVal;
61 retVal.resize(rTCSGram.getSize(), 0.0);
63 TCSGram smoothedTCSGram;
65 for (int iPosition = 0; iPosition < rTCSGram.getSize(); iPosition++)
69 int iLowerPos = iPosition - (m_iFilterWidth-1)/2;
70 int iUpperPos = iPosition + (m_iFilterWidth-1)/2;
74 iSkipLower = -iLowerPos;
78 if (iUpperPos >= rTCSGram.getSize())
80 int iMaxIndex = rTCSGram.getSize() - 1;
81 iUpperPos = iMaxIndex;
84 TCSVector smoothedVector;
86 // for every bin of the vector, calculate the smoothed value
87 for (int iPC = 0; iPC < 6; iPC++)
90 double dSmoothedValue = 0.0;
93 for (int i = iLowerPos; i <= iUpperPos; i++)
95 rTCSGram.getTCSVector(i, rCV);
96 dSmoothedValue += m_vaGaussian[iSkipLower + j++] * rCV[iPC];
99 smoothedVector[iPC] = dSmoothedValue;
102 smoothedTCSGram.addTCSVector(smoothedVector);
105 for (int iPosition = 0; iPosition < rTCSGram.getSize(); iPosition++)
108 TODO: calculate a confidence measure for the current estimation
109 if the current estimate is not confident enough, look further into the future/the past
110 e.g., High frequency content, zero crossing rate, spectral flatness
114 TCSVector previousTCS;
118 // while (previousTCS.magnitude() < 0.1 && (iPosition-iWindow) > 0)
120 smoothedTCSGram.getTCSVector(iPosition-iWindow, previousTCS);
121 // std::cout << previousTCS.magnitude() << std::endl;
127 // while (nextTCS.magnitude() < 0.1 && (iPosition+iWindow) < (rTCSGram.getSize()-1) )
129 smoothedTCSGram.getTCSVector(iPosition+iWindow, nextTCS);
133 double distance = 0.0;
134 // Euclidean distance
135 for (size_t j = 0; j < 6; j++)
137 distance += std::pow(nextTCS[j] - previousTCS[j], 2.0);
140 retVal[iPosition] = std::pow(distance, 0.5);