Mercurial > hg > qm-dsp
diff dsp/onsets/DetectionFunction.cpp @ 505:930b5b0f707d
Merge branch 'codestyle-and-tidy'
author | Chris Cannam <cannam@all-day-breakfast.com> |
---|---|
date | Wed, 05 Jun 2019 12:55:15 +0100 |
parents | af5b7ef02aa7 |
children |
line wrap: on
line diff
--- a/dsp/onsets/DetectionFunction.cpp Thu May 30 16:18:13 2019 +0100 +++ b/dsp/onsets/DetectionFunction.cpp Wed Jun 05 12:55:15 2019 +0100 @@ -20,7 +20,7 @@ // Construction/Destruction ////////////////////////////////////////////////////////////////////// -DetectionFunction::DetectionFunction( DFConfig Config ) : +DetectionFunction::DetectionFunction( DFConfig config ) : m_window(0) { m_magHistory = NULL; @@ -28,7 +28,7 @@ m_phaseHistoryOld = NULL; m_magPeaks = NULL; - initialise( Config ); + initialise( config ); } DetectionFunction::~DetectionFunction() @@ -54,7 +54,7 @@ m_magHistory = new double[ m_halfLength ]; memset(m_magHistory,0, m_halfLength*sizeof(double)); - + m_phaseHistory = new double[ m_halfLength ]; memset(m_phaseHistory,0, m_halfLength*sizeof(double)); @@ -116,7 +116,7 @@ void DetectionFunction::whiten() { - for (unsigned int i = 0; i < m_halfLength; ++i) { + for (int i = 0; i < m_halfLength; ++i) { double m = m_magnitude[i]; if (m < m_magPeaks[i]) { m = m + (m_magPeaks[i] - m) * m_whitenRelaxCoeff; @@ -134,82 +134,76 @@ switch( m_DFType ) { case DF_HFC: - retVal = HFC( m_halfLength, m_magnitude); - break; - + retVal = HFC( m_halfLength, m_magnitude); + break; + case DF_SPECDIFF: - retVal = specDiff( m_halfLength, m_magnitude); - break; - + retVal = specDiff( m_halfLength, m_magnitude); + break; + case DF_PHASEDEV: // Using the instantaneous phases here actually provides the // same results (for these calculations) as if we had used // unwrapped phases, but without the possible accumulation of // phase error over time - retVal = phaseDev( m_halfLength, m_thetaAngle); - break; - + retVal = phaseDev( m_halfLength, m_thetaAngle); + break; + case DF_COMPLEXSD: - retVal = complexSD( m_halfLength, m_magnitude, m_thetaAngle); - break; + retVal = complexSD( m_halfLength, m_magnitude, m_thetaAngle); + break; case DF_BROADBAND: retVal = broadband( m_halfLength, m_magnitude); break; } - + return retVal; } -double DetectionFunction::HFC(unsigned int length, double *src) +double DetectionFunction::HFC(int length, double *src) { - unsigned int i; double val = 0; - - for( i = 0; i < length; i++) - { - val += src[ i ] * ( i + 1); + for (int i = 0; i < length; i++) { + val += src[ i ] * ( i + 1); } return val; } -double DetectionFunction::specDiff(unsigned int length, double *src) +double DetectionFunction::specDiff(int length, double *src) { - unsigned int i; double val = 0.0; double temp = 0.0; double diff = 0.0; - for( i = 0; i < length; i++) - { - temp = fabs( (src[ i ] * src[ i ]) - (m_magHistory[ i ] * m_magHistory[ i ]) ); - - diff= sqrt(temp); + for (int i = 0; i < length; i++) { + + temp = fabs( (src[ i ] * src[ i ]) - (m_magHistory[ i ] * m_magHistory[ i ]) ); + + diff= sqrt(temp); // (See note in phaseDev below.) val += diff; - m_magHistory[ i ] = src[ i ]; + m_magHistory[ i ] = src[ i ]; } return val; } -double DetectionFunction::phaseDev(unsigned int length, double *srcPhase) +double DetectionFunction::phaseDev(int length, double *srcPhase) { - unsigned int i; double tmpPhase = 0; double tmpVal = 0; double val = 0; double dev = 0; - for( i = 0; i < length; i++) - { - tmpPhase = (srcPhase[ i ]- 2*m_phaseHistory[ i ]+m_phaseHistoryOld[ i ]); - dev = MathUtilities::princarg( tmpPhase ); + for (int i = 0; i < length; i++) { + tmpPhase = (srcPhase[ i ]- 2*m_phaseHistory[ i ]+m_phaseHistoryOld[ i ]); + dev = MathUtilities::princarg( tmpPhase ); // A previous version of this code only counted the value here // if the magnitude exceeded 0.1. My impression is that @@ -218,21 +212,20 @@ // does significantly damage its ability to work with quieter // music, so I'm removing it and counting the result always. // Same goes for the spectral difference measure above. - + tmpVal = fabs(dev); val += tmpVal ; - m_phaseHistoryOld[ i ] = m_phaseHistory[ i ] ; - m_phaseHistory[ i ] = srcPhase[ i ]; + m_phaseHistoryOld[ i ] = m_phaseHistory[ i ] ; + m_phaseHistory[ i ] = srcPhase[ i ]; } - + return val; } -double DetectionFunction::complexSD(unsigned int length, double *srcMagnitude, double *srcPhase) +double DetectionFunction::complexSD(int length, double *srcMagnitude, double *srcPhase) { - unsigned int i; double val = 0; double tmpPhase = 0; double tmpReal = 0; @@ -242,30 +235,30 @@ ComplexData meas = ComplexData( 0, 0 ); ComplexData j = ComplexData( 0, 1 ); - for( i = 0; i < length; i++) - { - tmpPhase = (srcPhase[ i ]- 2*m_phaseHistory[ i ]+m_phaseHistoryOld[ i ]); - dev= MathUtilities::princarg( tmpPhase ); - - meas = m_magHistory[i] - ( srcMagnitude[ i ] * exp( j * dev) ); + for (int i = 0; i < length; i++) { + + tmpPhase = (srcPhase[ i ]- 2*m_phaseHistory[ i ]+m_phaseHistoryOld[ i ]); + dev= MathUtilities::princarg( tmpPhase ); + + meas = m_magHistory[i] - ( srcMagnitude[ i ] * exp( j * dev) ); - tmpReal = real( meas ); - tmpImag = imag( meas ); + tmpReal = real( meas ); + tmpImag = imag( meas ); - val += sqrt( (tmpReal * tmpReal) + (tmpImag * tmpImag) ); - - m_phaseHistoryOld[ i ] = m_phaseHistory[ i ] ; - m_phaseHistory[ i ] = srcPhase[ i ]; - m_magHistory[ i ] = srcMagnitude[ i ]; + val += sqrt( (tmpReal * tmpReal) + (tmpImag * tmpImag) ); + + m_phaseHistoryOld[ i ] = m_phaseHistory[ i ] ; + m_phaseHistory[ i ] = srcPhase[ i ]; + m_magHistory[ i ] = srcMagnitude[ i ]; } return val; } -double DetectionFunction::broadband(unsigned int length, double *src) +double DetectionFunction::broadband(int length, double *src) { double val = 0; - for (unsigned int i = 0; i < length; ++i) { + for (int i = 0; i < length; ++i) { double sqrmag = src[i] * src[i]; if (m_magHistory[i] > 0.0) { double diff = 10.0 * log10(sqrmag / m_magHistory[i]);