Пример #1
0
    def test_restore_time_signal_with_str_window(self):
        x = self.x
        X = stft(x, window='hann')

        tc.assert_almost_equal(
            x, istft(X, 1024, 256, window='hann', num_samples=len(x)))
        tc.assert_equal(X.shape, (154, 513))
Пример #2
0
    def test_preemphasis(self):
        y = self.x
        yFilterd = transform.preemphasis(y, 0.97)

        tc.assert_equal(yFilterd.shape, y.shape)
        tc.assert_isreal(yFilterd)
        tc.assert_equal(yFilterd[0], y[0])
Пример #3
0
    def test_restore_time_signal_from_stft_and_istft(self):
        x = self.time_signal
        X = self.stft(self.torch_signal)

        tc.assert_almost_equal(
            self.stft.inverse(X)[..., :x.shape[-1]].numpy(), x)
        tc.assert_equal(X.shape, (154, self.num_features))
Пример #4
0
    def test_offcomp(self):
        y = self.x
        yFilterd = transform.offset_compensation(y)

        tc.assert_equal(yFilterd.shape, y.shape)
        tc.assert_isreal(yFilterd)
        tc.assert_array_not_equal(yFilterd[1:], y[1:])
        tc.assert_equal(yFilterd[0], y[0])
Пример #5
0
    def test_mfcc(self):
        path = get_file_path("sample.wav")

        y = audioread(path)[0]
        y_filtered = transform.mfcc(y)

        tc.assert_equal(y_filtered.shape, (291, 13))
        tc.assert_isreal(y_filtered)
Пример #6
0
    def test_mfcc(self):
        path = get_file_path("sample.wav")

        y = audioread(path)[0]
        yFilterd = transform.ssc(y)

        tc.assert_equal(yFilterd.shape, (294, 26))
        tc.assert_isreal(yFilterd)
Пример #7
0
    def test_window_length(self):
        X = stft(self.x, 512, 160, window_length=400)
        x_hat = istft(X, 512, 160, window_length=400)

        X_ref = istft(stft(self.x, 400, 160), 400, 160)
        tc.assert_equal(X.shape, (243, 257))

        tc.assert_allclose(X_ref, x_hat, rtol=1e-6, atol=1e-6)
Пример #8
0
 def test_curated_input_with_keepdims_true(self):
     labels = np.asarray([[0, 1, 2, 0]])
     dtype = np.float32
     expected_mask = np.asarray([[1, 0, 0, 1], [0, 1, 0, 0], [0, 0, 1, 0],
                                 [0, 0, 0, 0], [0, 0, 0, 0]],
                                dtype=dtype)
     tc.assert_equal(labels_to_one_hot(labels, categories=5, keepdims=True),
                     expected_mask)
Пример #9
0
    def test_samples_to_stft_frames(self):
        size = 1024
        shift = 256

        tc.assert_equal(_samples_to_stft_frames(1023, size, shift), 1)
        tc.assert_equal(_samples_to_stft_frames(1024, size, shift), 1)
        tc.assert_equal(_samples_to_stft_frames(1025, size, shift), 2)
        tc.assert_equal(_samples_to_stft_frames(1024 + 256, size, shift), 2)
        tc.assert_equal(_samples_to_stft_frames(1024 + 257, size, shift), 3)
Пример #10
0
    def test_fbank(self):
        path = get_file_path("sample.wav")

        y = audioread(path)[0]
        feature = transform.fbank(y)

        tc.assert_equal(feature.shape, (240, 23))
        tc.assert_isreal(feature)
        tc.assert_array_greater_equal(feature, 0)
Пример #11
0
    def test_dump_load(self, name, data, expect):
        with tempfile.TemporaryDirectory() as temp_dir:
            dump_hdf5(data, os.path.join(temp_dir, 'test.hdf5'))
            data_load = load_hdf5(os.path.join(temp_dir, 'test.hdf5'))

        assert 'key' in data_load.keys(), data_load

        assert type(expect) is type(data_load['key']), \
            (type(expect), type(data_load['key']), expect, data_load['key'])
        tc.assert_equal(expect, data_load['key'])
Пример #12
0
    def test_compare_both_biorthogonal_window_variants(self):
        window = signal.blackman(1024)
        shift = 256

        for_result = _biorthogonal_window_loopy(window, shift)
        vec_result = _biorthogonal_window(window, shift)
        brute_force_result = _biorthogonal_window_brute_force(window, shift)

        tc.assert_equal(for_result, vec_result)
        tc.assert_allclose(for_result, brute_force_result)
        tc.assert_equal(for_result.shape, (1024, ))
Пример #13
0
    def test_cosine_similarity(self):
        phi = 60 / 360 * 2 * np.pi

        W1 = np.array([4, 0, 0])
        W2 = np.array([1, 0, 0])
        W3 = np.array([0, 1, 0])
        W4 = np.array([1, 1j, 0])
        W5 = np.array([1, -1j, 0])
        W6 = np.array([np.cos(phi), np.sin(phi), 0])

        tc.assert_equal(cos_similarity(W1, W1), 1.0)
        tc.assert_equal(cos_similarity(W1, W2), 1.0)
        tc.assert_equal(cos_similarity(W1, W3), 0.0)
        tc.assert_equal(cos_similarity(W4, W5), 0.0)
        tc.assert_equal(cos_similarity(W1, W6), np.cos(phi))
Пример #14
0
    def test_spectrogram_and_energy(self):
        x = self.x
        X = stft(x)
        spectrogram = stft_to_spectrogram(X)
        energy = spectrogram_to_energy_per_frame(spectrogram)

        tc.assert_equal(X.shape, (154, 513))

        tc.assert_equal(spectrogram.shape, (154, 513))
        tc.assert_isreal(spectrogram)
        tc.assert_array_greater_equal(spectrogram, 0)

        tc.assert_equal(energy.shape, (154, ))
        tc.assert_isreal(energy)
        tc.assert_array_greater_equal(energy, 0)
Пример #15
0
    def test_stft_frame_count(self):
        stft = self.stft
        stft.fading = False
        x = torch.rand(size=[1019])
        X = stft(x)
        tc.assert_equal(X.shape, (50, self.fbins * 2))

        x = torch.rand(size=[1020])
        X = stft(x)
        tc.assert_equal(X.shape, (50, self.fbins * 2))

        x = torch.rand(size=[1021])
        X = stft(x)
        tc.assert_equal(X.shape, (51, self.fbins * 2))

        stft.fading = True
        x = torch.rand(size=[1019])
        X = stft(x)
        tc.assert_equal(X.shape, (52, self.fbins * 2))

        x = torch.rand(size=[1020])
        X = stft(x)
        tc.assert_equal(X.shape, (52, self.fbins * 2))

        x = torch.rand(size=[1021])
        X = stft(x)
        tc.assert_equal(X.shape, (53, self.fbins * 2))
Пример #16
0
 def test_uniform_scalar(self):
     scalar = random.uniform()
     tc.assert_equal(scalar.shape, ())
Пример #17
0
 def test_hermitian_many_dimensions(self):
     shape = (3, 4, 5, 2, 2)
     matrix = random.hermitian(*shape)
     tc.assert_equal(matrix.shape, shape)
     tc.assert_equal(matrix, np.swapaxes(matrix, -1, -2).conj())
Пример #18
0
 def test_hermitian_2D(self):
     shape = (2, 2)
     matrix = random.hermitian(*shape)
     tc.assert_equal(matrix.shape, shape)
     tc.assert_equal(matrix, np.swapaxes(matrix, -1, -2).conj())
Пример #19
0
    def test_restore_time_signal_from_stft_and_istft(self):
        x = self.x
        X = stft(x)

        tc.assert_almost_equal(x, istft(X, 1024, 256)[:len(x)])
        tc.assert_equal(X.shape, (154, 513))
Пример #20
0
    def test_stft_frames_to_samples(self):
        size = 1024
        shift = 256

        tc.assert_equal(_stft_frames_to_samples(1, size, shift), 1024)
        tc.assert_equal(_stft_frames_to_samples(2, size, shift), 1024 + 256)
Пример #21
0
 def test_swap_and_reshape(self):
     result = morph('T,B,F->T,F*B', A)
     tc.assert_equal(result.shape, (T, F * B))
     tc.assert_equal(result, A.swapaxes(-1, -2).reshape(T, F * B))
Пример #22
0
 def test_transpose_and_reshape(self):
     result = morph('T,B,F->F,B*T', A)
     tc.assert_equal(result.shape, (F, B * T))
     tc.assert_equal(result, A.transpose(2, 1, 0).reshape(F, B * T))
Пример #23
0
 def test_reshape_and_broadcast_many(self):
     result = morph('T,B,F->1,T,1,B*F,1', A)
     tc.assert_equal(result.shape, (1, T, 1, B * F, 1))
Пример #24
0
    def test_stft_frame_count(self):
        stft = self.stft
        stft.fading = False
        x = torch.rand(size=[1023])
        X = stft(x)
        tc.assert_equal(X.shape, (1, self.num_features))

        x = torch.rand(size=[1024])
        X = stft(x)
        tc.assert_equal(X.shape, (1, self.num_features))

        x = torch.rand(size=[1025])
        X = stft(x)
        tc.assert_equal(X.shape, (2, self.num_features))

        stft.fading = True
        x = torch.rand(size=[1023])
        X = stft(x)
        tc.assert_equal(X.shape, (7, self.num_features))

        x = torch.rand(size=[1024])
        X = stft(x)
        tc.assert_equal(X.shape, (7, self.num_features))

        x = torch.rand(size=[1025])
        X = stft(x)
        tc.assert_equal(X.shape, (8, self.num_features))
Пример #25
0
    def test_stft_frame_count(self):

        stft_params = dict(size=1024, shift=256, fading=False)

        x = np.random.normal(size=[1023])
        X = stft(x, **stft_params)
        tc.assert_equal(X.shape, (1, 513))

        x = np.random.normal(size=[1024])
        X = stft(x, **stft_params)
        tc.assert_equal(X.shape, (1, 513))

        x = np.random.normal(size=[1025])
        X = stft(x, **stft_params)
        tc.assert_equal(X.shape, (2, 513))

        stft_params = dict(size=1024, shift=256, fading=True)

        x = np.random.normal(size=[1023])
        X = stft(x, **stft_params)
        tc.assert_equal(X.shape, (7, 513))

        x = np.random.normal(size=[1024])
        X = stft(x, **stft_params)
        tc.assert_equal(X.shape, (7, 513))

        x = np.random.normal(size=[1025])
        X = stft(x, **stft_params)
        tc.assert_equal(X.shape, (8, 513))

        stft_params = dict(size=512,
                           shift=160,
                           window_length=400,
                           fading=False)

        x = np.random.normal(size=[399])
        X = stft(x, **stft_params)
        tc.assert_equal(X.shape, (1, 257))

        x = np.random.normal(size=[400])
        X = stft(x, **stft_params)
        tc.assert_equal(X.shape, (1, 257))

        x = np.random.normal(size=[401])
        X = stft(x, **stft_params)
        tc.assert_equal(X.shape, (2, 257))

        x = np.random.normal(size=[559])
        X = stft(x, **stft_params)
        tc.assert_equal(X.shape, (2, 257))

        x = np.random.normal(size=[560])
        X = stft(x, **stft_params)
        tc.assert_equal(X.shape, (2, 257))

        x = np.random.normal(size=[561])
        X = stft(x, **stft_params)
        tc.assert_equal(X.shape, (3, 257))
Пример #26
0
 def test_transpose_capital(self):
     result = morph('tbB->tBb', A)
     tc.assert_equal(result.shape, (T, F, B))
     tc.assert_equal(result, A.transpose(0, 2, 1))
Пример #27
0
    def test_restore_time_signal_from_stft_and_istft_kaldi_params(self):
        x = self.x
        X = stft(x, size=400, shift=160)

        tc.assert_almost_equal(x, istft(X, 400, 160)[:len(x)])
        tc.assert_equal(X.shape, (243, 201))
Пример #28
0
    def test_restore_time_signal_from_stft_and_istft_with_num_samples(self):
        x = self.x
        X = stft(x)

        tc.assert_almost_equal(x, istft(X, 1024, 256, num_samples=len(x)))
        tc.assert_equal(X.shape, (154, 513))
Пример #29
0
    def test_batch_mode(self):
        size = 1024
        shift = 256

        # Reference
        X = stft_single_channel(self.x)

        x1 = np.array([self.x, self.x])
        X1 = stft(x1)
        tc.assert_equal(X1.shape, (2, 154, 513))

        for d in np.ndindex(2):
            tc.assert_equal(X1[d, :, :].squeeze(), X)

        x11 = np.array([x1, x1])
        X11 = stft(x11)
        tc.assert_equal(X11.shape, (2, 2, 154, 513))
        for d, k in np.ndindex(2, 2):
            tc.assert_equal(X11[d, k, :, :].squeeze(), X)

        x2 = x1.transpose()
        X2 = stft(x2, axis=0)
        tc.assert_equal(X2.shape, (154, 513, 2))
        for d in np.ndindex(2):
            tc.assert_equal(X2[:, :, d].squeeze(), X)

        x21 = np.array([x2, x2])
        X21 = stft(x21, axis=1)
        tc.assert_equal(X21.shape, (2, 154, 513, 2))
        for d, k in np.ndindex(2, 2):
            tc.assert_equal(X21[d, :, :, k].squeeze(), X)

        x22 = x21.swapaxes(0, 1)
        X22 = stft(x22, axis=0)
        tc.assert_equal(X22.shape, (154, 513, 2, 2))
        for d, k in np.ndindex(2, 2):
            tc.assert_equal(X22[:, :, d, k].squeeze(), X)
Пример #30
0
 def test_reshape_and_broadcast(self):
     tc.assert_equal(morph('T,B,F->T,1,B*F', A).shape, (T, 1, B * F))
     tc.assert_equal(morph('T,B,F->T,1,B*F', A).ravel(), A.ravel())