annotate data/model/FFTModel.cpp @ 1651:7a56bb85030f single-point

Introduce deferred notifier, + start converting sparse time-value model (perhaps we should rename it too)
author Chris Cannam
date Mon, 18 Mar 2019 14:17:20 +0000
parents 70e172e6cc59
children c9a15ee5dc1d 901f37d32060
rev   line source
Chris@152 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@152 2
Chris@152 3 /*
Chris@152 4 Sonic Visualiser
Chris@152 5 An audio file viewer and annotation editor.
Chris@152 6 Centre for Digital Music, Queen Mary, University of London.
Chris@152 7 This file copyright 2006 Chris Cannam.
Chris@152 8
Chris@152 9 This program is free software; you can redistribute it and/or
Chris@152 10 modify it under the terms of the GNU General Public License as
Chris@152 11 published by the Free Software Foundation; either version 2 of the
Chris@152 12 License, or (at your option) any later version. See the file
Chris@152 13 COPYING included with this distribution for more information.
Chris@152 14 */
Chris@152 15
Chris@152 16 #include "FFTModel.h"
Chris@152 17 #include "DenseTimeValueModel.h"
Chris@152 18
Chris@183 19 #include "base/Profiler.h"
Chris@275 20 #include "base/Pitch.h"
Chris@1256 21 #include "base/HitCount.h"
Chris@1428 22 #include "base/Debug.h"
Chris@1573 23 #include "base/MovingMedian.h"
Chris@183 24
Chris@402 25 #include <algorithm>
Chris@402 26
Chris@152 27 #include <cassert>
Chris@1090 28 #include <deque>
Chris@152 29
Chris@1090 30 using namespace std;
Chris@1090 31
Chris@1256 32 static HitCount inSmallCache("FFTModel: Small FFT cache");
Chris@1256 33 static HitCount inSourceCache("FFTModel: Source data cache");
Chris@1256 34
Chris@152 35 FFTModel::FFTModel(const DenseTimeValueModel *model,
Chris@152 36 int channel,
Chris@152 37 WindowType windowType,
Chris@929 38 int windowSize,
Chris@929 39 int windowIncrement,
Chris@1090 40 int fftSize) :
Chris@1090 41 m_model(model),
Chris@1090 42 m_channel(channel),
Chris@1090 43 m_windowType(windowType),
Chris@1090 44 m_windowSize(windowSize),
Chris@1090 45 m_windowIncrement(windowIncrement),
Chris@1090 46 m_fftSize(fftSize),
Chris@1091 47 m_windower(windowType, windowSize),
Chris@1093 48 m_fft(fftSize),
Chris@1371 49 m_cacheWriteIndex(0),
Chris@1093 50 m_cacheSize(3)
Chris@152 51 {
Chris@1371 52 while (m_cached.size() < m_cacheSize) {
Chris@1371 53 m_cached.push_back({ -1, cvec(m_fftSize / 2 + 1) });
Chris@1371 54 }
Chris@1371 55
Chris@1091 56 if (m_windowSize > m_fftSize) {
Chris@1428 57 SVCERR << "ERROR: FFTModel::FFTModel: window size (" << m_windowSize
Chris@1428 58 << ") must be at least FFT size (" << m_fftSize << ")" << endl;
Chris@1091 59 throw invalid_argument("FFTModel window size must be at least FFT size");
Chris@1091 60 }
Chris@1133 61
Chris@1270 62 m_fft.initFloat();
Chris@1270 63
Chris@1133 64 connect(model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
Chris@1133 65 connect(model, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)),
Chris@1133 66 this, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)));
Chris@152 67 }
Chris@152 68
Chris@152 69 FFTModel::~FFTModel()
Chris@152 70 {
Chris@152 71 }
Chris@152 72
Chris@360 73 void
Chris@360 74 FFTModel::sourceModelAboutToBeDeleted()
Chris@360 75 {
Chris@1090 76 if (m_model) {
Chris@1428 77 SVDEBUG << "FFTModel[" << this << "]::sourceModelAboutToBeDeleted(" << m_model << ")" << endl;
Chris@1582 78 m_model = nullptr;
Chris@360 79 }
Chris@360 80 }
Chris@360 81
Chris@1091 82 int
Chris@1091 83 FFTModel::getWidth() const
Chris@1091 84 {
Chris@1091 85 if (!m_model) return 0;
Chris@1091 86 return int((m_model->getEndFrame() - m_model->getStartFrame())
Chris@1091 87 / m_windowIncrement) + 1;
Chris@1091 88 }
Chris@1091 89
Chris@1091 90 int
Chris@1091 91 FFTModel::getHeight() const
Chris@1091 92 {
Chris@1091 93 return m_fftSize / 2 + 1;
Chris@1091 94 }
Chris@1091 95
Chris@152 96 QString
Chris@929 97 FFTModel::getBinName(int n) const
Chris@152 98 {
Chris@1040 99 sv_samplerate_t sr = getSampleRate();
Chris@152 100 if (!sr) return "";
Chris@204 101 QString name = tr("%1 Hz").arg((n * sr) / ((getHeight()-1) * 2));
Chris@152 102 return name;
Chris@152 103 }
Chris@152 104
Chris@1091 105 FFTModel::Column
Chris@1091 106 FFTModel::getColumn(int x) const
Chris@1091 107 {
Chris@1091 108 auto cplx = getFFTColumn(x);
Chris@1091 109 Column col;
Chris@1154 110 col.reserve(cplx.size());
Chris@1091 111 for (auto c: cplx) col.push_back(abs(c));
Chris@1319 112 return col;
Chris@1091 113 }
Chris@1091 114
Chris@1200 115 FFTModel::Column
Chris@1200 116 FFTModel::getPhases(int x) const
Chris@1200 117 {
Chris@1200 118 auto cplx = getFFTColumn(x);
Chris@1200 119 Column col;
Chris@1200 120 col.reserve(cplx.size());
Chris@1201 121 for (auto c: cplx) {
Chris@1201 122 col.push_back(arg(c));
Chris@1201 123 }
Chris@1319 124 return col;
Chris@1200 125 }
Chris@1200 126
Chris@1091 127 float
Chris@1091 128 FFTModel::getMagnitudeAt(int x, int y) const
Chris@1091 129 {
Chris@1569 130 if (x < 0 || x >= getWidth() || y < 0 || y >= getHeight()) {
Chris@1569 131 return 0.f;
Chris@1569 132 }
Chris@1093 133 auto col = getFFTColumn(x);
Chris@1093 134 return abs(col[y]);
Chris@1091 135 }
Chris@1091 136
Chris@1091 137 float
Chris@1091 138 FFTModel::getMaximumMagnitudeAt(int x) const
Chris@1091 139 {
Chris@1091 140 Column col(getColumn(x));
Chris@1092 141 float max = 0.f;
Chris@1154 142 int n = int(col.size());
Chris@1154 143 for (int i = 0; i < n; ++i) {
Chris@1092 144 if (col[i] > max) max = col[i];
Chris@1092 145 }
Chris@1092 146 return max;
Chris@1091 147 }
Chris@1091 148
Chris@1091 149 float
Chris@1091 150 FFTModel::getPhaseAt(int x, int y) const
Chris@1091 151 {
Chris@1093 152 if (x < 0 || x >= getWidth() || y < 0 || y >= getHeight()) return 0.f;
Chris@1091 153 return arg(getFFTColumn(x)[y]);
Chris@1091 154 }
Chris@1091 155
Chris@1091 156 void
Chris@1091 157 FFTModel::getValuesAt(int x, int y, float &re, float &im) const
Chris@1091 158 {
Chris@1091 159 auto col = getFFTColumn(x);
Chris@1091 160 re = col[y].real();
Chris@1091 161 im = col[y].imag();
Chris@1091 162 }
Chris@1091 163
Chris@1091 164 bool
Chris@1091 165 FFTModel::getMagnitudesAt(int x, float *values, int minbin, int count) const
Chris@1091 166 {
Chris@1091 167 if (count == 0) count = getHeight();
Chris@1091 168 auto col = getFFTColumn(x);
Chris@1091 169 for (int i = 0; i < count; ++i) {
Chris@1091 170 values[i] = abs(col[minbin + i]);
Chris@1091 171 }
Chris@1091 172 return true;
Chris@1091 173 }
Chris@1091 174
Chris@1091 175 bool
Chris@1091 176 FFTModel::getPhasesAt(int x, float *values, int minbin, int count) const
Chris@1091 177 {
Chris@1091 178 if (count == 0) count = getHeight();
Chris@1091 179 auto col = getFFTColumn(x);
Chris@1091 180 for (int i = 0; i < count; ++i) {
Chris@1091 181 values[i] = arg(col[minbin + i]);
Chris@1091 182 }
Chris@1091 183 return true;
Chris@1091 184 }
Chris@1091 185
Chris@1091 186 bool
Chris@1091 187 FFTModel::getValuesAt(int x, float *reals, float *imags, int minbin, int count) const
Chris@1091 188 {
Chris@1091 189 if (count == 0) count = getHeight();
Chris@1091 190 auto col = getFFTColumn(x);
Chris@1091 191 for (int i = 0; i < count; ++i) {
Chris@1091 192 reals[i] = col[minbin + i].real();
Chris@1091 193 }
Chris@1091 194 for (int i = 0; i < count; ++i) {
Chris@1091 195 imags[i] = col[minbin + i].imag();
Chris@1091 196 }
Chris@1091 197 return true;
Chris@1091 198 }
Chris@1091 199
Chris@1326 200 FFTModel::fvec
Chris@1091 201 FFTModel::getSourceSamples(int column) const
Chris@1091 202 {
Chris@1094 203 // m_fftSize may be greater than m_windowSize, but not the reverse
Chris@1094 204
Chris@1094 205 // cerr << "getSourceSamples(" << column << ")" << endl;
Chris@1094 206
Chris@1091 207 auto range = getSourceSampleRange(column);
Chris@1094 208 auto data = getSourceData(range);
Chris@1094 209
Chris@1091 210 int off = (m_fftSize - m_windowSize) / 2;
Chris@1094 211
Chris@1094 212 if (off == 0) {
Chris@1094 213 return data;
Chris@1094 214 } else {
Chris@1094 215 vector<float> pad(off, 0.f);
Chris@1326 216 fvec padded;
Chris@1094 217 padded.reserve(m_fftSize);
Chris@1094 218 padded.insert(padded.end(), pad.begin(), pad.end());
Chris@1094 219 padded.insert(padded.end(), data.begin(), data.end());
Chris@1094 220 padded.insert(padded.end(), pad.begin(), pad.end());
Chris@1094 221 return padded;
Chris@1094 222 }
Chris@1094 223 }
Chris@1094 224
Chris@1326 225 FFTModel::fvec
Chris@1094 226 FFTModel::getSourceData(pair<sv_frame_t, sv_frame_t> range) const
Chris@1094 227 {
Chris@1094 228 // cerr << "getSourceData(" << range.first << "," << range.second
Chris@1094 229 // << "): saved range is (" << m_savedData.range.first
Chris@1094 230 // << "," << m_savedData.range.second << ")" << endl;
Chris@1094 231
Chris@1100 232 if (m_savedData.range == range) {
Chris@1256 233 inSourceCache.hit();
Chris@1100 234 return m_savedData.data;
Chris@1100 235 }
Chris@1094 236
Chris@1270 237 Profiler profiler("FFTModel::getSourceData (cache miss)");
Chris@1270 238
Chris@1094 239 if (range.first < m_savedData.range.second &&
Chris@1094 240 range.first >= m_savedData.range.first &&
Chris@1094 241 range.second > m_savedData.range.second) {
Chris@1094 242
Chris@1256 243 inSourceCache.partial();
Chris@1256 244
Chris@1100 245 sv_frame_t discard = range.first - m_savedData.range.first;
Chris@1100 246
Chris@1457 247 fvec data;
Chris@1457 248 data.reserve(range.second - range.first);
Chris@1094 249
Chris@1457 250 data.insert(data.end(),
Chris@1457 251 m_savedData.data.begin() + discard,
Chris@1457 252 m_savedData.data.end());
Chris@1100 253
Chris@1457 254 fvec rest = getSourceDataUncached
Chris@1457 255 ({ m_savedData.range.second, range.second });
Chris@1457 256
Chris@1457 257 data.insert(data.end(), rest.begin(), rest.end());
Chris@1094 258
Chris@1457 259 m_savedData = { range, data };
Chris@1457 260 return data;
Chris@1095 261
Chris@1095 262 } else {
Chris@1095 263
Chris@1256 264 inSourceCache.miss();
Chris@1256 265
Chris@1095 266 auto data = getSourceDataUncached(range);
Chris@1095 267 m_savedData = { range, data };
Chris@1095 268 return data;
Chris@1094 269 }
Chris@1095 270 }
Chris@1094 271
Chris@1326 272 FFTModel::fvec
Chris@1095 273 FFTModel::getSourceDataUncached(pair<sv_frame_t, sv_frame_t> range) const
Chris@1095 274 {
Chris@1457 275 Profiler profiler("FFTModel::getSourceDataUncached");
Chris@1457 276
Chris@1091 277 decltype(range.first) pfx = 0;
Chris@1091 278 if (range.first < 0) {
Chris@1091 279 pfx = -range.first;
Chris@1091 280 range = { 0, range.second };
Chris@1091 281 }
Chris@1096 282
Chris@1096 283 auto data = m_model->getData(m_channel,
Chris@1096 284 range.first,
Chris@1096 285 range.second - range.first);
Chris@1096 286
Chris@1281 287 if (data.empty()) {
Chris@1281 288 SVDEBUG << "NOTE: empty source data for range (" << range.first << ","
Chris@1281 289 << range.second << ") (model end frame "
Chris@1281 290 << m_model->getEndFrame() << ")" << endl;
Chris@1281 291 }
Chris@1281 292
Chris@1096 293 // don't return a partial frame
Chris@1096 294 data.resize(range.second - range.first, 0.f);
Chris@1096 295
Chris@1096 296 if (pfx > 0) {
Chris@1096 297 vector<float> pad(pfx, 0.f);
Chris@1096 298 data.insert(data.begin(), pad.begin(), pad.end());
Chris@1096 299 }
Chris@1096 300
Chris@1091 301 if (m_channel == -1) {
Chris@1429 302 int channels = m_model->getChannelCount();
Chris@1429 303 if (channels > 1) {
Chris@1096 304 int n = int(data.size());
Chris@1096 305 float factor = 1.f / float(channels);
Chris@1100 306 // use mean instead of sum for fft model input
Chris@1429 307 for (int i = 0; i < n; ++i) {
Chris@1429 308 data[i] *= factor;
Chris@1429 309 }
Chris@1429 310 }
Chris@1091 311 }
Chris@1094 312
Chris@1094 313 return data;
Chris@1091 314 }
Chris@1091 315
Chris@1371 316 const FFTModel::cvec &
Chris@1093 317 FFTModel::getFFTColumn(int n) const
Chris@1091 318 {
Chris@1258 319 // The small cache (i.e. the m_cached deque) is for cases where
Chris@1258 320 // values are looked up individually, and for e.g. peak-frequency
Chris@1258 321 // spectrograms where values from two consecutive columns are
Chris@1257 322 // needed at once. This cache gets essentially no hits when
Chris@1258 323 // scrolling through a magnitude spectrogram, but 95%+ hits with a
Chris@1569 324 // peak-frequency spectrogram or spectrum.
Chris@1257 325 for (const auto &incache : m_cached) {
Chris@1093 326 if (incache.n == n) {
Chris@1256 327 inSmallCache.hit();
Chris@1093 328 return incache.col;
Chris@1093 329 }
Chris@1093 330 }
Chris@1256 331 inSmallCache.miss();
Chris@1258 332
Chris@1258 333 Profiler profiler("FFTModel::getFFTColumn (cache miss)");
Chris@1093 334
Chris@1093 335 auto samples = getSourceSamples(n);
Chris@1567 336 m_windower.cut(samples.data() + (m_fftSize - m_windowSize) / 2);
Chris@1270 337 breakfastquay::v_fftshift(samples.data(), m_fftSize);
Chris@1270 338
Chris@1371 339 cvec &col = m_cached[m_cacheWriteIndex].col;
Chris@1270 340
Chris@1270 341 m_fft.forwardInterleaved(samples.data(),
Chris@1270 342 reinterpret_cast<float *>(col.data()));
Chris@1093 343
Chris@1371 344 m_cached[m_cacheWriteIndex].n = n;
Chris@1371 345
Chris@1371 346 m_cacheWriteIndex = (m_cacheWriteIndex + 1) % m_cacheSize;
Chris@1093 347
Chris@1319 348 return col;
Chris@1091 349 }
Chris@1091 350
Chris@275 351 bool
Chris@1045 352 FFTModel::estimateStableFrequency(int x, int y, double &frequency)
Chris@275 353 {
Chris@275 354 if (!isOK()) return false;
Chris@275 355
Chris@1090 356 frequency = double(y * getSampleRate()) / m_fftSize;
Chris@275 357
Chris@275 358 if (x+1 >= getWidth()) return false;
Chris@275 359
Chris@275 360 // At frequency f, a phase shift of 2pi (one cycle) happens in 1/f sec.
Chris@275 361 // At hopsize h and sample rate sr, one hop happens in h/sr sec.
Chris@275 362 // At window size w, for bin b, f is b*sr/w.
Chris@275 363 // thus 2pi phase shift happens in w/(b*sr) sec.
Chris@275 364 // We need to know what phase shift we expect from h/sr sec.
Chris@275 365 // -> 2pi * ((h/sr) / (w/(b*sr)))
Chris@275 366 // = 2pi * ((h * b * sr) / (w * sr))
Chris@275 367 // = 2pi * (h * b) / w.
Chris@275 368
Chris@1038 369 double oldPhase = getPhaseAt(x, y);
Chris@1038 370 double newPhase = getPhaseAt(x+1, y);
Chris@275 371
Chris@929 372 int incr = getResolution();
Chris@275 373
Chris@1090 374 double expectedPhase = oldPhase + (2.0 * M_PI * y * incr) / m_fftSize;
Chris@275 375
Chris@1038 376 double phaseError = princarg(newPhase - expectedPhase);
Chris@275 377
Chris@275 378 // The new frequency estimate based on the phase error resulting
Chris@275 379 // from assuming the "native" frequency of this bin
Chris@275 380
Chris@275 381 frequency =
Chris@1090 382 (getSampleRate() * (expectedPhase + phaseError - oldPhase)) /
Chris@1045 383 (2.0 * M_PI * incr);
Chris@275 384
Chris@275 385 return true;
Chris@275 386 }
Chris@275 387
Chris@275 388 FFTModel::PeakLocationSet
Chris@1191 389 FFTModel::getPeaks(PeakPickType type, int x, int ymin, int ymax) const
Chris@275 390 {
Chris@551 391 Profiler profiler("FFTModel::getPeaks");
Chris@1575 392
Chris@275 393 FFTModel::PeakLocationSet peaks;
Chris@275 394 if (!isOK()) return peaks;
Chris@275 395
Chris@275 396 if (ymax == 0 || ymax > getHeight() - 1) {
Chris@275 397 ymax = getHeight() - 1;
Chris@275 398 }
Chris@275 399
Chris@275 400 if (type == AllPeaks) {
Chris@551 401 int minbin = ymin;
Chris@551 402 if (minbin > 0) minbin = minbin - 1;
Chris@551 403 int maxbin = ymax;
Chris@551 404 if (maxbin < getHeight() - 1) maxbin = maxbin + 1;
Chris@551 405 const int n = maxbin - minbin + 1;
Chris@1218 406 float *values = new float[n];
Chris@551 407 getMagnitudesAt(x, values, minbin, maxbin - minbin + 1);
Chris@929 408 for (int bin = ymin; bin <= ymax; ++bin) {
Chris@551 409 if (bin == minbin || bin == maxbin) continue;
Chris@551 410 if (values[bin - minbin] > values[bin - minbin - 1] &&
Chris@551 411 values[bin - minbin] > values[bin - minbin + 1]) {
Chris@275 412 peaks.insert(bin);
Chris@275 413 }
Chris@275 414 }
Chris@1218 415 delete[] values;
Chris@275 416 return peaks;
Chris@275 417 }
Chris@275 418
Chris@551 419 Column values = getColumn(x);
Chris@1154 420 int nv = int(values.size());
Chris@275 421
Chris@500 422 float mean = 0.f;
Chris@1154 423 for (int i = 0; i < nv; ++i) mean += values[i];
Chris@1154 424 if (nv > 0) mean = mean / float(values.size());
Chris@1038 425
Chris@275 426 // For peak picking we use a moving median window, picking the
Chris@275 427 // highest value within each continuous region of values that
Chris@275 428 // exceed the median. For pitch adaptivity, we adjust the window
Chris@275 429 // size to a roughly constant pitch range (about four tones).
Chris@275 430
Chris@1040 431 sv_samplerate_t sampleRate = getSampleRate();
Chris@275 432
Chris@1090 433 vector<int> inrange;
Chris@1576 434 double dist = 0.5;
Chris@500 435
Chris@929 436 int medianWinSize = getPeakPickWindowSize(type, sampleRate, ymin, dist);
Chris@929 437 int halfWin = medianWinSize/2;
Chris@275 438
Chris@1573 439 MovingMedian<float> window(medianWinSize);
Chris@1573 440
Chris@929 441 int binmin;
Chris@275 442 if (ymin > halfWin) binmin = ymin - halfWin;
Chris@275 443 else binmin = 0;
Chris@275 444
Chris@929 445 int binmax;
Chris@1154 446 if (ymax + halfWin < nv) binmax = ymax + halfWin;
Chris@1154 447 else binmax = nv - 1;
Chris@275 448
Chris@929 449 int prevcentre = 0;
Chris@500 450
Chris@929 451 for (int bin = binmin; bin <= binmax; ++bin) {
Chris@275 452
Chris@275 453 float value = values[bin];
Chris@275 454
Chris@280 455 // so-called median will actually be the dist*100'th percentile
Chris@280 456 medianWinSize = getPeakPickWindowSize(type, sampleRate, bin, dist);
Chris@275 457 halfWin = medianWinSize/2;
Chris@275 458
Chris@1573 459 int actualSize = std::min(medianWinSize, bin - binmin + 1);
Chris@1573 460 window.resize(actualSize);
Chris@1573 461 window.setPercentile(dist * 100.0);
Chris@1573 462 window.push(value);
Chris@275 463
Chris@275 464 if (type == MajorPitchAdaptivePeaks) {
Chris@1154 465 if (ymax + halfWin < nv) binmax = ymax + halfWin;
Chris@1154 466 else binmax = nv - 1;
Chris@275 467 }
Chris@275 468
Chris@1573 469 float median = window.get();
Chris@275 470
Chris@929 471 int centrebin = 0;
Chris@500 472 if (bin > actualSize/2) centrebin = bin - actualSize/2;
Chris@500 473
Chris@500 474 while (centrebin > prevcentre || bin == binmin) {
Chris@275 475
Chris@500 476 if (centrebin > prevcentre) ++prevcentre;
Chris@500 477
Chris@500 478 float centre = values[prevcentre];
Chris@500 479
Chris@500 480 if (centre > median) {
Chris@500 481 inrange.push_back(centrebin);
Chris@500 482 }
Chris@500 483
Chris@1154 484 if (centre <= median || centrebin+1 == nv) {
Chris@500 485 if (!inrange.empty()) {
Chris@929 486 int peakbin = 0;
Chris@500 487 float peakval = 0.f;
Chris@929 488 for (int i = 0; i < (int)inrange.size(); ++i) {
Chris@500 489 if (i == 0 || values[inrange[i]] > peakval) {
Chris@500 490 peakval = values[inrange[i]];
Chris@500 491 peakbin = inrange[i];
Chris@500 492 }
Chris@500 493 }
Chris@500 494 inrange.clear();
Chris@500 495 if (peakbin >= ymin && peakbin <= ymax) {
Chris@500 496 peaks.insert(peakbin);
Chris@275 497 }
Chris@275 498 }
Chris@275 499 }
Chris@500 500
Chris@500 501 if (bin == binmin) break;
Chris@275 502 }
Chris@275 503 }
Chris@275 504
Chris@275 505 return peaks;
Chris@275 506 }
Chris@275 507
Chris@929 508 int
Chris@1040 509 FFTModel::getPeakPickWindowSize(PeakPickType type, sv_samplerate_t sampleRate,
Chris@1576 510 int bin, double &dist) const
Chris@275 511 {
Chris@1576 512 dist = 0.5; // dist is percentile / 100.0
Chris@275 513 if (type == MajorPeaks) return 10;
Chris@275 514 if (bin == 0) return 3;
Chris@280 515
Chris@1091 516 double binfreq = (sampleRate * bin) / m_fftSize;
Chris@1038 517 double hifreq = Pitch::getFrequencyForPitch(73, 0, binfreq);
Chris@280 518
Chris@1091 519 int hibin = int(lrint((hifreq * m_fftSize) / sampleRate));
Chris@275 520 int medianWinSize = hibin - bin;
Chris@1576 521
Chris@1575 522 if (medianWinSize < 3) {
Chris@1575 523 medianWinSize = 3;
Chris@1575 524 }
Chris@1576 525
Chris@1576 526 // We want to avoid the median window size changing too often, as
Chris@1576 527 // it requires a reallocation. So snap to a nearby round number.
Chris@1576 528
Chris@1575 529 if (medianWinSize > 20) {
Chris@1575 530 medianWinSize = (1 + medianWinSize / 10) * 10;
Chris@1575 531 }
Chris@1576 532 if (medianWinSize > 200) {
Chris@1576 533 medianWinSize = (1 + medianWinSize / 100) * 100;
Chris@1576 534 }
Chris@1576 535 if (medianWinSize > 2000) {
Chris@1576 536 medianWinSize = (1 + medianWinSize / 1000) * 1000;
Chris@1576 537 }
Chris@1576 538 if (medianWinSize > 20000) {
Chris@1576 539 medianWinSize = 20000;
Chris@1575 540 }
Chris@280 541
Chris@1576 542 if (medianWinSize < 100) {
Chris@1576 543 dist = 1.0 - (4.0 / medianWinSize);
Chris@1576 544 } else {
Chris@1576 545 dist = 1.0 - (8.0 / medianWinSize);
Chris@1576 546 }
Chris@1576 547 if (dist < 0.5) dist = 0.5;
Chris@1575 548
Chris@275 549 return medianWinSize;
Chris@275 550 }
Chris@275 551
Chris@275 552 FFTModel::PeakSet
Chris@929 553 FFTModel::getPeakFrequencies(PeakPickType type, int x,
Chris@1191 554 int ymin, int ymax) const
Chris@275 555 {
Chris@551 556 Profiler profiler("FFTModel::getPeakFrequencies");
Chris@551 557
Chris@275 558 PeakSet peaks;
Chris@275 559 if (!isOK()) return peaks;
Chris@275 560 PeakLocationSet locations = getPeaks(type, x, ymin, ymax);
Chris@275 561
Chris@1040 562 sv_samplerate_t sampleRate = getSampleRate();
Chris@929 563 int incr = getResolution();
Chris@275 564
Chris@275 565 // This duplicates some of the work of estimateStableFrequency to
Chris@275 566 // allow us to retrieve the phases in two separate vertical
Chris@275 567 // columns, instead of jumping back and forth between columns x and
Chris@275 568 // x+1, which may be significantly slower if re-seeking is needed
Chris@275 569
Chris@1090 570 vector<float> phases;
Chris@275 571 for (PeakLocationSet::iterator i = locations.begin();
Chris@275 572 i != locations.end(); ++i) {
Chris@275 573 phases.push_back(getPhaseAt(x, *i));
Chris@275 574 }
Chris@275 575
Chris@929 576 int phaseIndex = 0;
Chris@275 577 for (PeakLocationSet::iterator i = locations.begin();
Chris@275 578 i != locations.end(); ++i) {
Chris@1038 579 double oldPhase = phases[phaseIndex];
Chris@1038 580 double newPhase = getPhaseAt(x+1, *i);
Chris@1090 581 double expectedPhase = oldPhase + (2.0 * M_PI * *i * incr) / m_fftSize;
Chris@1038 582 double phaseError = princarg(newPhase - expectedPhase);
Chris@1038 583 double frequency =
Chris@275 584 (sampleRate * (expectedPhase + phaseError - oldPhase))
Chris@275 585 / (2 * M_PI * incr);
Chris@1045 586 peaks[*i] = frequency;
Chris@275 587 ++phaseIndex;
Chris@275 588 }
Chris@275 589
Chris@275 590 return peaks;
Chris@275 591 }
Chris@275 592