Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

fft.c

00001 /*****************************************************************************
00002  * fft.c: Iterative implementation of a FFT
00003  *****************************************************************************
00004  * $Id: fft.c 10101 2005-03-02 16:47:31Z robux4 $
00005  *
00006  * Mainly taken from XMMS's code
00007  *
00008  * Authors: Richard Boulton <[email protected]>
00009  *          Ralph Loader <[email protected]>
00010  *
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
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  /* pi */
00035  #endif
00036 #endif
00037 
00038 /******************************************************************************
00039  * Local prototypes
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 /* Table to speed up bit reverse copy */
00048 static unsigned int bitReverse[FFT_BUFFER_SIZE];
00049 
00050 /* The next two tables could be made to use less space in memory, since they
00051  * overlap hugely, but hey. */
00052 static float sintable[FFT_BUFFER_SIZE / 2];
00053 static float costable[FFT_BUFFER_SIZE / 2];
00054 
00055 /*****************************************************************************
00056  * These functions are the ones called externally
00057  *****************************************************************************/
00058 
00059 /*
00060  * Initialisation routine - sets up tables and space to work in.
00061  * Returns a pointer to internal state, to be used when performing calls.
00062  * On error, returns NULL.
00063  * The pointer should be freed when it is finished with, by fft_close().
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  * Do all the steps of the FFT, taking as input sound data (as described in
00090  * sound.h) and returning the intensities of each frequency as floats in the
00091  * range 0 to ((FFT_BUFFER_SIZE / 2) * 32768) ^ 2
00092  *
00093  * The input array is assumed to have FFT_BUFFER_SIZE elements,
00094  * and the output array is assumed to have (FFT_BUFFER_SIZE / 2 + 1) elements.
00095  * state is a (non-NULL) pointer returned by visual_fft_init.
00096  */
00097 void fft_perform(const sound_sample *input, float *output, fft_state *state) {
00098     /* Convert data from sound format to be ready for FFT */
00099     fft_prepare(input, state->real, state->imag);
00100 
00101     /* Do the actual FFT */
00102     fft_calculate(state->real, state->imag);
00103 
00104     /* Convert the FFT output into intensities */
00105     fft_output(state->real, state->imag, output);
00106 }
00107 
00108 /*
00109  * Free the state.
00110  */
00111 void fft_close(fft_state *state) {
00112     if(state) free(state);
00113 }
00114 
00115 /*****************************************************************************
00116  * These functions are called from the other ones
00117  *****************************************************************************/
00118 
00119 /*
00120  * Prepare data to perform an FFT on
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     /* Get input, in reverse bit order */
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  * Take result of an FFT and calculate the intensities of each frequency
00137  * Note: only produces half as many data points as the input had.
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     /* Do divisions to keep the constant and highest frequency terms in scale
00152      * with the other terms. */
00153     *output /= 4;
00154     *p_end /= 4;
00155 }
00156 
00157 
00158 /*
00159  * Actually perform the FFT
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     /* Set up some variables to reduce calculation in the loops */
00170     exchanges = 1;
00171     factfact = FFT_BUFFER_SIZE / 2;
00172 
00173     /* Loop through the divide and conquer steps */
00174     for(i = FFT_BUFFER_SIZE_LOG; i != 0; i--) {
00175         /* In this step, we have 2 ^ (i - 1) exchange groups, each with
00176          * 2 ^ (FFT_BUFFER_SIZE_LOG - i) exchanges
00177          */
00178         /* Loop through the exchanges in a group */
00179         for(j = 0; j != exchanges; j++) {
00180             /* Work out factor for this exchange
00181              * factor ^ (exchanges) = -1
00182              * So, real = cos(j * PI / exchanges),
00183              *     imag = sin(j * PI / exchanges)
00184              */
00185             fact_real = costable[j * factfact];
00186             fact_imag = sintable[j * factfact];
00187 
00188             /* Loop through all the exchange groups */
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 }

Generated on Tue Dec 20 10:14:59 2005 for vlc-0.8.4a by  doxygen 1.4.2