def test_fft_size(self): result = stft(sig_2d, window=None, fft_size=25) self.assertTrue(result.shape == (3, 12)) result = stft(sig_2d, window=None, fft_size=25, include_nyquist=True) self.assertTrue(result.shape == (3, 13)) # test only the first req bin res = [3. + 0.j, 4. + 0.j, 6. + 0.j] self.assertTrue(np.allclose(result[:, 0], res))
def test_fft_size(self): result = stft(sig_2d, window=None, fft_size=25) self.assertTrue(result.shape == (3, 12)) result = stft(sig_2d, window=None, fft_size=25, include_nyquist=True) print(result[:, 0]) self.assertTrue(result.shape == (3, 13)) # test only the first req bin res = [3. + 0.j, 4. + 0.j, 6. + 0.j] self.assertTrue(np.allclose(result[:, 0], res))
def test_block_processing(self): result = stft(sig_2d, window=None, fft_size=25) self.assertTrue(result.dtype == np.complex64) self.assertTrue(result.shape == (3, 12)) self.assertTrue(np.allclose(result[:, 0], [3, 4, 6])) result_1 = stft(sig_2d, window=None, fft_size=25, block_size=None) self.assertTrue(np.allclose(result, result_1)) result_2 = stft(sig_2d, window=None, fft_size=25, block_size=2) self.assertTrue(np.allclose(result, result_2))
def test_filterbank(self): fb = np.diag(np.full(12, 1)) result = stft(sig_2d, window=None, fft_size=25, filterbank=fb) self.assertTrue(result.dtype == np.float32) self.assertTrue(result.shape == (3, 12)) self.assertTrue(np.allclose(result[:, 0], [3, 4, 6])) # smaller filterbank result = stft(sig_2d, window=None, fft_size=25, filterbank=fb[:, :6]) self.assertTrue(result.dtype == np.float32) self.assertTrue(result.shape == (3, 6)) self.assertTrue(np.allclose(result[:, 0], [3, 4, 6]))
def test_circular_shift(self): result = stft(sig_2d, window=None, circular_shift=True) # signal length and FFT size = 12 # fft_freqs: 0, 1/12, 2/12, 3/12, 4/12, 5/12 # [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0] every 4th bin => 3/12 res = [3. + 0.j, 0. + 0.j, 0. + 0j, -3. + 0.j, 0. + 0.j, 0. + 0.j] self.assertTrue(np.allclose(result[0], res)) # [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0] every erd bin => 4/12 res = [4. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 4. + 0.j, 0. + 0.j] self.assertTrue(np.allclose(result[1], res)) # [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0] every 2nd bin => 6/12 # can't resolve any more res = [6. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j] self.assertTrue(np.allclose(result[2], res))
def test_dimensionality(self): # input must be 2D with self.assertRaises(ValueError): stft(np.arange(10), window=None) # like this: result = stft(np.arange(10).reshape(5, 2), window=None) self.assertEqual(result.shape, (5, 1)) # window size must match frame size with self.assertRaises(ValueError): stft(np.arange(10).reshape(5, 2), window=[1, 2, 3]) # fft size must be greater or equal frame size with self.assertRaises(ValueError): stft(np.arange(10).reshape(5, 2), window=None, fft_size=1)
def test_types(self): result = stft(np.arange(10).reshape(5, 2), window=None) self.assertIsInstance(result, np.ndarray) self.assertEqual(result.dtype, np.complex64)
def test_nyquist(self): result = stft(sig_2d, window=None, include_nyquist=True) self.assertTrue(result.shape == (3, 7)) # test only the last req bin res = [3. + 0.j, 0. + 0.j, 6. + 0.j] self.assertTrue(np.allclose(result[:, -1], res))
def test_window_size(self): # window size must match frame size with self.assertRaises(ValueError): stft(np.arange(10).reshape(5, 2), window=[1, 2, 3])
def test_complex(self): result = stft(sig_2d, window=None, fft_size=25, complex=False) self.assertTrue(result.dtype == np.float32) self.assertTrue(result.shape == (3, 12)) self.assertTrue(np.allclose(result[:, 0], [3, 4, 6]))