/
window.pde
105 lines (93 loc) · 2.68 KB
/
window.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
public class Window {
public static final int RECTANGULAR = 0; // equivilent to no window
public static final int HAMMING = 1;
public static final int HANN = 2;
public static final int COSINE = 3;
public static final int TRIANGULAR = 4;
public static final int BLACKMAN = 5;
public static final int GAUSS = 6;
private int mode = RECTANGULAR;
void setMode(int window) {
this.mode = window;
}
void transform(float[] samples) {
switch(mode) {
case HAMMING:
hammingWindow(samples);
break;
case HANN:
hannWindow(samples);
break;
case COSINE:
cosineWindow(samples);
break;
case TRIANGULAR:
triangularWindow(samples);
break;
case BLACKMAN:
blackmanWindow(samples);
break;
case GAUSS:
gaussWindow(samples);
break;
}
}
float[] drawCurve() {
float[] samples = new float[128];
for(int i = 0; i < samples.length; i++ ) {
samples[i] = 1; // 1 out samples
}
switch(mode) {
case HAMMING:
hammingWindow(samples);
break;
case HANN:
hannWindow(samples);
break;
case COSINE:
cosineWindow(samples);
break;
case TRIANGULAR:
triangularWindow(samples);
break;
case BLACKMAN:
blackmanWindow(samples);
break;
case GAUSS:
gaussWindow(samples);
break;
}
return samples;
}
void hammingWindow(float[] samples) {
for(int n = 0; n < samples.length; n++) {
samples[n] *= (0.54f - 0.46f * Math.cos(TWO_PI * n / (samples.length - 1)));
}
}
void hannWindow(float[] samples) {
for(int n = 0; n < samples.length; n++) {
samples[n] *= (0.5f * (1 - Math.cos(TWO_PI * n / (samples.length - 1))));
}
}
void cosineWindow(float[] samples) {
for(int n = 0; n < samples.length; n++) {
samples[n] *= Math.cos((Math.PI * n) / (samples.length - 1) - (Math.PI / 2));
}
}
void triangularWindow(float[] samples) {
for(int n = 0; n < samples.length; n++) {
samples[n] *= ((2.0f / samples.length) * ((samples.length / 2.0f) - Math.abs(n - (samples.length - 1) / 2.0f)));
}
}
void blackmanWindow(float[] samples) {
for(int n = 0; n < samples.length; n++) {
samples[n] *= (0.42f - 0.5f * Math.cos(TWO_PI * n / (samples.length - 1))) + (0.08f * Math.cos(4 * PI * n / (samples.length -1)));
}
}
void gaussWindow(float[] samples) {
double a = 2.5;
for (int n = 0; n < samples.length; n++ ) {
samples[n] *= Math.pow(Math.E, -0.5 * Math.pow((n - (samples.length - 1) / 2) / (0.1 * (samples.length - 1) / 2), 2));
}
}
}