comparison audacious/fft.c @ 0:cb178e5ad177 trunk

[svn] Import audacious source.
author nenolod
date Mon, 24 Oct 2005 03:06:47 -0700
parents
children f12d7e208b43
comparison
equal deleted inserted replaced
-1:000000000000 0:cb178e5ad177
1 /* fft.c: Iterative implementation of a FFT
2 * Copyright (C) 1999 Richard Boulton <richard@tartarus.org>
3 * Convolution stuff by Ralph Loader <suckfish@ihug.co.nz>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 /*
21 * TODO
22 * Remove compiling in of FFT_BUFFER_SIZE? (Might slow things down, but would
23 * be nice to be able to change size at runtime.)
24 * Finish making / checking thread-safety.
25 * More optimisations.
26 */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include "fft.h"
33
34 #include <glib.h>
35 #include <stdlib.h>
36 #include <math.h>
37 #ifndef PI
38 #ifdef M_PI
39 #define PI M_PI
40 #else
41 #define PI 3.14159265358979323846 /* pi */
42 #endif
43 #endif
44
45 /* ########### */
46 /* # Structs # */
47 /* ########### */
48
49 struct _struct_fft_state {
50 /* Temporary data stores to perform FFT in. */
51 float real[FFT_BUFFER_SIZE];
52 float imag[FFT_BUFFER_SIZE];
53 };
54
55 /* ############################# */
56 /* # Local function prototypes # */
57 /* ############################# */
58
59 static void fft_prepare(const sound_sample * input, float *re, float *im);
60 static void fft_calculate(float *re, float *im);
61 static void fft_output(const float *re, const float *im, float *output);
62 static int reverseBits(unsigned int initial);
63
64 /* #################### */
65 /* # Global variables # */
66 /* #################### */
67
68 /* Table to speed up bit reverse copy */
69 static unsigned int bitReverse[FFT_BUFFER_SIZE];
70
71 /* The next two tables could be made to use less space in memory, since they
72 * overlap hugely, but hey. */
73 static float sintable[FFT_BUFFER_SIZE / 2];
74 static float costable[FFT_BUFFER_SIZE / 2];
75
76 /* ############################## */
77 /* # Externally called routines # */
78 /* ############################## */
79
80 /* --------- */
81 /* FFT stuff */
82 /* --------- */
83
84 /*
85 * Initialisation routine - sets up tables and space to work in.
86 * Returns a pointer to internal state, to be used when performing calls.
87 * On error, returns NULL.
88 * The pointer should be freed when it is finished with, by fft_close().
89 */
90 fft_state *
91 fft_init(void)
92 {
93 fft_state *state;
94 unsigned int i;
95
96 state = (fft_state *) g_malloc(sizeof(fft_state));
97 if (!state)
98 return NULL;
99
100 for (i = 0; i < FFT_BUFFER_SIZE; i++) {
101 bitReverse[i] = reverseBits(i);
102 }
103 for (i = 0; i < FFT_BUFFER_SIZE / 2; i++) {
104 float j = 2 * PI * i / FFT_BUFFER_SIZE;
105 costable[i] = cos(j);
106 sintable[i] = sin(j);
107 }
108
109 return state;
110 }
111
112 /*
113 * Do all the steps of the FFT, taking as input sound data (as described in
114 * sound.h) and returning the intensities of each frequency as floats in the
115 * range 0 to ((FFT_BUFFER_SIZE / 2) * 32768) ^ 2
116 *
117 * FIXME - the above range assumes no frequencies present have an amplitude
118 * larger than that of the sample variation. But this is false: we could have
119 * a wave such that its maximums are always between samples, and it's just
120 * inside the representable range at the places samples get taken.
121 * Question: what _is_ the maximum value possible. Twice that value? Root
122 * two times that value? Hmmm. Think it depends on the frequency, too.
123 *
124 * The input array is assumed to have FFT_BUFFER_SIZE elements,
125 * and the output array is assumed to have (FFT_BUFFER_SIZE / 2 + 1) elements.
126 * state is a (non-NULL) pointer returned by fft_init.
127 */
128 void
129 fft_perform(const sound_sample * input, float *output, fft_state * state)
130 {
131 /* Convert data from sound format to be ready for FFT */
132 fft_prepare(input, state->real, state->imag);
133
134 /* Do the actual FFT */
135 fft_calculate(state->real, state->imag);
136
137 /* Convert the FFT output into intensities */
138 fft_output(state->real, state->imag, output);
139 }
140
141 /*
142 * Free the state.
143 */
144 void
145 fft_close(fft_state * state)
146 {
147 if (state)
148 free(state);
149 }
150
151 /* ########################### */
152 /* # Locally called routines # */
153 /* ########################### */
154
155 /*
156 * Prepare data to perform an FFT on
157 */
158 static void
159 fft_prepare(const sound_sample * input, float *re, float *im)
160 {
161 unsigned int i;
162 float *realptr = re;
163 float *imagptr = im;
164
165 /* Get input, in reverse bit order */
166 for (i = 0; i < FFT_BUFFER_SIZE; i++) {
167 *realptr++ = input[bitReverse[i]];
168 *imagptr++ = 0;
169 }
170 }
171
172 /*
173 * Take result of an FFT and calculate the intensities of each frequency
174 * Note: only produces half as many data points as the input had.
175 * This is roughly a consequence of the Nyquist sampling theorm thingy.
176 * (FIXME - make this comment better, and helpful.)
177 *
178 * The two divisions by 4 are also a consequence of this: the contributions
179 * returned for each frequency are split into two parts, one at i in the
180 * table, and the other at FFT_BUFFER_SIZE - i, except for i = 0 and
181 * FFT_BUFFER_SIZE which would otherwise get float (and then 4* when squared)
182 * the contributions.
183 */
184 static void
185 fft_output(const float *re, const float *im, float *output)
186 {
187 float *outputptr = output;
188 const float *realptr = re;
189 const float *imagptr = im;
190 float *endptr = output + FFT_BUFFER_SIZE / 2;
191
192 #ifdef DEBUG
193 unsigned int i, j;
194 #endif
195
196 while (outputptr <= endptr) {
197 *outputptr = (*realptr * *realptr) + (*imagptr * *imagptr);
198 outputptr++;
199 realptr++;
200 imagptr++;
201 }
202 /* Do divisions to keep the constant and highest frequency terms in scale
203 * with the other terms. */
204 *output /= 4;
205 *endptr /= 4;
206
207 #ifdef DEBUG
208 printf("Recalculated input:\n");
209 for (i = 0; i < FFT_BUFFER_SIZE; i++) {
210 float val_real = 0;
211 float val_imag = 0;
212 for (j = 0; j < FFT_BUFFER_SIZE; j++) {
213 float fact_real = cos(-2 * j * i * PI / FFT_BUFFER_SIZE);
214 float fact_imag = sin(-2 * j * i * PI / FFT_BUFFER_SIZE);
215 val_real += fact_real * re[j] - fact_imag * im[j];
216 val_imag += fact_real * im[j] + fact_imag * re[j];
217 }
218 printf("%5d = %8f + i * %8f\n", i,
219 val_real / FFT_BUFFER_SIZE, val_imag / FFT_BUFFER_SIZE);
220 }
221 printf("\n");
222 #endif
223 }
224
225 /*
226 * Actually perform the FFT
227 */
228 static void
229 fft_calculate(float *re, float *im)
230 {
231 unsigned int i, j, k;
232 unsigned int exchanges;
233 float fact_real, fact_imag;
234 float tmp_real, tmp_imag;
235 unsigned int factfact;
236
237 /* Set up some variables to reduce calculation in the loops */
238 exchanges = 1;
239 factfact = FFT_BUFFER_SIZE / 2;
240
241 /* Loop through the divide and conquer steps */
242 for (i = FFT_BUFFER_SIZE_LOG; i != 0; i--) {
243 /* In this step, we have 2 ^ (i - 1) exchange groups, each with
244 * 2 ^ (FFT_BUFFER_SIZE_LOG - i) exchanges
245 */
246 /* Loop through the exchanges in a group */
247 for (j = 0; j != exchanges; j++) {
248 /* Work out factor for this exchange
249 * factor ^ (exchanges) = -1
250 * So, real = cos(j * PI / exchanges),
251 * imag = sin(j * PI / exchanges)
252 */
253 fact_real = costable[j * factfact];
254 fact_imag = sintable[j * factfact];
255
256 /* Loop through all the exchange groups */
257 for (k = j; k < FFT_BUFFER_SIZE; k += exchanges << 1) {
258 int k1 = k + exchanges;
259 /* newval[k] := val[k] + factor * val[k1]
260 * newval[k1] := val[k] - factor * val[k1]
261 **/
262 #ifdef DEBUG
263 printf("%d %d %d\n", i, j, k);
264 printf("Exchange %d with %d\n", k, k1);
265 printf("Factor %9f + i * %8f\n", fact_real, fact_imag);
266 #endif
267 /* FIXME - potential scope for more optimization here? */
268 tmp_real = fact_real * re[k1] - fact_imag * im[k1];
269 tmp_imag = fact_real * im[k1] + fact_imag * re[k1];
270 re[k1] = re[k] - tmp_real;
271 im[k1] = im[k] - tmp_imag;
272 re[k] += tmp_real;
273 im[k] += tmp_imag;
274 #ifdef DEBUG
275 for (k1 = 0; k1 < FFT_BUFFER_SIZE; k1++) {
276 printf("%5d = %8f + i * %8f\n", k1, real[k1], imag[k1]);
277 }
278 #endif
279 }
280 }
281 exchanges <<= 1;
282 factfact >>= 1;
283 }
284 }
285
286 static int
287 reverseBits(unsigned int initial)
288 {
289 unsigned int reversed = 0, loop;
290 for (loop = 0; loop < FFT_BUFFER_SIZE_LOG; loop++) {
291 reversed <<= 1;
292 reversed += (initial & 1);
293 initial >>= 1;
294 }
295 return reversed;
296 }