-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
UWedgeSep.cpp
140 lines (106 loc) · 2.86 KB
/
UWedgeSep.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
130
131
132
133
134
135
136
137
138
139
140
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2013 Kevin Hughes
*/
#include <shogun/converter/ica/UWedgeSep.h>
#include <shogun/features/DenseFeatures.h>
#ifdef HAVE_EIGEN3
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/eigen3.h>
#include <shogun/mathematics/ajd/UWedge.h>
using namespace Eigen;
typedef Matrix< float64_t, Dynamic, 1, ColMajor > EVector;
typedef Matrix< float64_t, Dynamic, Dynamic, ColMajor > EMatrix;
using namespace shogun;
namespace { EMatrix cor(EMatrix x, int tau = 0, bool mean_flag = true); };
CUWedgeSep::CUWedgeSep() : CConverter()
{
m_tau = SGVector<float64_t>(4);
m_tau[0]=0; m_tau[1]=1; m_tau[2]=2; m_tau[3]=3;
init();
}
void CUWedgeSep::init()
{
m_mixing_matrix = SGMatrix<float64_t>();
SG_ADD(&m_tau, "tau", "tau vector", MS_AVAILABLE);
SG_ADD(&m_mixing_matrix, "mixing_matrix", "m_mixing_matrix", MS_NOT_AVAILABLE);
}
CUWedgeSep::~CUWedgeSep()
{
}
void CUWedgeSep::set_tau(SGVector<float64_t> tau)
{
m_tau = tau;
}
SGVector<float64_t> CUWedgeSep::get_tau() const
{
return m_tau;
}
SGMatrix<float64_t> CUWedgeSep::get_mixing_matrix() const
{
return m_mixing_matrix;
}
CFeatures* CUWedgeSep::apply(CFeatures* features)
{
REQUIRE(features, "features is null");
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;
Eigen::Map<EMatrix> 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;
SGNDArray< float64_t > M(M_dims, 3);
for (int t = 0; t < N; t++)
{
Eigen::Map<EMatrix> EM(M.get_matrix(t),n,n);
EM = cor(EX,m_tau[t]);
}
// Diagonalize
SGMatrix<float64_t> Q = CUWedge::diagonalize(M);
Eigen::Map<EMatrix> EQ(Q.matrix,n,n);
// Compute Mixing Matrix
m_mixing_matrix = SGMatrix<float64_t>(n,n);
Eigen::Map<EMatrix> 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
{
EMatrix cor(EMatrix x, int tau, bool mean_flag)
{
int m = x.rows();
int n = x.cols();
// Center the data
if ( mean_flag )
{
EVector mean = x.rowwise().sum();
mean /= n;
x = x.colwise() - mean;
}
// Time-delayed Signal Matrix
EMatrix L = x.leftCols(n-tau);
EMatrix R = x.rightCols(n-tau);
// Compute Correlations
EMatrix K(m,m);
K = (L * R.transpose()) / (n-tau);
// Symmetrize
K = (K + K.transpose()) / 2.0;
return K;
}
};
#endif // HAVE_EIGEN3