-
Notifications
You must be signed in to change notification settings - Fork 4
/
histogram.h
196 lines (161 loc) · 4.22 KB
/
histogram.h
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* Created by Anjuta version 1.2.4a */
/* This file will not be overwritten */
#ifndef HISTOGRAM
#define HISTOGRAM
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>
#include <math.h>
#define realtype float
using namespace std;
//Function for creating a normalized histogram of an input vector given a number of bins
//not currently used
void pdf(vector<realtype> &input, vector<realtype> &bins, vector<realtype> &hist,bool clear=true)
{
//if clear flag set
if(clear)
hist.clear();
int end_index = hist.size();
int bin_size = bins.size();
int input_size = input.size();
int counted = 0;
//for(int i=0;i<=bin_size;i++)
// hist.push_back(0.0);
//roll back modifications to make zero values count
hist.insert(hist.end(),bin_size+1,0.0);
//hist.insert(hist.end(),bin_size,0.0);
for(int j=0;j<input_size;j++)
{
realtype val=input[j];
if (val==-1) continue;
counted++;
int k;
for(k=end_index;k<end_index+bin_size;k++)
{
if(val<bins[k-end_index])
break;
}
//roll back modifications to make zero values count
//if(k>end_index)
// hist[k-1]+=1.0;
hist[k]+=1.0;
}
if(input.size()!=0)
//roll back modifications to make zero values count
for(int i=end_index;i<=end_index+bin_size;i++)
hist[i]/=(realtype)input.size();
//for(int i=end_index;i<end_index+bin_size;i++)
}
//optimize this later
//when you shift the window, instead of recalculating accum completely
//simply subtract the number leaving and add the number arriving
//then divide to get avg
void window_filter(vector<realtype> &input, int winsize, vector<realtype> &res)
{
res.clear();
int inpSize=input.size();
for(int i=0;i<=inpSize-winsize;i++)
{
realtype accum=0.0;
//int badcount=0;
for(int j=i;j<winsize+i;j++)
{
if(input[j]==-1) continue;
accum+=input[j];
}
accum/=(realtype)winsize;
res.push_back(accum);
}
}
//multiresolution histogram, not used currently
void multires_hist(vector<realtype> &input, vector<realtype> &bins, vector<int> &granularities, vector<realtype> &res)
{
vector<realtype> accum;
res.clear();
for(int i=0;i<(int)granularities.size();i++)
{
window_filter(input,granularities[i],accum);
pdf(accum,bins,res,false);
}
}
//store a vector in plain text in file
void write_vect(char *fn,vector<realtype> res)
{
ofstream output(fn);
vector<realtype>::iterator it1 = res.begin();
while(it1!=res.end())
{
output << (*it1) << endl;
it1++;
}
output.close();
}
//calculates item-wise difference between two vectors
realtype hist_diff(vector<realtype> &in1, vector<realtype> &in2)
{
int size=in1.size();
realtype diff_accum = 0.0;
for(int x=0;x<size;x++)
{
realtype diff = in1[x]-in2[x];
diff_accum+=abs(diff);
}
return diff_accum/size;
}
//test routines
void hist_test()
{
vector<realtype> inputs;
vector<realtype> inputs2;
vector<realtype> inputs3;
vector<realtype> inputs4;
vector<realtype> bins;
vector<realtype> res;
srand(20);
for(int i=1;i<10;i++)
bins.push_back(0.1*i);
for(int i=0;i<1000;i++)
{
inputs.push_back((realtype)rand()/(realtype)RAND_MAX);
inputs3.push_back((realtype)rand()/(realtype)RAND_MAX);
inputs2.push_back((cos((realtype)i/100.0)+1.0)/2.0);
inputs4.push_back((sin((realtype)i/500.0)+1.0)/2.0);
}
pdf(inputs,bins,res);
cout << "PDF Test " << endl;
for(int i=0;i<10;i++)
cout << res[i] << endl;
cout << "Window Test " << endl;
cout << "Little Win " << endl;
window_filter(inputs,1,res);
for(int i=0;i<10;i++)
cout << res[i] << endl;
cout << "Big Win " << endl;
window_filter(inputs,50,res);
for(int i=0;i<10;i++)
cout << res[i] << endl;
cout << "MultiRes Histogram Test" << endl;
vector<int> grans;
for(int i=0;i<20;i++)
grans.push_back(i*5+1);
multires_hist(inputs,bins,grans,res);
vector<realtype> res2;
multires_hist(inputs2,bins,grans,res2);
vector<realtype> res3;
multires_hist(inputs3,bins,grans,res3);
vector<realtype> res4;
multires_hist(inputs4,bins,grans,res4);
for(int i=0;i<(int)res.size();i++)
{
if(i%10==0)
cout << i/10 << ":" << endl;
cout << res[i] << endl;
}
cout << "Difference Test " << endl;
cout << hist_diff(res,res2) << endl;
cout << hist_diff(res,res3) << endl;
cout << hist_diff(res2,res4) << endl;
std::cout << "End Test" << std::endl;
}
#endif