-
Notifications
You must be signed in to change notification settings - Fork 91
/
scale.pde
76 lines (62 loc) · 1.83 KB
/
scale.pde
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
DataPoint[] reduceBuffer(double[] buffer, int length) {
DataPoint[] ret = new DataPoint[length];
float step = (float)length / (float)buffer.length;
int oldIndex = 0;
float position = 0;
double minValue = Double.POSITIVE_INFINITY;
double maxValue = Double.NEGATIVE_INFINITY;
int count = 0;
double sum = 0;
int sourcePos = 0;
while (position < length) {
if (oldIndex != (int)position) {
DataPoint dp = new DataPoint();
dp.x = (int)position - 1;
dp.yMin = minValue;
dp.yMax = maxValue;
dp.yAvg = (double)sum / (double)count;
ret[oldIndex] = dp;
sum = 0;
count = 0;
minValue = Double.POSITIVE_INFINITY;
maxValue = Double.NEGATIVE_INFINITY;
oldIndex = (int)position;
continue;
}
if (sourcePos < buffer.length) {
if (buffer[sourcePos] < minValue && buffer[sourcePos] != Double.NEGATIVE_INFINITY) {
minValue = buffer[sourcePos];
}
if (buffer[sourcePos] > maxValue) {
maxValue = buffer[sourcePos];
}
if (buffer[sourcePos] > Double.NEGATIVE_INFINITY && buffer[sourcePos] != Double.POSITIVE_INFINITY) {
sum += buffer[sourcePos];
count++;
}
}
position += step;
sourcePos++;
}
return ret;
}
DataPoint[] scaleBufferX(double[] buffer) {
double[] xscale = new double[graphWidth()];
DataPoint[] ret;
if(buffer == null) return new DataPoint[0];
if (graphWidth() < buffer.length) {
ret = reduceBuffer(buffer, graphWidth());
} else {
ret = new DataPoint[buffer.length];
float step = graphWidth() / (float)buffer.length;
for (int i = 0; i < buffer.length; i++) {
DataPoint dp = new DataPoint();
dp.x = (int)((float)i * step);
dp.yMin = buffer[i];
dp.yMax = buffer[i];
dp.yAvg = buffer[i];
ret[i] = dp;
}
}
return ret;
}