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