Changeset 20b1aed


Ignore:
Timestamp:
Mar 12, 2013, 4:44:38 AM (11 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
3cde629
Parents:
b130600
Message:

python/tests/test_pitch.py: indent and generate more tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/tests/test_pitch.py

    rb130600 r20b1aed  
    11#! /usr/bin/env python
    22
    3 from numpy.testing import TestCase
     3from unittest import TestCase
    44from numpy.testing import assert_equal, assert_almost_equal
    5 from numpy import random, sin, arange, mean, median
     5from numpy import random, sin, arange, mean, median, isnan
    66from math import pi
    7 from aubio import fvec, pitch
     7from aubio import fvec, pitch, freqtomidi
    88
    9 class aubio_mathutils_test_case(TestCase):
     9class aubio_pitch_Good_Values(TestCase):
    1010
    11   def test_members(self):
    12     p = pitch()
    13     assert_equal ( [p.method, p.buf_size, p.hop_size, p.samplerate],
    14       ['default', 1024, 512, 44100])
     11    def skip_test_new_default(self):
     12        " creating a pitch object without parameters "
     13        p = pitch()
     14        assert_equal ( [p.method, p.buf_size, p.hop_size, p.samplerate],
     15            ['default', 1024, 512, 44100])
    1516
    16   def test_members_not_default(self):
    17     p = pitch('mcomb', 2048, 512, 32000)
    18     assert_equal ( [p.method, p.buf_size, p.hop_size, p.samplerate],
    19       ['mcomb', 2048, 512, 32000])
     17    def test_run_on_silence(self):
     18        " creating a pitch object with parameters "
     19        p = pitch('default', 2048, 512, 32000)
     20        assert_equal ( [p.method, p.buf_size, p.hop_size, p.samplerate],
     21            ['default', 2048, 512, 32000])
    2022
    21   def test_run_on_zeros(self):
    22     p = pitch('mcomb', 2048, 512, 32000)
    23     f = fvec (512)
    24     assert_equal ( p(f), 0. )
     23    def test_run_on_zeros(self):
     24        " running on silence gives 0 "
     25        p = pitch('default', 2048, 512, 32000)
     26        f = fvec (512)
     27        for i in xrange(10): assert_equal (p(f), 0.)
    2528
    26   def test_run_on_ones(self):
    27     p = pitch('mcomb', 2048, 512, 32000)
    28     f = fvec (512)
    29     f[:] = 1
    30     assert( p(f) != 0. )
     29    def test_run_on_ones(self):
     30        " running on ones gives 0 "
     31        p = pitch('default', 2048, 512, 32000)
     32        f = fvec (512)
     33        f[:] = 1
     34        for i in xrange(10): assert_equal (p(f), 0.)
    3135
    32   def test_run_default_on_sinusoid(self):
    33     method = 'default'
    34     buf_size = 2048
    35     hop_size = 512
    36     samplerate = 32000
    37     freq = 450.
    38     self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq)
     36class aubio_pitch_Sinusoid(TestCase):
    3937
    40   def test_run_schmitt_on_sinusoid(self):
    41     method = 'schmitt'
    42     buf_size = 4096
    43     hop_size = 512
    44     samplerate = 44100
    45     freq = 800.
    46     self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq)
     38    def run_pitch_on_sinusoid(self, method, buf_size, hop_size, samplerate, freq):
     39        # create pitch object
     40        p = pitch(method, buf_size, hop_size, samplerate)
     41        # duration in seconds
     42        seconds = .3
     43        # duration in samples
     44        duration =  seconds * samplerate
     45        # increase to the next multiple of hop_size
     46        duration = duration - duration % hop_size + hop_size;
     47        # build sinusoid
     48        sinvec = self.build_sinusoid(duration, freq, samplerate)
    4749
    48   def test_run_mcomb_on_sinusoid(self):
    49     method = 'mcomb'
    50     buf_size = 2048
    51     hop_size = 512
    52     samplerate = 44100
    53     freq = 10000.
    54     self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq)
     50        self.run_pitch(p, sinvec, freq)
    5551
    56   def test_run_fcomb_on_sinusoid(self):
    57     method = 'fcomb'
    58     buf_size = 2048
    59     hop_size = 512
    60     samplerate = 32000
    61     freq = 440.
    62     self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq)
     52    def build_sinusoid(self, length, freq, samplerate):
     53        return sin( 2. * pi * arange(length).astype('float32') * freq / samplerate)
    6354
    64   def test_run_yin_on_sinusoid(self):
    65     method = 'yin'
    66     buf_size = 4096
    67     hop_size = 512
    68     samplerate = 32000
    69     freq = 880.
    70     self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq)
     55    def run_pitch(self, p, input_vec, freq):
     56        count = 0
     57        pitches, errors = [], []
     58        input_blocks = input_vec.reshape((-1, p.hop_size))
     59        for new_block in input_blocks:
     60            pitch = p(new_block)[0]
     61            pitches.append(pitch)
     62            errors.append(1. - freqtomidi(pitch) / freqtomidi(freq))
     63        assert_equal ( len(input_blocks), len(pitches) )
     64        assert_equal ( isnan(pitches), False )
     65        # cut the first candidates
     66        cut = ( p.buf_size - p.hop_size ) / p.hop_size
     67        pitches = pitches[2:]
     68        errors = errors[2:]
     69        # check that the mean of all relative errors is less than 10%
     70        assert abs (mean(errors) ) < 0.1, pitches
     71        assert abs (mean(errors) ) < 0.1, "error is bigger than 0.1 (%f)" % mean(errors)
     72        #print 'len(pitches), cut:', len(pitches), cut
     73        #print 'mean errors: ', mean(errors), 'mean pitches: ', mean(pitches)
    7174
    72   def test_run_yinfft_on_sinusoid(self):
    73     method = 'yinfft'
    74     buf_size = 2048
    75     hop_size = 512
    76     samplerate = 32000
    77     freq = 640.
    78     self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq)
     75pitch_algorithms = [ "default", "yinfft", "yin", "schmitt", "mcomb", "fcomb" ]
    7976
    80   def run_pitch_on_sinusoid(self, method, buf_size, hop_size, samplerate, freq):
    81     p = pitch(method, buf_size, hop_size, samplerate)
    82     sinvec = self.build_sinusoid(hop_size * 100, freq, samplerate)
    83     self.run_pitch(p, sinvec, freq)
     77signal_modes = [
     78        ( 2048,  512, 44100, 440. ),
     79        ( 2048, 1024, 44100, 440. ),
     80        ( 2048, 1024, 44100, 440. ),
     81        ( 2048, 1024, 32000, 440. ),
     82        ( 2048, 1024, 22050, 440. ),
     83        ( 1024,  256, 16000, 440. ),
     84        ( 1024,  256, 8000,  440. ),
     85        ( 1024, 512+256, 8000, 440. ),
     86        ]
    8487
    85   def build_sinusoid(self, length, freq, samplerate):
    86     return sin( 2. * pi * arange(length).astype('float32') * freq / samplerate)
     88def create_test (algo, mode):
     89    def do_test_pitch(self):
     90        self.run_pitch_on_sinusoid(algo, mode[0], mode[1], mode[2], mode[3])
     91    return do_test_pitch
    8792
    88   def run_pitch(self, p, input_vec, freq):
    89     count = 0
    90     pitches, errors = [], []
    91     for vec_slice in input_vec.reshape((-1, p.hop_size)):
    92       pitch = p(vec_slice)
    93       pitches.append(pitch)
    94       errors.append(1. - pitch / freq)
    95     # check that the mean of all relative errors is less than 10%
    96     assert_almost_equal (mean(errors), 0., decimal = 2)
     93for algo in pitch_algorithms:
     94    for mode in signal_modes:
     95        test_method = create_test (algo, mode)
     96        test_method.__name__ = 'test_pitch_%s_%d_%d_%dHz_sin_%.2f' % ( algo,
     97                mode[0], mode[1], mode[2], mode[3] )
     98        setattr (aubio_pitch_Sinusoid, test_method.__name__, test_method)
    9799
    98100if __name__ == '__main__':
    99   from unittest import main
    100   main()
    101 
     101    from unittest import main
     102    main()
Note: See TracChangeset for help on using the changeset viewer.