-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathNumericTools.cpp
91 lines (72 loc) · 2.1 KB
/
NumericTools.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
#include "NumericTools.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
NumericTools::NumericTools()
{
// initialize random number generator
srand((unsigned)time(NULL));
}
unsigned char NumericTools::quantize8BIT(float d, float max)
{
return (unsigned char)((d / max) * 255.0);
}
float NumericTools::dequantize8BIT(unsigned char d, float max)
{
return ((float)d)*max/255.0;
}
float NumericTools::dequantize16BIT(unsigned short d, float max)
{
return ((float)d)*max/65535.0;
}
unsigned short NumericTools::quantize16BIT(float d, float max)
{
return (unsigned short)((d / max) * 65535.0);
}
double NumericTools::generateUniformRand()
{
return ((double) rand() / (RAND_MAX+1));
}
double NumericTools::generateStandardDistributionRand(double &mu, double &sigma)
{
double u, v, x, y, q;
do {
u = ((double) rand() / (RAND_MAX+1)) ;
v = 1.7156*(((double) rand() / (RAND_MAX+1)) -0.5);
x = u - 0.449871;
y = fabs(v) + 0.386595;
q = pow(x,2.0) + y*(0.19600*y-0.25472*x);
} while (q > 0.27597&& (q > 0.27846 || pow(v,2.0) > -4.*log(u)*pow(u,2.0)));
return mu + sigma*v/u;
}
void NumericTools::variance( std::vector<float> arr, float *var, float *avg)
{
int i;
float sum = 0.0, sum2 = 0.0, tavg;
for (i = 0; i < arr.size(); i++)
sum += arr[i];
tavg = sum / (float)arr.size();;
for (i = 0; i < arr.size(); i++)
sum2 += (tavg - arr[i]) * (tavg - arr[i]);
*avg = tavg;
*var = sum2 / (float) (arr.size() - 1);
}
void NumericTools::variance( std::vector<float> arr, float *var, float *avg, float *maxV, float *minV)
{
* minV = std::numeric_limits<float>::max();
*maxV = std::numeric_limits<float>::min();
int i;
float sum = 0.0, sum2 = 0.0, tavg;
for (i = 0; i < arr.size(); i++)
{
sum += arr[i];
if ( arr[i] < *minV ) *minV = arr[i];
if ( arr[i] > *maxV ) *maxV = arr[i];
}
tavg = sum / (float)arr.size();;
for (i = 0; i < arr.size(); i++)
sum2 += (tavg - arr[i]) * (tavg - arr[i]);
*avg = tavg;
*var = sum2 / (float) (arr.size() - 1);
}