annotate dsp/onsets/DetectionFunction.cpp @ 129:6ec45e85ed81 kissfft

Drop in kissfft to replace the "old" fft, and add tests for newly-supported sizes
author Chris Cannam
date Tue, 15 Oct 2013 11:38:18 +0100
parents b0e98fcfacd7
children 2053a308bb4d
rev   line source
cannam@0 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@0 2
cannam@0 3 /*
cannam@0 4 QM DSP Library
cannam@0 5
cannam@0 6 Centre for Digital Music, Queen Mary, University of London.
Chris@84 7 This file 2005-2006 Christian Landone.
Chris@84 8
Chris@84 9 This program is free software; you can redistribute it and/or
Chris@84 10 modify it under the terms of the GNU General Public License as
Chris@84 11 published by the Free Software Foundation; either version 2 of the
Chris@84 12 License, or (at your option) any later version. See the file
Chris@84 13 COPYING included with this distribution for more information.
cannam@0 14 */
cannam@0 15
cannam@0 16 #include "DetectionFunction.h"
cannam@47 17 #include <cstring>
cannam@0 18
cannam@0 19 //////////////////////////////////////////////////////////////////////
cannam@0 20 // Construction/Destruction
cannam@0 21 //////////////////////////////////////////////////////////////////////
cannam@0 22
cannam@0 23 DetectionFunction::DetectionFunction( DFConfig Config ) :
cannam@0 24 m_window(0)
cannam@0 25 {
cannam@2 26 m_magHistory = NULL;
cannam@2 27 m_phaseHistory = NULL;
cannam@2 28 m_phaseHistoryOld = NULL;
cannam@14 29 m_magPeaks = NULL;
cannam@0 30
cannam@0 31 initialise( Config );
cannam@0 32 }
cannam@0 33
cannam@0 34 DetectionFunction::~DetectionFunction()
cannam@0 35 {
cannam@0 36 deInitialise();
cannam@0 37 }
cannam@0 38
cannam@0 39
cannam@0 40 void DetectionFunction::initialise( DFConfig Config )
cannam@0 41 {
cannam@0 42 m_dataLength = Config.frameLength;
Chris@115 43 m_halfLength = m_dataLength/2 + 1;
cannam@14 44
cannam@0 45 m_DFType = Config.DFType;
cannam@13 46 m_stepSize = Config.stepSize;
cannam@0 47
cannam@14 48 m_whiten = Config.adaptiveWhitening;
cannam@14 49 m_whitenRelaxCoeff = Config.whiteningRelaxCoeff;
cannam@14 50 m_whitenFloor = Config.whiteningFloor;
cannam@14 51 if (m_whitenRelaxCoeff < 0) m_whitenRelaxCoeff = 0.9997;
cannam@14 52 if (m_whitenFloor < 0) m_whitenFloor = 0.01;
cannam@14 53
cannam@2 54 m_magHistory = new double[ m_halfLength ];
cannam@2 55 memset(m_magHistory,0, m_halfLength*sizeof(double));
cannam@0 56
cannam@2 57 m_phaseHistory = new double[ m_halfLength ];
cannam@2 58 memset(m_phaseHistory,0, m_halfLength*sizeof(double));
cannam@0 59
cannam@2 60 m_phaseHistoryOld = new double[ m_halfLength ];
cannam@2 61 memset(m_phaseHistoryOld,0, m_halfLength*sizeof(double));
cannam@0 62
cannam@14 63 m_magPeaks = new double[ m_halfLength ];
cannam@14 64 memset(m_magPeaks,0, m_halfLength*sizeof(double));
cannam@14 65
Chris@119 66 // See note in processTimeDomain below
cannam@64 67 int actualLength = MathUtilities::previousPowerOfTwo(m_dataLength);
Chris@115 68 m_phaseVoc = new PhaseVocoder(actualLength, m_stepSize);
cannam@0 69
cannam@0 70 m_magnitude = new double[ m_halfLength ];
cannam@0 71 m_thetaAngle = new double[ m_halfLength ];
Chris@115 72 m_unwrapped = new double[ m_halfLength ];
cannam@0 73
cannam@0 74 m_window = new Window<double>(HanningWindow, m_dataLength);
Chris@119 75 m_windowed = new double[ m_dataLength ];
cannam@0 76 }
cannam@0 77
cannam@0 78 void DetectionFunction::deInitialise()
cannam@0 79 {
cannam@2 80 delete [] m_magHistory ;
cannam@2 81 delete [] m_phaseHistory ;
cannam@2 82 delete [] m_phaseHistoryOld ;
cannam@14 83 delete [] m_magPeaks ;
cannam@0 84
cannam@0 85 delete m_phaseVoc;
cannam@0 86
cannam@0 87 delete [] m_magnitude;
cannam@0 88 delete [] m_thetaAngle;
Chris@119 89 delete [] m_windowed;
Chris@119 90 delete [] m_unwrapped;
cannam@0 91
cannam@0 92 delete m_window;
cannam@0 93 }
cannam@0 94
Chris@119 95 double DetectionFunction::processTimeDomain(const double *samples)
cannam@0 96 {
Chris@119 97 m_window->cut(samples, m_windowed);
cannam@55 98
cannam@55 99 // Our own FFT implementation supports power-of-two sizes only.
cannam@55 100 // If we have to use this implementation (as opposed to the
cannam@55 101 // version of process() below that operates on frequency domain
cannam@55 102 // data directly), we will have to use the next smallest power of
cannam@55 103 // two from the block size. Results may vary accordingly!
cannam@55 104
Chris@106 105 int actualLength = MathUtilities::previousPowerOfTwo((int)m_dataLength);
cannam@55 106
Chris@102 107 if (actualLength != (int)m_dataLength) {
cannam@55 108 // Pre-fill mag and phase vectors with zero, as the FFT output
cannam@55 109 // will not fill the arrays
Chris@102 110 for (int i = actualLength/2; i < (int)m_dataLength/2; ++i) {
cannam@55 111 m_magnitude[i] = 0;
cannam@55 112 m_thetaAngle[0] = 0;
cannam@55 113 }
cannam@55 114 }
cannam@55 115
Chris@119 116 m_phaseVoc->processTimeDomain(m_windowed,
Chris@119 117 m_magnitude, m_thetaAngle, m_unwrapped);
cannam@0 118
cannam@14 119 if (m_whiten) whiten();
cannam@14 120
cannam@2 121 return runDF();
cannam@2 122 }
cannam@2 123
Chris@119 124 double DetectionFunction::processFrequencyDomain(const double *reals,
Chris@119 125 const double *imags)
cannam@2 126 {
Chris@119 127 m_phaseVoc->processFrequencyDomain(reals, imags,
Chris@119 128 m_magnitude, m_thetaAngle, m_unwrapped);
cannam@2 129
cannam@14 130 if (m_whiten) whiten();
cannam@14 131
cannam@2 132 return runDF();
cannam@2 133 }
cannam@2 134
cannam@14 135 void DetectionFunction::whiten()
cannam@14 136 {
cannam@14 137 for (unsigned int i = 0; i < m_halfLength; ++i) {
cannam@14 138 double m = m_magnitude[i];
cannam@14 139 if (m < m_magPeaks[i]) {
cannam@14 140 m = m + (m_magPeaks[i] - m) * m_whitenRelaxCoeff;
cannam@14 141 }
cannam@14 142 if (m < m_whitenFloor) m = m_whitenFloor;
cannam@14 143 m_magPeaks[i] = m;
cannam@14 144 m_magnitude[i] /= m;
cannam@14 145 }
cannam@14 146 }
cannam@14 147
cannam@2 148 double DetectionFunction::runDF()
cannam@2 149 {
cannam@2 150 double retVal = 0;
cannam@2 151
cannam@0 152 switch( m_DFType )
cannam@0 153 {
cannam@0 154 case DF_HFC:
cannam@0 155 retVal = HFC( m_halfLength, m_magnitude);
cannam@0 156 break;
cannam@0 157
cannam@13 158 case DF_SPECDIFF:
cannam@0 159 retVal = specDiff( m_halfLength, m_magnitude);
cannam@0 160 break;
cannam@0 161
cannam@0 162 case DF_PHASEDEV:
Chris@120 163 // Using the instantaneous phases here actually provides the
Chris@120 164 // same results (for these calculations) as if we had used
Chris@120 165 // unwrapped phases, but without the possible accumulation of
Chris@120 166 // phase error over time
cannam@14 167 retVal = phaseDev( m_halfLength, m_thetaAngle);
cannam@0 168 break;
cannam@0 169
cannam@0 170 case DF_COMPLEXSD:
cannam@0 171 retVal = complexSD( m_halfLength, m_magnitude, m_thetaAngle);
cannam@0 172 break;
cannam@12 173
cannam@12 174 case DF_BROADBAND:
cannam@14 175 retVal = broadband( m_halfLength, m_magnitude);
cannam@14 176 break;
cannam@0 177 }
cannam@0 178
cannam@0 179 return retVal;
cannam@0 180 }
cannam@0 181
cannam@0 182 double DetectionFunction::HFC(unsigned int length, double *src)
cannam@0 183 {
cannam@0 184 unsigned int i;
cannam@0 185 double val = 0;
cannam@0 186
cannam@0 187 for( i = 0; i < length; i++)
cannam@0 188 {
cannam@0 189 val += src[ i ] * ( i + 1);
cannam@0 190 }
cannam@0 191 return val;
cannam@0 192 }
cannam@0 193
cannam@0 194 double DetectionFunction::specDiff(unsigned int length, double *src)
cannam@0 195 {
cannam@0 196 unsigned int i;
cannam@0 197 double val = 0.0;
cannam@0 198 double temp = 0.0;
cannam@0 199 double diff = 0.0;
cannam@0 200
cannam@0 201 for( i = 0; i < length; i++)
cannam@0 202 {
cannam@2 203 temp = fabs( (src[ i ] * src[ i ]) - (m_magHistory[ i ] * m_magHistory[ i ]) );
cannam@0 204
cannam@0 205 diff= sqrt(temp);
cannam@0 206
cannam@13 207 // (See note in phaseDev below.)
cannam@13 208
cannam@13 209 val += diff;
cannam@0 210
cannam@2 211 m_magHistory[ i ] = src[ i ];
cannam@0 212 }
cannam@0 213
cannam@0 214 return val;
cannam@0 215 }
cannam@0 216
cannam@0 217
cannam@14 218 double DetectionFunction::phaseDev(unsigned int length, double *srcPhase)
cannam@0 219 {
cannam@0 220 unsigned int i;
cannam@0 221 double tmpPhase = 0;
cannam@0 222 double tmpVal = 0;
cannam@0 223 double val = 0;
cannam@0 224
cannam@0 225 double dev = 0;
cannam@0 226
cannam@0 227 for( i = 0; i < length; i++)
cannam@0 228 {
cannam@2 229 tmpPhase = (srcPhase[ i ]- 2*m_phaseHistory[ i ]+m_phaseHistoryOld[ i ]);
cannam@0 230 dev = MathUtilities::princarg( tmpPhase );
cannam@13 231
cannam@13 232 // A previous version of this code only counted the value here
cannam@13 233 // if the magnitude exceeded 0.1. My impression is that
cannam@13 234 // doesn't greatly improve the results for "loud" music (so
cannam@13 235 // long as the peak picker is reasonably sophisticated), but
cannam@13 236 // does significantly damage its ability to work with quieter
cannam@13 237 // music, so I'm removing it and counting the result always.
cannam@13 238 // Same goes for the spectral difference measure above.
cannam@0 239
cannam@13 240 tmpVal = fabs(dev);
cannam@13 241 val += tmpVal ;
cannam@0 242
cannam@2 243 m_phaseHistoryOld[ i ] = m_phaseHistory[ i ] ;
cannam@2 244 m_phaseHistory[ i ] = srcPhase[ i ];
cannam@0 245 }
cannam@0 246
cannam@0 247 return val;
cannam@0 248 }
cannam@0 249
cannam@0 250
cannam@0 251 double DetectionFunction::complexSD(unsigned int length, double *srcMagnitude, double *srcPhase)
cannam@0 252 {
cannam@0 253 unsigned int i;
cannam@0 254 double val = 0;
cannam@0 255 double tmpPhase = 0;
cannam@0 256 double tmpReal = 0;
cannam@0 257 double tmpImag = 0;
cannam@0 258
cannam@0 259 double dev = 0;
cannam@0 260 ComplexData meas = ComplexData( 0, 0 );
cannam@2 261 ComplexData j = ComplexData( 0, 1 );
cannam@0 262
cannam@0 263 for( i = 0; i < length; i++)
cannam@0 264 {
cannam@2 265 tmpPhase = (srcPhase[ i ]- 2*m_phaseHistory[ i ]+m_phaseHistoryOld[ i ]);
cannam@0 266 dev= MathUtilities::princarg( tmpPhase );
cannam@0 267
cannam@2 268 meas = m_magHistory[i] - ( srcMagnitude[ i ] * exp( j * dev) );
cannam@0 269
cannam@0 270 tmpReal = real( meas );
cannam@0 271 tmpImag = imag( meas );
cannam@0 272
cannam@0 273 val += sqrt( (tmpReal * tmpReal) + (tmpImag * tmpImag) );
cannam@0 274
cannam@2 275 m_phaseHistoryOld[ i ] = m_phaseHistory[ i ] ;
cannam@2 276 m_phaseHistory[ i ] = srcPhase[ i ];
cannam@2 277 m_magHistory[ i ] = srcMagnitude[ i ];
cannam@0 278 }
cannam@0 279
cannam@0 280 return val;
cannam@0 281 }
cannam@0 282
cannam@14 283 double DetectionFunction::broadband(unsigned int length, double *src)
cannam@12 284 {
cannam@12 285 double val = 0;
cannam@12 286 for (unsigned int i = 0; i < length; ++i) {
cannam@14 287 double sqrmag = src[i] * src[i];
cannam@12 288 if (m_magHistory[i] > 0.0) {
cannam@12 289 double diff = 10.0 * log10(sqrmag / m_magHistory[i]);
cannam@12 290 if (diff > m_dbRise) val = val + 1;
cannam@12 291 }
cannam@12 292 m_magHistory[i] = sqrmag;
cannam@12 293 }
cannam@12 294 return val;
cannam@12 295 }
cannam@12 296
cannam@0 297 double* DetectionFunction::getSpectrumMagnitude()
cannam@0 298 {
cannam@0 299 return m_magnitude;
cannam@0 300 }
cannam@0 301