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 2005-2006 Christian Landone.
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 "DetectionFunction.h"
19 //////////////////////////////////////////////////////////////////////
20 // Construction/Destruction
21 //////////////////////////////////////////////////////////////////////
23 DetectionFunction::DetectionFunction( DFConfig Config ) :
27 m_phaseHistory = NULL;
28 m_phaseHistoryOld = NULL;
34 DetectionFunction::~DetectionFunction()
40 void DetectionFunction::initialise( DFConfig Config )
42 m_dataLength = Config.frameLength;
43 m_halfLength = m_dataLength/2 + 1;
45 m_DFType = Config.DFType;
46 m_stepSize = Config.stepSize;
47 m_dbRise = Config.dbRise;
49 m_whiten = Config.adaptiveWhitening;
50 m_whitenRelaxCoeff = Config.whiteningRelaxCoeff;
51 m_whitenFloor = Config.whiteningFloor;
52 if (m_whitenRelaxCoeff < 0) m_whitenRelaxCoeff = 0.9997;
53 if (m_whitenFloor < 0) m_whitenFloor = 0.01;
55 m_magHistory = new double[ m_halfLength ];
56 memset(m_magHistory,0, m_halfLength*sizeof(double));
58 m_phaseHistory = new double[ m_halfLength ];
59 memset(m_phaseHistory,0, m_halfLength*sizeof(double));
61 m_phaseHistoryOld = new double[ m_halfLength ];
62 memset(m_phaseHistoryOld,0, m_halfLength*sizeof(double));
64 m_magPeaks = new double[ m_halfLength ];
65 memset(m_magPeaks,0, m_halfLength*sizeof(double));
67 m_phaseVoc = new PhaseVocoder(m_dataLength, m_stepSize);
69 m_magnitude = new double[ m_halfLength ];
70 m_thetaAngle = new double[ m_halfLength ];
71 m_unwrapped = new double[ m_halfLength ];
73 m_window = new Window<double>(HanningWindow, m_dataLength);
74 m_windowed = new double[ m_dataLength ];
77 void DetectionFunction::deInitialise()
79 delete [] m_magHistory ;
80 delete [] m_phaseHistory ;
81 delete [] m_phaseHistoryOld ;
82 delete [] m_magPeaks ;
86 delete [] m_magnitude;
87 delete [] m_thetaAngle;
89 delete [] m_unwrapped;
94 double DetectionFunction::processTimeDomain(const double *samples)
96 m_window->cut(samples, m_windowed);
98 m_phaseVoc->processTimeDomain(m_windowed,
99 m_magnitude, m_thetaAngle, m_unwrapped);
101 if (m_whiten) whiten();
106 double DetectionFunction::processFrequencyDomain(const double *reals,
109 m_phaseVoc->processFrequencyDomain(reals, imags,
110 m_magnitude, m_thetaAngle, m_unwrapped);
112 if (m_whiten) whiten();
117 void DetectionFunction::whiten()
119 for (unsigned int i = 0; i < m_halfLength; ++i) {
120 double m = m_magnitude[i];
121 if (m < m_magPeaks[i]) {
122 m = m + (m_magPeaks[i] - m) * m_whitenRelaxCoeff;
124 if (m < m_whitenFloor) m = m_whitenFloor;
130 double DetectionFunction::runDF()
137 retVal = HFC( m_halfLength, m_magnitude);
141 retVal = specDiff( m_halfLength, m_magnitude);
145 // Using the instantaneous phases here actually provides the
146 // same results (for these calculations) as if we had used
147 // unwrapped phases, but without the possible accumulation of
148 // phase error over time
149 retVal = phaseDev( m_halfLength, m_thetaAngle);
153 retVal = complexSD( m_halfLength, m_magnitude, m_thetaAngle);
157 retVal = broadband( m_halfLength, m_magnitude);
164 double DetectionFunction::HFC(unsigned int length, double *src)
169 for( i = 0; i < length; i++)
171 val += src[ i ] * ( i + 1);
176 double DetectionFunction::specDiff(unsigned int length, double *src)
183 for( i = 0; i < length; i++)
185 temp = fabs( (src[ i ] * src[ i ]) - (m_magHistory[ i ] * m_magHistory[ i ]) );
189 // (See note in phaseDev below.)
193 m_magHistory[ i ] = src[ i ];
200 double DetectionFunction::phaseDev(unsigned int length, double *srcPhase)
209 for( i = 0; i < length; i++)
211 tmpPhase = (srcPhase[ i ]- 2*m_phaseHistory[ i ]+m_phaseHistoryOld[ i ]);
212 dev = MathUtilities::princarg( tmpPhase );
214 // A previous version of this code only counted the value here
215 // if the magnitude exceeded 0.1. My impression is that
216 // doesn't greatly improve the results for "loud" music (so
217 // long as the peak picker is reasonably sophisticated), but
218 // does significantly damage its ability to work with quieter
219 // music, so I'm removing it and counting the result always.
220 // Same goes for the spectral difference measure above.
225 m_phaseHistoryOld[ i ] = m_phaseHistory[ i ] ;
226 m_phaseHistory[ i ] = srcPhase[ i ];
233 double DetectionFunction::complexSD(unsigned int length, double *srcMagnitude, double *srcPhase)
242 ComplexData meas = ComplexData( 0, 0 );
243 ComplexData j = ComplexData( 0, 1 );
245 for( i = 0; i < length; i++)
247 tmpPhase = (srcPhase[ i ]- 2*m_phaseHistory[ i ]+m_phaseHistoryOld[ i ]);
248 dev= MathUtilities::princarg( tmpPhase );
250 meas = m_magHistory[i] - ( srcMagnitude[ i ] * exp( j * dev) );
252 tmpReal = real( meas );
253 tmpImag = imag( meas );
255 val += sqrt( (tmpReal * tmpReal) + (tmpImag * tmpImag) );
257 m_phaseHistoryOld[ i ] = m_phaseHistory[ i ] ;
258 m_phaseHistory[ i ] = srcPhase[ i ];
259 m_magHistory[ i ] = srcMagnitude[ i ];
265 double DetectionFunction::broadband(unsigned int length, double *src)
268 for (unsigned int i = 0; i < length; ++i) {
269 double sqrmag = src[i] * src[i];
270 if (m_magHistory[i] > 0.0) {
271 double diff = 10.0 * log10(sqrmag / m_magHistory[i]);
272 if (diff > m_dbRise) val = val + 1;
274 m_magHistory[i] = sqrmag;
279 double* DetectionFunction::getSpectrumMagnitude()