annotate data/model/FFTModel.cpp @ 1837:1b688ab5f1b3

Unify various vectors to our base floatvec_t type; store columns in fft model cache at their desired height so we can return a reference (speeding up the peak-frequency spectrogram in particular)
author Chris Cannam
date Thu, 09 Apr 2020 11:22:55 +0100
parents dd51797e528e
children 915d316a5609
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@1744 35 FFTModel::FFTModel(ModelId modelId,
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@1744 41 m_model(modelId),
Chris@1780 42 m_sampleRate(0),
Chris@1090 43 m_channel(channel),
Chris@1090 44 m_windowType(windowType),
Chris@1090 45 m_windowSize(windowSize),
Chris@1090 46 m_windowIncrement(windowIncrement),
Chris@1090 47 m_fftSize(fftSize),
Chris@1091 48 m_windower(windowType, windowSize),
Chris@1093 49 m_fft(fftSize),
Chris@1780 50 m_maximumFrequency(0.0),
Chris@1371 51 m_cacheWriteIndex(0),
Chris@1093 52 m_cacheSize(3)
Chris@152 53 {
Chris@1837 54 clearCaches();
Chris@1371 55
Chris@1091 56 if (m_windowSize > m_fftSize) {
Chris@1428 57 SVCERR << "ERROR: FFTModel::FFTModel: window size (" << m_windowSize
Chris@1680 58 << ") may not exceed FFT size (" << m_fftSize << ")" << endl;
Chris@1680 59 throw invalid_argument("FFTModel window size may not exceed FFT size");
Chris@1091 60 }
Chris@1133 61
Chris@1270 62 m_fft.initFloat();
Chris@1270 63
Chris@1744 64 auto model = ModelById::getAs<DenseTimeValueModel>(m_model);
Chris@1744 65 if (model) {
Chris@1780 66 m_sampleRate = model->getSampleRate();
Chris@1780 67
Chris@1752 68 connect(model.get(), SIGNAL(modelChanged(ModelId)),
Chris@1752 69 this, SIGNAL(modelChanged(ModelId)));
Chris@1752 70 connect(model.get(), SIGNAL(modelChangedWithin(ModelId, sv_frame_t, sv_frame_t)),
Chris@1752 71 this, SIGNAL(modelChangedWithin(ModelId, sv_frame_t, sv_frame_t)));
Chris@1784 72 } else {
Chris@1784 73 m_error = QString("Model #%1 is not available").arg(m_model.untyped);
Chris@1744 74 }
Chris@152 75 }
Chris@152 76
Chris@152 77 FFTModel::~FFTModel()
Chris@152 78 {
Chris@152 79 }
Chris@152 80
Chris@1837 81 void
Chris@1837 82 FFTModel::clearCaches()
Chris@1837 83 {
Chris@1837 84 m_cached.clear();
Chris@1837 85 while (m_cached.size() < m_cacheSize) {
Chris@1837 86 m_cached.push_back({ -1, complexvec_t(m_fftSize / 2 + 1) });
Chris@1837 87 }
Chris@1837 88 m_cacheWriteIndex = 0;
Chris@1837 89 m_savedData.range = { 0, 0 };
Chris@1837 90 }
Chris@1837 91
Chris@1744 92 bool
Chris@1744 93 FFTModel::isOK() const
Chris@360 94 {
Chris@1744 95 auto model = ModelById::getAs<DenseTimeValueModel>(m_model);
Chris@1784 96 if (!model) {
Chris@1784 97 m_error = QString("Model #%1 is not available").arg(m_model.untyped);
Chris@1784 98 return false;
Chris@1784 99 }
Chris@1784 100 if (!model->isOK()) {
Chris@1784 101 m_error = QString("Model #%1 is not OK").arg(m_model.untyped);
Chris@1784 102 return false;
Chris@1784 103 }
Chris@1784 104 return true;
Chris@1744 105 }
Chris@1744 106
Chris@1744 107 int
Chris@1744 108 FFTModel::getCompletion() const
Chris@1744 109 {
Chris@1744 110 int c = 100;
Chris@1744 111 auto model = ModelById::getAs<DenseTimeValueModel>(m_model);
Chris@1744 112 if (model) {
Chris@1744 113 if (model->isReady(&c)) return 100;
Chris@360 114 }
Chris@1744 115 return c;
Chris@1744 116 }
Chris@1744 117
Chris@1780 118 void
Chris@1780 119 FFTModel::setMaximumFrequency(double freq)
Chris@1780 120 {
Chris@1780 121 m_maximumFrequency = freq;
Chris@1837 122 clearCaches();
Chris@360 123 }
Chris@360 124
Chris@1091 125 int
Chris@1091 126 FFTModel::getWidth() const
Chris@1091 127 {
Chris@1744 128 auto model = ModelById::getAs<DenseTimeValueModel>(m_model);
Chris@1744 129 if (!model) return 0;
Chris@1744 130 return int((model->getEndFrame() - model->getStartFrame())
Chris@1091 131 / m_windowIncrement) + 1;
Chris@1091 132 }
Chris@1091 133
Chris@1091 134 int
Chris@1091 135 FFTModel::getHeight() const
Chris@1091 136 {
Chris@1780 137 int height = m_fftSize / 2 + 1;
Chris@1780 138 if (m_maximumFrequency != 0.0) {
Chris@1780 139 int maxBin = int(ceil(m_maximumFrequency * m_fftSize) / m_sampleRate);
Chris@1780 140 if (maxBin >= 0 && maxBin < height) {
Chris@1780 141 return maxBin + 1;
Chris@1780 142 }
Chris@1780 143 }
Chris@1780 144 return height;
Chris@1091 145 }
Chris@1091 146
Chris@152 147 QString
Chris@929 148 FFTModel::getBinName(int n) const
Chris@152 149 {
Chris@1790 150 return tr("%1 Hz").arg(getBinValue(n));
Chris@1790 151 }
Chris@1790 152
Chris@1790 153 float
Chris@1790 154 FFTModel::getBinValue(int n) const
Chris@1790 155 {
Chris@1790 156 return float((m_sampleRate * n) / m_fftSize);
Chris@152 157 }
Chris@152 158
Chris@1091 159 FFTModel::Column
Chris@1091 160 FFTModel::getColumn(int x) const
Chris@1091 161 {
Chris@1091 162 auto cplx = getFFTColumn(x);
Chris@1091 163 Column col;
Chris@1154 164 col.reserve(cplx.size());
Chris@1091 165 for (auto c: cplx) col.push_back(abs(c));
Chris@1319 166 return col;
Chris@1091 167 }
Chris@1091 168
Chris@1200 169 FFTModel::Column
Chris@1200 170 FFTModel::getPhases(int x) const
Chris@1200 171 {
Chris@1200 172 auto cplx = getFFTColumn(x);
Chris@1200 173 Column col;
Chris@1200 174 col.reserve(cplx.size());
Chris@1201 175 for (auto c: cplx) {
Chris@1201 176 col.push_back(arg(c));
Chris@1201 177 }
Chris@1319 178 return col;
Chris@1200 179 }
Chris@1200 180
Chris@1091 181 float
Chris@1091 182 FFTModel::getMagnitudeAt(int x, int y) const
Chris@1091 183 {
Chris@1569 184 if (x < 0 || x >= getWidth() || y < 0 || y >= getHeight()) {
Chris@1569 185 return 0.f;
Chris@1569 186 }
Chris@1093 187 auto col = getFFTColumn(x);
Chris@1093 188 return abs(col[y]);
Chris@1091 189 }
Chris@1091 190
Chris@1091 191 float
Chris@1091 192 FFTModel::getMaximumMagnitudeAt(int x) const
Chris@1091 193 {
Chris@1091 194 Column col(getColumn(x));
Chris@1092 195 float max = 0.f;
Chris@1154 196 int n = int(col.size());
Chris@1154 197 for (int i = 0; i < n; ++i) {
Chris@1092 198 if (col[i] > max) max = col[i];
Chris@1092 199 }
Chris@1092 200 return max;
Chris@1091 201 }
Chris@1091 202
Chris@1091 203 float
Chris@1091 204 FFTModel::getPhaseAt(int x, int y) const
Chris@1091 205 {
Chris@1093 206 if (x < 0 || x >= getWidth() || y < 0 || y >= getHeight()) return 0.f;
Chris@1091 207 return arg(getFFTColumn(x)[y]);
Chris@1091 208 }
Chris@1091 209
Chris@1091 210 void
Chris@1091 211 FFTModel::getValuesAt(int x, int y, float &re, float &im) const
Chris@1091 212 {
Chris@1780 213 if (x < 0 || x >= getWidth() || y < 0 || y >= getHeight()) {
Chris@1780 214 re = 0.f;
Chris@1780 215 im = 0.f;
Chris@1780 216 return;
Chris@1780 217 }
Chris@1091 218 auto col = getFFTColumn(x);
Chris@1091 219 re = col[y].real();
Chris@1091 220 im = col[y].imag();
Chris@1091 221 }
Chris@1091 222
Chris@1091 223 bool
Chris@1091 224 FFTModel::getMagnitudesAt(int x, float *values, int minbin, int count) const
Chris@1091 225 {
Chris@1091 226 if (count == 0) count = getHeight();
Chris@1091 227 auto col = getFFTColumn(x);
Chris@1091 228 for (int i = 0; i < count; ++i) {
Chris@1091 229 values[i] = abs(col[minbin + i]);
Chris@1091 230 }
Chris@1091 231 return true;
Chris@1091 232 }
Chris@1091 233
Chris@1091 234 bool
Chris@1091 235 FFTModel::getPhasesAt(int x, float *values, int minbin, int count) const
Chris@1091 236 {
Chris@1091 237 if (count == 0) count = getHeight();
Chris@1091 238 auto col = getFFTColumn(x);
Chris@1091 239 for (int i = 0; i < count; ++i) {
Chris@1091 240 values[i] = arg(col[minbin + i]);
Chris@1091 241 }
Chris@1091 242 return true;
Chris@1091 243 }
Chris@1091 244
Chris@1091 245 bool
Chris@1091 246 FFTModel::getValuesAt(int x, float *reals, float *imags, int minbin, int count) const
Chris@1091 247 {
Chris@1091 248 if (count == 0) count = getHeight();
Chris@1091 249 auto col = getFFTColumn(x);
Chris@1091 250 for (int i = 0; i < count; ++i) {
Chris@1091 251 reals[i] = col[minbin + i].real();
Chris@1091 252 }
Chris@1091 253 for (int i = 0; i < count; ++i) {
Chris@1091 254 imags[i] = col[minbin + i].imag();
Chris@1091 255 }
Chris@1091 256 return true;
Chris@1091 257 }
Chris@1091 258
Chris@1837 259 floatvec_t
Chris@1091 260 FFTModel::getSourceSamples(int column) const
Chris@1091 261 {
Chris@1094 262 // m_fftSize may be greater than m_windowSize, but not the reverse
Chris@1094 263
Chris@1094 264 // cerr << "getSourceSamples(" << column << ")" << endl;
Chris@1094 265
Chris@1091 266 auto range = getSourceSampleRange(column);
Chris@1094 267 auto data = getSourceData(range);
Chris@1094 268
Chris@1091 269 int off = (m_fftSize - m_windowSize) / 2;
Chris@1094 270
Chris@1094 271 if (off == 0) {
Chris@1094 272 return data;
Chris@1094 273 } else {
Chris@1094 274 vector<float> pad(off, 0.f);
Chris@1837 275 floatvec_t padded;
Chris@1094 276 padded.reserve(m_fftSize);
Chris@1094 277 padded.insert(padded.end(), pad.begin(), pad.end());
Chris@1094 278 padded.insert(padded.end(), data.begin(), data.end());
Chris@1094 279 padded.insert(padded.end(), pad.begin(), pad.end());
Chris@1094 280 return padded;
Chris@1094 281 }
Chris@1094 282 }
Chris@1094 283
Chris@1837 284 floatvec_t
Chris@1094 285 FFTModel::getSourceData(pair<sv_frame_t, sv_frame_t> range) const
Chris@1094 286 {
Chris@1094 287 // cerr << "getSourceData(" << range.first << "," << range.second
Chris@1094 288 // << "): saved range is (" << m_savedData.range.first
Chris@1094 289 // << "," << m_savedData.range.second << ")" << endl;
Chris@1094 290
Chris@1100 291 if (m_savedData.range == range) {
Chris@1256 292 inSourceCache.hit();
Chris@1100 293 return m_savedData.data;
Chris@1100 294 }
Chris@1094 295
Chris@1270 296 Profiler profiler("FFTModel::getSourceData (cache miss)");
Chris@1270 297
Chris@1094 298 if (range.first < m_savedData.range.second &&
Chris@1094 299 range.first >= m_savedData.range.first &&
Chris@1094 300 range.second > m_savedData.range.second) {
Chris@1094 301
Chris@1256 302 inSourceCache.partial();
Chris@1256 303
Chris@1100 304 sv_frame_t discard = range.first - m_savedData.range.first;
Chris@1100 305
Chris@1837 306 floatvec_t data;
Chris@1457 307 data.reserve(range.second - range.first);
Chris@1094 308
Chris@1457 309 data.insert(data.end(),
Chris@1457 310 m_savedData.data.begin() + discard,
Chris@1457 311 m_savedData.data.end());
Chris@1100 312
Chris@1837 313 floatvec_t rest = getSourceDataUncached
Chris@1457 314 ({ m_savedData.range.second, range.second });
Chris@1457 315
Chris@1457 316 data.insert(data.end(), rest.begin(), rest.end());
Chris@1094 317
Chris@1457 318 m_savedData = { range, data };
Chris@1457 319 return data;
Chris@1095 320
Chris@1095 321 } else {
Chris@1095 322
Chris@1256 323 inSourceCache.miss();
Chris@1256 324
Chris@1095 325 auto data = getSourceDataUncached(range);
Chris@1095 326 m_savedData = { range, data };
Chris@1095 327 return data;
Chris@1094 328 }
Chris@1095 329 }
Chris@1094 330
Chris@1837 331 floatvec_t
Chris@1095 332 FFTModel::getSourceDataUncached(pair<sv_frame_t, sv_frame_t> range) const
Chris@1095 333 {
Chris@1457 334 Profiler profiler("FFTModel::getSourceDataUncached");
Chris@1688 335
Chris@1744 336 auto model = ModelById::getAs<DenseTimeValueModel>(m_model);
Chris@1744 337 if (!model) return {};
Chris@1457 338
Chris@1091 339 decltype(range.first) pfx = 0;
Chris@1091 340 if (range.first < 0) {
Chris@1091 341 pfx = -range.first;
Chris@1091 342 range = { 0, range.second };
Chris@1091 343 }
Chris@1096 344
Chris@1744 345 auto data = model->getData(m_channel,
Chris@1744 346 range.first,
Chris@1744 347 range.second - range.first);
Chris@1773 348 /*
Chris@1281 349 if (data.empty()) {
Chris@1281 350 SVDEBUG << "NOTE: empty source data for range (" << range.first << ","
Chris@1281 351 << range.second << ") (model end frame "
Chris@1744 352 << model->getEndFrame() << ")" << endl;
Chris@1281 353 }
Chris@1773 354 */
Chris@1281 355
Chris@1096 356 // don't return a partial frame
Chris@1096 357 data.resize(range.second - range.first, 0.f);
Chris@1096 358
Chris@1096 359 if (pfx > 0) {
Chris@1096 360 vector<float> pad(pfx, 0.f);
Chris@1096 361 data.insert(data.begin(), pad.begin(), pad.end());
Chris@1096 362 }
Chris@1096 363
Chris@1091 364 if (m_channel == -1) {
Chris@1744 365 int channels = model->getChannelCount();
Chris@1429 366 if (channels > 1) {
Chris@1096 367 int n = int(data.size());
Chris@1096 368 float factor = 1.f / float(channels);
Chris@1100 369 // use mean instead of sum for fft model input
Chris@1429 370 for (int i = 0; i < n; ++i) {
Chris@1429 371 data[i] *= factor;
Chris@1429 372 }
Chris@1429 373 }
Chris@1091 374 }
Chris@1094 375
Chris@1094 376 return data;
Chris@1091 377 }
Chris@1091 378
Chris@1837 379 const complexvec_t &
Chris@1093 380 FFTModel::getFFTColumn(int n) const
Chris@1091 381 {
Chris@1258 382 // The small cache (i.e. the m_cached deque) is for cases where
Chris@1258 383 // values are looked up individually, and for e.g. peak-frequency
Chris@1258 384 // spectrograms where values from two consecutive columns are
Chris@1257 385 // needed at once. This cache gets essentially no hits when
Chris@1258 386 // scrolling through a magnitude spectrogram, but 95%+ hits with a
Chris@1569 387 // peak-frequency spectrogram or spectrum.
Chris@1257 388 for (const auto &incache : m_cached) {
Chris@1093 389 if (incache.n == n) {
Chris@1256 390 inSmallCache.hit();
Chris@1837 391 return incache.col;
Chris@1093 392 }
Chris@1093 393 }
Chris@1256 394 inSmallCache.miss();
Chris@1258 395
Chris@1258 396 Profiler profiler("FFTModel::getFFTColumn (cache miss)");
Chris@1093 397
Chris@1093 398 auto samples = getSourceSamples(n);
Chris@1567 399 m_windower.cut(samples.data() + (m_fftSize - m_windowSize) / 2);
Chris@1270 400 breakfastquay::v_fftshift(samples.data(), m_fftSize);
Chris@1270 401
Chris@1837 402 complexvec_t &col = m_cached[m_cacheWriteIndex].col;
Chris@1837 403
Chris@1837 404 // expand to large enough for fft destination, if truncated previously
Chris@1837 405 col.resize(m_fftSize / 2 + 1);
Chris@1270 406
Chris@1270 407 m_fft.forwardInterleaved(samples.data(),
Chris@1270 408 reinterpret_cast<float *>(col.data()));
Chris@1093 409
Chris@1837 410 // keep only the number of elements we need - so that we can
Chris@1837 411 // return a const ref without having to resize on a cache hit
Chris@1837 412 col.resize(getHeight());
Chris@1837 413
Chris@1371 414 m_cached[m_cacheWriteIndex].n = n;
Chris@1371 415
Chris@1371 416 m_cacheWriteIndex = (m_cacheWriteIndex + 1) % m_cacheSize;
Chris@1093 417
Chris@1837 418 return col;
Chris@1091 419 }
Chris@1091 420
Chris@275 421 bool
Chris@1045 422 FFTModel::estimateStableFrequency(int x, int y, double &frequency)
Chris@275 423 {
Chris@275 424 if (!isOK()) return false;
Chris@275 425
Chris@1090 426 frequency = double(y * getSampleRate()) / m_fftSize;
Chris@275 427
Chris@275 428 if (x+1 >= getWidth()) return false;
Chris@275 429
Chris@275 430 // At frequency f, a phase shift of 2pi (one cycle) happens in 1/f sec.
Chris@275 431 // At hopsize h and sample rate sr, one hop happens in h/sr sec.
Chris@275 432 // At window size w, for bin b, f is b*sr/w.
Chris@275 433 // thus 2pi phase shift happens in w/(b*sr) sec.
Chris@275 434 // We need to know what phase shift we expect from h/sr sec.
Chris@275 435 // -> 2pi * ((h/sr) / (w/(b*sr)))
Chris@275 436 // = 2pi * ((h * b * sr) / (w * sr))
Chris@275 437 // = 2pi * (h * b) / w.
Chris@275 438
Chris@1038 439 double oldPhase = getPhaseAt(x, y);
Chris@1038 440 double newPhase = getPhaseAt(x+1, y);
Chris@275 441
Chris@929 442 int incr = getResolution();
Chris@275 443
Chris@1090 444 double expectedPhase = oldPhase + (2.0 * M_PI * y * incr) / m_fftSize;
Chris@275 445
Chris@1038 446 double phaseError = princarg(newPhase - expectedPhase);
Chris@275 447
Chris@275 448 // The new frequency estimate based on the phase error resulting
Chris@275 449 // from assuming the "native" frequency of this bin
Chris@275 450
Chris@275 451 frequency =
Chris@1090 452 (getSampleRate() * (expectedPhase + phaseError - oldPhase)) /
Chris@1045 453 (2.0 * M_PI * incr);
Chris@275 454
Chris@275 455 return true;
Chris@275 456 }
Chris@275 457
Chris@275 458 FFTModel::PeakLocationSet
Chris@1191 459 FFTModel::getPeaks(PeakPickType type, int x, int ymin, int ymax) const
Chris@275 460 {
Chris@551 461 Profiler profiler("FFTModel::getPeaks");
Chris@1575 462
Chris@275 463 FFTModel::PeakLocationSet peaks;
Chris@275 464 if (!isOK()) return peaks;
Chris@275 465
Chris@275 466 if (ymax == 0 || ymax > getHeight() - 1) {
Chris@275 467 ymax = getHeight() - 1;
Chris@275 468 }
Chris@275 469
Chris@275 470 if (type == AllPeaks) {
Chris@551 471 int minbin = ymin;
Chris@551 472 if (minbin > 0) minbin = minbin - 1;
Chris@551 473 int maxbin = ymax;
Chris@551 474 if (maxbin < getHeight() - 1) maxbin = maxbin + 1;
Chris@551 475 const int n = maxbin - minbin + 1;
Chris@1218 476 float *values = new float[n];
Chris@551 477 getMagnitudesAt(x, values, minbin, maxbin - minbin + 1);
Chris@929 478 for (int bin = ymin; bin <= ymax; ++bin) {
Chris@551 479 if (bin == minbin || bin == maxbin) continue;
Chris@551 480 if (values[bin - minbin] > values[bin - minbin - 1] &&
Chris@551 481 values[bin - minbin] > values[bin - minbin + 1]) {
Chris@275 482 peaks.insert(bin);
Chris@275 483 }
Chris@275 484 }
Chris@1218 485 delete[] values;
Chris@275 486 return peaks;
Chris@275 487 }
Chris@275 488
Chris@551 489 Column values = getColumn(x);
Chris@1154 490 int nv = int(values.size());
Chris@275 491
Chris@500 492 float mean = 0.f;
Chris@1154 493 for (int i = 0; i < nv; ++i) mean += values[i];
Chris@1154 494 if (nv > 0) mean = mean / float(values.size());
Chris@1038 495
Chris@275 496 // For peak picking we use a moving median window, picking the
Chris@275 497 // highest value within each continuous region of values that
Chris@275 498 // exceed the median. For pitch adaptivity, we adjust the window
Chris@275 499 // size to a roughly constant pitch range (about four tones).
Chris@275 500
Chris@1040 501 sv_samplerate_t sampleRate = getSampleRate();
Chris@275 502
Chris@1090 503 vector<int> inrange;
Chris@1576 504 double dist = 0.5;
Chris@500 505
Chris@929 506 int medianWinSize = getPeakPickWindowSize(type, sampleRate, ymin, dist);
Chris@929 507 int halfWin = medianWinSize/2;
Chris@275 508
Chris@1573 509 MovingMedian<float> window(medianWinSize);
Chris@1573 510
Chris@929 511 int binmin;
Chris@275 512 if (ymin > halfWin) binmin = ymin - halfWin;
Chris@275 513 else binmin = 0;
Chris@275 514
Chris@929 515 int binmax;
Chris@1154 516 if (ymax + halfWin < nv) binmax = ymax + halfWin;
Chris@1154 517 else binmax = nv - 1;
Chris@275 518
Chris@929 519 int prevcentre = 0;
Chris@500 520
Chris@929 521 for (int bin = binmin; bin <= binmax; ++bin) {
Chris@275 522
Chris@275 523 float value = values[bin];
Chris@275 524
Chris@280 525 // so-called median will actually be the dist*100'th percentile
Chris@280 526 medianWinSize = getPeakPickWindowSize(type, sampleRate, bin, dist);
Chris@275 527 halfWin = medianWinSize/2;
Chris@275 528
Chris@1573 529 int actualSize = std::min(medianWinSize, bin - binmin + 1);
Chris@1573 530 window.resize(actualSize);
Chris@1573 531 window.setPercentile(dist * 100.0);
Chris@1573 532 window.push(value);
Chris@275 533
Chris@275 534 if (type == MajorPitchAdaptivePeaks) {
Chris@1154 535 if (ymax + halfWin < nv) binmax = ymax + halfWin;
Chris@1154 536 else binmax = nv - 1;
Chris@275 537 }
Chris@275 538
Chris@1573 539 float median = window.get();
Chris@275 540
Chris@929 541 int centrebin = 0;
Chris@500 542 if (bin > actualSize/2) centrebin = bin - actualSize/2;
Chris@500 543
Chris@500 544 while (centrebin > prevcentre || bin == binmin) {
Chris@275 545
Chris@500 546 if (centrebin > prevcentre) ++prevcentre;
Chris@500 547
Chris@500 548 float centre = values[prevcentre];
Chris@500 549
Chris@500 550 if (centre > median) {
Chris@500 551 inrange.push_back(centrebin);
Chris@500 552 }
Chris@500 553
Chris@1154 554 if (centre <= median || centrebin+1 == nv) {
Chris@500 555 if (!inrange.empty()) {
Chris@929 556 int peakbin = 0;
Chris@500 557 float peakval = 0.f;
Chris@929 558 for (int i = 0; i < (int)inrange.size(); ++i) {
Chris@500 559 if (i == 0 || values[inrange[i]] > peakval) {
Chris@500 560 peakval = values[inrange[i]];
Chris@500 561 peakbin = inrange[i];
Chris@500 562 }
Chris@500 563 }
Chris@500 564 inrange.clear();
Chris@500 565 if (peakbin >= ymin && peakbin <= ymax) {
Chris@500 566 peaks.insert(peakbin);
Chris@275 567 }
Chris@275 568 }
Chris@275 569 }
Chris@500 570
Chris@500 571 if (bin == binmin) break;
Chris@275 572 }
Chris@275 573 }
Chris@275 574
Chris@275 575 return peaks;
Chris@275 576 }
Chris@275 577
Chris@929 578 int
Chris@1040 579 FFTModel::getPeakPickWindowSize(PeakPickType type, sv_samplerate_t sampleRate,
Chris@1576 580 int bin, double &dist) const
Chris@275 581 {
Chris@1576 582 dist = 0.5; // dist is percentile / 100.0
Chris@275 583 if (type == MajorPeaks) return 10;
Chris@275 584 if (bin == 0) return 3;
Chris@280 585
Chris@1091 586 double binfreq = (sampleRate * bin) / m_fftSize;
Chris@1038 587 double hifreq = Pitch::getFrequencyForPitch(73, 0, binfreq);
Chris@280 588
Chris@1091 589 int hibin = int(lrint((hifreq * m_fftSize) / sampleRate));
Chris@275 590 int medianWinSize = hibin - bin;
Chris@1576 591
Chris@1575 592 if (medianWinSize < 3) {
Chris@1575 593 medianWinSize = 3;
Chris@1575 594 }
Chris@1576 595
Chris@1576 596 // We want to avoid the median window size changing too often, as
Chris@1576 597 // it requires a reallocation. So snap to a nearby round number.
Chris@1576 598
Chris@1575 599 if (medianWinSize > 20) {
Chris@1575 600 medianWinSize = (1 + medianWinSize / 10) * 10;
Chris@1575 601 }
Chris@1576 602 if (medianWinSize > 200) {
Chris@1576 603 medianWinSize = (1 + medianWinSize / 100) * 100;
Chris@1576 604 }
Chris@1576 605 if (medianWinSize > 2000) {
Chris@1576 606 medianWinSize = (1 + medianWinSize / 1000) * 1000;
Chris@1576 607 }
Chris@1576 608 if (medianWinSize > 20000) {
Chris@1576 609 medianWinSize = 20000;
Chris@1575 610 }
Chris@280 611
Chris@1576 612 if (medianWinSize < 100) {
Chris@1576 613 dist = 1.0 - (4.0 / medianWinSize);
Chris@1576 614 } else {
Chris@1576 615 dist = 1.0 - (8.0 / medianWinSize);
Chris@1576 616 }
Chris@1576 617 if (dist < 0.5) dist = 0.5;
Chris@1575 618
Chris@275 619 return medianWinSize;
Chris@275 620 }
Chris@275 621
Chris@275 622 FFTModel::PeakSet
Chris@929 623 FFTModel::getPeakFrequencies(PeakPickType type, int x,
Chris@1191 624 int ymin, int ymax) const
Chris@275 625 {
Chris@551 626 Profiler profiler("FFTModel::getPeakFrequencies");
Chris@551 627
Chris@275 628 PeakSet peaks;
Chris@275 629 if (!isOK()) return peaks;
Chris@275 630 PeakLocationSet locations = getPeaks(type, x, ymin, ymax);
Chris@275 631
Chris@1040 632 sv_samplerate_t sampleRate = getSampleRate();
Chris@929 633 int incr = getResolution();
Chris@275 634
Chris@275 635 // This duplicates some of the work of estimateStableFrequency to
Chris@275 636 // allow us to retrieve the phases in two separate vertical
Chris@275 637 // columns, instead of jumping back and forth between columns x and
Chris@275 638 // x+1, which may be significantly slower if re-seeking is needed
Chris@275 639
Chris@1090 640 vector<float> phases;
Chris@275 641 for (PeakLocationSet::iterator i = locations.begin();
Chris@275 642 i != locations.end(); ++i) {
Chris@275 643 phases.push_back(getPhaseAt(x, *i));
Chris@275 644 }
Chris@275 645
Chris@929 646 int phaseIndex = 0;
Chris@275 647 for (PeakLocationSet::iterator i = locations.begin();
Chris@275 648 i != locations.end(); ++i) {
Chris@1038 649 double oldPhase = phases[phaseIndex];
Chris@1038 650 double newPhase = getPhaseAt(x+1, *i);
Chris@1090 651 double expectedPhase = oldPhase + (2.0 * M_PI * *i * incr) / m_fftSize;
Chris@1038 652 double phaseError = princarg(newPhase - expectedPhase);
Chris@1038 653 double frequency =
Chris@275 654 (sampleRate * (expectedPhase + phaseError - oldPhase))
Chris@275 655 / (2 * M_PI * incr);
Chris@1045 656 peaks[*i] = frequency;
Chris@275 657 ++phaseIndex;
Chris@275 658 }
Chris@275 659
Chris@275 660 return peaks;
Chris@275 661 }
Chris@275 662