Exemplo n.º 1
0
    def test_bandpass_gammatone(self):
        # check real valued output
        sig = audio.Signal(1, 1, 48000).add_tone(500)
        sig2 = sig.copy()
        sig.bandpass(500, 100, 'gammatone')
        assert not np.iscomplexobj(sig)
        assert sig.shape == sig2.shape

        # check complex output
        sig = audio.Signal(1, 1, 48000).add_tone(500)
        sig2 = sig.copy()
        sig.bandpass(500, 100, 'gammatone', return_complex=True)
        assert np.iscomplexobj(sig)
        assert sig.shape == sig2.shape

        # check equivalence of real and complex results
        sig = audio.Signal(1, 1, 48000).add_tone(500)
        sig2 = sig.copy()
        sig.bandpass(500, 100, 'gammatone')
        sig2.bandpass(500, 100, 'gammatone', return_complex=True)
        testing.assert_array_equal(sig2.real, sig)

        # check kwargs
        sig = audio.Signal(1, 1, 48000).add_tone(500)
        out = audio.filter.gammatone(sig, 500, 100,
                                     sig2.fs, order=2,
                                     attenuation_db=-1)
        sig.bandpass(500, 100, 'gammatone', order=2,
                     attenuation_db=-1)
        testing.assert_array_equal(sig, out.real)
Exemplo n.º 2
0
    def test_gammatone(self):
        # Tone located at center frequency
        sig = audio.Signal(1, 1, 48000).add_tone(500)
        out = filter.gammatone(sig, 500, 75, 48000, order=4, attenuation_db=-3)
        testing.assert_almost_equal(out.real.std(), 1 / np.sqrt(2), 2)

        # Tone located at -3dB frequency
        sig = audio.Signal(2, 1, 48000).add_tone(400)
        out = filter.gammatone(sig,
                               500,
                               200,
                               48000,
                               order=4,
                               attenuation_db=-3)
        testing.assert_almost_equal(out.real.std(), 0.5, 2)

        # Test Multichannel
        sig = audio.Signal((2, 2), 1, 48000).add_tone(400)
        out = filter.gammatone(sig,
                               500,
                               200,
                               48000,
                               order=4,
                               attenuation_db=-3)
        testing.assert_almost_equal(out.real.std(), 0.5, 2)
        assert (sig.shape == out.shape)
Exemplo n.º 3
0
    def test_calc_coherence(self):
        cf = 500
        bw = 100
        sig = audio.Signal(2, 100, 48000).add_noise().bandpass(cf, bw, 'brickwall')
        coh = audio.calc_coherence(sig)

        # Analytic coherence for aboves signal
        coh_analytic = (np.sin(np.pi * bw * sig.time[1:])
                        / (np.pi * bw * sig.time[1:])
                        * np.exp(1j * 2 * np.pi * cf * sig.time[1:]))

        assert isinstance(coh, audio.Signal)
        testing.assert_almost_equal(np.abs(coh[coh.time==0]), 1)
        nsamp = 1000
        start = np.where(coh.time == 0)[0][0]
        testing.assert_allclose(coh[start+1:start + nsamp],
                                coh_analytic[:nsamp-1], rtol=0, atol=0.03)

        # calculate auto-coherrence
        coh2 = audio.calc_coherence(sig.ch[0])
        testing.assert_array_equal(coh, coh2)

        # test using numpy arrays
        sig = np.asarray(sig)
        coh3 = audio.calc_coherence(sig)
        testing.assert_array_equal(coh3, coh)

        cf = 500
        bw = 100
        sig = audio.Signal(2, 100, 48000).add_uncorr_noise(0.5).bandpass(cf, bw, 'brickwall')
        coh = audio.calc_coherence(sig)
        testing.assert_allclose(coh.abs()[coh.time==0], 0.5, rtol=0.05)
Exemplo n.º 4
0
    def test_timeshift(self):
        #timeshift a tone by one full phase
        sig = audio.Signal(2, 1, 48000).add_tone(100)
        sig[:,
            1] = sig[:,
                     1].to_freqdomain().time_shift(1. / 100).to_timedomain()
        testing.assert_almost_equal(sig[:, 1], sig[:, 0])

        shift_samples = 500
        shift_time = shift_samples / 48000
        sig = audio.Signal(2, 1, 48000).add_noise()
        sig[:,
            1] = sig[:,
                     1].to_freqdomain().time_shift(shift_time).to_timedomain()
        testing.assert_almost_equal(sig[shift_samples:, 1],
                                    sig[:-shift_samples, 0])

        shift_time = 3.2e-4
        sig = audio.Signal(2, 1.000, 48000).add_noise()
        res = sig[:, 1].to_freqdomain().time_shift(shift_time).to_timedomain()
        assert np.all(np.isreal(res))

        shift_time = 3.2e-4
        sig = audio.Signal((2, 2), 1.000, 48000).add_noise()
        res = sig[:, 1].to_freqdomain().time_shift(shift_time).to_timedomain()
        assert np.all(np.isreal(res))
Exemplo n.º 5
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.º 6
0
 def test_plot(self):
     sig = audio.Signal(2, 1, 48000).add_noise()
     fig, ax = sig.plot()
     assert len(ax.lines) == sig.n_channels
     # Test for the correct colors in case of two channels
     assert ax.lines[0].get_color() == '#5c5cd6'
     assert ax.lines[1].get_color() == '#d65c5c'
Exemplo n.º 7
0
    def test_butterworth(self):
        fc = [100, 200, 5000]
        bw = [10, 5, 8]
        fs = 48000
        butter = create_filterbank(fc, bw, 'butter', fs)

        for i_filt, (fc, bw) in enumerate(zip(fc, bw)):
            low_f = fc - bw / 2
            high_f = fc + bw / 2
            sos = butterworth_filt.design_butterworth(low_f, high_f, fs)
            coeff = butter.coefficents[:, :, i_filt]
            testing.assert_array_equal(sos, coeff)

        # Test filter gain
        sig = audio.Signal(1, 1, 48000)
        fc = np.round(audio.freqarange(100, 4000, 1, 'erb'))
        bw = audio.calc_bandwidth(fc, 'erb')
        # add a tone at every fc
        for f in fc:
            sig.add_tone(f)
        # create filterbank an run signal
        bank = create_filterbank(fc, bw, 'butter', 48000)
        sig_out = bank.filt(sig)
        #check amplitudes at fc of every filter
        amps = np.zeros(len(fc))
        for i_fc, f in enumerate(fc):
            spec = sig_out.ch[i_fc].to_freqdomain()
            amps[i_fc] = np.abs(spec[spec.freq==f])
        # Amplitudes should be 0.5 (two sided spectrum)
        assert np.all((amps - 0.5) <= 0.01)
Exemplo n.º 8
0
    def test_gammatone(self):
        # Test if filter coefficents are equivalent tho those of
        # individual filters
        fc = [100, 200, 5000]
        bw = [10, 5, 8]
        fs = 48000
        gamma = create_filterbank(fc, bw, 'gammatone', fs)
        for i_filt, (fc, bw) in enumerate(zip(fc, bw)):
            b, a = gammatone_filt.design_gammatone(fc, bw, fs)
            b_bank = gamma.coefficents[0, i_filt]
            a_bank = gamma.coefficents[2:, i_filt]
            testing.assert_array_equal(a, a_bank)
            testing.assert_array_equal(b, b_bank)

        # Test the gain at fc
        sig = audio.Signal(1, 1, 48000)
        fc = np.round(audio.freqarange(100, 4000, 1, 'erb'))
        bw = audio.calc_bandwidth(fc, 'erb')
        # add a tone at every fc
        for f in fc:
            sig.add_tone(f)
        # create filterbank an run signal
        bank = create_filterbank(fc, bw, 'gammatone', 48000)
        sig_out = bank.filt(sig)
        #check amplitudes at fc of every filter
        amps = np.zeros(len(fc))
        for i_fc, f in enumerate(fc):
            spec = sig_out.ch[i_fc].to_freqdomain()
            amps[i_fc] = np.abs(spec[spec.freq==f])
        # Amplitudes hould be 1 (one sided spectrum)
        assert np.all((amps - 1) <= 0.01)
Exemplo n.º 9
0
    def test_bandpass(self):
        sig = audio.Signal(1, 1, 48000).add_noise()

        sig_out = audio.filter.bandpass(sig, 500, 100, 'gammatone')
        sig_out2 = audio.filter.gammatone(sig, 500, 100, 48000)
        testing.assert_array_equal(sig_out, sig_out2)

        sig = audio.Signal(1, 1, 48000).add_noise()
        sig_out = audio.filter.bandpass(sig, 500, 100, 'butter')
        sig_out2 = audio.filter.butterworth(sig, 450, 550, 48000)
        testing.assert_array_equal(sig_out, sig_out2)

        sig = audio.Signal(1, 1, 48000).add_noise()
        sig_out = audio.filter.bandpass(sig, 500, 100, 'brickwall')
        sig_out2 = audio.filter.brickwall(sig, 450, 550, 48000)
        testing.assert_array_equal(sig_out, sig_out2)
Exemplo n.º 10
0
    def test_duration_is_signal(self):

        #direct input
        duration, fs, n_ch = audio.audiotools._duration_is_signal(1, 2, 3)
        assert duration == 1
        assert fs == 2
        assert n_ch == 3

        duration, fs, n_ch = audio.audiotools._duration_is_signal(1, 2)
        assert duration == 1
        assert fs == 2
        assert n_ch == None

        #signal as input
        sig = audio.Signal((2, 3), 1, 2)
        duration, fs, n_ch = audio.audiotools._duration_is_signal(sig)
        assert duration == 1
        assert fs == 2
        assert n_ch == (2, 3)

        #Numpy array as input
        sig = np.zeros((11, 2, 3))
        duration, fs, n_ch = audio.audiotools._duration_is_signal(sig, 3)
        assert duration == 11 / 3
        assert fs == 3
        assert n_ch == (2, 3)
Exemplo n.º 11
0
    def test_gauss_fade_window(self):
        window = audio.gaussian_fade_window(np.zeros(1000), 100e-3, 1e3)
        n_window = 100

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

        #test starts at -60dB
        testing.assert_almost_equal(window[0], 0.001)

        #test setting cutoff
        window = audio.gaussian_fade_window(np.zeros(1000), 100e-3, 1e3, cutoff=-20)
        testing.assert_almost_equal(window[0], 0.1)

        # Test that the last sample in the window is not equal to 1
        nsamp = audio.nsamples(200e-3, 1e3)
        window = audio.gaussian_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.gaussian_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.gaussian_fade_window(sig, 100e-3)
        assert win.shape == sig.shape
        testing.assert_array_equal (win[:, 1, 0], win[:, 0, 1])
Exemplo n.º 12
0
    def test_highpass(self):
        types = ['brickwall', 'butter']
        f_cut = 300

        for ftype in types:
            sig = audio.Signal(2, 1, 48000).add_noise()
            sig2 = audio.filter.highpass(sig, f_cut, ftype)
            sig.highpass(f_cut, ftype)
            testing.assert_array_equal(sig, sig2)
Exemplo n.º 13
0
    def test_nsamples(self):
        duration = 1
        fs = 10

        assert audio.nsamples(duration, fs) == 10

        # test for directly using signal class
        sig = audio.Signal(1, 1, 10)
        assert audio.nsamples(sig) == 10
Exemplo n.º 14
0
    def test_highpass(self):
        sig = audio.Signal(1, 1, 48000).add_noise()

        sig_out = audio.filter.highpass(sig, 500, 'butter')
        sig_out2 = audio.filter.butterworth(sig, 500, None, 48000)
        testing.assert_array_equal(sig_out, sig_out2)

        sig_out = audio.filter.highpass(sig, 500, 'brickwall')
        sig_out2 = audio.filter.brickwall(sig, 500, None, 48000)
        testing.assert_array_equal(sig_out, sig_out2)
Exemplo n.º 15
0
 def test_bandpass_brickwall(self):
     sig = audio.Signal((2, 2), 1, 48000)
     sig.add_noise().bandpass(500, 100, 'brickwall')
     sig = sig.to_freqdomain()
     testing.assert_array_almost_equal(sig[np.abs(sig.freq) > 550],
                                       0)
     testing.assert_array_almost_equal(sig[np.abs(sig.freq) < 450],
                                       0)
     assert np.all(sig[(np.abs(sig.freq) < 550)
                       & (np.abs(sig.freq) > 450)] != 0)
Exemplo n.º 16
0
    def test_low_noise_noise(self):
        noise = audio.generate_low_noise_noise(1, 500, 200, fs=48000)
        assert noise.shape == (48000,)

        # test directly using signal
        sig = audio.Signal((2, 3), 1, 48000)
        noise = audio.generate_low_noise_noise(sig, 500, 200, n_rep=10)
        assert noise.shape == (48000, 2, 3)
        testing.assert_array_equal(noise[:, 0, :], noise[:, 1, :])
        testing.assert_array_equal(noise[:, :, 0], noise[:, :, 1])
Exemplo n.º 17
0
    def test_writewav_readwav(self):
        """Test invertability of readwav and writewav"""
        fs = 41200
        signal = audio.Signal(2, 2, fs)
        signal[:] = np.linspace(-1, 1, signal.n_samples)[:, None]

        bitdepth = [8, 16]
        for bd in bitdepth:
            wav.writewav('test.wav', signal, signal.fs, bd)
            out8, fs = wav.readwav('test.wav')
            testing.assert_allclose(out8, signal, atol=2 / 2**bd, rtol=1)
Exemplo n.º 18
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.º 19
0
    def test_analytical(self):
        sig = audio.Signal(2, 1, 48000).add_noise()
        fsig = sig.to_freqdomain()
        fsig.to_analytical()
        # All but the niquist bin should be zero
        testing.assert_array_equal(fsig[fsig.freq < 0][1:], 0)
        asig = fsig.to_timedomain()
        testing.assert_almost_equal(asig.real, sig)

        sig = audio.Signal(1, 0.1, 48000).add_tone(500)
        sig2 = audio.Signal(1, 0.1, 48000).add_tone(500,
                                                    start_phase=-np.pi / 2)
        fsig = sig.to_freqdomain()
        fsig.to_analytical()
        asig = fsig.to_timedomain()
        testing.assert_almost_equal(asig.imag, sig2)
        testing.assert_almost_equal(asig.real, sig)

        sig = audio.Signal((2, 2), 1, 48000).add_noise()
        fsig = sig.to_freqdomain()
        fsig.to_analytical()
        asig = fsig.to_timedomain()
        testing.assert_almost_equal(asig.real, sig)
Exemplo n.º 20
0
    def test_phaseshift(self):
        #phase shifting a full period
        sig = audio.Signal(2, 1, 48000).add_tone(100)
        sig[:,
            1] = sig[:,
                     1].to_freqdomain().phase_shift(2 * np.pi).to_timedomain()
        testing.assert_almost_equal(sig[:, 1], sig[:, 0])

        # phase shifting half a period
        sig = audio.Signal(2, 1, 48000)
        sig[:, 0].add_tone(100, start_phase=-0.5 * np.pi)
        sig[:, 1].add_tone(100)
        sig[:, 1] = sig[:, 1].to_freqdomain().phase_shift(
            0.5 * np.pi).to_timedomain()
        testing.assert_almost_equal(sig[:, 1], sig[:, 0])

        # phase shifting half a period
        sig = audio.Signal((2, 2), 1, 48000)
        sig[:, :, 0].add_tone(100, start_phase=-0.5 * np.pi)
        sig[:, :, 1].add_tone(100)
        sig[:, :, 1] = sig[:, :, 1].to_freqdomain().phase_shift(
            0.5 * np.pi).to_timedomain()
        testing.assert_almost_equal(sig[:, :, 1], sig[:, :, 0])
Exemplo n.º 21
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.º 22
0
 def test_brickwall(self):
     # Test the gain at fc
     sig = audio.Signal(1, 1, 48000)
     fc = np.round(audio.freqarange(100, 4000, 1, 'erb'))
     bw = audio.calc_bandwidth(fc, 'erb')
     # add a tone at every fc
     for f in fc:
         sig.add_tone(f)
     # create filterbank an run signal
     bank = create_filterbank(fc, bw, 'brickwall', 48000)
     sig_out = bank.filt(sig)
     #check amplitudes at fc of every filter
     amps = np.zeros(len(fc))
     for i_fc, f in enumerate(fc):
         spec = sig_out.ch[i_fc].to_freqdomain()
         amps[i_fc] = np.abs(spec[spec.freq==f])
     # Amplitudes hould be 0.5 (double sided spectrum)
     assert np.all((amps - 0.5) <= 0.01)
Exemplo n.º 23
0
    def test_generate_noise(self):
        duration = 1
        fs = 100e3

        noise = audio.generate_noise(duration, fs)
        assert len(noise) == audio.nsamples(duration, fs)
        assert np.ndim(noise) == 1
        # Test for whole spectrum
        spec = np.fft.fft(noise)
        assert np.all(~np.isclose(np.abs(spec)[1:], 0))
        testing.assert_almost_equal(np.abs(spec[0]), 0)
        testing.assert_almost_equal(np.var(noise), 1)

        # # Test no offset
        testing.assert_almost_equal(noise.mean(), 0)
        # test seed
        noise1 = audio.generate_noise(duration, fs, seed=1)
        noise2 = audio.generate_noise(duration, fs, seed=1)
        noise3 = audio.generate_noise(duration, fs, seed=2)
        testing.assert_equal(noise1, noise2)
        assert ~np.all(noise1 == noise3)

        # test directly handing over signal
        sig = audio.Signal((2, 3), 1, 10)
        noise = audio.generate_noise(sig)
        assert noise.shape == (10, 2, 3)
        testing.assert_array_equal(noise[:, 0, :], noise[:, 1, :])
        testing.assert_array_equal(noise[:, :, 0], noise[:, :, 1])


        # test directly handing over signal
        noise = audio.generate_noise(1, 10, n_channels=(2, 3))
        assert noise.shape == (10, 2, 3)
        testing.assert_array_equal(noise[:, 0, :], noise[:, 1, :])
        testing.assert_array_equal(noise[:, :, 0], noise[:, :, 1])

        # test multichannel
        noise = audio.generate_noise(1, 10, n_channels=(2, 3), ntype='pink')
        assert noise.shape == (10, 2, 3)
        testing.assert_array_equal(noise[:, 0, :], noise[:, 1, :])
        testing.assert_array_equal(noise[:, :, 0], noise[:, :, 1])
Exemplo n.º 24
0
    def test_writewav(self):
        """Test invertability of readwav and writewav"""
        fs = 41200
        signal = audio.Signal(2, 2, fs)
        signal[:] = np.linspace(-1, 1, signal.n_samples)[:, None]

        bitdepth = [8, 16, 32]
        for bd in bitdepth:
            wav.writewav('test.wav', signal, signal.fs, bd)

        self.assertRaises(ValueError,
                          wav.writewav,
                          filename='test.wav',
                          signal=signal,
                          fs=signal.fs,
                          bitdepth=7)

        self.assertRaises(ValueError,
                          wav.writewav,
                          filename='test.wav',
                          signal=signal,
                          fs=signal.fs,
                          bitdepth=64)
Exemplo n.º 25
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.º 26
0
    def to_timedomain(self):
        """Convert to timedomain.

        Convert to timedomain by means of inverse DFT. If the complex
        part after DFT is small (< 222e-16), it is neglected. This
        method is not applied in-place but a new
        :meth:'audiotools.Signal' object is returned

        Returns:
        --------
        The timedomain representation: Signal

        """

        # revert normalization
        self *= self.n_samples
        wv = np.fft.ifft(self, axis=0)
        wv = np.real_if_close(wv)
        signal = audio.Signal(self.n_channels,
                              self.duration,
                              self.fs,
                              dtype=wv.dtype)
        signal[:] = wv
        return signal
Exemplo n.º 27
0
    def test_butterworth_filt(self):
        # Test Lowpass
        sig = audio.Signal(10, 1, 48000).add_tone(500)
        sig_out = filter.butterworth(sig, None, 500, 48000)
        testing.assert_almost_equal(sig_out[:].std(), 0.5, 3)

        sig = audio.Signal(1, 1, 48000).add_tone(400)
        sig_out = filter.butterworth(sig, None, 500, 48000)
        assert sig_out[:].std() > 0.5

        sig = audio.Signal(1, 1, 48000).add_tone(600)
        sig_out = filter.butterworth(sig, None, 500, 48000)
        assert sig_out[:].std() < 0.5

        # test Highpass
        sig = audio.Signal(1, 1, 48000).add_tone(500)
        sig_out = filter.butterworth(sig, 500, None, 48000)
        testing.assert_almost_equal(sig_out[:].std(), 0.5, 3)

        sig = audio.Signal(1, 1, 48000).add_tone(400)
        sig_out = filter.butterworth(sig, 500, None, 48000)
        assert sig_out[:].std() < 0.5

        sig = audio.Signal(1, 1, 48000).add_tone(600)
        sig_out = filter.butterworth(sig, 500, None, 48000)
        assert sig_out[:].std() > 0.5

        sig = audio.Signal(1, 1, 48000).add_tone(500)
        sig_out = filter.butterworth(sig, 400, 600, 48000)
        testing.assert_almost_equal(sig_out[:].std(), 1 / np.sqrt(2), 3)
        sig = audio.Signal(1, 1, 48000).add_tone(400)
        sig_out = filter.butterworth(sig, 400, 600, 48000)
        testing.assert_almost_equal(sig_out[:].std(), 0.5, 3)
        sig = audio.Signal(1, 1, 48000).add_tone(600)
        sig_out = filter.butterworth(sig, 400, 600, 48000)
        testing.assert_almost_equal(sig_out[:].std(), 0.5, 3)

        sig = audio.Signal((2, 3), 1, 48000).add_tone(500)
        sig_out = filter.butterworth(sig, 400, 600, 48000)
        testing.assert_almost_equal(np.std(sig_out, axis=0), 1 / np.sqrt(2), 3)
        sig = audio.Signal((2, 3), 1, 48000).add_tone(400)
        sig_out = filter.butterworth(sig, 400, 600, 48000)
        testing.assert_almost_equal(np.std(sig_out, axis=0), 0.5, 3)
Exemplo n.º 28
0
    def test_bandpass_butterworth(self):
        sig = audio.Signal(1, 1, 48000).add_noise()

        sig2 = audio.filter.butterworth(sig, 100, 300)
        sig = sig.bandpass(200, 200, 'butter')
        testing.assert_array_equal(sig, sig2)
Exemplo n.º 29
0
 def test_analytical(self):
     sig = audio.Signal((2, 2), 1, 48000).add_noise()
     asig = sig.to_analytical()
     testing.assert_almost_equal(sig, asig.real)
Exemplo n.º 30
0
 def test_calc_dbspl(self):
     assert audio.calc_dbspl(2e-3) == 40
     assert audio.calc_dbspl(20e-6) == 0
     sig = audio.Signal(1, 1, 48000).add_tone(500)
     l_tone = 20*np.log10(np.sqrt(0.5) / 20e-6)
     assert audio.calc_dbspl(sig) == l_tone