forked from kichiki/WaoN
/
fft.c
429 lines (371 loc) · 9.97 KB
/
fft.c
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/* FFT subroutine for WaoN with FFTW library
* Copyright (C) 1998-2013 Kengo Ichiki <kengoichiki@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <math.h>
#include <stdlib.h> /* realloc() */
#include <stdio.h> /* fprintf() */
/* FFTW library */
#ifdef FFTW2
#include <rfftw.h>
#else // FFTW3
#include <fftw3.h>
#endif // FFTW2
#include "memory-check.h" // CHECK_MALLOC() macro
#include "hc.h" // HC_to_amp2()
/* Reference: "Numerical Recipes in C" 2nd Ed.
* by W.H.Press, S.A.Teukolsky, W.T.Vetterling, B.P.Flannery
* (1992) Cambridge University Press.
* ISBN 0-521-43108-5
* Sec.13.4 - Data Windowing
*/
double
parzen (int i, int nn)
{
return (1.0 - fabs (((double)i-0.5*(double)(nn-1))
/(0.5*(double)(nn+1))));
}
double
welch (int i, int nn)
{
return (1.0-(((double)i-0.5*(double)(nn-1))
/(0.5*(double)(nn+1)))
*(((double)i-0.5*(double)(nn-1))
/(0.5*(double)(nn+1))));
}
double
hanning (int i, int nn)
{
return ( 0.5 * (1.0 - cos (2.0*M_PI*(double)i/(double)(nn-1))) );
}
/* Reference: "Digital Filters and Signal Processing" 2nd Ed.
* by L. B. Jackson. (1989) Kluwer Academic Publishers.
* ISBN 0-89838-276-9
* Sec.7.3 - Windows in Spectrum Analysis
*/
double
hamming (int i, int nn)
{
return ( 0.54 - 0.46 * cos (2.0*M_PI*(double)i/(double)(nn-1)) );
}
double
blackman (int i, int nn)
{
return ( 0.42 - 0.5 * cos (2.0*M_PI*(double)i/(double)(nn-1))
+ 0.08 * cos (4.0*M_PI*(double)i/(double)(nn-1)) );
}
double
steeper (int i, int nn)
{
return ( 0.375
- 0.5 * cos (2.0*M_PI*(double)i/(double)(nn-1))
+ 0.125 * cos (4.0*M_PI*(double)i/(double)(nn-1)) );
}
/* apply window function to data[]
* INPUT
* flag_window : 0 : no-window (default -- that is, other than 1 ~ 6)
* 1 : parzen window
* 2 : welch window
* 3 : hanning window
* 4 : hamming window
* 5 : blackman window
* 6 : steeper 30-dB/octave rolloff window
*/
void
windowing (int n, const double *data, int flag_window, double scale,
double *out)
{
int i;
for (i = 0; i < n; i ++)
{
switch (flag_window)
{
case 1: // parzen window
out [i] = data [i] * parzen (i, n) / scale;
break;
case 2: // welch window
out [i] = data [i] * welch (i, n) / scale;
break;
case 3: // hanning window
out [i] = data [i] * hanning (i, n) / scale;
break;
case 4: // hamming window
out [i] = data [i] * hamming (i, n) / scale;
break;
case 5: // blackman window
out [i] = data [i] * blackman (i, n) / scale;
break;
case 6: // steeper 30-dB/octave rolloff window
out [i] = data [i] * steeper (i, n) / scale;
break;
default:
fprintf (stderr, "invalid flag_window\n");
case 0: // square (no window)
out [i] = data [i] / scale;
break;
}
}
}
void
fprint_window_name (FILE *out, int flag_window)
{
switch (flag_window)
{
case 0: // square (no window)
fprintf (stderr, "no window\n");
break;
case 1: // parzen window
fprintf (stderr, "parzen window\n");
break;
case 2: // welch window
fprintf (stderr, "welch window\n");
break;
case 3: // hanning window
fprintf (stderr, "hanning window\n");
break;
case 4: // hamming window
fprintf (stderr, "hamming window\n");
break;
case 5: // blackman window
fprintf (stderr, "blackman window\n");
break;
case 6: // steeper 30-dB/octave rolloff window
fprintf (stderr, "steeper 30-dB/octave rolloff window\n");
break;
default:
fprintf (stderr, "invalid window\n");
break;
}
}
/* apply FFT with the window and return amplitude and phase
* this is a wrapper mainly for phase vocoder process
* INPUT
* len : FFT length
* data[len] : data to analyze
* flag_window : window type
* plan, in[len], out[len] : for FFTW3
* scale : amplitude scale factor
* OUTPUT
* amp[len/2+1] : amplitude multiplied by the factor "scale" above
* phs[len/2+1] : phase
*/
void
apply_FFT (int len, const double *data, int flag_window,
fftw_plan plan, double *in, double *out,
double scale,
double *amp, double *phs)
{
int i;
windowing (len, data, flag_window, 1.0, in);
fftw_execute (plan); // FFT: in[] -> out[]
HC_to_polar (len, out, 0, amp, phs); // freq[] -> (amp, phs)
// some scaling
for (i = 0; i < (len/2)+1; i ++)
{
amp [i] /= scale;
}
}
/* prepare window for FFT
* INPUT
* n : # of samples for FFT
* flag_window : 0 : no-window (default -- that is, other than 1 ~ 6)
* 1 : parzen window
* 2 : welch window
* 3 : hanning window
* 4 : hamming window
* 5 : blackman window
* 6 : steeper 30-dB/octave rolloff window
* OUTPUT
* density factor as RETURN VALUE
*/
double
init_den (int n, char flag_window)
{
double den;
int i;
den = 0.0;
for (i = 0; i < n; i ++)
{
switch (flag_window)
{
case 1: // parzen window
den += parzen (i, n) * parzen (i, n);
break;
case 2: // welch window
den += welch (i, n) * welch (i, n);
break;
case 3: // hanning window
den += hanning (i, n) * hanning (i, n);
break;
case 4: // hamming window
den += hamming (i, n) * hamming (i, n);
break;
case 5: // blackman window
den += blackman (i, n) * blackman (i, n);
break;
case 6: // steeper 30-dB/octave rolloff window
den += steeper (i, n) * steeper (i, n);
break;
default:
fprintf (stderr, "invalid flag_window\n");
case 0: // square (no window)
den += 1.0;
break;
}
}
den *= (double)n;
return den;
}
/* calc power spectrum of real data x[n]
* INPUT
* n : # of data in x
* x[] : data
* y[] : for output (you have to allocate before calling)
* den : weight of window function; calculated by init_den().
* flag_window : 0 : no-window (default -- that is, other than 1 ~ 6)
* 1 : parzen window
* 2 : welch window
* 3 : hanning window
* 4 : hamming window
* 5 : blackman window
* 6 : steeper 30-dB/octave rolloff window
* OUTPUT
* y[] : fourier transform of x[]
* p[(n+1)/2] : stored only n/2 data
*/
void
power_spectrum_fftw (int n, double *x, double *y, double *p,
double den,
char flag_window,
#ifdef FFTW2
rfftw_plan plan)
#else // FFTW3
fftw_plan plan)
#endif // FFTW2
{
static double maxamp = 2147483647.0; /* 2^32-1 */
/* window */
windowing (n, x, flag_window, maxamp, x);
/* FFTW library */
#ifdef FFTW2
rfftw_one (plan, x, y);
#else // FFTW3
fftw_execute (plan); // x[] -> y[]
#endif
HC_to_amp2 (n, y, den, p);
}
/* subtract average from the power spectrum
* -- intend to remove non-tonal signal (such as drums, percussions)
* INPUT
* n : FFT size
* p[(n+1)/2] : power spectrum
* m : number of bins to average out
* factor : factor * average is subtracted from the power
* (factor = 0.0) means no subtraction
* (factor = 1.0) means full subtraction of the average
* (factor = 2.0) means over subtraction
* OUTPUT
* p[(n+1)/2] : subtracted power spectrum
*/
void
power_subtract_ave (int n, double *p, int m, double factor)
{
int nlen = n/2+1;
int i;
int k;
int nave;
static double *ave = NULL;
static int n_ave = 0;
if (ave == NULL)
{
ave = (double *)malloc (sizeof (double) * nlen);
CHECK_MALLOC (ave, "power_subtract_ave");
n_ave = nlen;
}
else if (n_ave < nlen)
{
ave = (double *)realloc (ave, sizeof (double) * nlen);
CHECK_MALLOC (ave, "power_subtract_ave");
n_ave = nlen;
}
for (i = 0; i < nlen; i ++) // full span
{
ave [i] = 0.0;
nave = 0;
for (k = -m; k <= m; k ++)
{
if ((i + k) < 0 || (i + k) >= nlen) continue;
ave [i] += p [i+k];
nave ++;
}
if (nave > 0) ave [i] /= (double)nave;
}
for (i = 0; i < nlen; i ++) // full span
{
p [i] = sqrt (p[i]) - factor * sqrt (ave [i]);
if (p [i] < 0.0) p [i] = 0.0;
else p [i] = p [i] * p [i];
}
//free (ave);
}
/* octave remover
* INPUT
* n : FFT size
* p[(n+1)/2] : power spectrum
* factor : factor * average is subtracted from the power
* (factor = 0.0) means no subtraction
* (factor = 1.0) means full subtraction of the average
* (factor = 2.0) means over subtraction
* OUTPUT
* p[(n+1)/2] : subtracted power spectrum
*/
void
power_subtract_octave (int n, double *p, double factor)
{
int nlen = (n+1)/2;
int i;
int i2;
static double *oct = NULL;
static int n_oct = 0;
if (oct == NULL)
{
oct = (double *)malloc (sizeof (double) * (n/2+1));
CHECK_MALLOC (oct, "power_subtract_octave");
n_oct = n/2+1;
}
else if (n_oct < (n/2+1))
{
oct = (double *)realloc (oct, sizeof (double) * (n/2+1));
CHECK_MALLOC (oct, "power_subtract_octave");
n_oct = n/2+1;
}
oct [0] = p [0];
for (i = 1; i < nlen/2+1; i ++)
{
i2 = i * 2;
if (i2 >= n/2+1) break;
oct [i2] = factor * p[i];
if (i2-1 > 0) oct [i2-1] = 0.5 * factor * p[i];
if (i2+1 < nlen) oct [i2+1] = 0.5 * factor * p[i];
}
for (i = 0; i < nlen; i ++) // full span
{
p [i] = sqrt (p[i]) - factor * sqrt (oct [i]);
if (p [i] < 0.0) p [i] = 0.0;
else p [i] = p [i] * p [i];
}
//free (oct);
}