-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
converter_jade_bss.cpp
110 lines (83 loc) · 2.64 KB
/
converter_jade_bss.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
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Kevin Hughes, Soeren Sonnenburg, Evgeniy Andreev, Viktor Gal,
* Björn Esser, Pan Deng
*/
#include <shogun/base/init.h>
#include <shogun/lib/common.h>
#include <iostream>
using namespace shogun;
#include <shogun/features/DenseFeatures.h>
#include <shogun/mathematics/Math.h>
// FIXME: do not use eigen in examples, as then we need eigen
// during compilation of an example.
#include <shogun/mathematics/eigen3.h>
#include <shogun/converter/ica/Jade.h>
#include <shogun/evaluation/ica/PermutationMatrix.h>
#include <shogun/evaluation/ica/AmariIndex.h>
using namespace Eigen;
void test()
{
// Generate sample data
auto prng = get_prng();
std::normal_distribution<float64_t> dist(0, 1);
int n_samples = 2000;
VectorXd time(n_samples, true);
time.setLinSpaced(n_samples,0,10);
// Source Signals
MatrixXd S(2,n_samples);
for(int i = 0; i < n_samples; i++)
{
// Sin wave
S(0,i) = sin(2*time[i]);
S(0, i) += 0.2 * dist(prng);
// Square wave
S(1,i) = sin(3*time[i]) < 0 ? -1 : 1;
S(1, i) += 0.2 * dist(prng);
}
// Standardize data
VectorXd avg = S.rowwise().sum() / n_samples;
VectorXd std = ((S.colwise() - avg).array().pow(2).rowwise().sum() / n_samples).array().sqrt();
for(int i = 0; i < n_samples; i++)
S.col(i) = S.col(i).cwiseQuotient(std);
// Mixing Matrix
SGMatrix<float64_t> mixing_matrix(2,2);
Map<MatrixXd> A(mixing_matrix.matrix,2,2);
A(0,0) = 1; A(0,1) = 0.5;
A(1,0) = 0.5; A(1,1) = 1;
std::cout << "Mixing Matrix:" << std::endl;
std::cout << A << std::endl << std::endl;
// Mix signals
SGMatrix<float64_t> X(2,n_samples);
Map<MatrixXd> EX(X.matrix,2,n_samples);
EX = A * S;
CDenseFeatures< float64_t >* mixed_signals = new CDenseFeatures< float64_t >(X);
// Separate
CJade* jade = new CJade();
SG_REF(jade);
CFeatures* signals = jade->apply(mixed_signals);
SG_REF(signals);
// Close to a permutation matrix (with random scales)
Map<MatrixXd> EA(jade->get_mixing_matrix().matrix,2,2);
std::cout << "Estimated Mixing Matrix:" << std::endl;
std::cout << EA << std::endl << std::endl;
SGMatrix<float64_t> P(2,2);
Eigen::Map<MatrixXd> EP(P.matrix,2,2);
EP = EA.inverse() * A;
bool isperm = is_permutation_matrix(P);
std::cout << "EA^-1 * A == Permuatation Matrix is: " << isperm << std::endl;
float64_t amari_err = amari_index(jade->get_mixing_matrix(), mixing_matrix, true);
std::cout << "Amari Error: " << amari_err << std::endl;
SG_UNREF(jade);
SG_UNREF(mixed_signals);
SG_UNREF(signals);
return;
}
int main(int argc, char ** argv)
{
init_shogun_with_defaults();
test();
exit_shogun();
return 0;
}