-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsmooth.pde
91 lines (71 loc) · 2.29 KB
/
smooth.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
public class Smooth {
public static final int NONE = 0;
public static final int RECTANGLE = 1;
public static final int TRIANGLE = 2;
public static final int ADJAVG = 3;
private int mode = NONE;
private int points = 3;
void setMode(int smoothType, int smoothPoints) {
this.mode = smoothType;
this.points = smoothPoints;
}
void apply(FFT fft) {
switch(mode) {
case RECTANGLE:
rectangleSmooth(fft, points);
break;
case TRIANGLE:
triangleSmooth(fft, points);
break;
case ADJAVG:
adjacentAverageSmooth(fft, points);
break;
}
}
// basic sliding average non-weighted smooth
protected void rectangleSmooth(FFT fft, int points) {
float smoothed;
// points must be odd, ie. a 3 point smooth is centred on the point to be smoothed, 1 point on either side.
if (points % 2 == 0) {
points++;
}
int sidePoints = (points - 1) / 2;
for(int i = sidePoints; i < fft.specSize() - sidePoints; i++) {
smoothed = fft.getBand(i);
for (int j = 0; j < sidePoints; j++) {
smoothed += fft.getBand(i-j) + fft.getBand(i+j);
}
fft.setBand(i, smoothed/(float)points);
}
}
// triangle smooth - weighted average smoothing
protected void triangleSmooth(FFT fft, int points) {
float smoothed;
if (points % 2 == 0) {
points++;
}
int sidePoints = (points - 1) / 2;
for(int i = sidePoints; i < fft.specSize() - sidePoints; i++) {
int weight = points / 2 + 1;
int divider = weight;
smoothed = fft.getBand(i) * weight;
for(int j = 0; j < sidePoints; j ++) {
weight--;
smoothed += (fft.getBand(i-j) * weight) + (fft.getBand(i+j) * weight);
divider += (weight * 2);
}
fft.setBand(i, smoothed/(float)divider);
}
}
// This smoother doesnt work well at all
// adjacent average smoothing - takes the average of 2 adjacent points
protected void adjacentAverageSmooth(FFT fft, int points) { ;
if (points % 2 == 0) {
points++;
}
int sidePoints = (points - 1) / 2;
for(int i = sidePoints; i < fft.specSize() - sidePoints; i++) {
fft.setBand(i, fft.getBand(i-sidePoints) + fft.getBand(i+sidePoints) / 2f);
}
}
}