00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "fft.h"
00027
00028 #include <stdlib.h>
00029 #include <math.h>
00030 #ifndef PI
00031 #ifdef M_PI
00032 #define PI M_PI
00033 #else
00034 #define PI 3.14159265358979323846
00035 #endif
00036 #endif
00037
00038
00039
00040
00041 static void fft_prepare(const sound_sample *input, float * re, float * im);
00042 static void fft_calculate(float * re, float * im);
00043 static void fft_output(const float *re, const float *im, float *output);
00044 static int reverseBits(unsigned int initial);
00045
00046
00047
00048 static unsigned int bitReverse[FFT_BUFFER_SIZE];
00049
00050
00051
00052 static float sintable[FFT_BUFFER_SIZE / 2];
00053 static float costable[FFT_BUFFER_SIZE / 2];
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 fft_state *visual_fft_init(void)
00066 {
00067 fft_state *p_state;
00068 unsigned int i;
00069
00070 p_state = (fft_state *) malloc (sizeof(fft_state));
00071 if(! p_state )
00072 return NULL;
00073
00074 for(i = 0; i < FFT_BUFFER_SIZE; i++)
00075 {
00076 bitReverse[i] = reverseBits(i);
00077 }
00078 for(i = 0; i < FFT_BUFFER_SIZE / 2; i++)
00079 {
00080 float j = 2 * PI * i / FFT_BUFFER_SIZE;
00081 costable[i] = cos(j);
00082 sintable[i] = sin(j);
00083 }
00084
00085 return p_state;
00086 }
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 void fft_perform(const sound_sample *input, float *output, fft_state *state) {
00098
00099 fft_prepare(input, state->real, state->imag);
00100
00101
00102 fft_calculate(state->real, state->imag);
00103
00104
00105 fft_output(state->real, state->imag, output);
00106 }
00107
00108
00109
00110
00111 void fft_close(fft_state *state) {
00112 if(state) free(state);
00113 }
00114
00115
00116
00117
00118
00119
00120
00121
00122 static void fft_prepare(const sound_sample *input, float * re, float * im) {
00123 unsigned int i;
00124 float *p_real = re;
00125 float *p_imag = im;
00126
00127
00128 for(i = 0; i < FFT_BUFFER_SIZE; i++)
00129 {
00130 *p_real++ = input[bitReverse[i]];
00131 *p_imag++ = 0;
00132 }
00133 }
00134
00135
00136
00137
00138
00139 static void fft_output(const float * re, const float * im, float *output)
00140 {
00141 float *p_output = output;
00142 const float *p_real = re;
00143 const float *p_imag = im;
00144 float *p_end = output + FFT_BUFFER_SIZE / 2;
00145
00146 while(p_output <= p_end)
00147 {
00148 *p_output = (*p_real * *p_real) + (*p_imag * *p_imag);
00149 p_output++; p_real++; p_imag++;
00150 }
00151
00152
00153 *output /= 4;
00154 *p_end /= 4;
00155 }
00156
00157
00158
00159
00160
00161 static void fft_calculate(float * re, float * im)
00162 {
00163 unsigned int i, j, k;
00164 unsigned int exchanges;
00165 float fact_real, fact_imag;
00166 float tmp_real, tmp_imag;
00167 unsigned int factfact;
00168
00169
00170 exchanges = 1;
00171 factfact = FFT_BUFFER_SIZE / 2;
00172
00173
00174 for(i = FFT_BUFFER_SIZE_LOG; i != 0; i--) {
00175
00176
00177
00178
00179 for(j = 0; j != exchanges; j++) {
00180
00181
00182
00183
00184
00185 fact_real = costable[j * factfact];
00186 fact_imag = sintable[j * factfact];
00187
00188
00189 for(k = j; k < FFT_BUFFER_SIZE; k += exchanges << 1) {
00190 int k1 = k + exchanges;
00191 tmp_real = fact_real * re[k1] - fact_imag * im[k1];
00192 tmp_imag = fact_real * im[k1] + fact_imag * re[k1];
00193 re[k1] = re[k] - tmp_real;
00194 im[k1] = im[k] - tmp_imag;
00195 re[k] += tmp_real;
00196 im[k] += tmp_imag;
00197 }
00198 }
00199 exchanges <<= 1;
00200 factfact >>= 1;
00201 }
00202 }
00203
00204 static int reverseBits(unsigned int initial)
00205 {
00206 unsigned int reversed = 0, loop;
00207 for(loop = 0; loop < FFT_BUFFER_SIZE_LOG; loop++) {
00208 reversed <<= 1;
00209 reversed += (initial & 1);
00210 initial >>= 1;
00211 }
00212 return reversed;
00213 }