]> git.sesse.net Git - pitch/blobdiff - pitch.cpp
Fix lower limit for overtone detection.
[pitch] / pitch.cpp
index 8aff1485a1e9e44091263aef52b7195cf12479e2..eac947c775c4001e558dac01e156f1928fb0c0e9 100644 (file)
--- a/pitch.cpp
+++ b/pitch.cpp
@@ -225,9 +225,9 @@ std::pair<double, double> find_peak(double *in, unsigned num_samples)
                        best_bin /= i;
 
                        // consider sliding one bin up or down
-                       if (best_bin > 0 && in[best_bin - 1] > in[best_bin] && in[best_bin - 1] > in[best_bin - 2]) {
+                       if (best_bin > 1 && in[best_bin - 1] > in[best_bin] && in[best_bin - 1] > in[best_bin - 2]) {
                                --best_bin;
-                       } else if (best_bin < num_samples / 2 && in[best_bin + 1] > in[best_bin] && in[best_bin + 1] > in[best_bin + 2]) {
+                       } else if (best_bin < num_samples / 2 - 1 && in[best_bin + 1] > in[best_bin] && in[best_bin + 1] > in[best_bin + 2]) {
                                ++best_bin;
                        }
                           
@@ -259,6 +259,8 @@ std::pair<double, double> adjust_for_overtones(std::pair<double, double> base, d
                unsigned middle = unsigned(floor(freq_to_bin(base.first, num_samples) * i + 0.5));
                unsigned lower = middle - (i+1)/2, upper = middle + (i+1)/2;
 
+               if (lower < 1)
+                       lower = 1;
                if (upper >= num_samples)
                        upper = num_samples - 2;