/********************************************************************** FFT.cpp Dominic Mazzoni September 2000 *******************************************************************//*! \file FFT.cpp \brief Fast Fourier Transform routines. This file contains a few FFT routines, including a real-FFT routine that is almost twice as fast as a normal complex FFT, and a power spectrum routine when you know you don't care about phase information. Some of this code was based on a free implementation of an FFT by Don Cross, available on the web at: http://www.intersrv.com/~dcross/fft.html The basic algorithm for his code was based on Numerican Recipes in Fortran. I optimized his code further by reducing array accesses, caching the bit reversal table, and eliminating float-to-double conversions, and I added the routines to calculate a real FFT and a real power spectrum. *//*******************************************************************/ #include #include #include #include "FFT.h" #include "RealFFTf.h" void DeinitFFT() { // Deallocate any unused RealFFTf tables CleanupFFT(); } /* * Real Fast Fourier Transform * */ void RealFFT(int NumSamples, float *RealIn, float *RealOut, float *ImagOut) { // Remap to RealFFTf() function int i; HFFT hFFT = GetFFT(NumSamples); float *pFFT = new float[NumSamples]; // Copy the data into the processing buffer for(i=0; iBitReversed[i] ]; ImagOut[i]=pFFT[hFFT->BitReversed[i]+1]; } // Handle the (real-only) DC and Fs/2 bins RealOut[0] = pFFT[0]; RealOut[i] = pFFT[1]; ImagOut[0] = ImagOut[i] = 0; // Fill in the upper half using symmetry properties for(i++ ; iBitReversed[i]]; } // Handle the (real-only) DC Out[0] = pFFT[0]; delete [] pFFT; ReleaseFFT(hFFT); } /* * PowerSpectrum * * This function computes the same as RealFFT, above, but * adds the squares of the real and imaginary part of each * coefficient, extracting the power and throwing away the * phase. * * For speed, it does not call RealFFT, but duplicates some * of its code. */ void PowerSpectrum(int NumSamples, float *In, float *Out) { // Remap to RealFFTf() function int i; HFFT hFFT = GetFFT(NumSamples); float *pFFT = new float[NumSamples]; // Copy the data into the processing buffer for(i=0; iBitReversed[i] ]*pFFT[hFFT->BitReversed[i] ]) + (pFFT[hFFT->BitReversed[i]+1]*pFFT[hFFT->BitReversed[i]+1]); } // Handle the (real-only) DC and Fs/2 bins Out[0] = pFFT[0]*pFFT[0]; Out[i] = pFFT[1]*pFFT[1]; delete [] pFFT; ReleaseFFT(hFFT); }