00001 #include "FastICA.h"
00002 #include "UniformRNG.h"
00003 #include "miscfunc.h"
00004 #include <itpp/base/fastica.h>
00005 #include <itpp/base/mat.h>
00006 #include <itpp/base/sigfun.h>
00007 #include <fstream>
00008 using namespace std;
00009 void WriteToFile(const itpp::Vec<double> &in,const itpp::Vec<double> &out,const string &base)
00010 {
00011 ofstream ints((base+".in").c_str());
00012 ofstream outts((base+".out").c_str());
00013 const int size = in.length();
00014 for (int i = 0; i < size; ++i)
00015 {
00016 ints << in(i) << endl;
00017 outts << out(i) << endl;
00018 }
00019 }
00020
00021 int main()
00022 {
00023 const int nchannels = 2;
00024 const int nsamples = 2000;
00025 const float minrand = -0.5;
00026 const float maxrand = 0.5;
00027 UniformRNG Random;
00028
00029 itpp::mat Input(nchannels,nsamples);
00030 itpp::mat Sources(nchannels,nsamples);
00031 rmat orig_source(nchannels,nsamples);
00032 rmat input(nchannels,nsamples);
00033 for (int j = 0; j < nsamples; ++j)
00034 {
00035 orig_source(0,j) = 100.9*Random.GetNumber(minrand,maxrand);
00036 orig_source(1,j) = 13.0*sin(j/13.678);
00037 }
00038 rmat orig_mix(nchannels,nchannels);
00039 for (int i = 0; i < nchannels; ++i)
00040 for (int j = 0; j < nchannels; ++j)
00041 orig_mix(i,j) = Random.GetNumber(minrand,maxrand);
00042
00043 noalias(input) = ublas::prod(orig_mix,orig_source);
00044
00045 for (size_t i = 0; i < nsamples; ++i)
00046 {
00047
00048 Input(0,i) = input(0,i);
00049 Input(1,i) = input(1,i);
00050 }
00051
00052 itpp::Fast_ICA fastica(Input);
00053 fastica.set_nrof_independent_components(nchannels);
00054 fastica.set_non_linearity(FICA_NONLIN_POW3);
00055 fastica.set_approach(FICA_APPROACH_SYMM);
00056 fastica.separate();
00057 itpp::mat Output = fastica.get_independent_components();
00058
00059
00060 rmat source(nchannels,nsamples);
00061 rmat mix(nchannels,nchannels);
00062
00063
00064
00065 FastICA(input,source,mix);
00066 WriteToFile(Output.get_row(0),Output.get_row(1),"1");
00067 std::cout << std::endl << std::endl;
00068
00069 std::cout << "Itpp WhM: " << fastica.get_whitening_matrix() << std::endl;
00070 std::cout << "orig_mix: " << orig_mix << std::endl<< std::endl;
00071 std::cout << "FastICA MixMat: " << fastica.get_mixing_matrix() << std::endl;
00072 std::cout << "FastICA SepMat: " << fastica.get_separating_matrix() << std::endl;
00073
00074 std::cout << "est_mix: " << mix << std::endl<< std::endl;
00075 std::ofstream outfile("fastica.out");
00076 for (int i = 0; i < nsamples; ++i)
00077 outfile << i << " " << input(0,i) << " " << input(1,i) <<" " << source(0,i) << " " << source(1,i) << " " << Output(0,i) << " " << Output(1,i) << std::endl;
00078 }