Exemple #1
0
    def get_csp_filters_multiclass(self, ClassData, n=5, csp_type="RG"):
        C = np.zeros((len(ClassData), ClassData[0].shape[1], ClassData[1].shape[1]))
        sample_weights = list()
        numClasses = len(ClassData)
        numChannels = ClassData[0].shape[1]
        for i in range(numClasses):
            class_ = np.transpose(ClassData[i], [1, 0, 2])
            class_ = class_.reshape(numChannels, -1)
            C[i] = np.cov(class_)
            weight = ClassData[i].shape[1]
            sample_weights.append(weight)

        w, v = _ajd_pham(C)
        mean_cov = np.average(C, axis=0, weights=sample_weights)

        w_, v_ = np.linalg.eig(C[0])
        w = np.asarray(w)
        max_filters = []
        min_filters = []
        if csp_type == "STD":
            w_sort = np.sort(w)
            max_filters = [v[:, np.where(w == k)[0][0]] for k in w_sort[-n:]]
            min_filters = [v[:, np.where(w == k)[0][0]] for k in w_sort[:n]]
        elif csp_type == "RG":
            max_div = []
            min_div = []
            for ind, k in enumerate(w):
                vect = v[:, ind]
                div = np.dot(np.dot(np.transpose(vect), R1), vect) / np.dot(np.dot(np.transpose(vect), R2), vect)
                max_div.append(div)
                min_div.append(div)
                div_idx = len(max_div) - 1
                while max_div[div_idx] > max_div[div_idx - 1] and div_idx > 0:
                    t = max_div[div_idx - 1]
                    max_div[div_idx - 1] = max_div[div_idx]
                    max_div[div_idx] = t
                    div_idx -= 1
                max_filters.insert(div_idx, vect)
                if len(max_div) > n:
                    max_div = max_div[:n]
                    max_filters = max_filters[:n]
                div_idx = len(min_div) - 1
                while min_div[div_idx] < min_div[div_idx - 1] and div_idx > 0:
                    t = min_div[div_idx - 1]
                    min_div[div_idx - 1] = min_div[div_idx]
                    min_div[div_idx] = t
                    div_idx -= 1
                min_filters.insert(div_idx, vect)
                if len(min_div) > n:
                    min_div = min_div[:n]
                    min_filters = min_filters[:n]
                print('max', max_div)
            print('min', min_div)
        self.csp_filters = min_filters + max_filters
def test_ajd():
    """Test approximate joint diagonalization."""
    # The implementation shuold obtain the same
    # results as the Matlab implementation by Pham Dinh-Tuan.
    # Generate a set of cavariances matrices for test purpose
    n_times, n_channels = 10, 3
    seed = np.random.RandomState(0)
    diags = 2.0 + 0.1 * seed.randn(n_times, n_channels)
    A = 2 * seed.rand(n_channels, n_channels) - 1
    A /= np.atleast_2d(np.sqrt(np.sum(A**2, 1))).T
    covmats = np.empty((n_times, n_channels, n_channels))
    for i in range(n_times):
        covmats[i] = np.dot(np.dot(A, np.diag(diags[i])), A.T)
    V, D = _ajd_pham(covmats)
    # Results obtained with original matlab implementation
    V_matlab = [[-3.507280775058041, -5.498189967306344, 7.720624541198574],
                [0.694689013234610, 0.775690358505945, -1.162043086446043],
                [-0.592603135588066, -0.598996925696260, 1.009550086271192]]
    assert_array_almost_equal(V, V_matlab)
Exemple #3
0
def test_ajd():
    """Test if Approximate joint diagonalization implementation obtains same
    results as the Matlab implementation by Pham Dinh-Tuan.
    """
    # Generate a set of cavariances matrices for test purpose
    n_times, n_channels = 10, 3
    seed = np.random.RandomState(0)
    diags = 2.0 + 0.1 * seed.randn(n_times, n_channels)
    A = 2 * seed.rand(n_channels, n_channels) - 1
    A /= np.atleast_2d(np.sqrt(np.sum(A ** 2, 1))).T
    covmats = np.empty((n_times, n_channels, n_channels))
    for i in range(n_times):
        covmats[i] = np.dot(np.dot(A, np.diag(diags[i])), A.T)
    V, D = _ajd_pham(covmats)
    # Results obtained with original matlab implementation
    V_matlab = [[-3.507280775058041, -5.498189967306344, 7.720624541198574],
                [0.694689013234610, 0.775690358505945, -1.162043086446043],
                [-0.592603135588066, -0.598996925696260, 1.009550086271192]]
    assert_array_almost_equal(V, V_matlab)