void FFT::process(bool p_bInverseTransform, const double *p_lpRealIn, const double *p_lpImagIn, double *p_lpRealOut, double *p_lpImagOut) { if (!p_lpRealIn || !p_lpRealOut || !p_lpImagOut) return; // std::cerr << "FFT::process(" << m_n << "," << p_bInverseTransform << ")" << std::endl; unsigned int NumBits; unsigned int i, j, k, n; unsigned int BlockSize, BlockEnd; double angle_numerator = 2.0 * M_PI; double tr, ti; if( !MathUtilities::isPowerOfTwo(m_n) ) { std::cerr << "ERROR: FFT::process: Non-power-of-two FFT size " << m_n << " not supported in this implementation" << std::endl; return; } if( p_bInverseTransform ) angle_numerator = -angle_numerator; NumBits = numberOfBitsNeeded ( m_n ); for( i=0; i < m_n; i++ ) { j = reverseBits ( i, NumBits ); p_lpRealOut[j] = p_lpRealIn[i]; p_lpImagOut[j] = (p_lpImagIn == 0) ? 0.0 : p_lpImagIn[i]; } BlockEnd = 1; for( BlockSize = 2; BlockSize <= m_n; BlockSize <<= 1 ) { double delta_angle = angle_numerator / (double)BlockSize; double sm2 = -sin ( -2 * delta_angle ); double sm1 = -sin ( -delta_angle ); double cm2 = cos ( -2 * delta_angle ); double cm1 = cos ( -delta_angle ); double w = 2 * cm1; double ar[3], ai[3]; for( i=0; i < m_n; i += BlockSize ) { ar[2] = cm2; ar[1] = cm1; ai[2] = sm2; ai[1] = sm1; for ( j=i, n=0; n < BlockEnd; j++, n++ ) { ar[0] = w*ar[1] - ar[2]; ar[2] = ar[1]; ar[1] = ar[0]; ai[0] = w*ai[1] - ai[2]; ai[2] = ai[1]; ai[1] = ai[0]; k = j + BlockEnd; tr = ar[0]*p_lpRealOut[k] - ai[0]*p_lpImagOut[k]; ti = ar[0]*p_lpImagOut[k] + ai[0]*p_lpRealOut[k]; p_lpRealOut[k] = p_lpRealOut[j] - tr; p_lpImagOut[k] = p_lpImagOut[j] - ti; p_lpRealOut[j] += tr; p_lpImagOut[j] += ti; } } BlockEnd = BlockSize; } if( p_bInverseTransform ) { double denom = (double)m_n; for ( i=0; i < m_n; i++ ) { p_lpRealOut[i] /= denom; p_lpImagOut[i] /= denom; } } }
bool FastFourierTransform::FFT(int NumSamples,bool InverseTransform,double *RealIn, double *ImagIn, double *RealOut, double *ImagOut){ int NumBits; /* Number of bits needed to store indices */ int i, j, k, n; int BlockSize, BlockEnd; double angle_numerator = 2.0 * PI; double tr, ti; /* temp real, temp imaginary */ if ( !isPowerOfTwo(NumSamples) ) { fprintf(stderr, "%d is not a power of two\n", NumSamples); return false; } if (!gFFTBitTable) initFFT(); if (InverseTransform) angle_numerator = -angle_numerator; NumBits = numberOfBitsNeeded(NumSamples); //Simultaneously data copy and bit-reversal ordering into outputs... for(i = 0; i < NumSamples; i++) { j = fastReverseBits(i, NumBits); RealOut[j] = RealIn[i]; ImagOut[j] = (ImagIn == NULL) ? 0.0 : ImagIn[i]; } //Do the FFT BlockEnd = 1; for (BlockSize = 2; BlockSize <= NumSamples; BlockSize <<= 1) { double delta_angle = angle_numerator / (double) BlockSize; double sm2 = sin(-2 * delta_angle); double sm1 = sin(-delta_angle); double cm2 = cos(-2 * delta_angle); double cm1 = cos(-delta_angle); double w = 2 * cm1; double ar0, ar1, ar2, ai0, ai1, ai2; for (i = 0; i < NumSamples; i += BlockSize) { ar2 = cm2; ar1 = cm1; ai2 = sm2; ai1 = sm1; for (j = i, n = 0; n < BlockEnd; j++, n++) { ar0 = w * ar1 - ar2; ar2 = ar1; ar1 = ar0; ai0 = w * ai1 - ai2; ai2 = ai1; ai1 = ai0; k = j + BlockEnd; tr = ar0 * RealOut[k] - ai0 * ImagOut[k]; ti = ar0 * ImagOut[k] + ai0 * RealOut[k]; RealOut[k] = RealOut[j] - tr; ImagOut[k] = ImagOut[j] - ti; RealOut[j] += tr; ImagOut[j] += ti; } } BlockEnd = BlockSize; } //Need to normalize the results if we are computing the inverse transform if( InverseTransform ){ double denom = (double) NumSamples; for(i = 0; i < NumSamples; i++) { RealOut[i] /= denom; ImagOut[i] /= denom; } } return true; }