Exemplo n.º 1
0
    def test_zeropad(self):
        signal = audio.generate_tone(1, 1, 1e3)

        buffered = audio.zeropad(signal, 10)

        assert len(buffered) - len(signal)  == 20
        assert np.array_equal(buffered[:10], buffered[-10:])
        assert np.array_equal(buffered[:10], np.zeros(10))

        buffered = audio.zeropad(signal, 0)
        assert len(buffered) == len(signal)

        # Test multichannel signal
        signal = audio.generate_tone(1, 1, 1e3)
        mc_signal = np.column_stack([signal, signal])
        mc_buffered = audio.zeropad(mc_signal, 10)
        assert np.array_equal(mc_buffered[:10, 0], mc_buffered[-10:, 1])

        # Test different start and end zeros
        signal = audio.generate_tone(1, 1, 1e3)
        mc_signal = np.column_stack([signal, signal])
        mc_buffered = audio.zeropad(mc_signal, (10, 5))
        assert np.all(mc_buffered[:10] == 0)
        assert np.all(mc_buffered[-5:] == 0)

        sig = audio.Signal(2, 1, 1)
        sig[:] = 1
        zpsig = audio.zeropad(sig, [2, 2])
        assert zpsig.shape == (5, 2)

        sig = audio.Signal((2, 3), 1, 1)
        sig[:] = 1
        zpsig = audio.zeropad(sig, [2, 2])
        assert zpsig.shape == (5, 2, 3)
Exemplo n.º 2
0
    def test_get_time(self):

        tone = audio.generate_tone(1, 1, 1e3)
        time = audio.get_time(tone, 1e3)

        #Test sampling rate
        assert time[2] - time[1] == 1./1e3

        #Test duration
        assert time[-1] == 1 - 1./1e3

        tone1 = audio.generate_tone(1, 1, 1e3)
        tone2 = audio.generate_tone(1, 1, 1e3)

        tone_two_channel = np.column_stack([tone1, tone2])

        time = audio.get_time(tone, 1e3)

        assert len(time) == len(tone_two_channel)

        #Test sampling rate
        assert time[2] - time[1] == 1./1e3

        #Test duration
        assert time[-1] == 1 - 1./1e3

        # Test appearence of extra sample due to numerics
        fs = 48e3
        left = np.linspace(0, 1, 50976)
        time = audio.get_time(left, fs)
        assert len(left) == len(time)
Exemplo n.º 3
0
    def test_delay_signal(self):

        signal = audio.generate_tone(1, 1, 1e3, start_phase = 0.5 * np.pi)
        signal += audio.generate_tone(1, 2, 1e3, start_phase = 0.5 * np.pi)

        delayed = audio.delay_signal(signal, 1.5e-3, 1e3)

        phase1 = 1.5e-3 * 1 * 2 * np.pi - 0.5 * np.pi
        phase2 = 1.5e-3 * 2 * 2 * np.pi - 0.5 * np.pi

        shifted = audio.generate_tone(1, 1, 1e3, start_phase=-phase1)
        shifted += audio.generate_tone(1, 2, 1e3, start_phase=-phase2)

        error = np.abs(shifted[:] - delayed[:-2, 1])
        assert np.max(error[10:-10]) <= 1e-3

        # Check if a negative delay results in inverted channels
        delayed_negative = audio.delay_signal(signal, -1.5e-3, 1e3)

        assert np.array_equal(delayed[:, 0], delayed_negative[:, 1])
        assert np.array_equal(delayed[:, 1], delayed_negative[:, 0])

        # Test with noise and full sample shift
        duration = 100e-3
        fs = 48e3
        noise = audio.generate_noise(duration, fs=fs)
        noise *= audio.cosine_fade_window(noise, 20e-3, fs)
        dt = 1. / fs
        delayed = audio.delay_signal(noise, dt * 5, fs)
        testing.assert_almost_equal(delayed[5:, 1], delayed[:-5, 0])
Exemplo n.º 4
0
    def test_addtone(self):
        fs = 48000
        duration = 100e-3

        sig = Signal(1, duration, fs)
        sig.add_tone(100)
        sig.add_tone(200, start_phase=np.pi)

        test = audio.generate_tone(duration, 100, fs)
        test += audio.generate_tone(duration, 200, fs, np.pi)

        testing.assert_equal(sig, test)

        sig = Signal(1, duration, fs)
        sig.add_tone(100, amplitude=2)

        test = 2 * audio.generate_tone(duration, 100, fs)
        testing.assert_equal(sig, test)

        sig = Signal(2, duration, fs)
        sig.add_tone(100, amplitude=2)

        test = 2 * audio.generate_tone(duration, 100, fs)
        testing.assert_equal(sig.ch[0], test)
        testing.assert_equal(sig.ch[1], test)

        sig = Signal((2, 2), duration, fs)
        sig.add_tone(100, amplitude=2)

        test = 2 * audio.generate_tone(duration, 100, fs)
        testing.assert_equal(sig.ch[0, 0], test)
        testing.assert_equal(sig.ch[1, 0], test)
        testing.assert_equal(sig.ch[0, 1], test)
        testing.assert_equal(sig.ch[1, 1], test)
Exemplo n.º 5
0
    def test_crest_factor(self):

        # Test that c for sine is equal to sqrt(2)
        signal = audio.generate_tone(100, 1, 100e3)
        c = audio.crest_factor(signal)
        testing.assert_almost_equal(c, 20*np.log10(np.sqrt(2)))

        # test that c for half wave rect. sine is 2
        signal = audio.generate_tone(100, 1, 100e3)
        signal[signal < 0] = 0
        c = audio.crest_factor(signal)
        testing.assert_almost_equal(c, 20*np.log10(2))
Exemplo n.º 6
0
    def test_phaseshift(self):
        fs = 48000
        duration = 100e-3

        sig = Signal(2, duration, fs)
        sig.add_tone(100)
        sig[:, 0].phase_shift(np.pi)

        test1 = audio.generate_tone(duration, 100, fs, np.pi)
        test2 = audio.generate_tone(duration, 100, fs)
        test = np.column_stack([test1, test2])

        testing.assert_almost_equal(sig, test)
Exemplo n.º 7
0
    def test_generate_tone(self):
        # test frequency, sampling rate and duration
        tone1 = audio.generate_tone(1, 1, 1e3)
        tone2 = audio.generate_tone(0.5, 2, 2e3)
        assert np.array_equal(tone1, tone2)

        # test phaseshift
        tone = audio.generate_tone(1, 1, 1e3, start_phase=np.pi / 2)
        testing.assert_almost_equal(tone[0], 0)
        tone = audio.generate_tone(1, 1, 1e3, start_phase=1 * np.pi)
        testing.assert_almost_equal(tone[0], -1)

        sig = audio.Signal((2, 3), 1, 48000).add_tone(50)
        tone = audio.generate_tone(sig, 50)
        testing.assert_array_equal(sig, tone)
Exemplo n.º 8
0
    def test_calc_dbfs(self):
        signal = audio.generate_tone(1000, 1, 48000)
        testing.assert_almost_equal(audio.calc_dbfs(signal), 0)

        signal = np.concatenate([-np.ones(10), np.ones(10)])
        signal = np.tile(signal, 100)
        rms_rect = 20 * np.log10(np.sqrt(2))
        testing.assert_almost_equal(audio.calc_dbfs(signal), rms_rect)
Exemplo n.º 9
0
    def test_cos_amp_modulator(self):
        fs = 48000
        sig = Signal(1, 1, fs).add_tone(100)
        sig.add_cos_modulator(5, 1)

        test = audio.generate_tone(1, 100, fs)
        test *= audio.cos_amp_modulator(test, 5, fs)

        testing.assert_array_equal(sig, test)

        fs = 48000
        sig = Signal(2, 1, fs).add_tone(100)
        sig.add_cos_modulator(5, 1)

        test = audio.generate_tone(1, 100, fs)
        test *= audio.cos_amp_modulator(test, 5, fs)

        testing.assert_array_equal(sig[:, 0], test)
        testing.assert_array_equal(sig[:, 1], test)
Exemplo n.º 10
0
    def test_setdbspl(self):
        fs = 48000
        duration = 100e-3

        sig = Signal(1, duration, fs)
        sig.add_tone(100).set_dbspl(50)

        test = audio.generate_tone(duration, 100, fs)
        test = audio.set_dbspl(test, 50)

        testing.assert_equal(sig, test)
Exemplo n.º 11
0
    def test_set_dbfs(self):
        signal = audio.generate_tone(1000, 1, 48000)
        signal = audio.set_dbfs(signal, -5)
        testing.assert_almost_equal(audio.calc_dbfs(signal), -5)

        # RMS value of a -5 db sine
        m = (10**(-5 / 20)) / np.sqrt(2)

        signal = np.concatenate([-np.ones(10), np.ones(10)])
        signal = np.tile(signal, 100)
        signal = audio.set_dbfs(signal, -5)
        assert(signal.max() == m)
Exemplo n.º 12
0
    def test_cos_amp_modulator(self):
        fs = 100e3
        signal = audio.generate_tone(1, 100, fs)
        mod = audio.cos_amp_modulator(signal, 5, fs)
        test = audio.generate_tone(1, 5, fs)
        testing.assert_array_almost_equal(mod, test + 1)
        assert max(mod) == 2.0

        mod = audio.cos_amp_modulator(signal, 5, fs, 0.5)
        assert mod[0] == 1.5

        mod = audio.cos_amp_modulator(signal, 5, fs, start_phase=np.pi)
        test = audio.generate_tone(1, 5, fs, start_phase=np.pi)
        testing.assert_array_almost_equal(mod, test + 1)

        sig = audio.Signal(1, 1, 48000).add_tone(5) + 1
        mod = audio.cos_amp_modulator(sig, 5, 1)
        testing.assert_array_equal(sig, mod)

        sig = audio.Signal((2, 3), 1, 48000).add_tone(5) + 1
        mod = audio.cos_amp_modulator(sig, 5, 1)
Exemplo n.º 13
0
    def test_gammatonefos_apply(self):
        # Check amplitude with on frequency tone
        b, a = gt.design_gammatone(500, 75, 48000, attenuation_db=-3)
        tone = audio.generate_tone(100e-3, 500, 48000)
        out, states = gt.gammatonefos_apply(tone, b, a, 4)
        assert (out.real[3000:].max() - 1) <= 5e-5
        assert (out.real[3000:].min() + 1) <= 5e-5

        # Check magnitude with tone at corner frequency
        b, a = gt.design_gammatone(500, 75, 48000, attenuation_db=-3)
        tone = audio.generate_tone(100e-3, 500 - 75 / 2, 48000)
        out, states = gt.gammatonefos_apply(tone, b, a, 4)
        # max should be - 3dB
        assert (20 * np.log10(out.real[3000:].max()) + 3) < 0.5e-3

        # Check magnitude with tone at corner frequency
        b, a = gt.design_gammatone(500, 200, 48000, attenuation_db=-3)
        tone = audio.generate_tone(100e-3, 500 - 200 / 2, 48000)
        out, states = gt.gammatonefos_apply(tone, b, a, 4)
        # max should be - 3dB
        assert (20 * np.log10(out.real[3000:].max()) + 3) < 0.05
Exemplo n.º 14
0
    def test_fadewindow(self):
        fs = 48000
        duration = 100e-3

        sig = Signal(1, duration, fs)
        sig.add_tone(100).add_fade_window(rise_time=10e-3, type='gauss')
        test = audio.generate_tone(duration, 100, fs)
        test *= audio.gaussian_fade_window(test, 10e-3, fs)
        testing.assert_equal(sig, test)

        sig = Signal(1, duration, fs)
        sig.add_tone(100).add_fade_window(rise_time=10e-3, type='cos')
        test = audio.generate_tone(duration, 100, fs)
        test *= audio.cosine_fade_window(test, 10e-3, fs)
        testing.assert_equal(sig, test)

        sig = Signal(1, duration, fs)
        sig.add_tone(100).add_fade_window(rise_time=10e-3, type='cos')
        test = audio.generate_tone(duration, 100, fs)
        test *= audio.cosine_fade_window(test, 10e-3, fs)
        testing.assert_equal(sig, test)
Exemplo n.º 15
0
    def test_extract_binaural_differences(self):

        from scipy.signal import hilbert

        # Check phase_difference
        fs = 48000
        signal1 = audio.generate_tone(1, 500,  fs)
        signal2 = audio.generate_tone(1, 500,  fs, start_phase=0.5 * np.pi)
        signal = np.column_stack([signal1, signal2])
        ipd, ild = audio.extract_binaural_differences(signal)

        assert len(ipd) == len(signal1)
        assert np.all(np.isclose(ild, 0))
        assert np.all(np.isclose(ipd, -np.pi * 0.5))

        # check log level difference
        signal1 = audio.set_dbspl(audio.generate_tone(1, 500, fs), 50)
        signal2 = audio.set_dbspl(audio.generate_tone(1, 500, fs), 60)
        signal = np.column_stack([signal1, signal2])
        ipd, ild = audio.extract_binaural_differences(signal)
        assert np.all(np.isclose(ild, -10))

        # check amplitude difference
        fs = 48000
        signal1 = audio.generate_tone(1, 500, fs)
        signal2 = audio.generate_tone(1, 500, fs) * 0.5
        signal = np.column_stack([signal1, signal2])
        ipd, ild = audio.extract_binaural_differences(signal,
                                                           log_ilds=False)
        assert np.all(np.isclose(ild, 2))
        assert np.all(np.isclose(ipd, 0))
Exemplo n.º 16
0
    def test_channel_indexing(self):
        sig = Signal((2, 2), 1, 48000).add_noise()
        testing.assert_equal(sig.ch[0, 0], sig[:, 0, 0])
        testing.assert_equal(sig.ch[0], sig[:, 0])

        sig = Signal(2, 1, 48000)
        sig.ch[0] = 1
        assert np.all(sig[:, 0] == 1)

        sig.ch[1].add_tone(500)
        tone_2 = audio.generate_tone(sig.duration, 500, sig.fs)
        testing.assert_equal(sig.ch[1], tone_2)

        #Indexing only one channel should still work
        sig = Signal(1, 1, 40000).add_noise()
        testing.assert_equal(sig.ch[0], sig)
        sig.ch[0] = 1
        testing.assert_equal(sig.ch[0], 1)
Exemplo n.º 17
0
    def test_cosine_fade_window(self):
        window = audio.cosine_fade_window(np.zeros(1000), 100e-3, 1e3)
        n_window = 100

        #test symmentry
        assert np.array_equal(window[:100], window[-100:][::-1])

        #test starts with 0
        assert window[0] == 0

        window = audio.cosine_fade_window(np.zeros(1000), 100e-3, 1e3)
        n_window = 100

        #test if the window is a cosine curve of the right type
        cos_curve = np.concatenate([window[:100], window[-101:]])
        sin = (0.5 * audio.generate_tone(0.2 + 1. / 1e3, 5, 1e3,
                                         start_phase=np.pi)) + 0.5
        testing.assert_array_almost_equal(cos_curve, sin)

        # Test that the last sample in the window is not equal to 1
        nsamp = audio.nsamples(200e-3, 1e3)
        window = audio.cosine_fade_window(np.zeros(nsamp + 1), 100e-3 , 1e3)
        n_window = 100
        assert window[int(nsamp / 2)] == 1
        assert window[int(nsamp / 2 - 1)] != 1
        assert window[int(nsamp / 2 + 1)] != 1
        assert window[int(nsamp / 2 + 1)] == window[int(nsamp / 2 - 1)]

        # Test multichannel window
        window = audio.cosine_fade_window(np.zeros([1000, 2]), 100e-3, 1e3)
        assert np.array_equal(window[:, 0], window[:, 1])
        assert np.array_equal(window[:100, 0], window[-100:, 0][::-1])

        sig = audio.Signal((2, 3), 1, 48000)
        win = audio.cosine_fade_window(sig, 100e-3)
        assert win.shape == sig.shape
        testing.assert_array_equal (win[:, 1, 0], win[:, 0, 1])
Exemplo n.º 18
0
 def test_set_dbsl(self):
     fs = 100e3
     signal = audio.generate_tone(100, 1, fs)
     signal = audio.set_dbspl(signal, 22)
     assert audio.calc_dbspl(signal) == 22
     assert audio.set_dbspl(1, 0) == 20e-6
Exemplo n.º 19
0
 def test_crest_factor(self):
     signal = audio.generate_tone(100, 1, 100e3)
     cfac = audio.crest_factor(signal)
     testing.assert_almost_equal(cfac, np.sqrt(2))