1 #ifndef KISSFFT_CLASS_HH
5 namespace kissfft_utils {
7 template <
typename T_scalar>
12 void fill_twiddles( std::complex<T_scalar> * dst ,
int nfft,
bool inverse)
14 T_scalar phinc = (inverse?2:-2)* acos( (T_scalar) -1) / nfft;
15 for (
int i=0;i<nfft;++i)
16 dst[i] = exp( std::complex<T_scalar>(0,i*phinc) );
20 std::vector< std::complex<T_scalar> > & dst,
21 int nfft,
bool inverse,
22 std::vector<int> & stageRadix,
23 std::vector<int> & stageRemainder )
38 default: p += 2;
break;
44 stageRadix.push_back(p);
45 stageRemainder.push_back(n);
56 template <
typename T_Scalar,
64 typedef typename traits_type::cpx_type
cpx_type;
67 :_nfft(nfft),_inverse(inverse),_traits(traits)
69 _traits.prepare(_twiddles, _nfft,_inverse ,_stageRadix, _stageRemainder);
74 kf_work(0, dst, src, 1,1);
78 void kf_work(
int stage,
cpx_type * Fout,
const cpx_type * f,
size_t fstride,
size_t in_stride)
80 int p = _stageRadix[stage];
81 int m = _stageRemainder[stage];
88 f += fstride*in_stride;
89 }
while(++Fout != Fout_end );
96 kf_work(stage+1, Fout , f, fstride*p,in_stride);
97 f += fstride*in_stride;
98 }
while( (Fout += m) != Fout_end );
105 case 2: kf_bfly2(Fout,fstride,m);
break;
106 case 3: kf_bfly3(Fout,fstride,m);
break;
107 case 4: kf_bfly4(Fout,fstride,m);
break;
108 case 5: kf_bfly5(Fout,fstride,m);
break;
109 default: kf_bfly_generic(Fout,fstride,m,p);
break;
123 void kf_bfly2(
cpx_type * Fout,
const size_t fstride,
int m)
125 for (
int k=0;k<m;++k) {
126 cpx_type t = Fout[m+k] * _traits.twiddle(((
int)k)*((
int)fstride));
127 Fout[m+k] = Fout[k] - t;
132 void kf_bfly4(
cpx_type * Fout,
const size_t fstride,
const size_t m)
135 int negative_if_inverse = _inverse * -2 +1;
136 for (
size_t k=0;k<m;++k) {
137 scratch[0] = Fout[k+m] * _traits.twiddle(((
int)k)*((
int)fstride));
138 scratch[1] = Fout[k+2*m] * _traits.twiddle(((
int)k)*((
int)fstride)*2);
139 scratch[2] = Fout[k+3*m] * _traits.twiddle(((
int)k)*((
int)fstride)*3);
140 scratch[5] = Fout[k] - scratch[1];
142 Fout[k] += scratch[1];
143 scratch[3] = scratch[0] + scratch[2];
144 scratch[4] = scratch[0] - scratch[2];
145 scratch[4] =
cpx_type( scratch[4].imag()*negative_if_inverse , -scratch[4].real()* negative_if_inverse );
147 Fout[k+2*m] = Fout[k] - scratch[3];
148 Fout[k] += scratch[3];
149 Fout[k+m] = scratch[5] + scratch[4];
150 Fout[k+3*m] = scratch[5] - scratch[4];
154 void kf_bfly3(
cpx_type * Fout,
const size_t fstride,
const size_t m)
157 const size_t m2 = 2*m;
161 epi3 = _twiddles[fstride*m];
163 tw1=tw2=&_twiddles[0];
166 C_FIXDIV(*Fout,3); C_FIXDIV(Fout[m],3); C_FIXDIV(Fout[m2],3);
168 C_MUL(scratch[1],Fout[m] , *tw1);
169 C_MUL(scratch[2],Fout[m2] , *tw2);
171 C_ADD(scratch[3],scratch[1],scratch[2]);
172 C_SUB(scratch[0],scratch[1],scratch[2]);
176 Fout[m] =
cpx_type( Fout->real() - HALF_OF(scratch[3].real() ) , Fout->imag() - HALF_OF(scratch[3].imag() ) );
178 C_MULBYSCALAR( scratch[0] , epi3.imag() );
180 C_ADDTO(*Fout,scratch[3]);
182 Fout[m2] =
cpx_type( Fout[m].real() + scratch[0].imag() , Fout[m].imag() - scratch[0].real() );
184 C_ADDTO( Fout[m] ,
cpx_type( -scratch[0].imag(),scratch[0].real() ) );
189 void kf_bfly5(
cpx_type * Fout,
const size_t fstride,
const size_t m)
191 cpx_type *Fout0,*Fout1,*Fout2,*Fout3,*Fout4;
194 cpx_type * twiddles = &_twiddles[0];
197 ya = twiddles[fstride*m];
198 yb = twiddles[fstride*2*m];
207 for ( u=0; u<m; ++u ) {
208 C_FIXDIV( *Fout0,5); C_FIXDIV( *Fout1,5); C_FIXDIV( *Fout2,5); C_FIXDIV( *Fout3,5); C_FIXDIV( *Fout4,5);
211 C_MUL(scratch[1] ,*Fout1, tw[u*fstride]);
212 C_MUL(scratch[2] ,*Fout2, tw[2*u*fstride]);
213 C_MUL(scratch[3] ,*Fout3, tw[3*u*fstride]);
214 C_MUL(scratch[4] ,*Fout4, tw[4*u*fstride]);
216 C_ADD( scratch[7],scratch[1],scratch[4]);
217 C_SUB( scratch[10],scratch[1],scratch[4]);
218 C_ADD( scratch[8],scratch[2],scratch[3]);
219 C_SUB( scratch[9],scratch[2],scratch[3]);
221 C_ADDTO( *Fout0, scratch[7]);
222 C_ADDTO( *Fout0, scratch[8]);
225 S_MUL(scratch[7].real(),ya.real() ) + S_MUL(scratch[8].real() ,yb.real() ),
226 S_MUL(scratch[7].imag(),ya.real()) + S_MUL(scratch[8].imag(),yb.real())
230 S_MUL(scratch[10].imag(),ya.imag()) + S_MUL(scratch[9].imag(),yb.imag()),
231 -S_MUL(scratch[10].real(),ya.imag()) - S_MUL(scratch[9].real(),yb.imag())
234 C_SUB(*Fout1,scratch[5],scratch[6]);
235 C_ADD(*Fout4,scratch[5],scratch[6]);
237 scratch[11] = scratch[0] +
239 S_MUL(scratch[7].real(),yb.real()) + S_MUL(scratch[8].real(),ya.real()),
240 S_MUL(scratch[7].imag(),yb.real()) + S_MUL(scratch[8].imag(),ya.real())
244 -S_MUL(scratch[10].imag(),yb.imag()) + S_MUL(scratch[9].imag(),ya.imag()),
245 S_MUL(scratch[10].real(),yb.imag()) - S_MUL(scratch[9].real(),ya.imag())
248 C_ADD(*Fout2,scratch[11],scratch[12]);
249 C_SUB(*Fout3,scratch[11],scratch[12]);
251 ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4;
256 void kf_bfly_generic(
258 const size_t fstride,
264 cpx_type * twiddles = &_twiddles[0];
267 std::vector<cpx_type> scratchbuf(p);
269 for ( u=0; u<m; ++u ) {
271 for ( q1=0 ; q1<p ; ++q1 ) {
272 scratchbuf[q1] = Fout[ k ];
273 C_FIXDIV(scratchbuf[q1],p);
278 for ( q1=0 ; q1<p ; ++q1 ) {
280 Fout[ k ] = scratchbuf[0];
282 twidx += fstride * k;
283 if (twidx>=Norig) twidx-=Norig;
284 C_MUL(t,scratchbuf[q] , twiddles[twidx] );
285 C_ADDTO( Fout[ k ] ,t);
294 std::vector<cpx_type> _twiddles;
295 std::vector<int> _stageRadix;
296 std::vector<int> _stageRemainder;