From: Paul Brossier Date: Fri, 8 Mar 2013 19:36:44 +0000 (-0500) Subject: python/tests/test_fft.py: update, indent X-Git-Tag: 0.4.0-beta1~212 X-Git-Url: https://git.aubio.org/?a=commitdiff_plain;h=0a42239f2bfcccbc718589022d259e301064e3ae;p=aubio.git python/tests/test_fft.py: update, indent --- diff --git a/python/demos/demo_onset_sinusoid.py b/python/demos/demo_onset_sinusoid.py deleted file mode 100755 index 97f9231d..00000000 --- a/python/demos/demo_onset_sinusoid.py +++ /dev/null @@ -1,84 +0,0 @@ -#! /usr/bin/env python - -from numpy import random, sin, arange, ones, zeros -from math import pi -from aubio import fvec, onset - -def build_sinusoid(length, freqs, samplerate): - return sin( 2. * pi * arange(length) * freqs / samplerate) - -def run_onset(p, input_vec): - f = fvec (p.hop_size) - cands = [] - count = 0 - for vec_slice in input_vec.reshape((-1, p.hop_size)): - f[:] = vec_slice - cands.append(o(f)) - return cands - -methods = ['default', - 'energy', - 'complex', - 'phase', - 'specdiff', - 'kl', - 'mkl', - 'specflux', - 'centroid', - 'spread', - 'skewness', - 'kurtosis', - 'slope', - 'decrease', - 'rolloff', - ] - -cands = {} -buf_size = 2048 -hop_size = 512 -samplerate = 44100 -sin_length = (samplerate * 10) % 512 * 512 -freqs = zeros(sin_length) - -partition = sin_length / 8 -pointer = 0 - -pointer += partition -freqs[pointer: pointer + partition] = 440 - -pointer += partition -pointer += partition -freqs[ pointer : pointer + partition ] = 740 - -pointer += partition -freqs[ pointer : pointer + partition ] = 1480 - -pointer += partition -pointer += partition -freqs[ pointer : pointer + partition ] = 400 + 5 * random.random(sin_length/8) - -a = build_sinusoid(sin_length, freqs, samplerate) - -for method in methods: - o = onset(method, buf_size, hop_size, samplerate) - cands[method] = run_onset(o, a) - -print "done computing" - -if 1: - from pylab import plot, show, xlabel, ylabel, legend, ylim, subplot - subplot (211) - legend(methods+['ground truth'], 'upper right') - xlabel('time (s)') - ylabel('amplitude') - ramp = arange(0, sin_length).astype('float') / samplerate - plot(ramp, a, ':') - subplot (212) - ramp = arange(0, sin_length / hop_size).astype('float') * hop_size / samplerate - for method in methods: - plot(ramp, cands[method],'.-') - legend(methods, 'upper right') - xlabel('time (s)') - ylabel('spectral descriptor value') - show() - diff --git a/python/demos/demo_specdesc.py b/python/demos/demo_specdesc.py new file mode 100755 index 00000000..97f9231d --- /dev/null +++ b/python/demos/demo_specdesc.py @@ -0,0 +1,84 @@ +#! /usr/bin/env python + +from numpy import random, sin, arange, ones, zeros +from math import pi +from aubio import fvec, onset + +def build_sinusoid(length, freqs, samplerate): + return sin( 2. * pi * arange(length) * freqs / samplerate) + +def run_onset(p, input_vec): + f = fvec (p.hop_size) + cands = [] + count = 0 + for vec_slice in input_vec.reshape((-1, p.hop_size)): + f[:] = vec_slice + cands.append(o(f)) + return cands + +methods = ['default', + 'energy', + 'complex', + 'phase', + 'specdiff', + 'kl', + 'mkl', + 'specflux', + 'centroid', + 'spread', + 'skewness', + 'kurtosis', + 'slope', + 'decrease', + 'rolloff', + ] + +cands = {} +buf_size = 2048 +hop_size = 512 +samplerate = 44100 +sin_length = (samplerate * 10) % 512 * 512 +freqs = zeros(sin_length) + +partition = sin_length / 8 +pointer = 0 + +pointer += partition +freqs[pointer: pointer + partition] = 440 + +pointer += partition +pointer += partition +freqs[ pointer : pointer + partition ] = 740 + +pointer += partition +freqs[ pointer : pointer + partition ] = 1480 + +pointer += partition +pointer += partition +freqs[ pointer : pointer + partition ] = 400 + 5 * random.random(sin_length/8) + +a = build_sinusoid(sin_length, freqs, samplerate) + +for method in methods: + o = onset(method, buf_size, hop_size, samplerate) + cands[method] = run_onset(o, a) + +print "done computing" + +if 1: + from pylab import plot, show, xlabel, ylabel, legend, ylim, subplot + subplot (211) + legend(methods+['ground truth'], 'upper right') + xlabel('time (s)') + ylabel('amplitude') + ramp = arange(0, sin_length).astype('float') / samplerate + plot(ramp, a, ':') + subplot (212) + ramp = arange(0, sin_length / hop_size).astype('float') * hop_size / samplerate + for method in methods: + plot(ramp, cands[method],'.-') + legend(methods, 'upper right') + xlabel('time (s)') + ylabel('spectral descriptor value') + show() + diff --git a/python/tests/test_fft.py b/python/tests/test_fft.py index ccb6e4cf..f8b2bfee 100755 --- a/python/tests/test_fft.py +++ b/python/tests/test_fft.py @@ -2,112 +2,111 @@ from numpy.testing import TestCase, run_module_suite from numpy.testing import assert_equal, assert_almost_equal -# WARNING: numpy also has an fft object from aubio import fvec, fft, cvec from numpy import array, shape from math import pi class aubio_fft_test_case(TestCase): - def test_members(self): - f = fft() - assert_equal (f.win_s, 1024) + def test_members(self): + """ check members are set correctly """ + win_s = 2048 + f = fft(win_s) + assert_equal (f.win_s, win_s) - def test_output_dimensions(self): - """ check the dimensions of output """ - win_s = 1024 - timegrain = fvec(win_s) - f = fft(win_s) - fftgrain = f (timegrain) - assert_equal (fftgrain.norm, 0) - assert_equal (shape(fftgrain.norm), (win_s/2+1,)) - assert_equal (fftgrain.phas, 0) - assert_equal (shape(fftgrain.phas), (win_s/2+1,)) + def test_output_dimensions(self): + """ check the dimensions of output """ + win_s = 1024 + timegrain = fvec(win_s) + f = fft (win_s) + fftgrain = f (timegrain) + assert_equal (shape(fftgrain.norm), (win_s/2+1,)) + assert_equal (shape(fftgrain.phas), (win_s/2+1,)) - def test_zeros(self): - """ check the transform of zeros """ - win_s = 512 - timegrain = fvec(win_s) - f = fft(win_s) - fftgrain = f(timegrain) - assert_equal ( fftgrain.norm == 0, True ) - assert_equal ( fftgrain.phas == 0, True ) + def test_zeros(self): + """ check the transform of zeros is all zeros """ + win_s = 512 + timegrain = fvec(win_s) + f = fft (win_s) + fftgrain = f (timegrain) + assert_equal ( fftgrain.norm, 0 ) + assert_equal ( fftgrain.phas, 0 ) - def test_impulse(self): - """ check the transform of one impulse at a random place """ - from random import random - from math import floor - win_s = 256 - i = floor(random()*win_s) - impulse = pi * random() - f = fft(win_s) - timegrain = fvec(win_s) - timegrain[i] = impulse - fftgrain = f ( timegrain ) - #self.plot_this ( fftgrain.phas ) - assert_almost_equal ( fftgrain.norm, impulse, decimal = 6 ) - assert_equal ( fftgrain.phas <= pi, True) - assert_equal ( fftgrain.phas >= -pi, True) + def test_impulse(self): + """ check the transform of one impulse at a random place """ + from random import random + from math import floor + win_s = 256 + i = floor(random()*win_s) + impulse = pi * random() + f = fft(win_s) + timegrain = fvec(win_s) + timegrain[i] = impulse + fftgrain = f ( timegrain ) + #self.plot_this ( fftgrain.phas ) + assert_almost_equal ( fftgrain.norm, impulse, decimal = 6 ) + assert_equal ( fftgrain.phas <= pi, True) + assert_equal ( fftgrain.phas >= -pi, True) - def test_impulse_negative(self): - """ check the transform of one impulse at a random place """ - from random import random - from math import floor - win_s = 256 - i = 0 - impulse = -10. - f = fft(win_s) - timegrain = fvec(win_s) - timegrain[i] = impulse - fftgrain = f ( timegrain ) - #self.plot_this ( fftgrain.phas ) - assert_almost_equal ( fftgrain.norm, abs(impulse), decimal = 6 ) - if impulse < 0: - # phase can be pi or -pi, as it is not unwrapped - assert_almost_equal ( abs(fftgrain.phas[1:-1]) , pi, decimal = 6 ) - assert_almost_equal ( fftgrain.phas[0], pi, decimal = 6) - assert_almost_equal ( fftgrain.phas[-1], pi, decimal = 6) - else: - assert_equal ( fftgrain.phas[1:-1] == 0, True) - assert_equal ( fftgrain.phas[0] == 0, True) - assert_equal ( fftgrain.phas[-1] == 0, True) - # now check the resynthesis - synthgrain = f.rdo ( fftgrain ) - #self.plot_this ( fftgrain.phas.T ) - assert_equal ( fftgrain.phas <= pi, True) - assert_equal ( fftgrain.phas >= -pi, True) - #self.plot_this ( synthgrain - timegrain ) - assert_almost_equal ( synthgrain, timegrain, decimal = 6 ) + def test_impulse_negative(self): + """ check the transform of one impulse at a random place """ + from random import random + from math import floor + win_s = 256 + i = 0 + impulse = -10. + f = fft(win_s) + timegrain = fvec(win_s) + timegrain[i] = impulse + fftgrain = f ( timegrain ) + #self.plot_this ( fftgrain.phas ) + assert_almost_equal ( fftgrain.norm, abs(impulse), decimal = 6 ) + if impulse < 0: + # phase can be pi or -pi, as it is not unwrapped + assert_almost_equal ( abs(fftgrain.phas[1:-1]) , pi, decimal = 6 ) + assert_almost_equal ( fftgrain.phas[0], pi, decimal = 6) + assert_almost_equal ( fftgrain.phas[-1], pi, decimal = 6) + else: + assert_equal ( fftgrain.phas[1:-1] == 0, True) + assert_equal ( fftgrain.phas[0] == 0, True) + assert_equal ( fftgrain.phas[-1] == 0, True) + # now check the resynthesis + synthgrain = f.rdo ( fftgrain ) + #self.plot_this ( fftgrain.phas.T ) + assert_equal ( fftgrain.phas <= pi, True) + assert_equal ( fftgrain.phas >= -pi, True) + #self.plot_this ( synthgrain - timegrain ) + assert_almost_equal ( synthgrain, timegrain, decimal = 6 ) - def test_impulse_at_zero(self): - """ check the transform of one impulse at a index 0 """ - win_s = 1024 - impulse = pi - f = fft(win_s) - timegrain = fvec(win_s) - timegrain[0] = impulse - fftgrain = f ( timegrain ) - #self.plot_this ( fftgrain.phas ) - assert_equal ( fftgrain.phas[0], 0) - # could be 0 or -0 depending on fft implementation (0 for fftw3, -0 for ooura) - assert_almost_equal ( fftgrain.phas[1], 0) - assert_almost_equal ( fftgrain.norm[0], impulse, decimal = 6 ) + def test_impulse_at_zero(self): + """ check the transform of one impulse at a index 0 """ + win_s = 1024 + impulse = pi + f = fft(win_s) + timegrain = fvec(win_s) + timegrain[0] = impulse + fftgrain = f ( timegrain ) + #self.plot_this ( fftgrain.phas ) + assert_equal ( fftgrain.phas[0], 0) + # could be 0 or -0 depending on fft implementation (0 for fftw3, -0 for ooura) + assert_almost_equal ( fftgrain.phas[1], 0) + assert_almost_equal ( fftgrain.norm[0], impulse, decimal = 6 ) - def test_rdo_before_do(self): - """ check running fft.rdo before fft.do works """ - win_s = 1024 - impulse = pi - f = fft(win_s) - fftgrain = cvec(win_s) - t = f.rdo( fftgrain ) - assert_equal ( t, 0 ) + def test_rdo_before_do(self): + """ check running fft.rdo before fft.do works """ + win_s = 1024 + impulse = pi + f = fft(win_s) + fftgrain = cvec(win_s) + t = f.rdo( fftgrain ) + assert_equal ( t, 0 ) - def plot_this(self, this): - from pylab import plot, show - plot ( this ) - show () + def plot_this(self, this): + from pylab import plot, show + plot ( this ) + show () if __name__ == '__main__': - from unittest import main - main() + from unittest import main + main()