annotate data/model/FFTModel.cpp @ 1780:6d6740b075c3

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