c@409
|
1 #ifndef KISSFFT_CLASS_HH
|
c@409
|
2 #define KISSFFT_CLASS_HH
|
c@409
|
3 #include <complex>
|
c@409
|
4 #include <vector>
|
c@409
|
5
|
c@409
|
6 namespace kissfft_utils {
|
c@409
|
7
|
c@409
|
8 template <typename T_scalar>
|
c@409
|
9 struct traits
|
c@409
|
10 {
|
c@409
|
11 typedef T_scalar scalar_type;
|
c@409
|
12 typedef std::complex<scalar_type> cpx_type;
|
c@409
|
13 void fill_twiddles( std::complex<T_scalar> * dst ,int nfft,bool inverse)
|
c@409
|
14 {
|
c@409
|
15 T_scalar phinc = (inverse?2:-2)* acos( (T_scalar) -1) / nfft;
|
c@409
|
16 for (int i=0;i<nfft;++i)
|
c@409
|
17 dst[i] = exp( std::complex<T_scalar>(0,i*phinc) );
|
c@409
|
18 }
|
c@409
|
19
|
c@409
|
20 void prepare(
|
c@409
|
21 std::vector< std::complex<T_scalar> > & dst,
|
c@409
|
22 int nfft,bool inverse,
|
c@409
|
23 std::vector<int> & stageRadix,
|
c@409
|
24 std::vector<int> & stageRemainder )
|
c@409
|
25 {
|
c@409
|
26 _twiddles.resize(nfft);
|
c@409
|
27 fill_twiddles( &_twiddles[0],nfft,inverse);
|
c@409
|
28 dst = _twiddles;
|
c@409
|
29
|
c@409
|
30 //factorize
|
c@409
|
31 //start factoring out 4's, then 2's, then 3,5,7,9,...
|
c@409
|
32 int n= nfft;
|
c@409
|
33 int p=4;
|
c@409
|
34 do {
|
c@409
|
35 while (n % p) {
|
c@409
|
36 switch (p) {
|
c@409
|
37 case 4: p = 2; break;
|
c@409
|
38 case 2: p = 3; break;
|
c@409
|
39 default: p += 2; break;
|
c@409
|
40 }
|
c@409
|
41 if (p*p>n)
|
c@409
|
42 p=n;// no more factors
|
c@409
|
43 }
|
c@409
|
44 n /= p;
|
c@409
|
45 stageRadix.push_back(p);
|
c@409
|
46 stageRemainder.push_back(n);
|
c@409
|
47 }while(n>1);
|
c@409
|
48 }
|
c@409
|
49 std::vector<cpx_type> _twiddles;
|
c@409
|
50
|
c@409
|
51
|
c@409
|
52 const cpx_type twiddle(int i) { return _twiddles[i]; }
|
c@409
|
53 };
|
c@409
|
54
|
c@409
|
55 }
|
c@409
|
56
|
c@409
|
57 template <typename T_Scalar,
|
c@409
|
58 typename T_traits=kissfft_utils::traits<T_Scalar>
|
c@409
|
59 >
|
c@409
|
60 class kissfft
|
c@409
|
61 {
|
c@409
|
62 public:
|
c@409
|
63 typedef T_traits traits_type;
|
c@409
|
64 typedef typename traits_type::scalar_type scalar_type;
|
c@409
|
65 typedef typename traits_type::cpx_type cpx_type;
|
c@409
|
66
|
c@409
|
67 kissfft(int nfft,bool inverse,const traits_type & traits=traits_type() )
|
c@409
|
68 :_nfft(nfft),_inverse(inverse),_traits(traits)
|
c@409
|
69 {
|
c@409
|
70 _traits.prepare(_twiddles, _nfft,_inverse ,_stageRadix, _stageRemainder);
|
c@409
|
71 }
|
c@409
|
72
|
c@409
|
73 void transform(const cpx_type * src , cpx_type * dst)
|
c@409
|
74 {
|
c@409
|
75 kf_work(0, dst, src, 1,1);
|
c@409
|
76 }
|
c@409
|
77
|
c@409
|
78 private:
|
c@409
|
79 void kf_work( int stage,cpx_type * Fout, const cpx_type * f, size_t fstride,size_t in_stride)
|
c@409
|
80 {
|
c@409
|
81 int p = _stageRadix[stage];
|
c@409
|
82 int m = _stageRemainder[stage];
|
c@409
|
83 cpx_type * Fout_beg = Fout;
|
c@409
|
84 cpx_type * Fout_end = Fout + p*m;
|
c@409
|
85
|
c@409
|
86 if (m==1) {
|
c@409
|
87 do{
|
c@409
|
88 *Fout = *f;
|
c@409
|
89 f += fstride*in_stride;
|
c@409
|
90 }while(++Fout != Fout_end );
|
c@409
|
91 }else{
|
c@409
|
92 do{
|
c@409
|
93 // recursive call:
|
c@409
|
94 // DFT of size m*p performed by doing
|
c@409
|
95 // p instances of smaller DFTs of size m,
|
c@409
|
96 // each one takes a decimated version of the input
|
c@409
|
97 kf_work(stage+1, Fout , f, fstride*p,in_stride);
|
c@409
|
98 f += fstride*in_stride;
|
c@409
|
99 }while( (Fout += m) != Fout_end );
|
c@409
|
100 }
|
c@409
|
101
|
c@409
|
102 Fout=Fout_beg;
|
c@409
|
103
|
c@409
|
104 // recombine the p smaller DFTs
|
c@409
|
105 switch (p) {
|
c@409
|
106 case 2: kf_bfly2(Fout,fstride,m); break;
|
c@409
|
107 case 3: kf_bfly3(Fout,fstride,m); break;
|
c@409
|
108 case 4: kf_bfly4(Fout,fstride,m); break;
|
c@409
|
109 case 5: kf_bfly5(Fout,fstride,m); break;
|
c@409
|
110 default: kf_bfly_generic(Fout,fstride,m,p); break;
|
c@409
|
111 }
|
c@409
|
112 }
|
c@409
|
113
|
c@409
|
114 // these were #define macros in the original kiss_fft
|
c@409
|
115 void C_ADD( cpx_type & c,const cpx_type & a,const cpx_type & b) { c=a+b;}
|
c@409
|
116 void C_MUL( cpx_type & c,const cpx_type & a,const cpx_type & b) { c=a*b;}
|
c@409
|
117 void C_SUB( cpx_type & c,const cpx_type & a,const cpx_type & b) { c=a-b;}
|
c@409
|
118 void C_ADDTO( cpx_type & c,const cpx_type & a) { c+=a;}
|
c@409
|
119 void C_FIXDIV( cpx_type & ,int ) {} // NO-OP for float types
|
c@409
|
120 scalar_type S_MUL( const scalar_type & a,const scalar_type & b) { return a*b;}
|
c@409
|
121 scalar_type HALF_OF( const scalar_type & a) { return a*.5;}
|
c@409
|
122 void C_MULBYSCALAR(cpx_type & c,const scalar_type & a) {c*=a;}
|
c@409
|
123
|
c@409
|
124 void kf_bfly2( cpx_type * Fout, const size_t fstride, int m)
|
c@409
|
125 {
|
c@409
|
126 for (int k=0;k<m;++k) {
|
c@409
|
127 cpx_type t = Fout[m+k] * _traits.twiddle(k*fstride);
|
c@409
|
128 Fout[m+k] = Fout[k] - t;
|
c@409
|
129 Fout[k] += t;
|
c@409
|
130 }
|
c@409
|
131 }
|
c@409
|
132
|
c@409
|
133 void kf_bfly4( cpx_type * Fout, const size_t fstride, const size_t m)
|
c@409
|
134 {
|
c@409
|
135 cpx_type scratch[7];
|
c@409
|
136 int negative_if_inverse = _inverse * -2 +1;
|
c@409
|
137 for (size_t k=0;k<m;++k) {
|
c@409
|
138 scratch[0] = Fout[k+m] * _traits.twiddle(k*fstride);
|
c@409
|
139 scratch[1] = Fout[k+2*m] * _traits.twiddle(k*fstride*2);
|
c@409
|
140 scratch[2] = Fout[k+3*m] * _traits.twiddle(k*fstride*3);
|
c@409
|
141 scratch[5] = Fout[k] - scratch[1];
|
c@409
|
142
|
c@409
|
143 Fout[k] += scratch[1];
|
c@409
|
144 scratch[3] = scratch[0] + scratch[2];
|
c@409
|
145 scratch[4] = scratch[0] - scratch[2];
|
c@409
|
146 scratch[4] = cpx_type( scratch[4].imag()*negative_if_inverse , -scratch[4].real()* negative_if_inverse );
|
c@409
|
147
|
c@409
|
148 Fout[k+2*m] = Fout[k] - scratch[3];
|
c@409
|
149 Fout[k] += scratch[3];
|
c@409
|
150 Fout[k+m] = scratch[5] + scratch[4];
|
c@409
|
151 Fout[k+3*m] = scratch[5] - scratch[4];
|
c@409
|
152 }
|
c@409
|
153 }
|
c@409
|
154
|
c@409
|
155 void kf_bfly3( cpx_type * Fout, const size_t fstride, const size_t m)
|
c@409
|
156 {
|
c@409
|
157 size_t k=m;
|
c@409
|
158 const size_t m2 = 2*m;
|
c@409
|
159 cpx_type *tw1,*tw2;
|
c@409
|
160 cpx_type scratch[5];
|
c@409
|
161 cpx_type epi3;
|
c@409
|
162 epi3 = _twiddles[fstride*m];
|
c@409
|
163
|
c@409
|
164 tw1=tw2=&_twiddles[0];
|
c@409
|
165
|
c@409
|
166 do{
|
c@409
|
167 C_FIXDIV(*Fout,3); C_FIXDIV(Fout[m],3); C_FIXDIV(Fout[m2],3);
|
c@409
|
168
|
c@409
|
169 C_MUL(scratch[1],Fout[m] , *tw1);
|
c@409
|
170 C_MUL(scratch[2],Fout[m2] , *tw2);
|
c@409
|
171
|
c@409
|
172 C_ADD(scratch[3],scratch[1],scratch[2]);
|
c@409
|
173 C_SUB(scratch[0],scratch[1],scratch[2]);
|
c@409
|
174 tw1 += fstride;
|
c@409
|
175 tw2 += fstride*2;
|
c@409
|
176
|
c@409
|
177 Fout[m] = cpx_type( Fout->real() - HALF_OF(scratch[3].real() ) , Fout->imag() - HALF_OF(scratch[3].imag() ) );
|
c@409
|
178
|
c@409
|
179 C_MULBYSCALAR( scratch[0] , epi3.imag() );
|
c@409
|
180
|
c@409
|
181 C_ADDTO(*Fout,scratch[3]);
|
c@409
|
182
|
c@409
|
183 Fout[m2] = cpx_type( Fout[m].real() + scratch[0].imag() , Fout[m].imag() - scratch[0].real() );
|
c@409
|
184
|
c@409
|
185 C_ADDTO( Fout[m] , cpx_type( -scratch[0].imag(),scratch[0].real() ) );
|
c@409
|
186 ++Fout;
|
c@409
|
187 }while(--k);
|
c@409
|
188 }
|
c@409
|
189
|
c@409
|
190 void kf_bfly5( cpx_type * Fout, const size_t fstride, const size_t m)
|
c@409
|
191 {
|
c@409
|
192 cpx_type *Fout0,*Fout1,*Fout2,*Fout3,*Fout4;
|
c@409
|
193 size_t u;
|
c@409
|
194 cpx_type scratch[13];
|
c@409
|
195 cpx_type * twiddles = &_twiddles[0];
|
c@409
|
196 cpx_type *tw;
|
c@409
|
197 cpx_type ya,yb;
|
c@409
|
198 ya = twiddles[fstride*m];
|
c@409
|
199 yb = twiddles[fstride*2*m];
|
c@409
|
200
|
c@409
|
201 Fout0=Fout;
|
c@409
|
202 Fout1=Fout0+m;
|
c@409
|
203 Fout2=Fout0+2*m;
|
c@409
|
204 Fout3=Fout0+3*m;
|
c@409
|
205 Fout4=Fout0+4*m;
|
c@409
|
206
|
c@409
|
207 tw=twiddles;
|
c@409
|
208 for ( u=0; u<m; ++u ) {
|
c@409
|
209 C_FIXDIV( *Fout0,5); C_FIXDIV( *Fout1,5); C_FIXDIV( *Fout2,5); C_FIXDIV( *Fout3,5); C_FIXDIV( *Fout4,5);
|
c@409
|
210 scratch[0] = *Fout0;
|
c@409
|
211
|
c@409
|
212 C_MUL(scratch[1] ,*Fout1, tw[u*fstride]);
|
c@409
|
213 C_MUL(scratch[2] ,*Fout2, tw[2*u*fstride]);
|
c@409
|
214 C_MUL(scratch[3] ,*Fout3, tw[3*u*fstride]);
|
c@409
|
215 C_MUL(scratch[4] ,*Fout4, tw[4*u*fstride]);
|
c@409
|
216
|
c@409
|
217 C_ADD( scratch[7],scratch[1],scratch[4]);
|
c@409
|
218 C_SUB( scratch[10],scratch[1],scratch[4]);
|
c@409
|
219 C_ADD( scratch[8],scratch[2],scratch[3]);
|
c@409
|
220 C_SUB( scratch[9],scratch[2],scratch[3]);
|
c@409
|
221
|
c@409
|
222 C_ADDTO( *Fout0, scratch[7]);
|
c@409
|
223 C_ADDTO( *Fout0, scratch[8]);
|
c@409
|
224
|
c@409
|
225 scratch[5] = scratch[0] + cpx_type(
|
c@409
|
226 S_MUL(scratch[7].real(),ya.real() ) + S_MUL(scratch[8].real() ,yb.real() ),
|
c@409
|
227 S_MUL(scratch[7].imag(),ya.real()) + S_MUL(scratch[8].imag(),yb.real())
|
c@409
|
228 );
|
c@409
|
229
|
c@409
|
230 scratch[6] = cpx_type(
|
c@409
|
231 S_MUL(scratch[10].imag(),ya.imag()) + S_MUL(scratch[9].imag(),yb.imag()),
|
c@409
|
232 -S_MUL(scratch[10].real(),ya.imag()) - S_MUL(scratch[9].real(),yb.imag())
|
c@409
|
233 );
|
c@409
|
234
|
c@409
|
235 C_SUB(*Fout1,scratch[5],scratch[6]);
|
c@409
|
236 C_ADD(*Fout4,scratch[5],scratch[6]);
|
c@409
|
237
|
c@409
|
238 scratch[11] = scratch[0] +
|
c@409
|
239 cpx_type(
|
c@409
|
240 S_MUL(scratch[7].real(),yb.real()) + S_MUL(scratch[8].real(),ya.real()),
|
c@409
|
241 S_MUL(scratch[7].imag(),yb.real()) + S_MUL(scratch[8].imag(),ya.real())
|
c@409
|
242 );
|
c@409
|
243
|
c@409
|
244 scratch[12] = cpx_type(
|
c@409
|
245 -S_MUL(scratch[10].imag(),yb.imag()) + S_MUL(scratch[9].imag(),ya.imag()),
|
c@409
|
246 S_MUL(scratch[10].real(),yb.imag()) - S_MUL(scratch[9].real(),ya.imag())
|
c@409
|
247 );
|
c@409
|
248
|
c@409
|
249 C_ADD(*Fout2,scratch[11],scratch[12]);
|
c@409
|
250 C_SUB(*Fout3,scratch[11],scratch[12]);
|
c@409
|
251
|
c@409
|
252 ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4;
|
c@409
|
253 }
|
c@409
|
254 }
|
c@409
|
255
|
c@409
|
256 /* perform the butterfly for one stage of a mixed radix FFT */
|
c@409
|
257 void kf_bfly_generic(
|
c@409
|
258 cpx_type * Fout,
|
c@409
|
259 const size_t fstride,
|
c@409
|
260 int m,
|
c@409
|
261 int p
|
c@409
|
262 )
|
c@409
|
263 {
|
c@409
|
264 int u,k,q1,q;
|
c@409
|
265 cpx_type * twiddles = &_twiddles[0];
|
c@409
|
266 cpx_type t;
|
c@409
|
267 int Norig = _nfft;
|
c@409
|
268 cpx_type scratchbuf[p];
|
c@409
|
269
|
c@409
|
270 for ( u=0; u<m; ++u ) {
|
c@409
|
271 k=u;
|
c@409
|
272 for ( q1=0 ; q1<p ; ++q1 ) {
|
c@409
|
273 scratchbuf[q1] = Fout[ k ];
|
c@409
|
274 C_FIXDIV(scratchbuf[q1],p);
|
c@409
|
275 k += m;
|
c@409
|
276 }
|
c@409
|
277
|
c@409
|
278 k=u;
|
c@409
|
279 for ( q1=0 ; q1<p ; ++q1 ) {
|
c@409
|
280 int twidx=0;
|
c@409
|
281 Fout[ k ] = scratchbuf[0];
|
c@409
|
282 for (q=1;q<p;++q ) {
|
c@409
|
283 twidx += fstride * k;
|
c@409
|
284 if (twidx>=Norig) twidx-=Norig;
|
c@409
|
285 C_MUL(t,scratchbuf[q] , twiddles[twidx] );
|
c@409
|
286 C_ADDTO( Fout[ k ] ,t);
|
c@409
|
287 }
|
c@409
|
288 k += m;
|
c@409
|
289 }
|
c@409
|
290 }
|
c@409
|
291 }
|
c@409
|
292
|
c@409
|
293 int _nfft;
|
c@409
|
294 bool _inverse;
|
c@409
|
295 std::vector<cpx_type> _twiddles;
|
c@409
|
296 std::vector<int> _stageRadix;
|
c@409
|
297 std::vector<int> _stageRemainder;
|
c@409
|
298 traits_type _traits;
|
c@409
|
299 };
|
c@409
|
300 #endif
|