forked from bsondermann/Blackbox2Sound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FourierTransform.pde
303 lines (284 loc) · 7.39 KB
/
FourierTransform.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
public abstract class FourierTransform{
public final int NONE = 0;
public final int HAMMING = 1;
protected static final int LINAVG = 2;
protected static final int LOGAVG = 3;
protected static final int NOAVG = 4;
protected static final float TWO_PI = (float) (2 * Math.PI);
protected int timeSize;
protected int sampleRate;
protected float bandWidth;
protected int whichWindow;
protected float[] real;
protected float[] imag;
protected float[] spectrum;
protected float[] averages;
protected int whichAverage;
protected int octaves;
protected int avgPerOctave;
FourierTransform(int ts, float sr)
{
timeSize = ts;
sampleRate = (int)sr;
bandWidth = (2f / timeSize) * ((float)sampleRate / 2f);
noAverages();
allocateArrays();
whichWindow = NONE;
}
protected abstract void allocateArrays();
protected void setComplex(float[] r, float[] i)
{
if (real.length != r.length && imag.length != i.length)
{
throw new IllegalArgumentException( "This won't work" );
} else
{
System.arraycopy(r, 0, real, 0, r.length);
System.arraycopy(i, 0, imag, 0, i.length);
}
}
protected void fillSpectrum()
{
for (int i = 0; i < spectrum.length; i++)
{
spectrum[i] = (float) Math.sqrt(real[i] * real[i] + imag[i] * imag[i]);
}
if (whichAverage == LINAVG)
{
int avgWidth = (int) spectrum.length / averages.length;
for (int i = 0; i < averages.length; i++)
{
float avg = 0;
int j;
for (j = 0; j < avgWidth; j++)
{
int offset = j + i * avgWidth;
if (offset < spectrum.length)
{
avg += spectrum[offset];
} else
{
break;
}
}
avg /= j + 1;
averages[i] = avg;
}
} else if (whichAverage == LOGAVG)
{
for (int i = 0; i < octaves; i++)
{
float lowFreq, hiFreq, freqStep;
if (i == 0)
{
lowFreq = 0;
} else
{
lowFreq = (sampleRate / 2) / (float) Math.pow(2, octaves - i);
}
hiFreq = (sampleRate / 2) / (float) Math.pow(2, octaves - i - 1);
freqStep = (hiFreq - lowFreq) / avgPerOctave;
float f = lowFreq;
for (int j = 0; j < avgPerOctave; j++)
{
int offset = j + i * avgPerOctave;
averages[offset] = calcAvg(f, f + freqStep);
f += freqStep;
}
}
}
}
public void noAverages()
{
averages = new float[0];
whichAverage = NOAVG;
}
public void linAverages(int numAvg)
{
if (numAvg > spectrum.length / 2)
{
throw new IllegalArgumentException("The number of averages for this transform can be at most " + spectrum.length / 2 + ".");
} else
{
averages = new float[numAvg];
}
whichAverage = LINAVG;
}
public void logAverages(int minBandwidth, int bandsPerOctave)
{
float nyq = (float) sampleRate / 2f;
octaves = 1;
while ((nyq /= 2) > minBandwidth)
{
octaves++;
}
avgPerOctave = bandsPerOctave;
averages = new float[octaves * bandsPerOctave];
whichAverage = LOGAVG;
}
public void window(int which)
{
if (which < 0 || which > 1)
{
throw new IllegalArgumentException("Invalid window type.");
} else
{
whichWindow = which;
}
}
protected void doWindow(float[] samples)
{
switch (whichWindow)
{
case HAMMING:
hamming(samples);
break;
}
}
protected void hamming(float[] samples)
{
for (int i = 0; i < samples.length; i++)
{
samples[i] *= (0.54f - 0.46f * Math.cos(TWO_PI * i / (samples.length - 1)));
}
}
public int timeSize()
{
return timeSize;
}
public int specSize()
{
return spectrum.length;
}
public float getBand(int i)
{
if (i < 0) i = 0;
if (i > spectrum.length - 1) i = spectrum.length - 1;
return spectrum[i];
}
public float getBandWidth()
{
return bandWidth;
}
public abstract void setBand(int i, float a);
public abstract void scaleBand(int i, float s);
public int freqToIndex(float freq)
{
if (freq < getBandWidth() / 2) return 0;
if (freq > sampleRate / 2 - getBandWidth() / 2) return spectrum.length - 1;
float fraction = freq / (float) sampleRate;
int i = Math.round(timeSize * fraction);
return i;
}
public float indexToFreq(int i)
{
float bw = getBandWidth();
// special case: the width of the first bin is half that of the others.
// so the center frequency is a quarter of the way.
if ( i == 0 ) return bw * 0.25f;
// special case: the width of the last bin is half that of the others.
if ( i == spectrum.length - 1 )
{
float lastBinBeginFreq = (sampleRate / 2) - (bw / 2);
float binHalfWidth = bw * 0.25f;
return lastBinBeginFreq + binHalfWidth;
}
// the center frequency of the ith band is simply i*bw
// because the first band is half the width of all others.
// treating it as if it wasn't offsets us to the middle
// of the band.
return i*bw;
}
public float getAverageCenterFrequency(int i)
{
if ( whichAverage == LINAVG )
{
int avgWidth = (int) spectrum.length / averages.length;
int centerBinIndex = i*avgWidth + avgWidth/2;
return indexToFreq(centerBinIndex);
} else if ( whichAverage == LOGAVG )
{
int octave = i / avgPerOctave;
int offset = i % avgPerOctave;
float lowFreq, hiFreq, freqStep;
if (octave == 0)
{
lowFreq = 0;
} else
{
lowFreq = (sampleRate / 2) / (float) Math.pow(2, octaves - octave);
}
hiFreq = (sampleRate / 2) / (float) Math.pow(2, octaves - octave - 1);
freqStep = (hiFreq - lowFreq) / avgPerOctave;
float f = lowFreq + offset*freqStep;
return f + freqStep/2;
}
return 0;
}
public float getFreq(float freq)
{
return getBand(freqToIndex(freq));
}
public void setFreq(float freq, float a)
{
setBand(freqToIndex(freq), a);
}
public void scaleFreq(float freq, float s)
{
scaleBand(freqToIndex(freq), s);
}
public int avgSize()
{
return averages.length;
}
public float getAvg(int i)
{
float ret;
if (averages.length > 0)
ret = averages[i];
else
ret = 0;
return ret;
}
public float calcAvg(float lowFreq, float hiFreq)
{
int lowBound = freqToIndex(lowFreq);
int hiBound = freqToIndex(hiFreq);
float avg = 0;
for (int i = lowBound; i <= hiBound; i++)
{
avg += spectrum[i];
}
avg /= (hiBound - lowBound + 1);
return avg;
}
public abstract void forward(float[] buffer);
public void forward(float[] buffer, int startAt)
{
if ( buffer.length - startAt < timeSize )
{
throw new IllegalArgumentException( "FourierTransform.forward: not enough samples in the buffer between " + startAt + " and " + buffer.length + " to perform a transform." );
}
float[] section = new float[timeSize];
System.arraycopy(buffer, startAt, section, 0, section.length);
forward(section);
}
public abstract void inverse(float[] buffer);
public void inverse(float[] freqReal, float[] freqImag, float[] buffer)
{
setComplex(freqReal, freqImag);
inverse(buffer);
}
public float[] getSpectrum( )
{
return spectrum;
}
public float[] getRealPart( )
{
return real;
}
public float[] getImaginaryPart( )
{
return imag;
}
}