annotate ext/kissfft/kissfft.hh @ 199:cc51baf8e37e

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