def test_stft(): "Test stft and istft tight frame property" sfreq = 1000. # Hz f = 7. # Hz for T in [253, 256]: # try with even and odd numbers t = np.arange(T).astype(np.float) x = np.sin(2 * np.pi * f * t / sfreq) x = np.array([x, x + 1.]) wsize = 128 tstep = 4 X = stft(x, wsize, tstep) xp = istft(X, tstep, Tx=T) freqs = stftfreq(wsize, sfreq=1000) max_freq = freqs[np.argmax(np.sum(np.abs(X[0])**2, axis=1))] assert_true(X.shape[1] == len(freqs)) assert_true(np.all(freqs >= 0.)) assert_true(np.abs(max_freq - f) < 1.) assert_array_almost_equal(x, xp, decimal=6) # norm conservation thanks to tight frame property assert_almost_equal(np.sqrt(stft_norm2(X)), map(linalg.norm, x), decimal=2) # Try with empty array x = np.zeros((0, T)) X = stft(x, wsize, tstep) xp = istft(X, tstep, T) assert_true(xp.shape == x.shape)
def test_stft(): "Test stft and istft tight frame property" sfreq = 1000. # Hz f = 7. # Hz for T in [253, 256]: # try with even and odd numbers t = np.arange(T).astype(np.float) x = np.sin(2 * np.pi * f * t / sfreq) x = np.array([x, x + 1.]) wsize = 128 tstep = 4 X = stft(x, wsize, tstep) xp = istft(X, tstep, Tx=T) freqs = stftfreq(wsize, sfreq=1000) max_freq = freqs[np.argmax(np.sum(np.abs(X[0]) ** 2, axis=1))] assert_true(X.shape[1] == len(freqs)) assert_true(np.all(freqs >= 0.)) assert_true(np.abs(max_freq - f) < 1.) assert_array_almost_equal(x, xp, decimal=6) # norm conservation thanks to tight frame property assert_almost_equal(np.sqrt(stft_norm2(X)), [linalg.norm(xx) for xx in x], decimal=2) # Try with empty array x = np.zeros((0, T)) X = stft(x, wsize, tstep) xp = istft(X, tstep, T) assert_true(xp.shape == x.shape)
def test_stft(): """Test stft and istft tight frame property.""" sfreq = 1000. # Hz f = 7. # Hz for T in [127, 128]: # try with even and odd numbers # Test with low frequency signal t = np.arange(T).astype(np.float) x = np.sin(2 * np.pi * f * t / sfreq) x = np.array([x, x + 1.]) wsize = 128 tstep = 4 X = stft(x, wsize, tstep) xp = istft(X, tstep, Tx=T) freqs = stftfreq(wsize, sfreq=1000) max_freq = freqs[np.argmax(np.sum(np.abs(X[0])**2, axis=1))] assert X.shape[1] == len(freqs) assert np.all(freqs >= 0.) assert np.abs(max_freq - f) < 1. assert_array_almost_equal(x, xp, decimal=6) # norm conservation thanks to tight frame property assert_almost_equal(np.sqrt(stft_norm2(X)), [linalg.norm(xx) for xx in x], decimal=6) # Test with random signal x = np.random.randn(2, T) wsize = 16 tstep = 8 X = stft(x, wsize, tstep) xp = istft(X, tstep, Tx=T) freqs = stftfreq(wsize, sfreq=1000) max_freq = freqs[np.argmax(np.sum(np.abs(X[0])**2, axis=1))] assert X.shape[1] == len(freqs) assert np.all(freqs >= 0.) assert_array_almost_equal(x, xp, decimal=6) # norm conservation thanks to tight frame property assert_almost_equal(np.sqrt(stft_norm2(X)), [linalg.norm(xx) for xx in x], decimal=6) # Try with empty array x = np.zeros((0, T)) X = stft(x, wsize, tstep) xp = istft(X, tstep, T) assert xp.shape == x.shape
# test if phi is still linear, it does not seems neumerically linear? A = np.random.randn(2,2) X1_sparse_vec1 = sparse_phi(A.dot(x),active_t_ind) X1_sparse_vec2 = A.dot(sparse_phi(x,active_t_ind)) print np.linalg.norm(X1_sparse_vec1-X1_sparse_vec2) \ /np.linalg.norm(X1_sparse_vec1) #=========== Note, a potential caveate of the mne's sparse representation===== # even if the stft coefs Z are sparse in time, after phiH and phi, it would be # sparse, the non-active set will have very small values near zero. # this may be why my algorithms are very slow, # and the L2 version of it does not work well # ================ test istft================================================== x0 = istft(X0, tstep = tstep) x1 = sparse_istft(X1_sparse,tstep, active_t_ind) x1_phi = sparse_phiT(X1_sparse_vec,active_t_ind) plt.figure() plt.plot(x0.T,'r') plt.plot(x1.T,'g') plt.plot(x.T,'b') plt.plot(x1_phi.T,'m') plt.show() x11 = sparse_phiT(A.dot(X1_sparse_vec), active_t_ind) x12 = A.dot(sparse_phiT(X1_sparse_vec, active_t_ind)) print np.linalg.norm(x11-x12) \ /np.linalg.norm(x12)