initial upload
This commit is contained in:
125
archive/addnoise/addnoise.h
Normal file
125
archive/addnoise/addnoise.h
Normal file
@@ -0,0 +1,125 @@
|
||||
#include "head.h"
|
||||
|
||||
class addnoise
|
||||
{
|
||||
public:
|
||||
addnoise(){}
|
||||
~addnoise(){}
|
||||
int runtine(char*,char*,char*,char*);
|
||||
nodeArray readata(char*,char*);
|
||||
int addNoise(char*);
|
||||
int outRes(char*);
|
||||
private:
|
||||
nodeArray data;
|
||||
double noise_mean,noise_dev;
|
||||
};
|
||||
|
||||
int addnoise::runtine(char* ifile,char* ofile,char* nos,char* col)
|
||||
{
|
||||
data = readata(ifile,col);
|
||||
if(data.empty()) return -1;
|
||||
if (addNoise(nos)) return -1;
|
||||
if (outRes(ofile)) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
nodeArray addnoise::readata(char* filename,char* order)
|
||||
{
|
||||
int temp_int;
|
||||
double temp_d;
|
||||
nodeArray input_data;
|
||||
node temp_node;
|
||||
_1dArray tempRow;
|
||||
_1iArray orders;
|
||||
string temp_str;
|
||||
stringstream temp_ss;
|
||||
|
||||
temp_ss.str("");
|
||||
temp_ss.clear();
|
||||
temp_ss << order;
|
||||
temp_ss >> temp_str;
|
||||
temp_str = replace_all(temp_str,","," ",temp_int);
|
||||
temp_ss.str("");
|
||||
temp_ss.clear();
|
||||
temp_ss << temp_str;
|
||||
while (temp_ss >> temp_int)
|
||||
{
|
||||
orders.push_back(temp_int);
|
||||
}
|
||||
|
||||
//打开文件
|
||||
ifstream datain;
|
||||
if (open_infile(datain,filename)) return input_data;
|
||||
|
||||
//读入数据
|
||||
while (getline(datain,temp_str))
|
||||
{
|
||||
if (*(temp_str.begin()) == '#') continue;
|
||||
else
|
||||
{
|
||||
//读入行
|
||||
temp_ss.str("");
|
||||
temp_ss.clear();
|
||||
temp_ss.str(temp_str);
|
||||
//解析数据行
|
||||
if(!tempRow.empty()) tempRow.clear();
|
||||
while (temp_ss >> temp_d)
|
||||
{
|
||||
tempRow.push_back(temp_d);
|
||||
}
|
||||
if(!temp_node.val.empty()) temp_node.val.clear();
|
||||
for (int i = 0; i < orders.size(); i++)
|
||||
{
|
||||
temp_node.val.push_back(tempRow.at(orders.at(i)));
|
||||
}
|
||||
|
||||
input_data.push_back(temp_node);
|
||||
}
|
||||
}
|
||||
|
||||
datain.close();
|
||||
return input_data;
|
||||
}
|
||||
|
||||
int addnoise::addNoise(char* para)
|
||||
{
|
||||
double noise_value;
|
||||
if (2 != sscanf(para,"%lf/%lf",&noise_mean,&noise_dev))
|
||||
{
|
||||
cout << "error ==> wrong parameters: " << para << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
//添加高斯噪声值
|
||||
default_random_engine generator;
|
||||
normal_distribution<double> dist(noise_mean, noise_dev);
|
||||
|
||||
for (int i = 0; i < data.size(); i++)
|
||||
{
|
||||
noise_value = dist(generator);
|
||||
data.at(i).val.push_back(data.at(i).val.back()+noise_value);
|
||||
data.at(i).val.push_back(noise_value);
|
||||
data.at(i).val.push_back(noise_dev);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int addnoise::outRes(char* filename)
|
||||
{
|
||||
ofstream outfile;
|
||||
if (open_outfile(outfile,filename)) return -1;
|
||||
|
||||
outfile << "# noise-mean = " << noise_mean << endl;
|
||||
outfile << "# noise-deviation = " << noise_dev << endl;
|
||||
outfile << "# <-- copied data --> input+noise noise dev" << endl;
|
||||
for (int i = 0; i < data.size(); i++)
|
||||
{
|
||||
for (int j = 0; j < data.at(i).val.size(); j++)
|
||||
{
|
||||
outfile << data.at(i).val.at(j) << " ";
|
||||
}
|
||||
outfile << endl;
|
||||
}
|
||||
outfile.close();
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user