/
MFParam.cpp
142 lines (115 loc) · 3.9 KB
/
MFParam.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
141
142
/*
* MFParam.cpp
*
* Created on: Feb 18, 2016
* Author: sonbol
*/
#include "MFParam.h"
using namespace std;
MFParam::MFParam():commandLine(), prms(numVariate),MFparam(numVariate),MFparam1(numVariate), power(numVariate) {
// TODO Auto-generated constructor stub
}
MFParam::~MFParam() {
// TODO Auto-generated destructor stub
}
/* It is in a case of univariate
* 1. Pass the surrogate date (endtoend certified) to fourier transfrom
* 2. select frequencies with highest power. Num of frequencies is euqal to MF
* 3. MF is predetermined in this version, so based on num of MF, we select the sine wave with the highest power and used their phase and amplitude
* 3.
* In a case of multivariate, we should note that for each variable, we need different set of membership functions
* 1. We need to have different inputs for each variate or at least they should be selected
* 2. We pass each variate to fast fourier transform and select the sine with highets power
* 3. use their amplitude, phase, and frequency as our MF parameters.*/
std::vector<vector<vector<double>>> * MFParam::getMfparam(std::vector<std::vector<double>> * input){
calMFparam(input);
return(&MFparam1);
}
void MFParam::calMFparam(std::vector<std::vector<double>> * input){
int row=((*input)[0].size())/2+1;
//TODO: this variable is defined in commandline but I should change it to be added in MFparam because it is kind of global here
LengthSurrodata=row;
cout<< "lengthSurrdata"<<LengthSurrodata<<endl;
for (int i=0 ; i<numVariate;i++){
prms[i].resize(row);
}
//(row, vector<pair<double,double>>(numVariate));//1 is for amplitude and 1 for phase
for (int i=0; i< numVariate;i++){ //For each variate an independent fourier transform is taken
fftw_plan pln;
int N=(*input)[i].size();
int Nout=floor(N/2+1);
double * inpt=&((*input)[i][0]);
fftw_complex *out=(fftw_complex*) fftw_malloc(sizeof(fftw_complex)*Nout);
//vector<double> power (numOfMF[i]);
vector<double> ampltd (Nout);
power[i].resize(Nout);
//Getting Fourier Transform
pln=fftw_plan_dft_r2c_1d(N,inpt,out,FFTW_ESTIMATE);
fftw_execute(pln);
//Get power for each data point
for (int j=0;j< Nout;j++){
double real=out[j][0];
double img=out[j][1];
double amplitude=sqrt(pow(real,2)+pow(img,2));
ampltd[j]=amplitude;
power[i][j]=amplitude;
//double phase=atan2(img,real);
prms[i][j]=make_pair(real,img);
}
//Max of powers
vector<int> index (Nout);
//save index for use in sort function
for (int j=0; j<Nout; j++){
index[j]=j;
}
//using lambda to get the index of max power
sort(index.begin(),index.end(),[&ltd](size_t ii, size_t jj){return ampltd[ii]>ampltd[jj];});
MFparam[i].resize(numOfMF[i]); //We multiply by 2 because we want to size indexes as well
MFparam1[i].resize(numOfMF[i]);
for (int h=0; h < numOfMF[i];h++){
MFparam1[i][h].resize(3); //real, image and index
}
for (int k=0; k< numOfMF[i];k++){ //index[0] is for DC power that we ignore it
//power[k]=ampltd[index[k+1]];
MFparam[i][k]=(prms[i][index[k+1]]);
MFparam1[i][k][0]=index[k+1];
MFparam1[i][k][1]=get<0>(prms[i][index[k+1]]);
MFparam1[i][k][2]=get<1>(prms[i][index[k+1]]);
}
fftw_destroy_plan(pln);
fftw_free(out);
}
}
vector<vector<double>>* MFParam::getPower(){
return(&power);
}
void mfparamtest(){
InputStream inS;
MFParam mfPar;
vector<vector<double>> * surodata;
vector<vector<vector<double>>> *mfParam;
inS.getOrigWindowN();
surodata=inS.getSurWindowN();
cout<< "this is surrogate data"<<endl;
for (auto i: *surodata){
for (auto j:i){
cout << j<< " ";
}
}
mfParam=mfPar.getMfparam(surodata);
cout<<endl<<"this is power"<<endl;
vector<vector<double>> *pw=mfPar.getPower();
for (auto i: *(pw)){
for (auto j: i){
cout << j<< " ";
}
}
cout <<endl<<"this is mf params coming out of MFParam" <<endl;
for (auto i:*mfParam){
for (auto j: i){
for (auto k: j){
cout << k << " ";
}
}
}
}