-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnormalize.java
39 lines (32 loc) · 968 Bytes
/
normalize.java
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
import com.cycling74.max.*;
public class normalize extends MaxObject {
float[] input;
normalize() {
declareInlets(new int[] { DataTypes.LIST });
declareOutlets(new int[] { DataTypes.LIST });
}
protected void list(final Atom[] a) {
input = Atom.toFloat(a);
bang();
}
protected void bang() {
float sum = 0;
float min = Float.POSITIVE_INFINITY;
float max = Float.NEGATIVE_INFINITY;
for (float f : input) {
sum += f;
if (f < min) {
min = f;
} else if (f > max) {
max = f;
}
}
float avg = sum / input.length;
float deviation = Math.max(max - avg, avg - min);
float[] output = new float[input.length];
for (int i = 0; i < output.length; i++) {
output[i] = (((input[i] - avg) / deviation) + 1.f) / 2.f;
}
outlet(0, output);
}
}