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


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:02