1 #ifndef KISSFFT_CLASS_HH
2 #define KISSFFT_CLASS_HH
6 namespace kissfft_utils {
8 template <typename T_scalar>
11 typedef T_scalar scalar_type;
12 typedef std::complex<scalar_type> cpx_type;
13 void fill_twiddles( std::complex<T_scalar> * dst ,int nfft,bool inverse)
15 T_scalar phinc = (inverse?2:-2)* acos( (T_scalar) -1) / nfft;
16 for (int i=0;i<nfft;++i)
17 dst[i] = exp( std::complex<T_scalar>(0,i*phinc) );
21 std::vector< std::complex<T_scalar> > & dst,
22 int nfft,bool inverse,
23 std::vector<int> & stageRadix,
24 std::vector<int> & stageRemainder )
26 _twiddles.resize(nfft);
27 fill_twiddles( &_twiddles[0],nfft,inverse);
31 //start factoring out 4's, then 2's, then 3,5,7,9,...
39 default: p += 2; break;
42 p=n;// no more factors
45 stageRadix.push_back(p);
46 stageRemainder.push_back(n);
49 std::vector<cpx_type> _twiddles;
52 const cpx_type twiddle(int i) { return _twiddles[i]; }
57 template <typename T_Scalar,
58 typename T_traits=kissfft_utils::traits<T_Scalar>
63 typedef T_traits traits_type;
64 typedef typename traits_type::scalar_type scalar_type;
65 typedef typename traits_type::cpx_type cpx_type;
67 kissfft(int nfft,bool inverse,const traits_type & traits=traits_type() )
68 :_nfft(nfft),_inverse(inverse),_traits(traits)
70 _traits.prepare(_twiddles, _nfft,_inverse ,_stageRadix, _stageRemainder);
73 void transform(const cpx_type * src , cpx_type * dst)
75 kf_work(0, dst, src, 1,1);
79 void kf_work( int stage,cpx_type * Fout, const cpx_type * f, size_t fstride,size_t in_stride)
81 int p = _stageRadix[stage];
82 int m = _stageRemainder[stage];
83 cpx_type * Fout_beg = Fout;
84 cpx_type * Fout_end = Fout + p*m;
89 f += fstride*in_stride;
90 }while(++Fout != Fout_end );
94 // DFT of size m*p performed by doing
95 // p instances of smaller DFTs of size m,
96 // each one takes a decimated version of the input
97 kf_work(stage+1, Fout , f, fstride*p,in_stride);
98 f += fstride*in_stride;
99 }while( (Fout += m) != Fout_end );
104 // recombine the p smaller DFTs
106 case 2: kf_bfly2(Fout,fstride,m); break;
107 case 3: kf_bfly3(Fout,fstride,m); break;
108 case 4: kf_bfly4(Fout,fstride,m); break;
109 case 5: kf_bfly5(Fout,fstride,m); break;
110 default: kf_bfly_generic(Fout,fstride,m,p); break;
114 // these were #define macros in the original kiss_fft
115 void C_ADD( cpx_type & c,const cpx_type & a,const cpx_type & b) { c=a+b;}
116 void C_MUL( cpx_type & c,const cpx_type & a,const cpx_type & b) { c=a*b;}
117 void C_SUB( cpx_type & c,const cpx_type & a,const cpx_type & b) { c=a-b;}
118 void C_ADDTO( cpx_type & c,const cpx_type & a) { c+=a;}
119 void C_FIXDIV( cpx_type & ,int ) {} // NO-OP for float types
120 scalar_type S_MUL( const scalar_type & a,const scalar_type & b) { return a*b;}
121 scalar_type HALF_OF( const scalar_type & a) { return a*.5;}
122 void C_MULBYSCALAR(cpx_type & c,const scalar_type & a) {c*=a;}
124 void kf_bfly2( cpx_type * Fout, const size_t fstride, int m)
126 for (int k=0;k<m;++k) {
127 cpx_type t = Fout[m+k] * _traits.twiddle(k*fstride);
128 Fout[m+k] = Fout[k] - t;
133 void kf_bfly4( cpx_type * Fout, const size_t fstride, const size_t m)
136 int negative_if_inverse = _inverse * -2 +1;
137 for (size_t k=0;k<m;++k) {
138 scratch[0] = Fout[k+m] * _traits.twiddle(k*fstride);
139 scratch[1] = Fout[k+2*m] * _traits.twiddle(k*fstride*2);
140 scratch[2] = Fout[k+3*m] * _traits.twiddle(k*fstride*3);
141 scratch[5] = Fout[k] - scratch[1];
143 Fout[k] += scratch[1];
144 scratch[3] = scratch[0] + scratch[2];
145 scratch[4] = scratch[0] - scratch[2];
146 scratch[4] = cpx_type( scratch[4].imag()*negative_if_inverse , -scratch[4].real()* negative_if_inverse );
148 Fout[k+2*m] = Fout[k] - scratch[3];
149 Fout[k] += scratch[3];
150 Fout[k+m] = scratch[5] + scratch[4];
151 Fout[k+3*m] = scratch[5] - scratch[4];
155 void kf_bfly3( cpx_type * Fout, const size_t fstride, const size_t m)
158 const size_t m2 = 2*m;
162 epi3 = _twiddles[fstride*m];
164 tw1=tw2=&_twiddles[0];
167 C_FIXDIV(*Fout,3); C_FIXDIV(Fout[m],3); C_FIXDIV(Fout[m2],3);
169 C_MUL(scratch[1],Fout[m] , *tw1);
170 C_MUL(scratch[2],Fout[m2] , *tw2);
172 C_ADD(scratch[3],scratch[1],scratch[2]);
173 C_SUB(scratch[0],scratch[1],scratch[2]);
177 Fout[m] = cpx_type( Fout->real() - HALF_OF(scratch[3].real() ) , Fout->imag() - HALF_OF(scratch[3].imag() ) );
179 C_MULBYSCALAR( scratch[0] , epi3.imag() );
181 C_ADDTO(*Fout,scratch[3]);
183 Fout[m2] = cpx_type( Fout[m].real() + scratch[0].imag() , Fout[m].imag() - scratch[0].real() );
185 C_ADDTO( Fout[m] , cpx_type( -scratch[0].imag(),scratch[0].real() ) );
190 void kf_bfly5( cpx_type * Fout, const size_t fstride, const size_t m)
192 cpx_type *Fout0,*Fout1,*Fout2,*Fout3,*Fout4;
194 cpx_type scratch[13];
195 cpx_type * twiddles = &_twiddles[0];
198 ya = twiddles[fstride*m];
199 yb = twiddles[fstride*2*m];
208 for ( u=0; u<m; ++u ) {
209 C_FIXDIV( *Fout0,5); C_FIXDIV( *Fout1,5); C_FIXDIV( *Fout2,5); C_FIXDIV( *Fout3,5); C_FIXDIV( *Fout4,5);
212 C_MUL(scratch[1] ,*Fout1, tw[u*fstride]);
213 C_MUL(scratch[2] ,*Fout2, tw[2*u*fstride]);
214 C_MUL(scratch[3] ,*Fout3, tw[3*u*fstride]);
215 C_MUL(scratch[4] ,*Fout4, tw[4*u*fstride]);
217 C_ADD( scratch[7],scratch[1],scratch[4]);
218 C_SUB( scratch[10],scratch[1],scratch[4]);
219 C_ADD( scratch[8],scratch[2],scratch[3]);
220 C_SUB( scratch[9],scratch[2],scratch[3]);
222 C_ADDTO( *Fout0, scratch[7]);
223 C_ADDTO( *Fout0, scratch[8]);
225 scratch[5] = scratch[0] + cpx_type(
226 S_MUL(scratch[7].real(),ya.real() ) + S_MUL(scratch[8].real() ,yb.real() ),
227 S_MUL(scratch[7].imag(),ya.real()) + S_MUL(scratch[8].imag(),yb.real())
230 scratch[6] = cpx_type(
231 S_MUL(scratch[10].imag(),ya.imag()) + S_MUL(scratch[9].imag(),yb.imag()),
232 -S_MUL(scratch[10].real(),ya.imag()) - S_MUL(scratch[9].real(),yb.imag())
235 C_SUB(*Fout1,scratch[5],scratch[6]);
236 C_ADD(*Fout4,scratch[5],scratch[6]);
238 scratch[11] = scratch[0] +
240 S_MUL(scratch[7].real(),yb.real()) + S_MUL(scratch[8].real(),ya.real()),
241 S_MUL(scratch[7].imag(),yb.real()) + S_MUL(scratch[8].imag(),ya.real())
244 scratch[12] = cpx_type(
245 -S_MUL(scratch[10].imag(),yb.imag()) + S_MUL(scratch[9].imag(),ya.imag()),
246 S_MUL(scratch[10].real(),yb.imag()) - S_MUL(scratch[9].real(),ya.imag())
249 C_ADD(*Fout2,scratch[11],scratch[12]);
250 C_SUB(*Fout3,scratch[11],scratch[12]);
252 ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4;
256 /* perform the butterfly for one stage of a mixed radix FFT */
257 void kf_bfly_generic(
259 const size_t fstride,
265 cpx_type * twiddles = &_twiddles[0];
268 cpx_type scratchbuf[p];
270 for ( u=0; u<m; ++u ) {
272 for ( q1=0 ; q1<p ; ++q1 ) {
273 scratchbuf[q1] = Fout[ k ];
274 C_FIXDIV(scratchbuf[q1],p);
279 for ( q1=0 ; q1<p ; ++q1 ) {
281 Fout[ k ] = scratchbuf[0];
283 twidx += fstride * k;
284 if (twidx>=Norig) twidx-=Norig;
285 C_MUL(t,scratchbuf[q] , twiddles[twidx] );
286 C_ADDTO( Fout[ k ] ,t);
295 std::vector<cpx_type> _twiddles;
296 std::vector<int> _stageRadix;
297 std::vector<int> _stageRemainder;