00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 namespace internal {
00026
00027
00028
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
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;
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);
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
00086
00087
00088
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
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
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 EIGEN_UNUSED_VARIABLE(dst);
00298 EIGEN_UNUSED_VARIABLE(src);
00299 EIGEN_UNUSED_VARIABLE(n0);
00300 EIGEN_UNUSED_VARIABLE(n1);
00301 }
00302
00303 inline
00304 void inv2( Complex * dst,const Complex *src,int n0,int n1)
00305 {
00306 EIGEN_UNUSED_VARIABLE(dst);
00307 EIGEN_UNUSED_VARIABLE(src);
00308 EIGEN_UNUSED_VARIABLE(n0);
00309 EIGEN_UNUSED_VARIABLE(n1);
00310 }
00311
00312
00313
00314
00315
00316 inline
00317 void fwd( Complex * dst,const Scalar * src,int nfft)
00318 {
00319 if ( nfft&3 ) {
00320
00321 m_tmpBuf1.resize(nfft);
00322 get_plan(nfft,false).work(0, &m_tmpBuf1[0], src, 1,1);
00323 std::copy(m_tmpBuf1.begin(),m_tmpBuf1.begin()+(nfft>>1)+1,dst );
00324 }else{
00325 int ncfft = nfft>>1;
00326 int ncfft2 = nfft>>2;
00327 Complex * rtw = real_twiddles(ncfft2);
00328
00329
00330 fwd( dst, reinterpret_cast<const Complex*> (src), ncfft);
00331 Complex dc = dst[0].real() + dst[0].imag();
00332 Complex nyquist = dst[0].real() - dst[0].imag();
00333 int k;
00334 for ( k=1;k <= ncfft2 ; ++k ) {
00335 Complex fpk = dst[k];
00336 Complex fpnk = conj(dst[ncfft-k]);
00337 Complex f1k = fpk + fpnk;
00338 Complex f2k = fpk - fpnk;
00339 Complex tw= f2k * rtw[k-1];
00340 dst[k] = (f1k + tw) * Scalar(.5);
00341 dst[ncfft-k] = conj(f1k -tw)*Scalar(.5);
00342 }
00343 dst[0] = dc;
00344 dst[ncfft] = nyquist;
00345 }
00346 }
00347
00348
00349 inline
00350 void inv(Complex * dst,const Complex *src,int nfft)
00351 {
00352 get_plan(nfft,true).work(0, dst, src, 1,1);
00353 }
00354
00355
00356 inline
00357 void inv( Scalar * dst,const Complex * src,int nfft)
00358 {
00359 if (nfft&3) {
00360 m_tmpBuf1.resize(nfft);
00361 m_tmpBuf2.resize(nfft);
00362 std::copy(src,src+(nfft>>1)+1,m_tmpBuf1.begin() );
00363 for (int k=1;k<(nfft>>1)+1;++k)
00364 m_tmpBuf1[nfft-k] = conj(m_tmpBuf1[k]);
00365 inv(&m_tmpBuf2[0],&m_tmpBuf1[0],nfft);
00366 for (int k=0;k<nfft;++k)
00367 dst[k] = m_tmpBuf2[k].real();
00368 }else{
00369
00370 int ncfft = nfft>>1;
00371 int ncfft2 = nfft>>2;
00372 Complex * rtw = real_twiddles(ncfft2);
00373 m_tmpBuf1.resize(ncfft);
00374 m_tmpBuf1[0] = Complex( src[0].real() + src[ncfft].real(), src[0].real() - src[ncfft].real() );
00375 for (int k = 1; k <= ncfft / 2; ++k) {
00376 Complex fk = src[k];
00377 Complex fnkc = conj(src[ncfft-k]);
00378 Complex fek = fk + fnkc;
00379 Complex tmp = fk - fnkc;
00380 Complex fok = tmp * conj(rtw[k-1]);
00381 m_tmpBuf1[k] = fek + fok;
00382 m_tmpBuf1[ncfft-k] = conj(fek - fok);
00383 }
00384 get_plan(ncfft,true).work(0, reinterpret_cast<Complex*>(dst), &m_tmpBuf1[0], 1,1);
00385 }
00386 }
00387
00388 protected:
00389 typedef kiss_cpx_fft<Scalar> PlanData;
00390 typedef std::map<int,PlanData> PlanMap;
00391
00392 PlanMap m_plans;
00393 std::map<int, std::vector<Complex> > m_realTwiddles;
00394 std::vector<Complex> m_tmpBuf1;
00395 std::vector<Complex> m_tmpBuf2;
00396
00397 inline
00398 int PlanKey(int nfft, bool isinverse) const { return (nfft<<1) | int(isinverse); }
00399
00400 inline
00401 PlanData & get_plan(int nfft, bool inverse)
00402 {
00403
00404 PlanData & pd = m_plans[ PlanKey(nfft,inverse) ];
00405 if ( pd.m_twiddles.size() == 0 ) {
00406 pd.make_twiddles(nfft,inverse);
00407 pd.factorize(nfft);
00408 }
00409 return pd;
00410 }
00411
00412 inline
00413 Complex * real_twiddles(int ncfft2)
00414 {
00415 std::vector<Complex> & twidref = m_realTwiddles[ncfft2];
00416 if ( (int)twidref.size() != ncfft2 ) {
00417 twidref.resize(ncfft2);
00418 int ncfft= ncfft2<<1;
00419 Scalar pi = acos( Scalar(-1) );
00420 for (int k=1;k<=ncfft2;++k)
00421 twidref[k-1] = exp( Complex(0,-pi * (Scalar(k) / ncfft + Scalar(.5)) ) );
00422 }
00423 return &twidref[0];
00424 }
00425 };
00426
00427 }
00428
00429
00430