Mercurial > hg > qm-dsp
comparison maths/MathUtilities.cpp @ 54:5bec06ecc88a
* First cut at Matthew's downbeat estimator -- untested so far
author | cannam |
---|---|
date | Tue, 10 Feb 2009 12:52:43 +0000 |
parents | ad645e404d0c |
children | 7fe29d8a7eaf |
comparison
equal
deleted
inserted
replaced
53:796170a9c8e4 | 54:5bec06ecc88a |
---|---|
142 retVal = s / (double)len; | 142 retVal = s / (double)len; |
143 | 143 |
144 return retVal; | 144 return retVal; |
145 } | 145 } |
146 | 146 |
147 double MathUtilities::mean(const std::vector<double> &src, | |
148 unsigned int start, | |
149 unsigned int count) | |
150 { | |
151 double sum = 0.; | |
152 | |
153 for (int i = 0; i < count; ++i) | |
154 { | |
155 sum += src[start + i]; | |
156 } | |
157 | |
158 return sum / count; | |
159 } | |
160 | |
147 void MathUtilities::getFrameMinMax(const double *data, unsigned int len, double *min, double *max) | 161 void MathUtilities::getFrameMinMax(const double *data, unsigned int len, double *min, double *max) |
148 { | 162 { |
149 unsigned int i; | 163 unsigned int i; |
150 double temp = 0.0; | 164 double temp = 0.0; |
151 double a=0.0; | 165 double a=0.0; |
187 index = i; | 201 index = i; |
188 } | 202 } |
189 | 203 |
190 } | 204 } |
191 | 205 |
192 *pMax = max; | 206 if (pMax) *pMax = max; |
207 | |
208 | |
209 return index; | |
210 } | |
211 | |
212 int MathUtilities::getMax( const std::vector<double> & data, double* pMax ) | |
213 { | |
214 unsigned int index = 0; | |
215 unsigned int i; | |
216 double temp = 0.0; | |
217 | |
218 double max = data[0]; | |
219 | |
220 for( i = 0; i < data.size(); i++) | |
221 { | |
222 temp = data[ i ]; | |
223 | |
224 if( temp > max ) | |
225 { | |
226 max = temp ; | |
227 index = i; | |
228 } | |
229 | |
230 } | |
231 | |
232 if (pMax) *pMax = max; | |
193 | 233 |
194 | 234 |
195 return index; | 235 return index; |
196 } | 236 } |
197 | 237 |
287 break; | 327 break; |
288 | 328 |
289 } | 329 } |
290 } | 330 } |
291 | 331 |
332 void MathUtilities::adaptiveThreshold(std::vector<double> &data) | |
333 { | |
334 int sz = int(data.size()); | |
335 if (sz == 0) return; | |
336 | |
337 std::vector<double> smoothed(sz); | |
338 | |
339 int p_pre = 8; | |
340 int p_post = 7; | |
341 | |
342 for (int i = 0; i < sz; ++i) { | |
343 | |
344 int first = std::max(0, i - p_pre); | |
345 int last = std::min(sz - 1, i + p_post); | |
346 | |
347 smoothed[i] = mean(data, first, last - first + 1); | |
348 } | |
349 | |
350 for (int i = 0; i < sz; i++) { | |
351 data[i] -= smoothed[i]; | |
352 if (data[i] < 0.0) data[i] = 0.0; | |
353 } | |
354 } | |
292 | 355 |
293 | 356 |