Exemple #1
0
    def test_sine_extraction_scale(self):

        frame_size = 1024
        hop_size = 512
        bin_freq = 44100. / float(frame_size)
        num_frames = 10
        batch_size = 10
        spectral = SpectralSummarized()
        features = np.ndarray((
            10,
            22,
        ))

        for i in range(batch_size):
            sine = utils.make_test_cosine(22050, bin_freq * (i + 1), 44100)
            audio = AudioBuffer(sine, 44100)
            features[i] = spectral(audio)

        assert features.shape == (10, 22)
        scaled = spectral.fit_scaler(features)

        assert scaled.mean() == pytest.approx(0.)
        assert scaled.std() == pytest.approx(1.)
        np.testing.assert_array_almost_equal(scaled.mean(0), np.zeros(22))
        np.testing.assert_array_almost_equal(scaled.std(0), np.ones(22))
Exemple #2
0
    def test_batch_scale_magnitude(self):

        bin_freq = 44100. / 1024.
        feature_batch = np.zeros((10, 513))
        fft = FFT(output='magnitude', scale_axis=None)

        for i in range(10):
            bin = (i + 1) * 2
            sine = utils.make_test_cosine(1024, bin_freq * bin, 44100)
            audio = AudioBuffer(sine, 44100)
            feature_batch[i] = fft(audio)

        scaled = fft.fit_scaler(feature_batch)
        expected_mean = (512.0 * 10) / (513.0 * 10)

        assert fft.scaler.mean == pytest.approx(expected_mean)
Exemple #3
0
    def test_sine_wave_stft_power(self):

        bin_freq = 44100. / 1024.
        sine = utils.make_test_cosine(1024 + 1, bin_freq * 10, 44100)
        audio = AudioBuffer(sine, 44100)
        stft = STFT(output='power', fft_size=1024)
        features = stft(audio)
        expected = np.zeros((513, 3))

        # amplitude is spread out over neighbouring bins
        expected[9, :] = 128. * 128.
        expected[10, :] = 256. * 256.
        expected[11, :] = 128. * 128.

        assert features.shape == (513, 3)
        assert isinstance(features[0][0], np.float32)
        np.testing.assert_array_almost_equal(features, expected)
Exemple #4
0
    def test_sine_wave_fft_power_phase(self):

        bin_freq = 44100. / 1024.

        # Using a cosine here to get zero initial phase
        sine = utils.make_test_cosine(1024, bin_freq * 10, 44100)
        audio = AudioBuffer(sine, 44100)
        fft = FFT(output='power_phase')
        features = fft(audio)
        expected = np.zeros((513, 2))
        expected[10][0] = 512. * 512.

        assert features.shape == (513, 2)
        assert isinstance(features[0][0], np.float32)
        assert isinstance(features[0][1], np.float32)

        # Assert correct magnitude
        np.testing.assert_array_almost_equal(features[:, 0], expected[:, 0])

        # Uncertain about phase, except for 10th bin
        assert features[10][1] == pytest.approx(0.)
Exemple #5
0
    def test_sine_wave_stft_magnitude_scaled(self):

        bin_freq = 44100. / 1024.
        length = (1024. * 10) + 1
        stft = STFT(output='magnitude',
                    fft_size=1024,
                    hop_size=512,
                    time_major=True,
                    scale_axis=None)
        batch_size = 10
        features = np.zeros((10, 21, 513))

        for i in range(batch_size):
            sine = utils.make_test_cosine(int(length), bin_freq * (i + 10),
                                          44100)
            audio = AudioBuffer(sine, 44100)
            features[i] = stft(audio)

        scaled = stft.fit_scaler(features)
        assert scaled.shape == (10, 21, 513)
        assert scaled.mean() == pytest.approx(0.)
        assert scaled.std() == pytest.approx(1.)
Exemple #6
0
    def test_sine_extraction(self):

        frame_size = 1024
        hop_size = 512
        bin_freq = 44100. / float(frame_size)

        sine = utils.make_test_cosine(1024 + 1, bin_freq * 10, 44100)
        audio = AudioBuffer(sine, 44100)
        spectral = SpectralSummarized(frame_size=1024, hop_size=512)
        features = spectral(audio)

        assert features.shape == (22, )
        assert features[0] == pytest.approx(bin_freq * 10)
        assert features[1] == pytest.approx(0.)
        assert features[2] == pytest.approx(30.452547928239625)
        assert features[3] == pytest.approx(0.)
        assert features[4] == pytest.approx(0.)
        assert features[5] == pytest.approx(0.)
        assert features[6] == pytest.approx(bin_freq * 11)
        assert features[7] == pytest.approx(0.)

        # Spectral flatness subbands
        assert features[8] == pytest.approx(44.0823996531185)
        assert features[9] == pytest.approx(0.)
        assert features[10] == pytest.approx(44.0823996531185)
        assert features[11] == pytest.approx(0.)
        assert features[12] == pytest.approx(124.0823996531185)
        assert features[13] == pytest.approx(0.)
        assert features[14] == pytest.approx(44.0823996531185)
        assert features[15] == pytest.approx(0.)
        assert features[16] == pytest.approx(44.0823996531185)
        assert features[17] == pytest.approx(0.)
        assert features[18] == pytest.approx(44.0823996531185)
        assert features[19] == pytest.approx(0.)
        assert features[20] == pytest.approx(44.0823996531185)
        assert features[21] == pytest.approx(0.)