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_stepSecs = Config.stepSecs;
|
c@238
|
42 m_stepSize = Config.stepSize;
|
c@225
|
43
|
c@239
|
44 m_whiten = Config.adaptiveWhitening;
|
c@239
|
45 m_whitenRelaxCoeff = Config.whiteningRelaxCoeff;
|
c@239
|
46 m_whitenFloor = Config.whiteningFloor;
|
c@239
|
47 if (m_whitenRelaxCoeff < 0) m_whitenRelaxCoeff = 0.9997;
|
c@239
|
48 if (m_whitenFloor < 0) m_whitenFloor = 0.01;
|
c@239
|
49
|
c@227
|
50 m_magHistory = new double[ m_halfLength ];
|
c@227
|
51 memset(m_magHistory,0, m_halfLength*sizeof(double));
|
c@225
|
52
|
c@227
|
53 m_phaseHistory = new double[ m_halfLength ];
|
c@227
|
54 memset(m_phaseHistory,0, m_halfLength*sizeof(double));
|
c@225
|
55
|
c@227
|
56 m_phaseHistoryOld = new double[ m_halfLength ];
|
c@227
|
57 memset(m_phaseHistoryOld,0, m_halfLength*sizeof(double));
|
c@225
|
58
|
c@239
|
59 m_magPeaks = new double[ m_halfLength ];
|
c@239
|
60 memset(m_magPeaks,0, m_halfLength*sizeof(double));
|
c@239
|
61
|
c@225
|
62 m_phaseVoc = new PhaseVocoder;
|
c@225
|
63
|
c@225
|
64 m_DFWindowedFrame = new double[ m_dataLength ];
|
c@225
|
65 m_magnitude = new double[ m_halfLength ];
|
c@225
|
66 m_thetaAngle = new double[ m_halfLength ];
|
c@225
|
67
|
c@225
|
68 m_window = new Window<double>(HanningWindow, m_dataLength);
|
c@225
|
69 }
|
c@225
|
70
|
c@225
|
71 void DetectionFunction::deInitialise()
|
c@225
|
72 {
|
c@227
|
73 delete [] m_magHistory ;
|
c@227
|
74 delete [] m_phaseHistory ;
|
c@227
|
75 delete [] m_phaseHistoryOld ;
|
c@239
|
76 delete [] m_magPeaks ;
|
c@225
|
77
|
c@225
|
78 delete m_phaseVoc;
|
c@225
|
79
|
c@225
|
80 delete [] m_DFWindowedFrame;
|
c@225
|
81 delete [] m_magnitude;
|
c@225
|
82 delete [] m_thetaAngle;
|
c@225
|
83
|
c@225
|
84 delete m_window;
|
c@225
|
85 }
|
c@225
|
86
|
c@225
|
87 double DetectionFunction::process( double *TDomain )
|
c@225
|
88 {
|
c@225
|
89 m_window->cut( TDomain, m_DFWindowedFrame );
|
c@225
|
90
|
c@225
|
91 m_phaseVoc->process( m_dataLength, m_DFWindowedFrame, m_magnitude, m_thetaAngle );
|
c@225
|
92
|
c@239
|
93 if (m_whiten) whiten();
|
c@239
|
94
|
c@227
|
95 return runDF();
|
c@227
|
96 }
|
c@227
|
97
|
c@227
|
98 double DetectionFunction::process( double *magnitudes, double *phases )
|
c@227
|
99 {
|
c@227
|
100 for (size_t i = 0; i < m_halfLength; ++i) {
|
c@227
|
101 m_magnitude[i] = magnitudes[i];
|
c@227
|
102 m_thetaAngle[i] = phases[i];
|
c@227
|
103 }
|
c@227
|
104
|
c@239
|
105 if (m_whiten) whiten();
|
c@239
|
106
|
c@227
|
107 return runDF();
|
c@227
|
108 }
|
c@227
|
109
|
c@239
|
110 void DetectionFunction::whiten()
|
c@239
|
111 {
|
c@239
|
112 for (unsigned int i = 0; i < m_halfLength; ++i) {
|
c@239
|
113 double m = m_magnitude[i];
|
c@239
|
114 if (m < m_magPeaks[i]) {
|
c@239
|
115 m = m + (m_magPeaks[i] - m) * m_whitenRelaxCoeff;
|
c@239
|
116 }
|
c@239
|
117 if (m < m_whitenFloor) m = m_whitenFloor;
|
c@239
|
118 m_magPeaks[i] = m;
|
c@239
|
119 m_magnitude[i] /= m;
|
c@239
|
120 }
|
c@239
|
121 }
|
c@239
|
122
|
c@227
|
123 double DetectionFunction::runDF()
|
c@227
|
124 {
|
c@227
|
125 double retVal = 0;
|
c@227
|
126
|
c@225
|
127 switch( m_DFType )
|
c@225
|
128 {
|
c@225
|
129 case DF_HFC:
|
c@225
|
130 retVal = HFC( m_halfLength, m_magnitude);
|
c@225
|
131 break;
|
c@225
|
132
|
c@238
|
133 case DF_SPECDIFF:
|
c@225
|
134 retVal = specDiff( m_halfLength, m_magnitude);
|
c@225
|
135 break;
|
c@225
|
136
|
c@225
|
137 case DF_PHASEDEV:
|
c@239
|
138 retVal = phaseDev( m_halfLength, m_thetaAngle);
|
c@225
|
139 break;
|
c@225
|
140
|
c@225
|
141 case DF_COMPLEXSD:
|
c@225
|
142 retVal = complexSD( m_halfLength, m_magnitude, m_thetaAngle);
|
c@225
|
143 break;
|
c@237
|
144
|
c@237
|
145 case DF_BROADBAND:
|
c@239
|
146 retVal = broadband( m_halfLength, m_magnitude);
|
c@239
|
147 break;
|
c@225
|
148 }
|
c@225
|
149
|
c@225
|
150 return retVal;
|
c@225
|
151 }
|
c@225
|
152
|
c@225
|
153 double DetectionFunction::HFC(unsigned int length, double *src)
|
c@225
|
154 {
|
c@225
|
155 unsigned int i;
|
c@225
|
156 double val = 0;
|
c@225
|
157
|
c@225
|
158 for( i = 0; i < length; i++)
|
c@225
|
159 {
|
c@225
|
160 val += src[ i ] * ( i + 1);
|
c@225
|
161 }
|
c@225
|
162 return val;
|
c@225
|
163 }
|
c@225
|
164
|
c@225
|
165 double DetectionFunction::specDiff(unsigned int length, double *src)
|
c@225
|
166 {
|
c@225
|
167 unsigned int i;
|
c@225
|
168 double val = 0.0;
|
c@225
|
169 double temp = 0.0;
|
c@225
|
170 double diff = 0.0;
|
c@225
|
171
|
c@225
|
172 for( i = 0; i < length; i++)
|
c@225
|
173 {
|
c@227
|
174 temp = fabs( (src[ i ] * src[ i ]) - (m_magHistory[ i ] * m_magHistory[ i ]) );
|
c@225
|
175
|
c@225
|
176 diff= sqrt(temp);
|
c@225
|
177
|
c@238
|
178 // (See note in phaseDev below.)
|
c@238
|
179
|
c@238
|
180 val += diff;
|
c@225
|
181
|
c@227
|
182 m_magHistory[ i ] = src[ i ];
|
c@225
|
183 }
|
c@225
|
184
|
c@225
|
185 return val;
|
c@225
|
186 }
|
c@225
|
187
|
c@225
|
188
|
c@239
|
189 double DetectionFunction::phaseDev(unsigned int length, double *srcPhase)
|
c@225
|
190 {
|
c@225
|
191 unsigned int i;
|
c@225
|
192 double tmpPhase = 0;
|
c@225
|
193 double tmpVal = 0;
|
c@225
|
194 double val = 0;
|
c@225
|
195
|
c@225
|
196 double dev = 0;
|
c@225
|
197
|
c@225
|
198 for( i = 0; i < length; i++)
|
c@225
|
199 {
|
c@227
|
200 tmpPhase = (srcPhase[ i ]- 2*m_phaseHistory[ i ]+m_phaseHistoryOld[ i ]);
|
c@225
|
201 dev = MathUtilities::princarg( tmpPhase );
|
c@238
|
202
|
c@238
|
203 // A previous version of this code only counted the value here
|
c@238
|
204 // if the magnitude exceeded 0.1. My impression is that
|
c@238
|
205 // doesn't greatly improve the results for "loud" music (so
|
c@238
|
206 // long as the peak picker is reasonably sophisticated), but
|
c@238
|
207 // does significantly damage its ability to work with quieter
|
c@238
|
208 // music, so I'm removing it and counting the result always.
|
c@238
|
209 // Same goes for the spectral difference measure above.
|
c@225
|
210
|
c@238
|
211 tmpVal = fabs(dev);
|
c@238
|
212 val += tmpVal ;
|
c@225
|
213
|
c@227
|
214 m_phaseHistoryOld[ i ] = m_phaseHistory[ i ] ;
|
c@227
|
215 m_phaseHistory[ i ] = srcPhase[ i ];
|
c@225
|
216 }
|
c@225
|
217
|
c@225
|
218
|
c@225
|
219 return val;
|
c@225
|
220 }
|
c@225
|
221
|
c@225
|
222
|
c@225
|
223 double DetectionFunction::complexSD(unsigned int length, double *srcMagnitude, double *srcPhase)
|
c@225
|
224 {
|
c@225
|
225 unsigned int i;
|
c@225
|
226 double val = 0;
|
c@225
|
227 double tmpPhase = 0;
|
c@225
|
228 double tmpReal = 0;
|
c@225
|
229 double tmpImag = 0;
|
c@225
|
230
|
c@225
|
231 double dev = 0;
|
c@225
|
232 ComplexData meas = ComplexData( 0, 0 );
|
c@227
|
233 ComplexData j = ComplexData( 0, 1 );
|
c@225
|
234
|
c@225
|
235 for( i = 0; i < length; i++)
|
c@225
|
236 {
|
c@227
|
237 tmpPhase = (srcPhase[ i ]- 2*m_phaseHistory[ i ]+m_phaseHistoryOld[ i ]);
|
c@225
|
238 dev= MathUtilities::princarg( tmpPhase );
|
c@225
|
239
|
c@227
|
240 meas = m_magHistory[i] - ( srcMagnitude[ i ] * exp( j * dev) );
|
c@225
|
241
|
c@225
|
242 tmpReal = real( meas );
|
c@225
|
243 tmpImag = imag( meas );
|
c@225
|
244
|
c@225
|
245 val += sqrt( (tmpReal * tmpReal) + (tmpImag * tmpImag) );
|
c@225
|
246
|
c@227
|
247 m_phaseHistoryOld[ i ] = m_phaseHistory[ i ] ;
|
c@227
|
248 m_phaseHistory[ i ] = srcPhase[ i ];
|
c@227
|
249 m_magHistory[ i ] = srcMagnitude[ i ];
|
c@225
|
250 }
|
c@225
|
251
|
c@225
|
252 return val;
|
c@225
|
253 }
|
c@225
|
254
|
c@239
|
255 double DetectionFunction::broadband(unsigned int length, double *src)
|
c@237
|
256 {
|
c@237
|
257 double val = 0;
|
c@237
|
258 for (unsigned int i = 0; i < length; ++i) {
|
c@239
|
259 double sqrmag = src[i] * src[i];
|
c@237
|
260 if (m_magHistory[i] > 0.0) {
|
c@237
|
261 double diff = 10.0 * log10(sqrmag / m_magHistory[i]);
|
c@237
|
262 if (diff > m_dbRise) val = val + 1;
|
c@237
|
263 }
|
c@237
|
264 m_magHistory[i] = sqrmag;
|
c@237
|
265 }
|
c@237
|
266 return val;
|
c@237
|
267 }
|
c@237
|
268
|
c@225
|
269 double* DetectionFunction::getSpectrumMagnitude()
|
c@225
|
270 {
|
c@225
|
271 return m_magnitude;
|
c@225
|
272 }
|
c@225
|
273
|