initial upload

This commit is contained in:
2024-09-10 20:25:18 +08:00
parent b8de03ee4f
commit f1cc876972
377 changed files with 2721267 additions and 34 deletions

View File

@@ -0,0 +1,5 @@
add_executable(addnoise main.cpp)
# 第二种方式指定目标文件的路径 这个方式目标文件夹中将只包含目标文件而没有中间文件
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
# 设置安装地址
install(TARGETS addnoise RUNTIME DESTINATION selfpro)

125
archive/addnoise/addnoise.h Normal file
View 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;
}

68
archive/addnoise/head.h Normal file
View File

@@ -0,0 +1,68 @@
#ifndef _HEAD_H
#define _HEAD_H
#include "ctype.h"
#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
#include "iostream"
#include "fstream"
#include "sstream"
#include "iomanip"
#include "vector"
#include "ctime"
#include "cmath"
#include "random"
#include "string.h"
#define BOLDRED "\033[1m\033[31m"
#define RESET "\033[0m"
using namespace std;
typedef vector<double> _1dArray;
typedef vector<int> _1iArray;
int open_infile(ifstream &infile,char* filename)
{
infile.open(filename);
if (!infile)
{
cout << BOLDRED << "error ==> " << RESET << "file not found: " << filename << endl;
return -1;
}
return 0;
}
int open_outfile(ofstream &outfile,char* filename)
{
outfile.open(filename);
if (!outfile)
{
cout << BOLDRED << "error ==> " << RESET << "fail to create the file: " << filename << endl;
return -1;
}
return 0;
}
string& replace_all(string& str,const string& old_value,const string& new_value,int &num) //替换str中所有lod_value为new_value,返回被替换的lod_value的个数
{
int count = 0;
for(string::size_type pos(0);pos!=string::npos;pos+=new_value.length())
{
if((pos=str.find(old_value,pos))!=string::npos)
{
str.replace(pos,old_value.length(),new_value);
count++;
}
else break;
}
num = count;
return str;
}
struct node
{
_1dArray val;
};
typedef vector<node> nodeArray;
#endif

89
archive/addnoise/main.cpp Normal file
View File

@@ -0,0 +1,89 @@
#include "addnoise.h"
void disp_help(char* proname)
{
cout << proname << " - 0.1 add Gaussian noise to a 1D, 2D or 3D data set" << endl
<< "Author: zhangyi.cugwuhan@gmail.com" << endl << endl
<< "usage: " << proname << " -i<input-file> -o<output-file> -n<mean>/<deviation> [-d<x-col>[,<y-col>,<z-col>],<val-col>] [-h]" << endl
<< "-i\tinput file (ASCII)" << endl
<< "-o\toutput file" << endl
<< "-n\tnoise parameters, mean value and standard deviation of the Gaussian noise" << endl
<< "-d\tdata columns, the default is 0,1,2 for a 2D data set. The program uses the last column as input data, all others will be simply copied to the output" << endl
<< "-h\tshow this info" << endl;
}
int main(int argc, char** argv)
{
char infilename[1024] = "NULL";
char outfilename[1024] = "NULL";
char cols[1024] = "0,1,2";
char noise[1024] = "NULL";
opterr = 0; //内置参数 若不为0则会在发生遭遇错误时输出一条信息到屏幕
if (argc == 1)
{
cout << "use -h option for help information" << endl;
return 0;
}
int curr;
/*循环拾取参数 最后一个参数为-1 需要变量的参数后跟一个冒号 可有可无参数跟两个冒号*/
while((curr = getopt(argc,argv,"hi:o:n:d:")) != -1)
{
/*匹配命令*/
switch (curr)
{
case 'h': //显示帮助信息
disp_help(argv[0]);
break;
case 'i':
if (1!=sscanf(optarg,"%s",infilename))
{
cout << "error ==> wrong format of " << optarg << endl;
}
break;
case 'o':
if (1!=sscanf(optarg,"%s",outfilename))
{
cout << "error ==> wrong format of " << optarg << endl;
}
break;
case 'n':
if (1!=sscanf(optarg,"%s",noise))
{
cout << "error ==> wrong format of " << optarg << endl;
}
break;
case 'd':
if (1!=sscanf(optarg,"%s",cols))
{
cout << "error ==> wrong format of " << optarg << endl;
}
break;
case '?': //处理未定义或错误参数
if (optopt == 'i' || optopt == 'o' || optopt == 'n' || optopt == 'd')
{
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
return -1;
}
else if (isprint(optopt))
{
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
return -1;
}
else
{
fprintf (stderr,"Unknown option character `\\x%x'.\n",optopt);
return -1;
}
break;
default:
abort();
}
}
addnoise as;
as.runtine(infilename,outfilename,noise,cols);
return 0;
}