blob: f2668ba1fd64524575b767d8aa282f09b4fceb91 (
plain)
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
|
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <vector>
using namespace std;
vector<double> currentHistogramValues;
int main(int argc, char* argv[]) {
int c;
string usage = "standard-deviation -l <list-of-files> -i <input-path> -o <output-file> -t <type>";
string inputFileListName;
string inputPath;
string outputFileName;
string distributionType;
string inputFileList;
string currentFileName;
while ((c = getopt (argc, argv, "i:o:l:t:h")) != -1)
switch (c) {
case 'l':
inputFileListName = optarg;
break;
case 'i':
inputPath = optarg;
break;
case 'o':
outputFileName = optarg;
break;
case 't':
distributionType = optarg;
break;
case 'h':
cout << usage << endl;
exit(1);
break;
default:
break;
}
if ( inputFileListName.empty() || inputPath.empty() || distributionType.empty() || outputFileName.empty() ) {
cerr << usage << endl;
exit(1);
}
ifstream inputFileNames(inputFileListName.c_str());
ofstream outputFile(outputFileName.c_str());
string prefixPath(inputPath.c_str());
string metricName(distributionType.c_str());
if (inputFileNames.fail() == true) {
cout << "Input File can not be opened"<<endl;
exit(0);
}
outputFile << left << setw(20) << "File Name" << left << setw(20) << "Mean" << left << setw(20) << "Standard Deviation" << endl;
while( inputFileNames >> currentFileName ) {
string currentFileWithExtension = prefixPath + currentFileName + "/" + currentFileName + "_"+ metricName +".txt";
cout<<"File: "<<currentFileWithExtension<<endl;
ifstream currentFile(currentFileWithExtension.c_str());
if (currentFile.fail() == true) {
cout << currentFileName + " cannot be opened"<<endl;
exit(0);
}
// N = sum(H_i) for i = 0 to M-1, M = number of samples in the histogram
// mean = 1/N*(sum(i*H_i)) for i = 0 to M-1
double currentValueOfHistogram;
double sumOfValues = 0.0;
currentHistogramValues.clear();
double i = 0.0;
double N = 0.0;
double M = 0.0;
while (currentFile >> currentValueOfHistogram) {
sumOfValues = sumOfValues + i*currentValueOfHistogram;
currentHistogramValues.push_back(currentValueOfHistogram);
N = N + currentValueOfHistogram;
i = i+1.0;
}
M = currentHistogramValues.size();
// mean
double mean = sumOfValues/N;
// sigma^2 = (sum( (i-mean)^2*H_i ) )/(N-1) for i = 0 to M-1
double standardDev = 0.0;
double sumSquaredResults = 0.0;
int j = 0;
for ( i = 0.0; i < M; i = i+1.0 ) {
sumSquaredResults += pow((i-mean), 2.0)*currentHistogramValues[j];
j++;
}
// standard deviation
standardDev = sumSquaredResults/(N-1);
standardDev = sqrt(standardDev);
outputFile<<left<<setw(20)<<currentFileName<<left<<setw(20)<<mean<<left<<setw(20)<<standardDev<<endl;
currentFile.close();
}
inputFileNames.close();
outputFile.close();
return(0);
}
|