annotate data/model/FFTModel.cpp @ 1570:410819150cd3

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