示例#1
0
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)
示例#2
0
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
示例#4
0
    def fit(self, data):
        """ Fit data

        Parameters
        ---------
        data : ndarray, shape (channels, times)

        """

        print "First do stft"
        stft_ = stft(data, self.wsize, self.tstep)

        # bandpass filter
        if self.sfreq:
            freqs = stftfreq(self.wsize, self.sfreq)

            hpass, lpass = 0, len(freqs)
            if self.hpass:
                hpass = min(np.where(freqs >= self.hpass)[0])
            if self.lpass:
                lpass = max(np.where(freqs <= self.lpass)[0])

            self._freqs = freqs[hpass:lpass]

            stft_ = stft_[:, hpass:lpass, :]

        # store shape to retrieve it later
        self._stft_shape = stft_.shape

        # concatenate data 
        data2d = self._concat(stft_)

        print "Whiten data"
        dewhitening, whitened = self._whiten(data2d)
        
        print "Do ICA"
        mixing_, ic_ = self._fastica(whitened)

        # sort according to objective value
        objectives = []
        for i in range(ic_.shape[0]):
            component = ic_[i, :]
            g_ = np.log(1 + np.abs(component)**2)
            objectives.append(np.mean(g_))
        indices = np.argsort(objectives)[::-1]

        sorted_ic = ic_[np.argsort(objectives), :]
        sorted_mixing = mixing_[:, np.argsort(objectives)]

        # store for retrieving
        self._mixing = sorted_mixing
        self._dewhitening = dewhitening
        self._source_stft = self._split(sorted_ic)
stc_data[0][:len(Ws[0])] = np.real(Ws[0])
stc_data[1][:len(Ws[1])] = np.real(Ws[1])
x = stc_data
plt.plot(x.T)
plt.show()
n_times = x.shape[1]

wsize = 16
tstep = 4
n_freq = wsize // 2 + 1
n_step = n_step = int(ceil(n_times / float(tstep)))
sparse_phi = sparse_Phi(wsize, tstep)
sparse_phiT = sparse_PhiT(tstep, n_freq, n_step, n_times)

# =============== test stft====================================================
X0= stft(x, wsize = wsize, tstep = tstep )
X0_norm = np.abs(X0[0,:,:,])
active_t_ind = np.any(X0_norm>1E-3, axis=0) 
X1_sparse = sparse_stft(x,wsize, tstep, active_t_ind)
X1 = np.zeros(X0.shape, dtype =np.complex)
X1[:,:, active_t_ind] = X1_sparse

X1_sparse_vec = sparse_phi(x,active_t_ind)
X1_sparse_vec_mat = np.reshape(X1_sparse_vec,[2,n_freq,-1])
print np.linalg.norm(X1_sparse - X1_sparse_vec_mat)

# order, 3d array, the outer part is the first, the innter part is the last
# C order:
# ‘C’ means to read / write the elements using C-like index order,
# with the last axis index changing fastest,
# back to the first axis index changing slowest.