diff data/model/FFTModel.cpp @ 551:408e56d30f58

* Re-enable peak frequencies display in spectrogram
author Chris Cannam
date Thu, 05 Feb 2009 17:33:21 +0000
parents 55ad231c9db7
children d7f3dfe6f9a4
line wrap: on
line diff
--- a/data/model/FFTModel.cpp	Thu Feb 05 14:17:11 2009 +0000
+++ b/data/model/FFTModel.cpp	Thu Feb 05 17:33:21 2009 +0000
@@ -241,6 +241,8 @@
 FFTModel::PeakLocationSet
 FFTModel::getPeaks(PeakPickType type, size_t x, size_t ymin, size_t ymax)
 {
+    Profiler profiler("FFTModel::getPeaks");
+
     FFTModel::PeakLocationSet peaks;
     if (!isOK()) return peaks;
 
@@ -248,27 +250,28 @@
         ymax = getHeight() - 1;
     }
 
-    Column values;
-
     if (type == AllPeaks) {
-        for (size_t y = ymin; y <= ymax; ++y) {
-            values.push_back(getMagnitudeAt(x, y));
-        }
-        size_t i = 0;
+        int minbin = ymin;
+        if (minbin > 0) minbin = minbin - 1;
+        int maxbin = ymax;
+        if (maxbin < getHeight() - 1) maxbin = maxbin + 1;
+        const int n = maxbin - minbin + 1;
+        float values[n];
+        getMagnitudesAt(x, values, minbin, maxbin - minbin + 1);
         for (size_t bin = ymin; bin <= ymax; ++bin) {
-            if ((i == 0 || values[i] > values[i-1]) &&
-                (i == values.size()-1 || values[i] >= values[i+1])) {
+            if (bin == minbin || bin == maxbin) continue;
+            if (values[bin - minbin] > values[bin - minbin - 1] &&
+                values[bin - minbin] > values[bin - minbin + 1]) {
                 peaks.insert(bin);
             }
-            ++i;
         }
         return peaks;
     }
 
-    values = getColumn(x);
+    Column values = getColumn(x);
 
     float mean = 0.f;
-    for (int i =0; i < values.size(); ++i) mean += values[i];
+    for (int i = 0; i < values.size(); ++i) mean += values[i];
     if (values.size() >0) mean /= values.size();
 
     // For peak picking we use a moving median window, picking the
@@ -382,6 +385,8 @@
 FFTModel::getPeakFrequencies(PeakPickType type, size_t x,
                              size_t ymin, size_t ymax)
 {
+    Profiler profiler("FFTModel::getPeakFrequencies");
+
     PeakSet peaks;
     if (!isOK()) return peaks;
     PeakLocationSet locations = getPeaks(type, x, ymin, ymax);