c@225: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@225: c@225: /* c@225: QM DSP Library c@225: c@225: Centre for Digital Music, Queen Mary, University of London. c@225: This file is based on Don Cross's public domain FFT implementation. c@225: */ c@225: c@225: #include "FFT.h" c@225: #include c@225: c@225: ////////////////////////////////////////////////////////////////////// c@225: // Construction/Destruction c@225: ////////////////////////////////////////////////////////////////////// c@225: c@225: FFT::FFT() c@225: { c@225: c@225: } c@225: c@225: FFT::~FFT() c@225: { c@225: c@225: } c@225: c@225: void FFT::process(unsigned int p_nSamples, bool p_bInverseTransform, double *p_lpRealIn, double *p_lpImagIn, double *p_lpRealOut, double *p_lpImagOut) c@225: { c@225: c@225: if(!p_lpRealIn || !p_lpRealOut || !p_lpImagOut) return; c@225: c@225: c@225: unsigned int NumBits; c@225: unsigned int i, j, k, n; c@225: unsigned int BlockSize, BlockEnd; c@225: c@225: double angle_numerator = 2.0 * M_PI; c@225: double tr, ti; c@225: c@225: if( !isPowerOfTwo(p_nSamples) ) c@225: { c@225: return; c@225: } c@225: c@225: if( p_bInverseTransform ) angle_numerator = -angle_numerator; c@225: c@225: NumBits = numberOfBitsNeeded ( p_nSamples ); c@225: c@225: c@225: for( i=0; i < p_nSamples; i++ ) c@225: { c@225: j = reverseBits ( i, NumBits ); c@225: p_lpRealOut[j] = p_lpRealIn[i]; c@225: p_lpImagOut[j] = (p_lpImagIn == 0) ? 0.0 : p_lpImagIn[i]; c@225: } c@225: c@225: c@225: BlockEnd = 1; c@225: for( BlockSize = 2; BlockSize <= p_nSamples; BlockSize <<= 1 ) c@225: { c@225: double delta_angle = angle_numerator / (double)BlockSize; c@225: double sm2 = -sin ( -2 * delta_angle ); c@225: double sm1 = -sin ( -delta_angle ); c@225: double cm2 = cos ( -2 * delta_angle ); c@225: double cm1 = cos ( -delta_angle ); c@225: double w = 2 * cm1; c@225: double ar[3], ai[3]; c@225: c@225: for( i=0; i < p_nSamples; i += BlockSize ) c@225: { c@225: c@225: ar[2] = cm2; c@225: ar[1] = cm1; c@225: c@225: ai[2] = sm2; c@225: ai[1] = sm1; c@225: c@225: for ( j=i, n=0; n < BlockEnd; j++, n++ ) c@225: { c@225: c@225: ar[0] = w*ar[1] - ar[2]; c@225: ar[2] = ar[1]; c@225: ar[1] = ar[0]; c@225: c@225: ai[0] = w*ai[1] - ai[2]; c@225: ai[2] = ai[1]; c@225: ai[1] = ai[0]; c@225: c@225: k = j + BlockEnd; c@225: tr = ar[0]*p_lpRealOut[k] - ai[0]*p_lpImagOut[k]; c@225: ti = ar[0]*p_lpImagOut[k] + ai[0]*p_lpRealOut[k]; c@225: c@225: p_lpRealOut[k] = p_lpRealOut[j] - tr; c@225: p_lpImagOut[k] = p_lpImagOut[j] - ti; c@225: c@225: p_lpRealOut[j] += tr; c@225: p_lpImagOut[j] += ti; c@225: c@225: } c@225: } c@225: c@225: BlockEnd = BlockSize; c@225: c@225: } c@225: c@225: c@225: if( p_bInverseTransform ) c@225: { c@225: double denom = (double)p_nSamples; c@225: c@225: for ( i=0; i < p_nSamples; i++ ) c@225: { c@225: p_lpRealOut[i] /= denom; c@225: p_lpImagOut[i] /= denom; c@225: } c@225: } c@225: } c@225: c@225: bool FFT::isPowerOfTwo(unsigned int p_nX) c@225: { c@225: if( p_nX < 2 ) return false; c@225: c@225: if( p_nX & (p_nX-1) ) return false; c@225: c@225: return true; c@225: } c@225: c@225: unsigned int FFT::numberOfBitsNeeded(unsigned int p_nSamples) c@225: { c@225: int i; c@225: c@225: if( p_nSamples < 2 ) c@225: { c@225: return 0; c@225: } c@225: c@225: for ( i=0; ; i++ ) c@225: { c@225: if( p_nSamples & (1 << i) ) return i; c@225: } c@225: } c@225: c@225: unsigned int FFT::reverseBits(unsigned int p_nIndex, unsigned int p_nBits) c@225: { c@225: unsigned int i, rev; c@225: c@225: for(i=rev=0; i < p_nBits; i++) c@225: { c@225: rev = (rev << 1) | (p_nIndex & 1); c@225: p_nIndex >>= 1; c@225: } c@225: c@225: return rev; c@225: }