shithub: aubio

Download patch

ref: 893e699deadca272129fa1493aaeb04b4b6ba1c6
parent: 568fc6056c256200f7ccec8e6e50230b0e8822fb
parent: 9ef3c6e531d0b937f18abc3aacd550946c682ace
author: Paul Brossier <piem@piem.org>
date: Sat Nov 10 06:58:54 EST 2018

Merge branch 'master' into feature/pytest

--- a/python/tests/test_filterbank_mel.py
+++ b/python/tests/test_filterbank_mel.py
@@ -4,7 +4,7 @@
 from numpy.testing import TestCase, assert_equal, assert_almost_equal
 from _tools import assert_warns
 
-from aubio import cvec, filterbank, float_type
+from aubio import fvec, cvec, filterbank, float_type
 
 class aubio_filterbank_mel_test_case(TestCase):
 
@@ -77,6 +77,70 @@
         freqs = np.array(freq_list, dtype = float_type)
         with assert_warns(UserWarning):
             f.set_triangle_bands(freqs, 44100)
+
+    def test_triangle_freqs_with_zeros(self):
+        """make sure set_triangle_bands works when list starts with 0"""
+        freq_list = [0, 40, 80]
+        freqs = np.array(freq_list, dtype = float_type)
+        f = filterbank(len(freqs)-2, 1024)
+        f.set_triangle_bands(freqs, 48000)
+        assert_equal ( f(cvec(1024)), 0)
+        self.assertIsInstance(f.get_coeffs(), np.ndarray)
+
+    def test_triangle_freqs_with_wrong_negative(self):
+        """make sure set_triangle_bands fails when list contains a negative"""
+        freq_list = [-10, 0, 80]
+        f = filterbank(len(freq_list)-2, 1024)
+        with self.assertRaises(ValueError):
+            f.set_triangle_bands(fvec(freq_list), 48000)
+
+    def test_triangle_freqs_with_wrong_ordering(self):
+        """make sure set_triangle_bands fails when list not ordered"""
+        freq_list = [0, 80, 40]
+        f = filterbank(len(freq_list)-2, 1024)
+        with self.assertRaises(ValueError):
+            f.set_triangle_bands(fvec(freq_list), 48000)
+
+    def test_triangle_freqs_with_large_freq(self):
+        """make sure set_triangle_bands warns when freq > nyquist"""
+        samplerate = 22050
+        freq_list = [0, samplerate//4, samplerate // 2 + 1]
+        f = filterbank(len(freq_list)-2, 1024)
+        # TODO add assert_warns
+        f.set_triangle_bands(fvec(freq_list), samplerate)
+
+    def test_triangle_freqs_with_not_enough_filters(self):
+        """make sure set_triangle_bands warns when not enough filters"""
+        samplerate = 22050
+        freq_list = [0, 100, 1000, 4000, 8000, 10000]
+        f = filterbank(len(freq_list)-3, 1024)
+        # TODO add assert_warns
+        f.set_triangle_bands(fvec(freq_list), samplerate)
+
+    def test_triangle_freqs_with_too_many_filters(self):
+        """make sure set_triangle_bands warns when too many filters"""
+        samplerate = 22050
+        freq_list = [0, 100, 1000, 4000, 8000, 10000]
+        f = filterbank(len(freq_list)-1, 1024)
+        # TODO add assert_warns
+        f.set_triangle_bands(fvec(freq_list), samplerate)
+
+    def test_triangle_freqs_with_double_value(self):
+        """make sure set_triangle_bands works with 2 duplicate freqs"""
+        samplerate = 22050
+        freq_list = [0, 100, 1000, 4000, 4000, 4000, 10000]
+        f = filterbank(len(freq_list)-2, 1024)
+        # TODO add assert_warns
+        f.set_triangle_bands(fvec(freq_list), samplerate)
+
+    def test_triangle_freqs_with_triple(self):
+        """make sure set_triangle_bands works with 3 duplicate freqs"""
+        samplerate = 22050
+        freq_list = [0, 100, 1000, 4000, 4000, 4000, 10000]
+        f = filterbank(len(freq_list)-2, 1024)
+        # TODO add assert_warns
+        f.set_triangle_bands(fvec(freq_list), samplerate)
+
 
 if __name__ == '__main__':
     from unittest import main
--- a/src/spectral/filterbank_mel.c
+++ b/src/spectral/filterbank_mel.c
@@ -54,9 +54,21 @@
         n_filters, freqs->length - 2);
   }
 
-  if (freqs->data[freqs->length - 1] > samplerate / 2) {
-    AUBIO_WRN ("Nyquist frequency is %fHz, but highest frequency band ends at \
-%fHz\n", samplerate / 2, freqs->data[freqs->length - 1]);
+  for (fn = 0; fn < freqs->length; fn++) {
+    if (freqs->data[fn] < 0) {
+      AUBIO_ERR("filterbank_mel: freqs must contain only positive values.\n");
+      return AUBIO_FAIL;
+    } else if (freqs->data[fn] > samplerate / 2) {
+      AUBIO_WRN("filterbank_mel: freqs should contain only "
+          "values < samplerate / 2.\n");
+    } else if (fn > 0 && freqs->data[fn] < freqs->data[fn-1]) {
+      AUBIO_ERR("filterbank_mel: freqs should be a list of frequencies "
+          "sorted from low to high, but freq[%d] < freq[%d-1]\n", fn, fn);
+      return AUBIO_FAIL;
+    } else if (fn > 0 && freqs->data[fn] == freqs->data[fn-1]) {
+      AUBIO_WRN("filterbank_mel: set_triangle_bands received a list "
+          "with twice the frequency %f\n", freqs->data[fn]);
+    }
   }
 
   /* convenience reference to lower/center/upper frequency for each triangle */
@@ -92,17 +104,6 @@
   /* zeroing of all filters */
   fmat_zeros (filters);
 
-  if (fft_freqs->data[1] >= lower_freqs->data[0]) {
-    /* - 1 to make sure we don't miss the smallest power of two */
-    uint_t min_win_s =
-        (uint_t) FLOOR (samplerate / lower_freqs->data[0]) - 1;
-    AUBIO_WRN ("Lowest frequency bin (%.2fHz) is higher than lowest frequency \
-band (%.2f-%.2fHz). Consider increasing the window size from %d to %d.\n",
-        fft_freqs->data[1], lower_freqs->data[0],
-        upper_freqs->data[0], (win_s - 1) * 2,
-        aubio_next_power_of_two (min_win_s));
-  }
-
   /* building each filter table */
   for (fn = 0; fn < n_filters; fn++) {
 
@@ -160,7 +161,7 @@
   del_fvec (triangle_heights);
   del_fvec (fft_freqs);
 
-  return 0;
+  return AUBIO_OK;
 }
 
 uint_t