# HG changeset patch # User Chris Cannam # Date 1559296948 -3600 # Node ID cbe668c7d7249604456b1114b35a5069a967bd8e # Parent de5f557a270fb7645be3da0ab494d9a644259d43 Untabify, indent, tidy diff -r de5f557a270f -r cbe668c7d724 dsp/mfcc/MFCC.cpp --- a/dsp/mfcc/MFCC.cpp Fri May 31 10:54:02 2019 +0100 +++ b/dsp/mfcc/MFCC.cpp Fri May 31 11:02:28 2019 +0100 @@ -236,7 +236,7 @@ tmp = tmp + (mfccFilterWeights[i][j] * fftMag[j]); } if (tmp > 0) earMag[i] = log10(tmp); - else earMag[i] = 0.0; + else earMag[i] = 0.0; if (logPower != 1.0) { earMag[i] = pow(earMag[i], logPower); @@ -259,9 +259,7 @@ } outceps[i] = tmp; } - } - else - { + } else { for (i = 1; i < nceps+1; i++) { double tmp = 0.; for (j = 0; j < totalFilters; j++){ diff -r de5f557a270f -r cbe668c7d724 dsp/tonal/ChangeDetectionFunction.cpp --- a/dsp/tonal/ChangeDetectionFunction.cpp Fri May 31 10:54:02 2019 +0100 +++ b/dsp/tonal/ChangeDetectionFunction.cpp Fri May 31 11:02:28 2019 +0100 @@ -19,12 +19,10 @@ #define PI (3.14159265358979232846) #endif - - ChangeDetectionFunction::ChangeDetectionFunction(ChangeDFConfig config) : - m_dFilterSigma(0.0), m_iFilterWidth(0) + m_dFilterSigma(0.0), m_iFilterWidth(0) { - setFilterWidth(config.smoothingWidth); + setFilterWidth(config.smoothingWidth); } ChangeDetectionFunction::~ChangeDetectionFunction() @@ -33,112 +31,107 @@ void ChangeDetectionFunction::setFilterWidth(const int iWidth) { - m_iFilterWidth = iWidth*2+1; - - // it is assumed that the gaussian is 0 outside of +/- FWHM - // => filter width = 2*FWHM = 2*2.3548*sigma - m_dFilterSigma = double(m_iFilterWidth) / double(2*2.3548); - m_vaGaussian.resize(m_iFilterWidth); - - double dScale = 1.0 / (m_dFilterSigma*sqrt(2*PI)); - - for (int x = -(m_iFilterWidth-1)/2; x <= (m_iFilterWidth-1)/2; x++) - { - double w = dScale * std::exp ( -(x*x)/(2*m_dFilterSigma*m_dFilterSigma) ); - m_vaGaussian[x + (m_iFilterWidth-1)/2] = w; - } - + m_iFilterWidth = iWidth*2+1; + + // it is assumed that the gaussian is 0 outside of +/- FWHM + // => filter width = 2*FWHM = 2*2.3548*sigma + m_dFilterSigma = double(m_iFilterWidth) / double(2*2.3548); + m_vaGaussian.resize(m_iFilterWidth); + + double dScale = 1.0 / (m_dFilterSigma*sqrt(2*PI)); + + for (int x = -(m_iFilterWidth-1)/2; x <= (m_iFilterWidth-1)/2; x++) { + double w = dScale * std::exp ( -(x*x)/(2*m_dFilterSigma*m_dFilterSigma) ); + m_vaGaussian[x + (m_iFilterWidth-1)/2] = w; + } + #ifdef DEBUG_CHANGE_DETECTION_FUNCTION - std::cerr << "Filter sigma: " << m_dFilterSigma << std::endl; - std::cerr << "Filter width: " << m_iFilterWidth << std::endl; + std::cerr << "Filter sigma: " << m_dFilterSigma << std::endl; + std::cerr << "Filter width: " << m_iFilterWidth << std::endl; #endif } ChangeDistance ChangeDetectionFunction::process(const TCSGram& rTCSGram) { - ChangeDistance retVal; - retVal.resize(rTCSGram.getSize(), 0.0); - - TCSGram smoothedTCSGram; + ChangeDistance retVal; + retVal.resize(rTCSGram.getSize(), 0.0); + + TCSGram smoothedTCSGram; - for (int iPosition = 0; iPosition < rTCSGram.getSize(); iPosition++) - { - int iSkipLower = 0; - - int iLowerPos = iPosition - (m_iFilterWidth-1)/2; - int iUpperPos = iPosition + (m_iFilterWidth-1)/2; - - if (iLowerPos < 0) - { - iSkipLower = -iLowerPos; - iLowerPos = 0; - } - - if (iUpperPos >= rTCSGram.getSize()) - { - int iMaxIndex = rTCSGram.getSize() - 1; - iUpperPos = iMaxIndex; - } - - TCSVector smoothedVector; + for (int iPosition = 0; iPosition < rTCSGram.getSize(); iPosition++) { + + int iSkipLower = 0; + + int iLowerPos = iPosition - (m_iFilterWidth-1)/2; + int iUpperPos = iPosition + (m_iFilterWidth-1)/2; + + if (iLowerPos < 0) { + iSkipLower = -iLowerPos; + iLowerPos = 0; + } + + if (iUpperPos >= rTCSGram.getSize()) { + int iMaxIndex = rTCSGram.getSize() - 1; + iUpperPos = iMaxIndex; + } + + TCSVector smoothedVector; - // for every bin of the vector, calculate the smoothed value - for (int iPC = 0; iPC < 6; iPC++) - { - size_t j = 0; - double dSmoothedValue = 0.0; - TCSVector rCV; - - for (int i = iLowerPos; i <= iUpperPos; i++) - { - rTCSGram.getTCSVector(i, rCV); - dSmoothedValue += m_vaGaussian[iSkipLower + j++] * rCV[iPC]; - } + // for every bin of the vector, calculate the smoothed value + for (int iPC = 0; iPC < 6; iPC++) { - smoothedVector[iPC] = dSmoothedValue; - } - - smoothedTCSGram.addTCSVector(smoothedVector); - } + size_t j = 0; + double dSmoothedValue = 0.0; + TCSVector rCV; + + for (int i = iLowerPos; i <= iUpperPos; i++) { + rTCSGram.getTCSVector(i, rCV); + dSmoothedValue += m_vaGaussian[iSkipLower + j++] * rCV[iPC]; + } - for (int iPosition = 0; iPosition < rTCSGram.getSize(); iPosition++) - { - /* - TODO: calculate a confidence measure for the current estimation - if the current estimate is not confident enough, look further into the future/the past - e.g., High frequency content, zero crossing rate, spectral flatness - */ - - TCSVector nextTCS; - TCSVector previousTCS; - - int iWindow = 1; + smoothedVector[iPC] = dSmoothedValue; + } + + smoothedTCSGram.addTCSVector(smoothedVector); + } - // while (previousTCS.magnitude() < 0.1 && (iPosition-iWindow) > 0) - { - smoothedTCSGram.getTCSVector(iPosition-iWindow, previousTCS); - // std::cout << previousTCS.magnitude() << std::endl; - iWindow++; - } - - iWindow = 1; - - // while (nextTCS.magnitude() < 0.1 && (iPosition+iWindow) < (rTCSGram.getSize()-1) ) - { - smoothedTCSGram.getTCSVector(iPosition+iWindow, nextTCS); - iWindow++; - } + for (int iPosition = 0; iPosition < rTCSGram.getSize(); iPosition++) { + + /* + TODO: calculate a confidence measure for the current estimation + if the current estimate is not confident enough, look further into the future/the past + e.g., High frequency content, zero crossing rate, spectral flatness + */ + + TCSVector nextTCS; + TCSVector previousTCS; + + int iWindow = 1; - double distance = 0.0; - // Euclidean distance - for (size_t j = 0; j < 6; j++) - { - distance += std::pow(nextTCS[j] - previousTCS[j], 2.0); - } - - retVal[iPosition] = std::pow(distance, 0.5); - } + // while (previousTCS.magnitude() < 0.1 && (iPosition-iWindow) > 0) + { + smoothedTCSGram.getTCSVector(iPosition-iWindow, previousTCS); + // std::cout << previousTCS.magnitude() << std::endl; + iWindow++; + } + + iWindow = 1; + + // while (nextTCS.magnitude() < 0.1 && (iPosition+iWindow) < (rTCSGram.getSize()-1) ) + { + smoothedTCSGram.getTCSVector(iPosition+iWindow, nextTCS); + iWindow++; + } - return retVal; + double distance = 0.0; + // Euclidean distance + for (size_t j = 0; j < 6; j++) { + distance += std::pow(nextTCS[j] - previousTCS[j], 2.0); + } + + retVal[iPosition] = std::pow(distance, 0.5); + } + + return retVal; } diff -r de5f557a270f -r cbe668c7d724 dsp/tonal/ChangeDetectionFunction.h --- a/dsp/tonal/ChangeDetectionFunction.h Fri May 31 10:54:02 2019 +0100 +++ b/dsp/tonal/ChangeDetectionFunction.h Fri May 31 11:02:28 2019 +0100 @@ -16,33 +16,31 @@ #ifndef _CHANGEDETECTIONFUNCTION_ #define _CHANGEDETECTIONFUNCTION_ -//#define DEBUG_CHANGE_DETECTION_FUNCTION 1 - #include "TCSgram.h" #include using std::valarray; -typedef valarray ChangeDistance; +typedef valarray ChangeDistance; struct ChangeDFConfig { - int smoothingWidth; + int smoothingWidth; }; class ChangeDetectionFunction { public: - ChangeDetectionFunction(ChangeDFConfig); - ~ChangeDetectionFunction(); - ChangeDistance process(const TCSGram& rTCSGram); + ChangeDetectionFunction(ChangeDFConfig); + ~ChangeDetectionFunction(); + ChangeDistance process(const TCSGram& rTCSGram); private: - void setFilterWidth(const int iWidth); - + void setFilterWidth(const int iWidth); + private: - valarray m_vaGaussian; - double m_dFilterSigma; - int m_iFilterWidth; + valarray m_vaGaussian; + double m_dFilterSigma; + int m_iFilterWidth; }; #endif // _CHANGDETECTIONFUNCTION_ diff -r de5f557a270f -r cbe668c7d724 dsp/tonal/TCSgram.cpp --- a/dsp/tonal/TCSgram.cpp Fri May 31 10:54:02 2019 +0100 +++ b/dsp/tonal/TCSgram.cpp Fri May 31 11:02:28 2019 +0100 @@ -23,7 +23,7 @@ #include "maths/MathUtilities.h" TCSGram::TCSGram() : - m_uNumBins(6) + m_uNumBins(6) { } @@ -34,44 +34,44 @@ void TCSGram::getTCSVector(int iPosition, TCSVector& rTCSVector) const { - if (iPosition < 0) - rTCSVector = TCSVector(); - else if (iPosition >= int(m_VectorList.size())) - rTCSVector = TCSVector(); - else - rTCSVector = m_VectorList[iPosition].second; + if (iPosition < 0) { + rTCSVector = TCSVector(); + } else if (iPosition >= int(m_VectorList.size())) { + rTCSVector = TCSVector(); + } else { + rTCSVector = m_VectorList[iPosition].second; + } } long TCSGram::getTime(size_t uPosition) const { - return m_VectorList[uPosition].first; + return m_VectorList[uPosition].first; } void TCSGram::addTCSVector(const TCSVector& rTCSVector) { - size_t uSize = m_VectorList.size(); - long lMilliSeconds = static_cast(uSize*m_dFrameDurationMS); - std::pair p; - p.first = lMilliSeconds; - p.second = rTCSVector; - - m_VectorList.push_back(p); + size_t uSize = m_VectorList.size(); + long lMilliSeconds = static_cast(uSize*m_dFrameDurationMS); + std::pair p; + p.first = lMilliSeconds; + p.second = rTCSVector; + + m_VectorList.push_back(p); } long TCSGram::getDuration() const { - size_t uSize = m_VectorList.size(); - return static_cast(uSize*m_dFrameDurationMS); + size_t uSize = m_VectorList.size(); + return static_cast(uSize*m_dFrameDurationMS); } void TCSGram::printDebug() { - vectorlist_t::iterator vectorIterator = m_VectorList.begin(); - - while (vectorIterator != m_VectorList.end()) - { - vectorIterator->second.printDebug(); - vectorIterator++; - } + vectorlist_t::iterator vectorIterator = m_VectorList.begin(); + + while (vectorIterator != m_VectorList.end()) { + vectorIterator->second.printDebug(); + vectorIterator++; + } } diff -r de5f557a270f -r cbe668c7d724 dsp/tonal/TCSgram.h --- a/dsp/tonal/TCSgram.h Fri May 31 10:54:02 2019 +0100 +++ b/dsp/tonal/TCSgram.h Fri May 31 11:02:28 2019 +0100 @@ -26,24 +26,28 @@ class TCSGram { -public: - TCSGram(); - ~TCSGram(); - void getTCSVector(int, TCSVector&) const; - void addTCSVector(const TCSVector&); - long getTime(size_t) const; - long getDuration() const; - void printDebug(); - int getSize() const { return m_VectorList.size(); } - void reserve(size_t uSize) { m_VectorList.reserve(uSize); } - void clear() { m_VectorList.clear(); } - void setFrameDuration(const double dFrameDurationMS) { m_dFrameDurationMS = dFrameDurationMS; } - void setNumBins(const unsigned int uNumBins) { m_uNumBins = uNumBins; } - void normalize(); +public: + TCSGram(); + ~TCSGram(); + void getTCSVector(int, TCSVector&) const; + void addTCSVector(const TCSVector&); + long getTime(size_t) const; + long getDuration() const; + void printDebug(); + int getSize() const { return m_VectorList.size(); } + void reserve(size_t uSize) { m_VectorList.reserve(uSize); } + void clear() { m_VectorList.clear(); } + void setFrameDuration(const double dFrameDurationMS) { + m_dFrameDurationMS = dFrameDurationMS; + } + void setNumBins(const unsigned int uNumBins) { + m_uNumBins = uNumBins; + } + void normalize(); protected: - vectorlist_t m_VectorList; - unsigned int m_uNumBins; - double m_dFrameDurationMS; + vectorlist_t m_VectorList; + unsigned int m_uNumBins; + double m_dFrameDurationMS; }; #endif diff -r de5f557a270f -r cbe668c7d724 dsp/tonal/TonalEstimator.cpp --- a/dsp/tonal/TonalEstimator.cpp Fri May 31 10:54:02 2019 +0100 +++ b/dsp/tonal/TonalEstimator.cpp Fri May 31 11:02:28 2019 +0100 @@ -24,61 +24,55 @@ TonalEstimator::TonalEstimator() { - m_Basis.resize(6); + m_Basis.resize(6); - int i = 0; - - - // circle of fifths - m_Basis[i].resize(12); - for (int iP = 0; iP < 12; iP++) - { - m_Basis[i][iP] = std::sin( (7.0 / 6.0) * iP * PI); - } - - i++; + int i = 0; + + + // circle of fifths + m_Basis[i].resize(12); + for (int iP = 0; iP < 12; iP++) { + m_Basis[i][iP] = std::sin( (7.0 / 6.0) * iP * PI); + } + + i++; - m_Basis[i].resize(12); - for (int iP = 0; iP < 12; iP++) - { - m_Basis[i][iP] = std::cos( (7.0 / 6.0) * iP * PI); - } - - i++; - - - // circle of major thirds - m_Basis[i].resize(12); - for (int iP = 0; iP < 12; iP++) - { - m_Basis[i][iP] = 0.6 * std::sin( (2.0 / 3.0) * iP * PI); - } - - i++; + m_Basis[i].resize(12); + for (int iP = 0; iP < 12; iP++) { + m_Basis[i][iP] = std::cos( (7.0 / 6.0) * iP * PI); + } + + i++; + + + // circle of major thirds + m_Basis[i].resize(12); + for (int iP = 0; iP < 12; iP++) { + m_Basis[i][iP] = 0.6 * std::sin( (2.0 / 3.0) * iP * PI); + } + + i++; - m_Basis[i].resize(12); - for (int iP = 0; iP < 12; iP++) - { - m_Basis[i][iP] = 0.6 * std::cos( (2.0 / 3.0) * iP * PI); - } + m_Basis[i].resize(12); + for (int iP = 0; iP < 12; iP++) { + m_Basis[i][iP] = 0.6 * std::cos( (2.0 / 3.0) * iP * PI); + } - i++; + i++; - // circle of minor thirds - m_Basis[i].resize(12); - for (int iP = 0; iP < 12; iP++) - { - m_Basis[i][iP] = 1.1 * std::sin( (3.0 / 2.0) * iP * PI); - } - - i++; + // circle of minor thirds + m_Basis[i].resize(12); + for (int iP = 0; iP < 12; iP++) { + m_Basis[i][iP] = 1.1 * std::sin( (3.0 / 2.0) * iP * PI); + } + + i++; - m_Basis[i].resize(12); - for (int iP = 0; iP < 12; iP++) - { - m_Basis[i][iP] = 1.1 * std::cos( (3.0 / 2.0) * iP * PI); - } + m_Basis[i].resize(12); + for (int iP = 0; iP < 12; iP++) { + m_Basis[i][iP] = 1.1 * std::cos( (3.0 / 2.0) * iP * PI); + } } @@ -88,16 +82,14 @@ TCSVector TonalEstimator::transform2TCS(const ChromaVector& rVector) { - TCSVector vaRetVal; - vaRetVal.resize(6, 0.0); - - for (int i = 0; i < 6; i++) - { - for (int iP = 0; iP < 12; iP++) - { - vaRetVal[i] += m_Basis[i][iP] * rVector[iP]; - } - } - - return vaRetVal; + TCSVector vaRetVal; + vaRetVal.resize(6, 0.0); + + for (int i = 0; i < 6; i++) { + for (int iP = 0; iP < 12; iP++) { + vaRetVal[i] += m_Basis[i][iP] * rVector[iP]; + } + } + + return vaRetVal; } diff -r de5f557a270f -r cbe668c7d724 dsp/tonal/TonalEstimator.h --- a/dsp/tonal/TonalEstimator.h Fri May 31 10:54:02 2019 +0100 +++ b/dsp/tonal/TonalEstimator.h Fri May 31 11:02:28 2019 +0100 @@ -25,81 +25,68 @@ class ChromaVector : public std::valarray { public: - ChromaVector(size_t uSize = 12) : std::valarray() - { resize(uSize, 0.0f); } - - virtual ~ChromaVector() {}; - - void printDebug() - { - for (int i = 0; i < int(size()); i++) - { - std::cout << (*this)[i] << ";"; - } - - std::cout << std::endl; - } - - void normalizeL1() - { - // normalize the chroma vector (L1 norm) - double dSum = 0.0; - - for (size_t i = 0; i < 12; (dSum += std::abs((*this)[i++]))) ; - for (size_t i = 0; i < 12; dSum > 0.0000001?((*this)[i] /= dSum):(*this)[i]=0.0, i++) ; + ChromaVector(size_t uSize = 12) : std::valarray() { + resize(uSize, 0.0f); + } + + virtual ~ChromaVector() {}; + + void printDebug() { + for (int i = 0; i < int(size()); i++) { + std::cout << (*this)[i] << ";"; + } + std::cout << std::endl; + } + + void normalizeL1() { + // normalize the chroma vector (L1 norm) + double dSum = 0.0; + + for (size_t i = 0; i < 12; (dSum += std::abs((*this)[i++]))) ; + for (size_t i = 0; i < 12; dSum > 0.0000001?((*this)[i] /= dSum):(*this)[i]=0.0, i++) ; + } - } - - void clear() - { + void clear() { for (size_t i = 0; i < 12; ++i) (*this)[i] = 0.0; } - - }; class TCSVector : public std::valarray { public: - TCSVector() : std::valarray() - { resize(6, 0.0f); } - - virtual ~TCSVector() {}; + TCSVector() : std::valarray() { + resize(6, 0.0f); + } + + virtual ~TCSVector() {}; - void printDebug() - { - for (int i = 0; i < int(size()); i++) - { - std::cout << (*this)[i] << ";"; - } - - std::cout << std::endl; - } - - double magnitude() const - { - double dMag = 0.0; - - for (size_t i = 0; i < 6; i++) - { - dMag += std::pow((*this)[i], 2.0); - } - - return std::sqrt(dMag); - } - + void printDebug() { + for (int i = 0; i < int(size()); i++) { + std::cout << (*this)[i] << ";"; + } + std::cout << std::endl; + } + + double magnitude() const { + double dMag = 0.0; + + for (size_t i = 0; i < 6; i++) { + dMag += std::pow((*this)[i], 2.0); + } + + return std::sqrt(dMag); + } }; - - class TonalEstimator { public: - TonalEstimator(); - virtual ~TonalEstimator(); - TCSVector transform2TCS(const ChromaVector& rVector); + TonalEstimator(); + virtual ~TonalEstimator(); + TCSVector transform2TCS(const ChromaVector& rVector); + protected: - std::valarray< std::valarray > m_Basis; + std::valarray< std::valarray > m_Basis; }; #endif // _TONALESTIMATOR_