shithub: aubio

ref: 33916b8718563444fc6498083b41af0c0cf2bb78
dir: /interfaces/python/test_filter.py/

View raw version
from numpy.testing import TestCase, run_module_suite
from numpy.testing import assert_equal, assert_almost_equal
from _aubio import *
from numpy import array

def array_from_text_file(filename, dtype = 'float'):
  return array([line.split() for line in open(filename).readlines()], 
      dtype = dtype)

class aubio_filter_test_case(TestCase):

  def test_members(self):
    f = digital_filter()
    assert_equal ([f.channels, f.order, f.samplerate], [1, 7, 44100])
    f = digital_filter(48000, 5, 2)
    assert_equal ([f.channels, f.order, f.samplerate], [2, 5, 48000])
    f(fvec())
  
  def test_cweighting_error(self):
    f = digital_filter (44100, 2, 1)
    self.assertRaises ( ValueError, f.set_c_weighting )
    f = digital_filter (44100, 8, 1)
    self.assertRaises ( ValueError, f.set_c_weighting )
    f = digital_filter (4000, 5, 1)
    self.assertRaises ( ValueError, f.set_c_weighting )
    f = digital_filter (193000, 5, 1)
    self.assertRaises ( ValueError, f.set_c_weighting )
    f = digital_filter (193000, 7, 1)
    self.assertRaises ( ValueError, f.set_a_weighting )
    f = digital_filter (192000, 5, 1)
    self.assertRaises ( ValueError, f.set_a_weighting )

  def test_c_weighting(self):
    expected = array_from_text_file('c_weighting_test_simple.expected')
    f = digital_filter(44100, 5, 1)
    f.set_c_weighting()
    v = fvec(32)
    v[0][12] = .5
    u = f(v)
    assert_almost_equal (expected[1], u)

  def test_a_weighting(self):
    expected = array_from_text_file('a_weighting_test_simple.expected')
    f = digital_filter(44100, 7, 1)
    f.set_a_weighting()
    v = fvec(32)
    v[0][12] = .5
    u = f(v)
    assert_almost_equal (expected[1], u)

  def test_a_weighting_parted(self):
    expected = array_from_text_file('a_weighting_test_simple.expected')
    f = digital_filter(44100, 7, 1)
    f.set_a_weighting()
    v = fvec(16)
    v[0][12] = .5
    u = f(v)
    assert_almost_equal (expected[1][:16], u)
    # one more time
    v = fvec(16)
    u = f(v)
    assert_almost_equal (expected[1][16:], u)

if __name__ == '__main__':
  from unittest import main
  main()