-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
FFSep.cpp
130 lines (98 loc) · 2.42 KB
/
FFSep.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Kevin Hughes, Heiko Strathmann, Bjoern Esser
*/
#include <shogun/converter/ica/FFSep.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/eigen3.h>
#include <shogun/mathematics/ajd/FFDiag.h>
using namespace shogun;
using namespace Eigen;
namespace { MatrixXd cor(MatrixXd x, int tau = 0, bool mean_flag = true); };
CFFSep::CFFSep() : CICAConverter()
{
init();
}
void CFFSep::init()
{
m_tau = SGVector<float64_t>(4);
m_tau[0]=0; m_tau[1]=1; m_tau[2]=2; m_tau[3]=3;
m_covs = SGNDArray<float64_t>();
SG_ADD(&m_tau, "tau", "tau vector", MS_AVAILABLE);
}
CFFSep::~CFFSep()
{
}
void CFFSep::set_tau(SGVector<float64_t> tau)
{
m_tau = tau;
}
SGVector<float64_t> CFFSep::get_tau() const
{
return m_tau;
}
SGNDArray<float64_t> CFFSep::get_covs() const
{
return m_covs;
}
CFeatures* CFFSep::apply(CFeatures* features, bool inplace)
{
ASSERT(features);
SG_REF(features);
SGMatrix<float64_t> X = ((CDenseFeatures<float64_t>*)features)->get_feature_matrix();
int n = X.num_rows;
int m = X.num_cols;
int N = m_tau.vlen;
Map<MatrixXd> EX(X.matrix,n,m);
// Compute Correlation Matrices
index_t * M_dims = SG_MALLOC(index_t, 3);
M_dims[0] = n;
M_dims[1] = n;
M_dims[2] = N;
m_covs = SGNDArray< float64_t >(M_dims, 3);
for (int t = 0; t < N; t++)
{
Map<MatrixXd> EM(m_covs.get_matrix(t),n,n);
EM = cor(EX,m_tau[t]);
}
// Diagonalize
SGMatrix<float64_t> Q = CFFDiag::diagonalize(m_covs, m_mixing_matrix, tol, max_iter);
Map<MatrixXd> EQ(Q.matrix,n,n);
// Compute Mixing Matrix
m_mixing_matrix = SGMatrix<float64_t>(n,n);
Map<MatrixXd> C(m_mixing_matrix.matrix,n,n);
C = EQ.inverse();
// Normalize Estimated Mixing Matrix
for (int t = 0; t < C.cols(); t++)
C.col(t) /= C.col(t).maxCoeff();
// Unmix
EX = C.inverse() * EX;
return features;
}
// Computing time delayed correlation matrix
namespace
{
MatrixXd cor(MatrixXd x, int tau, bool mean_flag)
{
int m = x.rows();
int n = x.cols();
// Center the data
if ( mean_flag )
{
VectorXd mean = x.rowwise().sum();
mean /= n;
x = x.colwise() - mean;
}
// Time-delayed Signal Matrix
MatrixXd L = x.leftCols(n-tau);
MatrixXd R = x.rightCols(n-tau);
// Compute Correlations
MatrixXd K(m,m);
K = (L * R.transpose()) / (n-tau);
// Symmetrize
K = (K + K.transpose()) / 2.0;
return K;
}
};