comparison fft.cpp @ 5:5f3c32dc6e17

* Adjust comment syntax to permit Doxygen to generate HTML documentation; add Doxyfile
author Chris Cannam
date Wed, 06 Oct 2010 15:19:49 +0100
parents fc19d45615d1
children 977f541d6683
comparison
equal deleted inserted replaced
4:92ee28024c05 5:5f3c32dc6e17
2 2
3 #include <string.h> 3 #include <string.h>
4 #include <stdlib.h> 4 #include <stdlib.h>
5 #include "fft.h" 5 #include "fft.h"
6 6
7 //--------------------------------------------------------------------------- 7 /** \file fft.h */
8 /* 8
9 //---------------------------------------------------------------------------
10 /**
9 function Atan2: (0, 0)-safe atan2 11 function Atan2: (0, 0)-safe atan2
10 12
11 Returns 0 is x=y=0, atan2(x,y) otherwise. 13 Returns 0 is x=y=0, atan2(x,y) otherwise.
12 */ 14 */
13 double Atan2(double y, double x) 15 double Atan2(double y, double x)
14 { 16 {
15 if (x==0 && y==0) return 0; 17 if (x==0 && y==0) return 0;
16 else return atan2(y, x); 18 else return atan2(y, x);
17 }//Atan2 19 }//Atan2
18 20
19 /* 21 /**
20 function BitInv: inverse bit order of Value within an $Order-bit expression. 22 function BitInv: inverse bit order of Value within an $Order-bit expression.
21 23
22 In: integer Value smaller than 2^Order 24 In: integer Value smaller than 2^Order
23 25
24 Returns an integer whose lowest Order bits are the lowest Order bits of Value in reverse order. 26 Returns an integer whose lowest Order bits are the lowest Order bits of Value in reverse order.
33 Value=Value>>1; 35 Value=Value>>1;
34 } 36 }
35 return Result; 37 return Result;
36 }//BitInv 38 }//BitInv
37 39
38 /* 40 /**
39 function SetTwiddleFactors: fill w[N/2] with twiddle factors used in N-point complex FFT. 41 function SetTwiddleFactors: fill w[N/2] with twiddle factors used in N-point complex FFT.
40 42
41 In: N 43 In: N
42 Out: array w[N/2] containing twiddle factors 44 Out: array w[N/2] containing twiddle factors
43 45
52 w[i].x=cos(tmp), w[i].y=sin(tmp); 54 w[i].x=cos(tmp), w[i].y=sin(tmp);
53 } 55 }
54 }//SetTwiddleFactors 56 }//SetTwiddleFactors
55 57
56 //--------------------------------------------------------------------------- 58 //---------------------------------------------------------------------------
57 /* 59 /**
58 function CFFTCbii: basic complex DIF-FFT module, applied after bit-inversed ordering of inputs 60 function CFFTCbii: basic complex DIF-FFT module, applied after bit-inversed ordering of inputs
59 61
60 In: Order: integer, equals log2(Wid) 62 In: Order: integer, equals log2(Wid)
61 W[Wid/2]: twiddle factors 63 W[Wid/2]: twiddle factors
62 X[Wid]: complex waveform 64 X[Wid]: complex waveform
91 X0+=ElemsPerGroup*2; 93 X0+=ElemsPerGroup*2;
92 } 94 }
93 } 95 }
94 }//CFFTCbii 96 }//CFFTCbii
95 97
96 /* 98 /**
97 function CFFTC: in-place complex FFT 99 function CFFTC: in-place complex FFT
98 100
99 In: Order: integer, equals log2(Wid) 101 In: Order: integer, equals log2(Wid)
100 W[Wid/2]: twiddle factors 102 W[Wid/2]: twiddle factors
101 X[Wid]: complex waveform 103 X[Wid]: complex waveform
122 } 124 }
123 if (!bitinv1) free(bitinv); 125 if (!bitinv1) free(bitinv);
124 CFFTCbii(Order, W, X); 126 CFFTCbii(Order, W, X);
125 }//CFFTC 127 }//CFFTC
126 128
127 /* 129 /**
128 function CFFTC: complex FFT 130 function CFFTC: complex FFT
129 131
130 In: Input[Wid]: complex waveform 132 In: Input[Wid]: complex waveform
131 Order: integer, equals log2(Wid) 133 Order: integer, equals log2(Wid)
132 W[Wid/2]: twiddle factors 134 W[Wid/2]: twiddle factors
147 if (Amp) for (i=0; i<N; i++) Amp[i]=sqrt(X[i].x*X[i].x+X[i].y*X[i].y); 149 if (Amp) for (i=0; i<N; i++) Amp[i]=sqrt(X[i].x*X[i].x+X[i].y*X[i].y);
148 if (Arg) for (i=0; i<N; i++) Arg[i]=Atan2(X[i].y, X[i].x); 150 if (Arg) for (i=0; i<N; i++) Arg[i]=Atan2(X[i].y, X[i].x);
149 }//CFFTC 151 }//CFFTC
150 152
151 //--------------------------------------------------------------------------- 153 //---------------------------------------------------------------------------
152 /* 154 /**
153 function CIFFTCbii: basic complex IFFT module, applied after bit-inversed ordering of inputs 155 function CIFFTCbii: basic complex IFFT module, applied after bit-inversed ordering of inputs
154 156
155 In: Order: integer, equals log2(Wid) 157 In: Order: integer, equals log2(Wid)
156 W[Wid/2]: twiddle factors 158 W[Wid/2]: twiddle factors
157 X[Wid]: complex spectrum 159 X[Wid]: complex spectrum
191 X[i].x/=N; 193 X[i].x/=N;
192 X[i].y/=N; 194 X[i].y/=N;
193 } 195 }
194 }//CIFFTCbii 196 }//CIFFTCbii
195 197
196 /* 198 /**
197 function CIFFTC: in-place complex IFFT 199 function CIFFTC: in-place complex IFFT
198 200
199 In: Order: integer, equals log2(Wid) 201 In: Order: integer, equals log2(Wid)
200 W[Wid/2]: twiddle factors 202 W[Wid/2]: twiddle factors
201 X[Wid]: complex spectrum 203 X[Wid]: complex spectrum
221 } 223 }
222 if (!bitinv1) free(bitinv); 224 if (!bitinv1) free(bitinv);
223 CIFFTCbii(Order, W, X); 225 CIFFTCbii(Order, W, X);
224 }//CIFFTC 226 }//CIFFTC
225 227
226 /* 228 /**
227 function CIFFTC: complex IFFT 229 function CIFFTC: complex IFFT
228 230
229 In: Input[Wid]: complex spectrum 231 In: Input[Wid]: complex spectrum
230 Order: integer, equals log2(Wid) 232 Order: integer, equals log2(Wid)
231 W[Wid/2]: twiddle factors 233 W[Wid/2]: twiddle factors
241 if (bitinv) CIFFTC(Order, W, X, bitinv); 243 if (bitinv) CIFFTC(Order, W, X, bitinv);
242 else CIFFTC(Order, W, X); 244 else CIFFTC(Order, W, X);
243 }//CIFFTC 245 }//CIFFTC
244 246
245 //--------------------------------------------------------------------------- 247 //---------------------------------------------------------------------------
246 /* 248 /**
247 function CIFFTR: complex-to-real IFFT 249 function CIFFTR: complex-to-real IFFT
248 250
249 In: Input[Wid/2+1]: complex spectrum, imaginary parts of Input[0] and Input[Wid/2] are ignored 251 In: Input[Wid/2+1]: complex spectrum, imaginary parts of Input[0] and Input[Wid/2] are ignored
250 Order: integer, equals log2(Wid) 252 Order: integer, equals log2(Wid)
251 W[Wid/2]: twiddle factors 253 W[Wid/2]: twiddle factors
319 for (int i=0; i<N; i++) X[i]*=norm; 321 for (int i=0; i<N; i++) X[i]*=norm;
320 if (!hbitinv1) free(hbitinv); 322 if (!hbitinv1) free(hbitinv);
321 }//CIFFTR 323 }//CIFFTR
322 324
323 //--------------------------------------------------------------------------- 325 //---------------------------------------------------------------------------
324 /* 326 /**
325 function CreateBitInvTable: creates a table of bit-inversed integers 327 function CreateBitInvTable: creates a table of bit-inversed integers
326 328
327 In: Order: interger, equals log2(size of table) 329 In: Order: interger, equals log2(size of table)
328 330
329 Returns a pointer to a newly allocated array containing the table. The returned pointer must be freed 331 Returns a pointer to a newly allocated array containing the table. The returned pointer must be freed
337 return result; 339 return result;
338 }//CreateBitInvTable 340 }//CreateBitInvTable
339 341
340 342
341 //--------------------------------------------------------------------------- 343 //---------------------------------------------------------------------------
342 /* 344 /**
343 function RFFTC_ual: unaligned real-to-complex FFT 345 function RFFTC_ual: unaligned real-to-complex FFT
344 346
345 In: Input[Wid]: real waveform 347 In: Input[Wid]: real waveform
346 Order; integer, equals log2(Wid) 348 Order; integer, equals log2(Wid)
347 W[Wid/2]: twiddle factors 349 W[Wid/2]: twiddle factors
425 if (Amp) for (i=0; i<N; i++) Amp[i]=sqrt(X[i].x*X[i].x+X[i].y*X[i].y); 427 if (Amp) for (i=0; i<N; i++) Amp[i]=sqrt(X[i].x*X[i].x+X[i].y*X[i].y);
426 if (Arg) for (i=0; i<N; i++) Arg[i]=Atan2(X[i].x, X[i].y); 428 if (Arg) for (i=0; i<N; i++) Arg[i]=Atan2(X[i].x, X[i].y);
427 }//RFFTC_ual 429 }//RFFTC_ual
428 430
429 //--------------------------------------------------------------------------- 431 //---------------------------------------------------------------------------
430 /* 432 /**
431 function RFFTCW: real-to-complex FFT with window 433 function RFFTCW: real-to-complex FFT with window
432 434
433 In: Input[Wid]: real waveform 435 In: Input[Wid]: real waveform
434 Window[Wid]: window function 436 Window[Wid]: window function
435 Order; integer, equals log2(Wid) 437 Order; integer, equals log2(Wid)
473 475
474 RFFTC_ual(0, Amp, Arg, Order, W, X, hbitinv); 476 RFFTC_ual(0, Amp, Arg, Order, W, X, hbitinv);
475 if (!hbitinv1) free(hbitinv); 477 if (!hbitinv1) free(hbitinv);
476 }//RFFTCW 478 }//RFFTCW
477 479
478 /* 480 /**
479 function RFFTCW: real-to-complex FFT with window 481 function RFFTCW: real-to-complex FFT with window
480 482
481 In: Input[Wid]: real waveform as _int16 array 483 In: Input[Wid]: real waveform as _int16 array
482 Window[Wid]: window function 484 Window[Wid]: window function
483 Order; integer, equals log2(Wid) 485 Order; integer, equals log2(Wid)
503 RFFTC_ual(0, Amp, Arg, Order, W, X, hbitinv); 505 RFFTC_ual(0, Amp, Arg, Order, W, X, hbitinv);
504 if (!hbitinv1) free(hbitinv); 506 if (!hbitinv1) free(hbitinv);
505 }//RFFTCW 507 }//RFFTCW
506 508
507 //--------------------------------------------------------------------------- 509 //---------------------------------------------------------------------------
508 /* 510 /**
509 function CFFTCW: complex FFT with window 511 function CFFTCW: complex FFT with window
510 512
511 In: Window[Wid]: window function 513 In: Window[Wid]: window function
512 Order: integer, equals log2(Wid) 514 Order: integer, equals log2(Wid)
513 W[Wid/2]: twiddle factors 515 W[Wid/2]: twiddle factors
522 int N=1<<Order; 524 int N=1<<Order;
523 for (int i=0; i<N; i++) X[i].x*=Window[i], X[i].y*=Window[i]; 525 for (int i=0; i<N; i++) X[i].x*=Window[i], X[i].y*=Window[i];
524 CFFTC(Order, W, X, bitinv); 526 CFFTC(Order, W, X, bitinv);
525 }//CFFTCW 527 }//CFFTCW
526 528
527 /* 529 /**
528 function CFFTCW: complex FFT with window 530 function CFFTCW: complex FFT with window
529 531
530 In: Input[Wid]: complex waveform 532 In: Input[Wid]: complex waveform
531 Window[Wid]: window function 533 Window[Wid]: window function
532 Order: integer, equals log2(Wid) 534 Order: integer, equals log2(Wid)
545 for (int i=0; i<N; i++) X[i].x=Input[i].x*Window[i], X[i].y=Input[i].y*Window[i]; 547 for (int i=0; i<N; i++) X[i].x=Input[i].x*Window[i], X[i].y=Input[i].y*Window[i];
546 CFFTC(X, Amp, Arg, Order, W, X, bitinv); 548 CFFTC(X, Amp, Arg, Order, W, X, bitinv);
547 }//CFFTCW 549 }//CFFTCW
548 550
549 //--------------------------------------------------------------------------- 551 //---------------------------------------------------------------------------
550 /* 552 /**
551 function RDCT1: fast DCT1 implemented using FFT. DCT-I has the time scale 0.5-sample shifted from the DFT. 553 function RDCT1: fast DCT1 implemented using FFT. DCT-I has the time scale 0.5-sample shifted from the DFT.
552 554
553 In: Input[Wid]: real waveform 555 In: Input[Wid]: real waveform
554 Order: integer, equals log2(Wid) 556 Order: integer, equals log2(Wid)
555 qW[Wid/8]: quarter table of twiddle factors 557 qW[Wid/8]: quarter table of twiddle factors
642 if (createbitinv) free(qbitinv); 644 if (createbitinv) free(qbitinv);
643 free8(even); 645 free8(even);
644 }//RDCT1 646 }//RDCT1
645 647
646 //--------------------------------------------------------------------------- 648 //---------------------------------------------------------------------------
647 /* 649 /**
648 function RDCT4: fast DCT4 implemented using FFT. DCT-IV has both the time and frequency scales 650 function RDCT4: fast DCT4 implemented using FFT. DCT-IV has both the time and frequency scales
649 0.5-sample or 0.5-bin shifted from DFT. 651 0.5-sample or 0.5-bin shifted from DFT.
650 652
651 In: Input[Wid]: real waveform 653 In: Input[Wid]: real waveform
652 Order: integer, equals log2(Wid) 654 Order: integer, equals log2(Wid)
689 Output[N-1-2*i]=-gr*si-gi*co; 691 Output[N-1-2*i]=-gr*si-gi*co;
690 } 692 }
691 }//RDCT4 693 }//RDCT4
692 694
693 //--------------------------------------------------------------------------- 695 //---------------------------------------------------------------------------
694 /* 696 /**
695 function RIDCT1: fast IDCT1 implemented using FFT. 697 function RIDCT1: fast IDCT1 implemented using FFT.
696 698
697 In: Input[Wid]: DCT-I of some real waveform. 699 In: Input[Wid]: DCT-I of some real waveform.
698 Order: integer, equals log2(Wid) 700 Order: integer, equals log2(Wid)
699 qW[Wid/8]: quarter table of twiddle factors 701 qW[Wid/8]: quarter table of twiddle factors
799 if (createbitinv) free(qbitinv); 801 if (createbitinv) free(qbitinv);
800 free8(even); 802 free8(even);
801 }//RIDCT1 803 }//RIDCT1
802 804
803 //--------------------------------------------------------------------------- 805 //---------------------------------------------------------------------------
804 /* 806 /**
805 function RIDCT4: fast IDCT4 implemented using FFT. 807 function RIDCT4: fast IDCT4 implemented using FFT.
806 808
807 In: Input[Wid]: DCT-IV of some real waveform 809 In: Input[Wid]: DCT-IV of some real waveform
808 Order: integer, equals log2(Wid) 810 Order: integer, equals log2(Wid)
809 hW[Wid/4]: half table of twiddle factors 811 hW[Wid/4]: half table of twiddle factors
846 Output[N-1-2*i]=(-gr*si-gi*co)*i2N; 848 Output[N-1-2*i]=(-gr*si-gi*co)*i2N;
847 } 849 }
848 }//RIDCT4 850 }//RIDCT4
849 851
850 //--------------------------------------------------------------------------- 852 //---------------------------------------------------------------------------
851 /* 853 /**
852 function RLCT: real local cosine transform of equal frame widths Wid=2^Order 854 function RLCT: real local cosine transform of equal frame widths Wid=2^Order
853 855
854 In: data[Count]: real waveform 856 In: data[Count]: real waveform
855 Order: integer, equals log2(Wid), Wid being the cosine atom size 857 Order: integer, equals log2(Wid), Wid being the cosine atom size
856 g[wid]: lap window, designed so that g[k] increases from 0 to 1 and g[k]^2+g[wid-1-k]^2=1 858 g[wid]: lap window, designed so that g[k] increases from 0 to 1 and g[k]^2+g[wid-1-k]^2=1
906 free(hbitinv); 908 free(hbitinv);
907 free8(hx); 909 free8(hx);
908 }//RLCT 910 }//RLCT
909 911
910 //--------------------------------------------------------------------------- 912 //---------------------------------------------------------------------------
911 /* 913 /**
912 function RILCT: inverse local cosine transform of equal frame widths Wid=2^Order 914 function RILCT: inverse local cosine transform of equal frame widths Wid=2^Order
913 915
914 In: spec[Fr][Wid]: the local cosine transform 916 In: spec[Fr][Wid]: the local cosine transform
915 Order: integer, equals log2(Wid), Wid being the cosine atom size 917 Order: integer, equals log2(Wid), Wid being the cosine atom size
916 g[wid]: lap window, designed so that g[k] increases from 0 to 1 and g[k]^2+g[wid-1-k]^2=1. 918 g[wid]: lap window, designed so that g[k] increases from 0 to 1 and g[k]^2+g[wid-1-k]^2=1.
951 ////The following line can be removed if the corresponding line in RLCT(...) is removed 953 ////The following line can be removed if the corresponding line in RLCT(...) is removed
952 for (int i=0; i<Count; i++) data[i]*=norm; 954 for (int i=0; i<Count; i++) data[i]*=norm;
953 }//RILCT 955 }//RILCT
954 956
955 //--------------------------------------------------------------------------- 957 //---------------------------------------------------------------------------
956 /* 958 /**
957 function CMFTC: radix-2 complex multiresolution Fourier transform 959 function CMFTC: radix-2 complex multiresolution Fourier transform
958 960
959 In: x[Wid]: complex waveform 961 In: x[Wid]: complex waveform
960 Order: integer, equals log2(Wid) 962 Order: integer, equals log2(Wid)
961 W[Wid/2]: twiddle factors 963 W[Wid/2]: twiddle factors