annotate libs/kiss_fft130/kiss_fft.c @ 114:d6d9df2db3e1

Update documentation
author Adam Stark <adamstark.uk@gmail.com>
date Fri, 18 Aug 2023 10:48:26 +0200
parents 4aa362058011
children
rev   line source
adamstark@93 1 /*
adamstark@93 2 Copyright (c) 2003-2010, Mark Borgerding
adamstark@93 3
adamstark@93 4 All rights reserved.
adamstark@93 5
adamstark@93 6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
adamstark@93 7
adamstark@93 8 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
adamstark@93 9 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
adamstark@93 10 * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission.
adamstark@93 11
adamstark@93 12 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
adamstark@93 13 */
adamstark@93 14
adamstark@93 15
adamstark@93 16 #include "_kiss_fft_guts.h"
adamstark@93 17 /* The guts header contains all the multiplication and addition macros that are defined for
adamstark@93 18 fixed or floating point complex numbers. It also delares the kf_ internal functions.
adamstark@93 19 */
adamstark@93 20
adamstark@93 21 static void kf_bfly2(
adamstark@93 22 kiss_fft_cpx * Fout,
adamstark@93 23 const size_t fstride,
adamstark@93 24 const kiss_fft_cfg st,
adamstark@93 25 int m
adamstark@93 26 )
adamstark@93 27 {
adamstark@93 28 kiss_fft_cpx * Fout2;
adamstark@93 29 kiss_fft_cpx * tw1 = st->twiddles;
adamstark@93 30 kiss_fft_cpx t;
adamstark@93 31 Fout2 = Fout + m;
adamstark@93 32 do{
adamstark@93 33 C_FIXDIV(*Fout,2); C_FIXDIV(*Fout2,2);
adamstark@93 34
adamstark@93 35 C_MUL (t, *Fout2 , *tw1);
adamstark@93 36 tw1 += fstride;
adamstark@93 37 C_SUB( *Fout2 , *Fout , t );
adamstark@93 38 C_ADDTO( *Fout , t );
adamstark@93 39 ++Fout2;
adamstark@93 40 ++Fout;
adamstark@93 41 }while (--m);
adamstark@93 42 }
adamstark@93 43
adamstark@93 44 static void kf_bfly4(
adamstark@93 45 kiss_fft_cpx * Fout,
adamstark@93 46 const size_t fstride,
adamstark@93 47 const kiss_fft_cfg st,
adamstark@93 48 const size_t m
adamstark@93 49 )
adamstark@93 50 {
adamstark@93 51 kiss_fft_cpx *tw1,*tw2,*tw3;
adamstark@93 52 kiss_fft_cpx scratch[6];
adamstark@93 53 size_t k=m;
adamstark@93 54 const size_t m2=2*m;
adamstark@93 55 const size_t m3=3*m;
adamstark@93 56
adamstark@93 57
adamstark@93 58 tw3 = tw2 = tw1 = st->twiddles;
adamstark@93 59
adamstark@93 60 do {
adamstark@93 61 C_FIXDIV(*Fout,4); C_FIXDIV(Fout[m],4); C_FIXDIV(Fout[m2],4); C_FIXDIV(Fout[m3],4);
adamstark@93 62
adamstark@93 63 C_MUL(scratch[0],Fout[m] , *tw1 );
adamstark@93 64 C_MUL(scratch[1],Fout[m2] , *tw2 );
adamstark@93 65 C_MUL(scratch[2],Fout[m3] , *tw3 );
adamstark@93 66
adamstark@93 67 C_SUB( scratch[5] , *Fout, scratch[1] );
adamstark@93 68 C_ADDTO(*Fout, scratch[1]);
adamstark@93 69 C_ADD( scratch[3] , scratch[0] , scratch[2] );
adamstark@93 70 C_SUB( scratch[4] , scratch[0] , scratch[2] );
adamstark@93 71 C_SUB( Fout[m2], *Fout, scratch[3] );
adamstark@93 72 tw1 += fstride;
adamstark@93 73 tw2 += fstride*2;
adamstark@93 74 tw3 += fstride*3;
adamstark@93 75 C_ADDTO( *Fout , scratch[3] );
adamstark@93 76
adamstark@93 77 if(st->inverse) {
adamstark@93 78 Fout[m].r = scratch[5].r - scratch[4].i;
adamstark@93 79 Fout[m].i = scratch[5].i + scratch[4].r;
adamstark@93 80 Fout[m3].r = scratch[5].r + scratch[4].i;
adamstark@93 81 Fout[m3].i = scratch[5].i - scratch[4].r;
adamstark@93 82 }else{
adamstark@93 83 Fout[m].r = scratch[5].r + scratch[4].i;
adamstark@93 84 Fout[m].i = scratch[5].i - scratch[4].r;
adamstark@93 85 Fout[m3].r = scratch[5].r - scratch[4].i;
adamstark@93 86 Fout[m3].i = scratch[5].i + scratch[4].r;
adamstark@93 87 }
adamstark@93 88 ++Fout;
adamstark@93 89 }while(--k);
adamstark@93 90 }
adamstark@93 91
adamstark@93 92 static void kf_bfly3(
adamstark@93 93 kiss_fft_cpx * Fout,
adamstark@93 94 const size_t fstride,
adamstark@93 95 const kiss_fft_cfg st,
adamstark@93 96 size_t m
adamstark@93 97 )
adamstark@93 98 {
adamstark@93 99 size_t k=m;
adamstark@93 100 const size_t m2 = 2*m;
adamstark@93 101 kiss_fft_cpx *tw1,*tw2;
adamstark@93 102 kiss_fft_cpx scratch[5];
adamstark@93 103 kiss_fft_cpx epi3;
adamstark@93 104 epi3 = st->twiddles[fstride*m];
adamstark@93 105
adamstark@93 106 tw1=tw2=st->twiddles;
adamstark@93 107
adamstark@93 108 do{
adamstark@93 109 C_FIXDIV(*Fout,3); C_FIXDIV(Fout[m],3); C_FIXDIV(Fout[m2],3);
adamstark@93 110
adamstark@93 111 C_MUL(scratch[1],Fout[m] , *tw1);
adamstark@93 112 C_MUL(scratch[2],Fout[m2] , *tw2);
adamstark@93 113
adamstark@93 114 C_ADD(scratch[3],scratch[1],scratch[2]);
adamstark@93 115 C_SUB(scratch[0],scratch[1],scratch[2]);
adamstark@93 116 tw1 += fstride;
adamstark@93 117 tw2 += fstride*2;
adamstark@93 118
adamstark@93 119 Fout[m].r = Fout->r - HALF_OF(scratch[3].r);
adamstark@93 120 Fout[m].i = Fout->i - HALF_OF(scratch[3].i);
adamstark@93 121
adamstark@93 122 C_MULBYSCALAR( scratch[0] , epi3.i );
adamstark@93 123
adamstark@93 124 C_ADDTO(*Fout,scratch[3]);
adamstark@93 125
adamstark@93 126 Fout[m2].r = Fout[m].r + scratch[0].i;
adamstark@93 127 Fout[m2].i = Fout[m].i - scratch[0].r;
adamstark@93 128
adamstark@93 129 Fout[m].r -= scratch[0].i;
adamstark@93 130 Fout[m].i += scratch[0].r;
adamstark@93 131
adamstark@93 132 ++Fout;
adamstark@93 133 }while(--k);
adamstark@93 134 }
adamstark@93 135
adamstark@93 136 static void kf_bfly5(
adamstark@93 137 kiss_fft_cpx * Fout,
adamstark@93 138 const size_t fstride,
adamstark@93 139 const kiss_fft_cfg st,
adamstark@93 140 int m
adamstark@93 141 )
adamstark@93 142 {
adamstark@93 143 kiss_fft_cpx *Fout0,*Fout1,*Fout2,*Fout3,*Fout4;
adamstark@93 144 int u;
adamstark@93 145 kiss_fft_cpx scratch[13];
adamstark@93 146 kiss_fft_cpx * twiddles = st->twiddles;
adamstark@93 147 kiss_fft_cpx *tw;
adamstark@93 148 kiss_fft_cpx ya,yb;
adamstark@93 149 ya = twiddles[fstride*m];
adamstark@93 150 yb = twiddles[fstride*2*m];
adamstark@93 151
adamstark@93 152 Fout0=Fout;
adamstark@93 153 Fout1=Fout0+m;
adamstark@93 154 Fout2=Fout0+2*m;
adamstark@93 155 Fout3=Fout0+3*m;
adamstark@93 156 Fout4=Fout0+4*m;
adamstark@93 157
adamstark@93 158 tw=st->twiddles;
adamstark@93 159 for ( u=0; u<m; ++u ) {
adamstark@93 160 C_FIXDIV( *Fout0,5); C_FIXDIV( *Fout1,5); C_FIXDIV( *Fout2,5); C_FIXDIV( *Fout3,5); C_FIXDIV( *Fout4,5);
adamstark@93 161 scratch[0] = *Fout0;
adamstark@93 162
adamstark@93 163 C_MUL(scratch[1] ,*Fout1, tw[u*fstride]);
adamstark@93 164 C_MUL(scratch[2] ,*Fout2, tw[2*u*fstride]);
adamstark@93 165 C_MUL(scratch[3] ,*Fout3, tw[3*u*fstride]);
adamstark@93 166 C_MUL(scratch[4] ,*Fout4, tw[4*u*fstride]);
adamstark@93 167
adamstark@93 168 C_ADD( scratch[7],scratch[1],scratch[4]);
adamstark@93 169 C_SUB( scratch[10],scratch[1],scratch[4]);
adamstark@93 170 C_ADD( scratch[8],scratch[2],scratch[3]);
adamstark@93 171 C_SUB( scratch[9],scratch[2],scratch[3]);
adamstark@93 172
adamstark@93 173 Fout0->r += scratch[7].r + scratch[8].r;
adamstark@93 174 Fout0->i += scratch[7].i + scratch[8].i;
adamstark@93 175
adamstark@93 176 scratch[5].r = scratch[0].r + S_MUL(scratch[7].r,ya.r) + S_MUL(scratch[8].r,yb.r);
adamstark@93 177 scratch[5].i = scratch[0].i + S_MUL(scratch[7].i,ya.r) + S_MUL(scratch[8].i,yb.r);
adamstark@93 178
adamstark@93 179 scratch[6].r = S_MUL(scratch[10].i,ya.i) + S_MUL(scratch[9].i,yb.i);
adamstark@93 180 scratch[6].i = -S_MUL(scratch[10].r,ya.i) - S_MUL(scratch[9].r,yb.i);
adamstark@93 181
adamstark@93 182 C_SUB(*Fout1,scratch[5],scratch[6]);
adamstark@93 183 C_ADD(*Fout4,scratch[5],scratch[6]);
adamstark@93 184
adamstark@93 185 scratch[11].r = scratch[0].r + S_MUL(scratch[7].r,yb.r) + S_MUL(scratch[8].r,ya.r);
adamstark@93 186 scratch[11].i = scratch[0].i + S_MUL(scratch[7].i,yb.r) + S_MUL(scratch[8].i,ya.r);
adamstark@93 187 scratch[12].r = - S_MUL(scratch[10].i,yb.i) + S_MUL(scratch[9].i,ya.i);
adamstark@93 188 scratch[12].i = S_MUL(scratch[10].r,yb.i) - S_MUL(scratch[9].r,ya.i);
adamstark@93 189
adamstark@93 190 C_ADD(*Fout2,scratch[11],scratch[12]);
adamstark@93 191 C_SUB(*Fout3,scratch[11],scratch[12]);
adamstark@93 192
adamstark@93 193 ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4;
adamstark@93 194 }
adamstark@93 195 }
adamstark@93 196
adamstark@93 197 /* perform the butterfly for one stage of a mixed radix FFT */
adamstark@93 198 static void kf_bfly_generic(
adamstark@93 199 kiss_fft_cpx * Fout,
adamstark@93 200 const size_t fstride,
adamstark@93 201 const kiss_fft_cfg st,
adamstark@93 202 int m,
adamstark@93 203 int p
adamstark@93 204 )
adamstark@93 205 {
adamstark@93 206 int u,k,q1,q;
adamstark@93 207 kiss_fft_cpx * twiddles = st->twiddles;
adamstark@93 208 kiss_fft_cpx t;
adamstark@93 209 int Norig = st->nfft;
adamstark@93 210
adamstark@93 211 kiss_fft_cpx * scratch = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC(sizeof(kiss_fft_cpx)*p);
adamstark@93 212
adamstark@93 213 for ( u=0; u<m; ++u ) {
adamstark@93 214 k=u;
adamstark@93 215 for ( q1=0 ; q1<p ; ++q1 ) {
adamstark@93 216 scratch[q1] = Fout[ k ];
adamstark@93 217 C_FIXDIV(scratch[q1],p);
adamstark@93 218 k += m;
adamstark@93 219 }
adamstark@93 220
adamstark@93 221 k=u;
adamstark@93 222 for ( q1=0 ; q1<p ; ++q1 ) {
adamstark@93 223 int twidx=0;
adamstark@93 224 Fout[ k ] = scratch[0];
adamstark@93 225 for (q=1;q<p;++q ) {
adamstark@93 226 twidx += fstride * k;
adamstark@93 227 if (twidx>=Norig) twidx-=Norig;
adamstark@93 228 C_MUL(t,scratch[q] , twiddles[twidx] );
adamstark@93 229 C_ADDTO( Fout[ k ] ,t);
adamstark@93 230 }
adamstark@93 231 k += m;
adamstark@93 232 }
adamstark@93 233 }
adamstark@93 234 KISS_FFT_TMP_FREE(scratch);
adamstark@93 235 }
adamstark@93 236
adamstark@93 237 static
adamstark@93 238 void kf_work(
adamstark@93 239 kiss_fft_cpx * Fout,
adamstark@93 240 const kiss_fft_cpx * f,
adamstark@93 241 const size_t fstride,
adamstark@93 242 int in_stride,
adamstark@93 243 int * factors,
adamstark@93 244 const kiss_fft_cfg st
adamstark@93 245 )
adamstark@93 246 {
adamstark@93 247 kiss_fft_cpx * Fout_beg=Fout;
adamstark@93 248 const int p=*factors++; /* the radix */
adamstark@93 249 const int m=*factors++; /* stage's fft length/p */
adamstark@93 250 const kiss_fft_cpx * Fout_end = Fout + p*m;
adamstark@93 251
adamstark@93 252 #ifdef _OPENMP
adamstark@93 253 // use openmp extensions at the
adamstark@93 254 // top-level (not recursive)
adamstark@93 255 if (fstride==1 && p<=5)
adamstark@93 256 {
adamstark@93 257 int k;
adamstark@93 258
adamstark@93 259 // execute the p different work units in different threads
adamstark@93 260 # pragma omp parallel for
adamstark@93 261 for (k=0;k<p;++k)
adamstark@93 262 kf_work( Fout +k*m, f+ fstride*in_stride*k,fstride*p,in_stride,factors,st);
adamstark@93 263 // all threads have joined by this point
adamstark@93 264
adamstark@93 265 switch (p) {
adamstark@93 266 case 2: kf_bfly2(Fout,fstride,st,m); break;
adamstark@93 267 case 3: kf_bfly3(Fout,fstride,st,m); break;
adamstark@93 268 case 4: kf_bfly4(Fout,fstride,st,m); break;
adamstark@93 269 case 5: kf_bfly5(Fout,fstride,st,m); break;
adamstark@93 270 default: kf_bfly_generic(Fout,fstride,st,m,p); break;
adamstark@93 271 }
adamstark@93 272 return;
adamstark@93 273 }
adamstark@93 274 #endif
adamstark@93 275
adamstark@93 276 if (m==1) {
adamstark@93 277 do{
adamstark@93 278 *Fout = *f;
adamstark@93 279 f += fstride*in_stride;
adamstark@93 280 }while(++Fout != Fout_end );
adamstark@93 281 }else{
adamstark@93 282 do{
adamstark@93 283 // recursive call:
adamstark@93 284 // DFT of size m*p performed by doing
adamstark@93 285 // p instances of smaller DFTs of size m,
adamstark@93 286 // each one takes a decimated version of the input
adamstark@93 287 kf_work( Fout , f, fstride*p, in_stride, factors,st);
adamstark@93 288 f += fstride*in_stride;
adamstark@93 289 }while( (Fout += m) != Fout_end );
adamstark@93 290 }
adamstark@93 291
adamstark@93 292 Fout=Fout_beg;
adamstark@93 293
adamstark@93 294 // recombine the p smaller DFTs
adamstark@93 295 switch (p) {
adamstark@93 296 case 2: kf_bfly2(Fout,fstride,st,m); break;
adamstark@93 297 case 3: kf_bfly3(Fout,fstride,st,m); break;
adamstark@93 298 case 4: kf_bfly4(Fout,fstride,st,m); break;
adamstark@93 299 case 5: kf_bfly5(Fout,fstride,st,m); break;
adamstark@93 300 default: kf_bfly_generic(Fout,fstride,st,m,p); break;
adamstark@93 301 }
adamstark@93 302 }
adamstark@93 303
adamstark@93 304 /* facbuf is populated by p1,m1,p2,m2, ...
adamstark@93 305 where
adamstark@93 306 p[i] * m[i] = m[i-1]
adamstark@93 307 m0 = n */
adamstark@93 308 static
adamstark@93 309 void kf_factor(int n,int * facbuf)
adamstark@93 310 {
adamstark@93 311 int p=4;
adamstark@93 312 double floor_sqrt;
adamstark@93 313 floor_sqrt = floor( sqrt((double)n) );
adamstark@93 314
adamstark@93 315 /*factor out powers of 4, powers of 2, then any remaining primes */
adamstark@93 316 do {
adamstark@93 317 while (n % p) {
adamstark@93 318 switch (p) {
adamstark@93 319 case 4: p = 2; break;
adamstark@93 320 case 2: p = 3; break;
adamstark@93 321 default: p += 2; break;
adamstark@93 322 }
adamstark@93 323 if (p > floor_sqrt)
adamstark@93 324 p = n; /* no more factors, skip to end */
adamstark@93 325 }
adamstark@93 326 n /= p;
adamstark@93 327 *facbuf++ = p;
adamstark@93 328 *facbuf++ = n;
adamstark@93 329 } while (n > 1);
adamstark@93 330 }
adamstark@93 331
adamstark@93 332 /*
adamstark@93 333 *
adamstark@93 334 * User-callable function to allocate all necessary storage space for the fft.
adamstark@93 335 *
adamstark@93 336 * The return value is a contiguous block of memory, allocated with malloc. As such,
adamstark@93 337 * It can be freed with free(), rather than a kiss_fft-specific function.
adamstark@93 338 * */
adamstark@93 339 kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem )
adamstark@93 340 {
adamstark@93 341 kiss_fft_cfg st=NULL;
adamstark@93 342 size_t memneeded = sizeof(struct kiss_fft_state)
adamstark@93 343 + sizeof(kiss_fft_cpx)*(nfft-1); /* twiddle factors*/
adamstark@93 344
adamstark@93 345 if ( lenmem==NULL ) {
adamstark@93 346 st = ( kiss_fft_cfg)KISS_FFT_MALLOC( memneeded );
adamstark@93 347 }else{
adamstark@93 348 if (mem != NULL && *lenmem >= memneeded)
adamstark@93 349 st = (kiss_fft_cfg)mem;
adamstark@93 350 *lenmem = memneeded;
adamstark@93 351 }
adamstark@93 352 if (st) {
adamstark@93 353 int i;
adamstark@93 354 st->nfft=nfft;
adamstark@93 355 st->inverse = inverse_fft;
adamstark@93 356
adamstark@93 357 for (i=0;i<nfft;++i) {
adamstark@93 358 const double pi=3.141592653589793238462643383279502884197169399375105820974944;
adamstark@93 359 double phase = -2*pi*i / nfft;
adamstark@93 360 if (st->inverse)
adamstark@93 361 phase *= -1;
adamstark@93 362 kf_cexp(st->twiddles+i, phase );
adamstark@93 363 }
adamstark@93 364
adamstark@93 365 kf_factor(nfft,st->factors);
adamstark@93 366 }
adamstark@93 367 return st;
adamstark@93 368 }
adamstark@93 369
adamstark@93 370
adamstark@93 371 void kiss_fft_stride(kiss_fft_cfg st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int in_stride)
adamstark@93 372 {
adamstark@93 373 if (fin == fout) {
adamstark@93 374 //NOTE: this is not really an in-place FFT algorithm.
adamstark@93 375 //It just performs an out-of-place FFT into a temp buffer
adamstark@93 376 kiss_fft_cpx * tmpbuf = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC( sizeof(kiss_fft_cpx)*st->nfft);
adamstark@93 377 kf_work(tmpbuf,fin,1,in_stride, st->factors,st);
adamstark@93 378 memcpy(fout,tmpbuf,sizeof(kiss_fft_cpx)*st->nfft);
adamstark@93 379 KISS_FFT_TMP_FREE(tmpbuf);
adamstark@93 380 }else{
adamstark@93 381 kf_work( fout, fin, 1,in_stride, st->factors,st );
adamstark@93 382 }
adamstark@93 383 }
adamstark@93 384
adamstark@93 385 void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout)
adamstark@93 386 {
adamstark@93 387 kiss_fft_stride(cfg,fin,fout,1);
adamstark@93 388 }
adamstark@93 389
adamstark@93 390
adamstark@93 391 void kiss_fft_cleanup(void)
adamstark@93 392 {
adamstark@93 393 // nothing needed any more
adamstark@93 394 }
adamstark@93 395
adamstark@93 396 int kiss_fft_next_fast_size(int n)
adamstark@93 397 {
adamstark@93 398 while(1) {
adamstark@93 399 int m=n;
adamstark@93 400 while ( (m%2) == 0 ) m/=2;
adamstark@93 401 while ( (m%3) == 0 ) m/=3;
adamstark@93 402 while ( (m%5) == 0 ) m/=5;
adamstark@93 403 if (m<=1)
adamstark@93 404 break; /* n is completely factorable by twos, threes, and fives */
adamstark@93 405 n++;
adamstark@93 406 }
adamstark@93 407 return n;
adamstark@93 408 }