ei_kissfft_impl.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2009 Mark Borgerding mark a borgerding net
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 namespace Eigen { 
00011 
00012 namespace internal {
00013 
00014   // This FFT implementation was derived from kissfft http:sourceforge.net/projects/kissfft
00015   // Copyright 2003-2009 Mark Borgerding
00016 
00017 template <typename _Scalar>
00018 struct kiss_cpx_fft
00019 {
00020   typedef _Scalar Scalar;
00021   typedef std::complex<Scalar> Complex;
00022   std::vector<Complex> m_twiddles;
00023   std::vector<int> m_stageRadix;
00024   std::vector<int> m_stageRemainder;
00025   std::vector<Complex> m_scratchBuf;
00026   bool m_inverse;
00027 
00028   inline
00029     void make_twiddles(int nfft,bool inverse)
00030     {
00031       m_inverse = inverse;
00032       m_twiddles.resize(nfft);
00033       Scalar phinc =  (inverse?2:-2)* acos( (Scalar) -1)  / nfft;
00034       for (int i=0;i<nfft;++i)
00035         m_twiddles[i] = exp( Complex(0,i*phinc) );
00036     }
00037 
00038   void factorize(int nfft)
00039   {
00040     //start factoring out 4's, then 2's, then 3,5,7,9,...
00041     int n= nfft;
00042     int p=4;
00043     do {
00044       while (n % p) {
00045         switch (p) {
00046           case 4: p = 2; break;
00047           case 2: p = 3; break;
00048           default: p += 2; break;
00049         }
00050         if (p*p>n)
00051           p=n;// impossible to have a factor > sqrt(n)
00052       }
00053       n /= p;
00054       m_stageRadix.push_back(p);
00055       m_stageRemainder.push_back(n);
00056       if ( p > 5 )
00057         m_scratchBuf.resize(p); // scratchbuf will be needed in bfly_generic
00058     }while(n>1);
00059   }
00060 
00061   template <typename _Src>
00062     inline
00063     void work( int stage,Complex * xout, const _Src * xin, size_t fstride,size_t in_stride)
00064     {
00065       int p = m_stageRadix[stage];
00066       int m = m_stageRemainder[stage];
00067       Complex * Fout_beg = xout;
00068       Complex * Fout_end = xout + p*m;
00069 
00070       if (m>1) {
00071         do{
00072           // recursive call:
00073           // DFT of size m*p performed by doing
00074           // p instances of smaller DFTs of size m, 
00075           // each one takes a decimated version of the input
00076           work(stage+1, xout , xin, fstride*p,in_stride);
00077           xin += fstride*in_stride;
00078         }while( (xout += m) != Fout_end );
00079       }else{
00080         do{
00081           *xout = *xin;
00082           xin += fstride*in_stride;
00083         }while(++xout != Fout_end );
00084       }
00085       xout=Fout_beg;
00086 
00087       // recombine the p smaller DFTs 
00088       switch (p) {
00089         case 2: bfly2(xout,fstride,m); break;
00090         case 3: bfly3(xout,fstride,m); break;
00091         case 4: bfly4(xout,fstride,m); break;
00092         case 5: bfly5(xout,fstride,m); break;
00093         default: bfly_generic(xout,fstride,m,p); break;
00094       }
00095     }
00096 
00097   inline
00098     void bfly2( Complex * Fout, const size_t fstride, int m)
00099     {
00100       for (int k=0;k<m;++k) {
00101         Complex t = Fout[m+k] * m_twiddles[k*fstride];
00102         Fout[m+k] = Fout[k] - t;
00103         Fout[k] += t;
00104       }
00105     }
00106 
00107   inline
00108     void bfly4( Complex * Fout, const size_t fstride, const size_t m)
00109     {
00110       Complex scratch[6];
00111       int negative_if_inverse = m_inverse * -2 +1;
00112       for (size_t k=0;k<m;++k) {
00113         scratch[0] = Fout[k+m] * m_twiddles[k*fstride];
00114         scratch[1] = Fout[k+2*m] * m_twiddles[k*fstride*2];
00115         scratch[2] = Fout[k+3*m] * m_twiddles[k*fstride*3];
00116         scratch[5] = Fout[k] - scratch[1];
00117 
00118         Fout[k] += scratch[1];
00119         scratch[3] = scratch[0] + scratch[2];
00120         scratch[4] = scratch[0] - scratch[2];
00121         scratch[4] = Complex( scratch[4].imag()*negative_if_inverse , -scratch[4].real()* negative_if_inverse );
00122 
00123         Fout[k+2*m]  = Fout[k] - scratch[3];
00124         Fout[k] += scratch[3];
00125         Fout[k+m] = scratch[5] + scratch[4];
00126         Fout[k+3*m] = scratch[5] - scratch[4];
00127       }
00128     }
00129 
00130   inline
00131     void bfly3( Complex * Fout, const size_t fstride, const size_t m)
00132     {
00133       size_t k=m;
00134       const size_t m2 = 2*m;
00135       Complex *tw1,*tw2;
00136       Complex scratch[5];
00137       Complex epi3;
00138       epi3 = m_twiddles[fstride*m];
00139 
00140       tw1=tw2=&m_twiddles[0];
00141 
00142       do{
00143         scratch[1]=Fout[m] * *tw1;
00144         scratch[2]=Fout[m2] * *tw2;
00145 
00146         scratch[3]=scratch[1]+scratch[2];
00147         scratch[0]=scratch[1]-scratch[2];
00148         tw1 += fstride;
00149         tw2 += fstride*2;
00150         Fout[m] = Complex( Fout->real() - Scalar(.5)*scratch[3].real() , Fout->imag() - Scalar(.5)*scratch[3].imag() );
00151         scratch[0] *= epi3.imag();
00152         *Fout += scratch[3];
00153         Fout[m2] = Complex(  Fout[m].real() + scratch[0].imag() , Fout[m].imag() - scratch[0].real() );
00154         Fout[m] += Complex( -scratch[0].imag(),scratch[0].real() );
00155         ++Fout;
00156       }while(--k);
00157     }
00158 
00159   inline
00160     void bfly5( Complex * Fout, const size_t fstride, const size_t m)
00161     {
00162       Complex *Fout0,*Fout1,*Fout2,*Fout3,*Fout4;
00163       size_t u;
00164       Complex scratch[13];
00165       Complex * twiddles = &m_twiddles[0];
00166       Complex *tw;
00167       Complex ya,yb;
00168       ya = twiddles[fstride*m];
00169       yb = twiddles[fstride*2*m];
00170 
00171       Fout0=Fout;
00172       Fout1=Fout0+m;
00173       Fout2=Fout0+2*m;
00174       Fout3=Fout0+3*m;
00175       Fout4=Fout0+4*m;
00176 
00177       tw=twiddles;
00178       for ( u=0; u<m; ++u ) {
00179         scratch[0] = *Fout0;
00180 
00181         scratch[1]  = *Fout1 * tw[u*fstride];
00182         scratch[2]  = *Fout2 * tw[2*u*fstride];
00183         scratch[3]  = *Fout3 * tw[3*u*fstride];
00184         scratch[4]  = *Fout4 * tw[4*u*fstride];
00185 
00186         scratch[7] = scratch[1] + scratch[4];
00187         scratch[10] = scratch[1] - scratch[4];
00188         scratch[8] = scratch[2] + scratch[3];
00189         scratch[9] = scratch[2] - scratch[3];
00190 
00191         *Fout0 +=  scratch[7];
00192         *Fout0 +=  scratch[8];
00193 
00194         scratch[5] = scratch[0] + Complex(
00195             (scratch[7].real()*ya.real() ) + (scratch[8].real() *yb.real() ),
00196             (scratch[7].imag()*ya.real()) + (scratch[8].imag()*yb.real())
00197             );
00198 
00199         scratch[6] = Complex(
00200             (scratch[10].imag()*ya.imag()) + (scratch[9].imag()*yb.imag()),
00201             -(scratch[10].real()*ya.imag()) - (scratch[9].real()*yb.imag())
00202             );
00203 
00204         *Fout1 = scratch[5] - scratch[6];
00205         *Fout4 = scratch[5] + scratch[6];
00206 
00207         scratch[11] = scratch[0] +
00208           Complex(
00209               (scratch[7].real()*yb.real()) + (scratch[8].real()*ya.real()),
00210               (scratch[7].imag()*yb.real()) + (scratch[8].imag()*ya.real())
00211               );
00212 
00213         scratch[12] = Complex(
00214             -(scratch[10].imag()*yb.imag()) + (scratch[9].imag()*ya.imag()),
00215             (scratch[10].real()*yb.imag()) - (scratch[9].real()*ya.imag())
00216             );
00217 
00218         *Fout2=scratch[11]+scratch[12];
00219         *Fout3=scratch[11]-scratch[12];
00220 
00221         ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4;
00222       }
00223     }
00224 
00225   /* perform the butterfly for one stage of a mixed radix FFT */
00226   inline
00227     void bfly_generic(
00228         Complex * Fout,
00229         const size_t fstride,
00230         int m,
00231         int p
00232         )
00233     {
00234       int u,k,q1,q;
00235       Complex * twiddles = &m_twiddles[0];
00236       Complex t;
00237       int Norig = static_cast<int>(m_twiddles.size());
00238       Complex * scratchbuf = &m_scratchBuf[0];
00239 
00240       for ( u=0; u<m; ++u ) {
00241         k=u;
00242         for ( q1=0 ; q1<p ; ++q1 ) {
00243           scratchbuf[q1] = Fout[ k  ];
00244           k += m;
00245         }
00246 
00247         k=u;
00248         for ( q1=0 ; q1<p ; ++q1 ) {
00249           int twidx=0;
00250           Fout[ k ] = scratchbuf[0];
00251           for (q=1;q<p;++q ) {
00252             twidx += static_cast<int>(fstride) * k;
00253             if (twidx>=Norig) twidx-=Norig;
00254             t=scratchbuf[q] * twiddles[twidx];
00255             Fout[ k ] += t;
00256           }
00257           k += m;
00258         }
00259       }
00260     }
00261 };
00262 
00263 template <typename _Scalar>
00264 struct kissfft_impl
00265 {
00266   typedef _Scalar Scalar;
00267   typedef std::complex<Scalar> Complex;
00268 
00269   void clear() 
00270   {
00271     m_plans.clear();
00272     m_realTwiddles.clear();
00273   }
00274 
00275   inline
00276     void fwd( Complex * dst,const Complex *src,int nfft)
00277     {
00278       get_plan(nfft,false).work(0, dst, src, 1,1);
00279     }
00280 
00281   inline
00282     void fwd2( Complex * dst,const Complex *src,int n0,int n1)
00283     {
00284         EIGEN_UNUSED_VARIABLE(dst);
00285         EIGEN_UNUSED_VARIABLE(src);
00286         EIGEN_UNUSED_VARIABLE(n0);
00287         EIGEN_UNUSED_VARIABLE(n1);
00288     }
00289 
00290   inline
00291     void inv2( Complex * dst,const Complex *src,int n0,int n1)
00292     {
00293         EIGEN_UNUSED_VARIABLE(dst);
00294         EIGEN_UNUSED_VARIABLE(src);
00295         EIGEN_UNUSED_VARIABLE(n0);
00296         EIGEN_UNUSED_VARIABLE(n1);
00297     }
00298 
00299   // real-to-complex forward FFT
00300   // perform two FFTs of src even and src odd
00301   // then twiddle to recombine them into the half-spectrum format
00302   // then fill in the conjugate symmetric half
00303   inline
00304     void fwd( Complex * dst,const Scalar * src,int nfft) 
00305     {
00306       if ( nfft&3  ) {
00307         // use generic mode for odd
00308         m_tmpBuf1.resize(nfft);
00309         get_plan(nfft,false).work(0, &m_tmpBuf1[0], src, 1,1);
00310         std::copy(m_tmpBuf1.begin(),m_tmpBuf1.begin()+(nfft>>1)+1,dst );
00311       }else{
00312         int ncfft = nfft>>1;
00313         int ncfft2 = nfft>>2;
00314         Complex * rtw = real_twiddles(ncfft2);
00315 
00316         // use optimized mode for even real
00317         fwd( dst, reinterpret_cast<const Complex*> (src), ncfft);
00318         Complex dc = dst[0].real() +  dst[0].imag();
00319         Complex nyquist = dst[0].real() -  dst[0].imag();
00320         int k;
00321         for ( k=1;k <= ncfft2 ; ++k ) {
00322           Complex fpk = dst[k];
00323           Complex fpnk = conj(dst[ncfft-k]);
00324           Complex f1k = fpk + fpnk;
00325           Complex f2k = fpk - fpnk;
00326           Complex tw= f2k * rtw[k-1];
00327           dst[k] =  (f1k + tw) * Scalar(.5);
00328           dst[ncfft-k] =  conj(f1k -tw)*Scalar(.5);
00329         }
00330         dst[0] = dc;
00331         dst[ncfft] = nyquist;
00332       }
00333     }
00334 
00335   // inverse complex-to-complex
00336   inline
00337     void inv(Complex * dst,const Complex  *src,int nfft)
00338     {
00339       get_plan(nfft,true).work(0, dst, src, 1,1);
00340     }
00341 
00342   // half-complex to scalar
00343   inline
00344     void inv( Scalar * dst,const Complex * src,int nfft) 
00345     {
00346       if (nfft&3) {
00347         m_tmpBuf1.resize(nfft);
00348         m_tmpBuf2.resize(nfft);
00349         std::copy(src,src+(nfft>>1)+1,m_tmpBuf1.begin() );
00350         for (int k=1;k<(nfft>>1)+1;++k)
00351           m_tmpBuf1[nfft-k] = conj(m_tmpBuf1[k]);
00352         inv(&m_tmpBuf2[0],&m_tmpBuf1[0],nfft);
00353         for (int k=0;k<nfft;++k)
00354           dst[k] = m_tmpBuf2[k].real();
00355       }else{
00356         // optimized version for multiple of 4
00357         int ncfft = nfft>>1;
00358         int ncfft2 = nfft>>2;
00359         Complex * rtw = real_twiddles(ncfft2);
00360         m_tmpBuf1.resize(ncfft);
00361         m_tmpBuf1[0] = Complex( src[0].real() + src[ncfft].real(), src[0].real() - src[ncfft].real() );
00362         for (int k = 1; k <= ncfft / 2; ++k) {
00363           Complex fk = src[k];
00364           Complex fnkc = conj(src[ncfft-k]);
00365           Complex fek = fk + fnkc;
00366           Complex tmp = fk - fnkc;
00367           Complex fok = tmp * conj(rtw[k-1]);
00368           m_tmpBuf1[k] = fek + fok;
00369           m_tmpBuf1[ncfft-k] = conj(fek - fok);
00370         }
00371         get_plan(ncfft,true).work(0, reinterpret_cast<Complex*>(dst), &m_tmpBuf1[0], 1,1);
00372       }
00373     }
00374 
00375   protected:
00376   typedef kiss_cpx_fft<Scalar> PlanData;
00377   typedef std::map<int,PlanData> PlanMap;
00378 
00379   PlanMap m_plans;
00380   std::map<int, std::vector<Complex> > m_realTwiddles;
00381   std::vector<Complex> m_tmpBuf1;
00382   std::vector<Complex> m_tmpBuf2;
00383 
00384   inline
00385     int PlanKey(int nfft, bool isinverse) const { return (nfft<<1) | int(isinverse); }
00386 
00387   inline
00388     PlanData & get_plan(int nfft, bool inverse)
00389     {
00390       // TODO look for PlanKey(nfft, ! inverse) and conjugate the twiddles
00391       PlanData & pd = m_plans[ PlanKey(nfft,inverse) ];
00392       if ( pd.m_twiddles.size() == 0 ) {
00393         pd.make_twiddles(nfft,inverse);
00394         pd.factorize(nfft);
00395       }
00396       return pd;
00397     }
00398 
00399   inline
00400     Complex * real_twiddles(int ncfft2)
00401     {
00402       std::vector<Complex> & twidref = m_realTwiddles[ncfft2];// creates new if not there
00403       if ( (int)twidref.size() != ncfft2 ) {
00404         twidref.resize(ncfft2);
00405         int ncfft= ncfft2<<1;
00406         Scalar pi =  acos( Scalar(-1) );
00407         for (int k=1;k<=ncfft2;++k) 
00408           twidref[k-1] = exp( Complex(0,-pi * (Scalar(k) / ncfft + Scalar(.5)) ) );
00409       }
00410       return &twidref[0];
00411     }
00412 };
00413 
00414 } // end namespace internal
00415 
00416 } // end namespace Eigen
00417 
00418 /* vim: set filetype=cpp et sw=2 ts=2 ai: */


win_eigen
Author(s): Daniel Stonier
autogenerated on Mon Oct 6 2014 12:24:23