Skip to content

Commit 2822c9a

Browse files
authored
Initial commit
1 parent 9ad2874 commit 2822c9a

File tree

97 files changed

+5769
-8
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+5769
-8
lines changed

DPLM.m

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
% This is the supervised version of the DPLM algorithm which
2+
% needs the labels. Please cite the following paper if you use
3+
% this code:
4+
% Davoudi, Alireza, Saeed Shiry Ghidary, and Khadijeh Sadatnejad.
5+
% "Dimensionality reduction based on distance preservation to local
6+
% mean for symmetric positive definite matrices and its application
7+
% in brain–computer interfaces." Journal of Neural Engineering 14.3
8+
% (2017): 036019.
9+
%
10+
% PARAMS:
11+
% data: An m*m*n dimensional matrix which contains n SPD matrix of size m*m
12+
% dim : The dimensionality of SPD matrices after DM
13+
% labels : A verctor of size n which contains the label of each point
14+
% k : Number of neighbours
15+
% Adj : The adjacency matrix calculated by `DPLM_adjmat` function
16+
% M : The means matrix calculated by `DPLM_adjmat` function
17+
%
18+
% RETUTNS:
19+
% U : The calculated transformation matrix which can be used as
20+
% below to transform an m*m SPD matrix `x` to an dim*dim
21+
% SPD matrix y:
22+
% y = U'*x*U
23+
% obj : Final value of the objective function
24+
% Adj : The adjacency matrix calculated by `DPLM_adjmat` function
25+
% M : The means matrix calculated by `DPLM_adjmat` function
26+
%
27+
function [U, obj, Adj, M] = DPLM( data, labels, dim, k, varargin )
28+
29+
if(~isempty(varargin))
30+
Adj = varargin{1};
31+
M = varargin{2};
32+
if(length(varargin) > 2)
33+
verbose = varargin{3};
34+
else
35+
verbose = 1;
36+
end
37+
else
38+
[ Adj, M ] = DPLM_adjmat( data, labels, k );
39+
verbose = 1;
40+
end
41+
42+
u0 = randn(size(data, 1), dim);
43+
u0 = orth(u0);
44+
45+
opts.record = verbose;
46+
opts.mxitr = 1000;
47+
opts.xtol = .1;
48+
opts.gtol = .1;
49+
opts.ftol = .1;
50+
%obj.tau = 1e-3;
51+
%opts.nt = 1;
52+
53+
t = tic;
54+
[U, obj]= OptStiefelGBB(u0, @objfunc, opts);
55+
tsolve = toc(t);
56+
57+
if(verbose)
58+
[f,~] = objfunc(u0);
59+
[flast,~] = objfunc(U);
60+
61+
fprintf('Elapsed time: %f\n', tsolve);
62+
fprintf('Stoped: %s , Init f: %f, Last f: %f \n',obj.msg, f, flast);
63+
end
64+
65+
66+
function [F, G] = objfunc(u)
67+
68+
F = 0;
69+
G = 0;
70+
for i = 1:size(data, 3)
71+
for j = 1:size(data, 3)
72+
if(Adj(i,j) == 0)
73+
continue;
74+
end
75+
76+
Ft = distance_ld(data(:,:,j),M(:,:,i))...
77+
-distance_ld(u' * data(:,:,j) * u, u' * M(:,:,i) * u);
78+
F = F + abs(Ft);
79+
80+
G = G -sign(Ft) * (2 * (data(:,:,j) + M(:,:,i)) * u ...
81+
* inv( u' * (data(:,:,j) + M(:,:,i)) * u) ...
82+
- data(:,:,j) * u * inv(u' * data(:,:,j) * u) ...
83+
- M(:,:,i) * u * inv(u' * M(:,:,i) * u));
84+
85+
end
86+
end
87+
88+
end
89+
90+
end
91+

DPLM_adjmat.m

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
% This function calculates the adjacency matrix (Adj) and
2+
% the means (M) of every point's neighbourhood (according to the
3+
% adjacency matrix).
4+
% Note that this is an within class adjacancy matrix (i.e. the
5+
% value of Adj(i,j) for the two points i and j of two different
6+
% classes is zero.
7+
%
8+
%
9+
% Please cite the following paper if you use
10+
% this code:
11+
% Davoudi, Alireza, Saeed Shiry Ghidary, and Khadijeh Sadatnejad.
12+
% "Dimensionality reduction based on distance preservation to local
13+
% mean for symmetric positive definite matrices and its application
14+
% in brain–computer interfaces." Journal of Neural Engineering 14.3
15+
% (2017): 036019.
16+
%
17+
%
18+
function [ Adj, M ] = DPLM_adjmat( data, labels, k )
19+
20+
dist = inf * ones(numel(labels));
21+
22+
for i = 1:numel(labels)
23+
for j = i+1:numel(labels)
24+
if(labels(i) == labels(j))
25+
dist(i,j) = distance_riemann(data(:,:,i), data(:,:,j));
26+
dist(j,i) = dist(i,j);
27+
end
28+
end
29+
end
30+
31+
M = zeros(size(data,1),size(data,1),numel(labels));
32+
Adj = zeros(numel(labels));
33+
for i = 1:numel(labels)
34+
[~,idx] = sort(dist(i,:), 'ascend');
35+
Adj(i,idx(1:k)) = 1;
36+
M(:,:,i) = mean_covariances(data(:,:,idx),'riemann');
37+
end
38+

Dependencies/FOptM/MGramSchmidt.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function V = MGramSchmidt(V)
2+
[n,k] = size(V);
3+
4+
for dj = 1:k
5+
for di = 1:dj-1
6+
V(:,dj) = V(:,dj) - proj(V(:,di), V(:,dj));
7+
end
8+
V(:,dj) = V(:,dj)/norm(V(:,dj));
9+
end
10+
end
11+
12+
13+
%project v onto u
14+
function v = proj(u,v)
15+
v = (dot(v,u)/dot(u,u))*u;
16+
end

0 commit comments

Comments
 (0)