diff data/model/FFTModel.cpp @ 1573:f04038819c26 spectrogramparam

Introduce & make use of faster MovingMedian class (now with resize capability)
author Chris Cannam
date Thu, 08 Nov 2018 15:02:30 +0000
parents 5fe24e4af12c
children 054bbf17ac17
line wrap: on
line diff
--- a/data/model/FFTModel.cpp	Thu Nov 08 14:39:34 2018 +0000
+++ b/data/model/FFTModel.cpp	Thu Nov 08 15:02:30 2018 +0000
@@ -20,6 +20,7 @@
 #include "base/Pitch.h"
 #include "base/HitCount.h"
 #include "base/Debug.h"
+#include "base/MovingMedian.h"
 
 #include <algorithm>
 
@@ -429,13 +430,14 @@
 
     sv_samplerate_t sampleRate = getSampleRate();
 
-    deque<float> window;
     vector<int> inrange;
     float dist = 0.5;
 
     int medianWinSize = getPeakPickWindowSize(type, sampleRate, ymin, dist);
     int halfWin = medianWinSize/2;
 
+    MovingMedian<float> window(medianWinSize);
+
     int binmin;
     if (ymin > halfWin) binmin = ymin - halfWin;
     else binmin = 0;
@@ -450,27 +452,21 @@
 
         float value = values[bin];
 
-        window.push_back(value);
-
         // so-called median will actually be the dist*100'th percentile
         medianWinSize = getPeakPickWindowSize(type, sampleRate, bin, dist);
-
         halfWin = medianWinSize/2;
 
-        while ((int)window.size() > medianWinSize) {
-            window.pop_front();
-        }
-
-        int actualSize = int(window.size());
+        int actualSize = std::min(medianWinSize, bin - binmin + 1);
+        window.resize(actualSize);
+        window.setPercentile(dist * 100.0);
+        window.push(value);
 
         if (type == MajorPitchAdaptivePeaks) {
             if (ymax + halfWin < nv) binmax = ymax + halfWin;
             else binmax = nv - 1;
         }
 
-        vector<float> sorted(window.begin(), window.end());
-        sort(sorted.begin(), sorted.end());
-        float median = sorted[int(float(sorted.size()) * dist)];
+        float median = window.get();
 
         int centrebin = 0;
         if (bin > actualSize/2) centrebin = bin - actualSize/2;