Mercurial > hg > qm-dsp
comparison 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 |
comparison
equal
deleted
inserted
replaced
471:e3335cb213da | 505:930b5b0f707d |
---|---|
18 | 18 |
19 ////////////////////////////////////////////////////////////////////// | 19 ////////////////////////////////////////////////////////////////////// |
20 // Construction/Destruction | 20 // Construction/Destruction |
21 ////////////////////////////////////////////////////////////////////// | 21 ////////////////////////////////////////////////////////////////////// |
22 | 22 |
23 DetectionFunction::DetectionFunction( DFConfig Config ) : | 23 DetectionFunction::DetectionFunction( DFConfig config ) : |
24 m_window(0) | 24 m_window(0) |
25 { | 25 { |
26 m_magHistory = NULL; | 26 m_magHistory = NULL; |
27 m_phaseHistory = NULL; | 27 m_phaseHistory = NULL; |
28 m_phaseHistoryOld = NULL; | 28 m_phaseHistoryOld = NULL; |
29 m_magPeaks = NULL; | 29 m_magPeaks = NULL; |
30 | 30 |
31 initialise( Config ); | 31 initialise( config ); |
32 } | 32 } |
33 | 33 |
34 DetectionFunction::~DetectionFunction() | 34 DetectionFunction::~DetectionFunction() |
35 { | 35 { |
36 deInitialise(); | 36 deInitialise(); |
52 if (m_whitenRelaxCoeff < 0) m_whitenRelaxCoeff = 0.9997; | 52 if (m_whitenRelaxCoeff < 0) m_whitenRelaxCoeff = 0.9997; |
53 if (m_whitenFloor < 0) m_whitenFloor = 0.01; | 53 if (m_whitenFloor < 0) m_whitenFloor = 0.01; |
54 | 54 |
55 m_magHistory = new double[ m_halfLength ]; | 55 m_magHistory = new double[ m_halfLength ]; |
56 memset(m_magHistory,0, m_halfLength*sizeof(double)); | 56 memset(m_magHistory,0, m_halfLength*sizeof(double)); |
57 | 57 |
58 m_phaseHistory = new double[ m_halfLength ]; | 58 m_phaseHistory = new double[ m_halfLength ]; |
59 memset(m_phaseHistory,0, m_halfLength*sizeof(double)); | 59 memset(m_phaseHistory,0, m_halfLength*sizeof(double)); |
60 | 60 |
61 m_phaseHistoryOld = new double[ m_halfLength ]; | 61 m_phaseHistoryOld = new double[ m_halfLength ]; |
62 memset(m_phaseHistoryOld,0, m_halfLength*sizeof(double)); | 62 memset(m_phaseHistoryOld,0, m_halfLength*sizeof(double)); |
114 return runDF(); | 114 return runDF(); |
115 } | 115 } |
116 | 116 |
117 void DetectionFunction::whiten() | 117 void DetectionFunction::whiten() |
118 { | 118 { |
119 for (unsigned int i = 0; i < m_halfLength; ++i) { | 119 for (int i = 0; i < m_halfLength; ++i) { |
120 double m = m_magnitude[i]; | 120 double m = m_magnitude[i]; |
121 if (m < m_magPeaks[i]) { | 121 if (m < m_magPeaks[i]) { |
122 m = m + (m_magPeaks[i] - m) * m_whitenRelaxCoeff; | 122 m = m + (m_magPeaks[i] - m) * m_whitenRelaxCoeff; |
123 } | 123 } |
124 if (m < m_whitenFloor) m = m_whitenFloor; | 124 if (m < m_whitenFloor) m = m_whitenFloor; |
132 double retVal = 0; | 132 double retVal = 0; |
133 | 133 |
134 switch( m_DFType ) | 134 switch( m_DFType ) |
135 { | 135 { |
136 case DF_HFC: | 136 case DF_HFC: |
137 retVal = HFC( m_halfLength, m_magnitude); | 137 retVal = HFC( m_halfLength, m_magnitude); |
138 break; | 138 break; |
139 | 139 |
140 case DF_SPECDIFF: | 140 case DF_SPECDIFF: |
141 retVal = specDiff( m_halfLength, m_magnitude); | 141 retVal = specDiff( m_halfLength, m_magnitude); |
142 break; | 142 break; |
143 | 143 |
144 case DF_PHASEDEV: | 144 case DF_PHASEDEV: |
145 // Using the instantaneous phases here actually provides the | 145 // Using the instantaneous phases here actually provides the |
146 // same results (for these calculations) as if we had used | 146 // same results (for these calculations) as if we had used |
147 // unwrapped phases, but without the possible accumulation of | 147 // unwrapped phases, but without the possible accumulation of |
148 // phase error over time | 148 // phase error over time |
149 retVal = phaseDev( m_halfLength, m_thetaAngle); | 149 retVal = phaseDev( m_halfLength, m_thetaAngle); |
150 break; | 150 break; |
151 | 151 |
152 case DF_COMPLEXSD: | 152 case DF_COMPLEXSD: |
153 retVal = complexSD( m_halfLength, m_magnitude, m_thetaAngle); | 153 retVal = complexSD( m_halfLength, m_magnitude, m_thetaAngle); |
154 break; | 154 break; |
155 | 155 |
156 case DF_BROADBAND: | 156 case DF_BROADBAND: |
157 retVal = broadband( m_halfLength, m_magnitude); | 157 retVal = broadband( m_halfLength, m_magnitude); |
158 break; | 158 break; |
159 } | 159 } |
160 | 160 |
161 return retVal; | 161 return retVal; |
162 } | 162 } |
163 | 163 |
164 double DetectionFunction::HFC(unsigned int length, double *src) | 164 double DetectionFunction::HFC(int length, double *src) |
165 { | 165 { |
166 unsigned int i; | 166 double val = 0; |
167 double val = 0; | 167 for (int i = 0; i < length; i++) { |
168 | 168 val += src[ i ] * ( i + 1); |
169 for( i = 0; i < length; i++) | 169 } |
170 { | 170 return val; |
171 val += src[ i ] * ( i + 1); | 171 } |
172 } | 172 |
173 return val; | 173 double DetectionFunction::specDiff(int length, double *src) |
174 } | 174 { |
175 | |
176 double DetectionFunction::specDiff(unsigned int length, double *src) | |
177 { | |
178 unsigned int i; | |
179 double val = 0.0; | 175 double val = 0.0; |
180 double temp = 0.0; | 176 double temp = 0.0; |
181 double diff = 0.0; | 177 double diff = 0.0; |
182 | 178 |
183 for( i = 0; i < length; i++) | 179 for (int i = 0; i < length; i++) { |
184 { | 180 |
185 temp = fabs( (src[ i ] * src[ i ]) - (m_magHistory[ i ] * m_magHistory[ i ]) ); | 181 temp = fabs( (src[ i ] * src[ i ]) - (m_magHistory[ i ] * m_magHistory[ i ]) ); |
186 | 182 |
187 diff= sqrt(temp); | 183 diff= sqrt(temp); |
188 | 184 |
189 // (See note in phaseDev below.) | 185 // (See note in phaseDev below.) |
190 | 186 |
191 val += diff; | 187 val += diff; |
192 | 188 |
193 m_magHistory[ i ] = src[ i ]; | 189 m_magHistory[ i ] = src[ i ]; |
194 } | 190 } |
195 | 191 |
196 return val; | 192 return val; |
197 } | 193 } |
198 | 194 |
199 | 195 |
200 double DetectionFunction::phaseDev(unsigned int length, double *srcPhase) | 196 double DetectionFunction::phaseDev(int length, double *srcPhase) |
201 { | 197 { |
202 unsigned int i; | |
203 double tmpPhase = 0; | 198 double tmpPhase = 0; |
204 double tmpVal = 0; | 199 double tmpVal = 0; |
205 double val = 0; | 200 double val = 0; |
206 | 201 |
207 double dev = 0; | 202 double dev = 0; |
208 | 203 |
209 for( i = 0; i < length; i++) | 204 for (int i = 0; i < length; i++) { |
210 { | 205 tmpPhase = (srcPhase[ i ]- 2*m_phaseHistory[ i ]+m_phaseHistoryOld[ i ]); |
211 tmpPhase = (srcPhase[ i ]- 2*m_phaseHistory[ i ]+m_phaseHistoryOld[ i ]); | 206 dev = MathUtilities::princarg( tmpPhase ); |
212 dev = MathUtilities::princarg( tmpPhase ); | |
213 | 207 |
214 // A previous version of this code only counted the value here | 208 // A previous version of this code only counted the value here |
215 // if the magnitude exceeded 0.1. My impression is that | 209 // if the magnitude exceeded 0.1. My impression is that |
216 // doesn't greatly improve the results for "loud" music (so | 210 // doesn't greatly improve the results for "loud" music (so |
217 // long as the peak picker is reasonably sophisticated), but | 211 // long as the peak picker is reasonably sophisticated), but |
218 // does significantly damage its ability to work with quieter | 212 // does significantly damage its ability to work with quieter |
219 // music, so I'm removing it and counting the result always. | 213 // music, so I'm removing it and counting the result always. |
220 // Same goes for the spectral difference measure above. | 214 // Same goes for the spectral difference measure above. |
221 | 215 |
222 tmpVal = fabs(dev); | 216 tmpVal = fabs(dev); |
223 val += tmpVal ; | 217 val += tmpVal ; |
224 | 218 |
225 m_phaseHistoryOld[ i ] = m_phaseHistory[ i ] ; | 219 m_phaseHistoryOld[ i ] = m_phaseHistory[ i ] ; |
226 m_phaseHistory[ i ] = srcPhase[ i ]; | 220 m_phaseHistory[ i ] = srcPhase[ i ]; |
227 } | 221 } |
228 | 222 |
229 return val; | 223 return val; |
230 } | 224 } |
231 | 225 |
232 | 226 |
233 double DetectionFunction::complexSD(unsigned int length, double *srcMagnitude, double *srcPhase) | 227 double DetectionFunction::complexSD(int length, double *srcMagnitude, double *srcPhase) |
234 { | 228 { |
235 unsigned int i; | |
236 double val = 0; | 229 double val = 0; |
237 double tmpPhase = 0; | 230 double tmpPhase = 0; |
238 double tmpReal = 0; | 231 double tmpReal = 0; |
239 double tmpImag = 0; | 232 double tmpImag = 0; |
240 | 233 |
241 double dev = 0; | 234 double dev = 0; |
242 ComplexData meas = ComplexData( 0, 0 ); | 235 ComplexData meas = ComplexData( 0, 0 ); |
243 ComplexData j = ComplexData( 0, 1 ); | 236 ComplexData j = ComplexData( 0, 1 ); |
244 | 237 |
245 for( i = 0; i < length; i++) | 238 for (int i = 0; i < length; i++) { |
246 { | 239 |
247 tmpPhase = (srcPhase[ i ]- 2*m_phaseHistory[ i ]+m_phaseHistoryOld[ i ]); | 240 tmpPhase = (srcPhase[ i ]- 2*m_phaseHistory[ i ]+m_phaseHistoryOld[ i ]); |
248 dev= MathUtilities::princarg( tmpPhase ); | 241 dev= MathUtilities::princarg( tmpPhase ); |
249 | 242 |
250 meas = m_magHistory[i] - ( srcMagnitude[ i ] * exp( j * dev) ); | 243 meas = m_magHistory[i] - ( srcMagnitude[ i ] * exp( j * dev) ); |
251 | 244 |
252 tmpReal = real( meas ); | 245 tmpReal = real( meas ); |
253 tmpImag = imag( meas ); | 246 tmpImag = imag( meas ); |
254 | 247 |
255 val += sqrt( (tmpReal * tmpReal) + (tmpImag * tmpImag) ); | 248 val += sqrt( (tmpReal * tmpReal) + (tmpImag * tmpImag) ); |
256 | 249 |
257 m_phaseHistoryOld[ i ] = m_phaseHistory[ i ] ; | 250 m_phaseHistoryOld[ i ] = m_phaseHistory[ i ] ; |
258 m_phaseHistory[ i ] = srcPhase[ i ]; | 251 m_phaseHistory[ i ] = srcPhase[ i ]; |
259 m_magHistory[ i ] = srcMagnitude[ i ]; | 252 m_magHistory[ i ] = srcMagnitude[ i ]; |
260 } | 253 } |
261 | 254 |
262 return val; | 255 return val; |
263 } | 256 } |
264 | 257 |
265 double DetectionFunction::broadband(unsigned int length, double *src) | 258 double DetectionFunction::broadband(int length, double *src) |
266 { | 259 { |
267 double val = 0; | 260 double val = 0; |
268 for (unsigned int i = 0; i < length; ++i) { | 261 for (int i = 0; i < length; ++i) { |
269 double sqrmag = src[i] * src[i]; | 262 double sqrmag = src[i] * src[i]; |
270 if (m_magHistory[i] > 0.0) { | 263 if (m_magHistory[i] > 0.0) { |
271 double diff = 10.0 * log10(sqrmag / m_magHistory[i]); | 264 double diff = 10.0 * log10(sqrmag / m_magHistory[i]); |
272 if (diff > m_dbRise) val = val + 1; | 265 if (diff > m_dbRise) val = val + 1; |
273 } | 266 } |