annotate constant-q-cpp/src/ext/kissfft/kissfft.hh @ 372:af71cbdab621 tip

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