| 1 |
|
| /* Fft.c |
| 2 |
|
| |
| 3 |
|
| 1-d fast Fourier transform subroutine. |
| 4 |
|
| From Claerbout (1985) p. 70. |
| 5 |
|
| |
| 6 |
|
| Usage: int lx; |
| 7 |
|
| float signi, scale; |
| 8 |
|
| struct complex cx[]; |
| 9 |
|
| |
| 10 |
|
| fft(lx, cx, signi, scale); |
| 11 |
|
| |
| 12 |
|
| Compile: cc prog.c fft.c -fswitch -lm -o prog.c |
| 13 |
|
| |
| 14 |
|
| Arguments: lx number of elements of vector to transform |
| 15 |
|
| MUST BE A POWER OF 2 |
| 16 |
|
| cx pointer to vector of complex structures |
| 17 |
|
| signi sign of transform- +1 forward to Fourier domain |
| 18 |
|
| -1 inverse to real domain |
| 19 |
|
| scale scale factor to apply to data before tranform |
| 20 |
|
| */ |
| 21 |
|
| |
| 22 |
|
| /* Header info has been moved to fft.h, struct complex is now a typedef */ |
| 23 |
|
| /* and other slight changes have been made. -- Walter Szeliga Mar 05 */ |
| 24 |
|
| |
| 25 |
|
| /* Include the usual header files */ |
| 26 |
|
| #include <stdio.h> |
| 27 |
|
| #include <math.h> |
| 28 |
|
| #include "fft.h" |
| 29 |
|
| |
| 30 |
|
| /* Define complex multiplication and its conjugate */ |
| 31 |
|
| #define rmul(x,y) (x.re * y.re - x.im * y.im) |
| 32 |
|
| #define imul(x,y) (x.im * y.re + x.re * y.im) |
| 33 |
|
| #define rcmul(x,y) (x.re * y.re + x.im * y.im) |
| 34 |
|
| #define icmul(x,y) (x.im * y.re - x.re * y.im) |
| 35 |
|
| |
| 36 |
|
| /* Body of subroutine */ |
| 37 |
|
| int |
| 38 |
4 |
| fft(int lx,complex *cx,float signi,float sc) |
| 39 |
|
| { |
| 40 |
|
| /* Declare all local variables */ |
| 41 |
|
| int i, j, k, m, istep; |
| 42 |
|
| float arg; |
| 43 |
|
| complex cw, ct; |
| 44 |
|
| |
| 45 |
4 |
| j = 0; |
| 46 |
4 |
| k = 1; |
| 47 |
|
| /* Left out for compatibility with colmft.c |
| 48 |
|
| sc = (float)(sqrt(1.0/(double)(lx))); |
| 49 |
|
| */ |
| 50 |
452 |
▶ | for(i=0; i<lx; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<lx; i++) |
448 |
4 |
452 |
|
| 51 |
|
| { |
| 52 |
448 |
▶ | if (i <= j) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i <= j) |
252 |
196 |
448 |
|
| 53 |
|
| { |
| 54 |
252 |
| ct.re = sc * cx[j].re; |
| 55 |
252 |
| ct.im = sc * cx[j].im; |
| 56 |
252 |
| cx[j].re = sc * cx[i].re; |
| 57 |
252 |
| cx[j].im = sc * cx[i].im; |
| 58 |
252 |
| cx[i].re = ct.re; |
| 59 |
252 |
| cx[i].im = ct.im; |
| 60 |
|
| } |
| 61 |
448 |
| m = lx/2; |
| 62 |
888 |
▶ | while (j > m-1) |
Branch: 1 branch
| Branch | True | False | Total |
| while (j > m-1) |
444 |
444 |
888 |
|
| 63 |
|
| { |
| 64 |
444 |
| j = j - m; |
| 65 |
444 |
| m = m/2; |
| 66 |
444 |
▶ | if (m < 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (m < 1) |
4 |
440 |
444 |
|
| 67 |
4 |
| break; |
| 68 |
|
| } |
| 69 |
448 |
| j = j + m; |
| 70 |
|
| } |
| 71 |
|
| do |
| 72 |
|
| { |
| 73 |
27 |
| istep = 2*k; |
| 74 |
471 |
▶ | for (m=0; m<k; m++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (m=0; m<k; m++) |
444 |
27 |
471 |
|
| 75 |
|
| { |
| 76 |
444 |
| arg = 3.14159265*signi*m/k; |
| 77 |
444 |
| cw.re = (float)(cos((double)(arg))); |
| 78 |
444 |
| cw.im = (float)(sin((double)(arg))); |
| 79 |
1980 |
▶ | for (i=m; i<lx; i+=istep) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=m; i<lx; i+=istep) |
1536 |
444 |
1980 |
|
| 80 |
|
| { |
| 81 |
1536 |
| ct.re = rmul(cw, cx[i+k]); |
| 82 |
1536 |
| ct.im = imul(cw, cx[i+k]); |
| 83 |
1536 |
| cx[i+k].re = cx[i].re - ct.re; |
| 84 |
1536 |
| cx[i+k].im = cx[i].im - ct.im; |
| 85 |
1536 |
| cx[i].re = cx[i].re + ct.re; |
| 86 |
1536 |
| cx[i].im = cx[i].im + ct.im; |
| 87 |
|
| } |
| 88 |
|
| } |
| 89 |
27 |
| k = istep; |
| 90 |
|
| } |
| 91 |
27 |
▶ | while (k < lx); |
Branch: 1 branch
| Branch | True | False | Total |
| while (k < lx); |
23 |
4 |
27 |
|
| 92 |
4 |
| return(0); |
| 93 |
|
| } |