comparison spectrum-compare-plugin/TuningDifference.cpp @ 14:812e4d021443

Merge
author Chris Cannam
date Wed, 04 Feb 2015 15:11:42 +0000
parents spectrum-compare/TuningDifference.cpp@d35ddf54e21f spectrum-compare/TuningDifference.cpp@23572f9d25d9
children
comparison
equal deleted inserted replaced
13:c74071731d74 14:812e4d021443
177 int targetBinMin = int(floor(targetFmin * m_blockSize / m_inputSampleRate)); 177 int targetBinMin = int(floor(targetFmin * m_blockSize / m_inputSampleRate));
178 int targetBinMax = int(ceil(targetFmax * m_blockSize / m_inputSampleRate)); 178 int targetBinMax = int(ceil(targetFmax * m_blockSize / m_inputSampleRate));
179 cerr << "target bin range: " << targetBinMin << " -> " << targetBinMax << endl; 179 cerr << "target bin range: " << targetBinMin << " -> " << targetBinMax << endl;
180 180
181 d.identifier = "averages"; 181 d.identifier = "averages";
182 d.name = "Spectrum averages"; 182 d.name = "Harmonic spectrum averages";
183 d.description = "Average magnitude spectrum for each channel."; 183 d.description = "Average of harmonic spectrum for each channel.";
184 d.unit = ""; 184 d.unit = "";
185 d.hasFixedBinCount = true; 185 d.hasFixedBinCount = true;
186 d.binCount = (targetBinMax > targetBinMin ? targetBinMax - targetBinMin + 1 : 100); 186 d.binCount = (targetBinMax > targetBinMin ? targetBinMax - targetBinMin + 1 : 100);
187 d.hasKnownExtents = false; 187 d.hasKnownExtents = false;
188 d.isQuantized = false; 188 d.isQuantized = false;
223 { 223 {
224 for (int c = 0; c < 2; ++c) { 224 for (int c = 0; c < 2; ++c) {
225 if (m_sum[c].size() == 0) { 225 if (m_sum[c].size() == 0) {
226 m_sum[c].resize(m_blockSize/2 + 1, 0.0); 226 m_sum[c].resize(m_blockSize/2 + 1, 0.0);
227 } 227 }
228 vector<double> mags(m_blockSize/2 + 1, 0.0);
228 for (int i = 0; i <= m_blockSize/2; ++i) { 229 for (int i = 0; i <= m_blockSize/2; ++i) {
229 double energy = 230 double energy =
230 inputBuffers[c][i*2 ] * inputBuffers[c][i*2 ] + 231 inputBuffers[c][i*2 ] * inputBuffers[c][i*2 ] +
231 inputBuffers[c][i*2+1] * inputBuffers[c][i*2+1]; 232 inputBuffers[c][i*2+1] * inputBuffers[c][i*2+1];
232 double mag = sqrt(energy); 233 double mag = sqrt(energy);
233 m_sum[c][i] += mag; 234 mags[i] = mag;
234 m_sum[c][i/2] += mag; 235 }
236 for (int i = 0; i <= m_blockSize/2; ++i) {
237 for (int h = 2; h <= 4; ++h) {
238 double max = 0.0;
239 for (int j = 0; j <= h; ++j) {
240 int ix = i * h + j - h/2;
241 if (ix <= m_blockSize/2) {
242 double harmMag = mags[ix];
243 if (j == 0 || harmMag > max) {
244 max = harmMag;
245 }
246 }
247 }
248 mags[i] += max;
249 }
250 }
251 for (int i = 0; i <= m_blockSize/2; ++i) {
252 m_sum[c][i] += mags[i];
235 } 253 }
236 } 254 }
237 255
238 ++m_frameCount; 256 ++m_frameCount;
239 return FeatureSet(); 257 return FeatureSet();
257 m_sum[0][i] /= m_frameCount; 275 m_sum[0][i] /= m_frameCount;
258 m_sum[1][i] /= m_frameCount; 276 m_sum[1][i] /= m_frameCount;
259 } 277 }
260 278
261 for (int c = 0; c < 2; ++c) { 279 for (int c = 0; c < 2; ++c) {
280 for (int i = 0; i < n; ++i) {
281 if (i == 0 || i == n-1 ||
282 m_sum[c][i] < m_sum[c][i-1] ||
283 m_sum[c][i] < m_sum[c][i+1]) {
284 m_sum[c][i] = 0.0;
285 }
286 }
287 }
288
289 int targetBinMin = int(floor(targetFmin * m_blockSize / m_inputSampleRate));
290 int targetBinMax = int(ceil(targetFmax * m_blockSize / m_inputSampleRate));
291 cerr << "target bin range: " << targetBinMin << " -> " << targetBinMax << endl;
292
293 for (int c = 0; c < 2; ++c) {
262 double tot = 0.0; 294 double tot = 0.0;
263 for (int i = 0; i < n; ++i) { 295 for (int i = targetBinMin; i <= targetBinMax; ++i) {
264 tot += m_sum[c][i]; 296 tot += m_sum[c][i];
265 } 297 }
266 if (tot != 0.0) { 298 if (tot != 0.0) {
267 for (int i = 0; i < n; ++i) { 299 for (int i = 0; i < n; ++i) {
268 m_sum[c][i] /= tot; 300 m_sum[c][i] /= tot;
269 } 301 }
270 } 302 }
271 } 303 }
272
273 int targetBinMin = int(floor(targetFmin * m_blockSize / m_inputSampleRate));
274 int targetBinMax = int(ceil(targetFmax * m_blockSize / m_inputSampleRate));
275 cerr << "target bin range: " << targetBinMin << " -> " << targetBinMax << endl;
276 304
277 f.values.clear(); 305 f.values.clear();
278 for (int i = targetBinMin; i < targetBinMax; ++i) { 306 for (int i = targetBinMin; i < targetBinMax; ++i) {
279 f.values.push_back(m_sum[0][i]); 307 f.values.push_back(m_sum[0][i]);
280 } 308 }
339 f.timestamp = Vamp::RealTime::zeroTime; 367 f.timestamp = Vamp::RealTime::zeroTime;
340 f.hasTimestamp = true; 368 f.hasTimestamp = true;
341 f.label = ""; 369 f.label = "";
342 370
343 f.values.clear(); 371 f.values.clear();
344 // cerr << "best dist = " << bestdist << " at shift " << bestshift << endl; 372 cerr << "best dist = " << bestdist << " at shift " << bestshift << endl;
345 f.values.push_back(-bestshift); 373 f.values.push_back(-bestshift);
346 fs[0].push_back(f); 374 fs[0].push_back(f);
347 375
348 f.values.clear(); 376 f.values.clear();
349 f.values.push_back(440.0 / pow(2.0, double(bestshift) / 1200.0)); 377 f.values.push_back(440.0 / pow(2.0, double(bestshift) / 1200.0));