Download code

Jump to: navigation, search

Back to Cooley-Tukey_FFT_algorithm_(C)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

fft.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Cooley-Tukey_FFT_algorithm_(C)?oldid=19032
14 */
15 
16 #include "fft.h"
17 #include <stdlib.h>
18 
19 
20 #define PI  3.1415926535897932
21 
22 complex* DFT_naive_1(complex* x, int N) {
23     complex* X = (complex*) malloc(sizeof(struct complex_t) * N);
24     int k, n;
25     for(k=0; k<N; k++) {
26         X[k].re = X[k].im = 0.0;
27         for(n=0; n<N; n++) {
28             X[k] = complex_add(X[k], complex_mult(x[n], complex_from_polar(1, -2*PI*n*k/N)));
29         }
30     }
31     return X;
32 }
33 complex* DFT_naive_2(complex* x, int N) {
34     complex* X = (complex*) malloc(sizeof(struct complex_t) * N);
35     complex* Nth_root = (complex*) malloc(sizeof(struct complex_t) * N);
36     int k, n;
37     for(k=0; k<N; k++) {
38         Nth_root[k] = complex_from_polar(1, -2*PI*k/N);
39     }
40     for(k=0; k<N; k++) {
41         X[k].re = X[k].im = 0.0;
42         for(n=0; n<N; n++) {
43             X[k] = complex_add(X[k], complex_mult(x[n], Nth_root[(n*k) % N]));
44         }
45     }
46     free(Nth_root);
47     return X;
48 }
49 
50 complex* FFT_simple(complex* x, int N /* must be a power of 2 */) {
51     complex* X = (complex*) malloc(sizeof(struct complex_t) * N);
52     complex * d, * e, * D, * E;
53     int k;
54 
55     if (N == 1) {
56         X[0] = x[0];
57         return X;
58     }
59 
60     e = (complex*) malloc(sizeof(struct complex_t) * N/2);
61     d = (complex*) malloc(sizeof(struct complex_t) * N/2);
62     for(k = 0; k < N/2; k++) {
63         e[k] = x[2*k];
64         d[k] = x[2*k + 1];
65     }
66 
67     E = FFT_simple(e, N/2);
68     D = FFT_simple(d, N/2);
69     
70     for(k = 0; k < N/2; k++) {
71         /* Multiply entries of D by the twiddle factors e^(-2*pi*i/N * k) */
72         D[k] = complex_mult(complex_from_polar(1, -2.0*PI*k/N), D[k]);
73     }
74 
75     for(k = 0; k < N/2; k++) {
76         X[k]       = complex_add(E[k], D[k]);
77         X[k + N/2] = complex_sub(E[k], D[k]);
78     }
79 
80     free(D);
81     free(E);
82     return X;
83 }
84 


hijacker
hijacker
hijacker
hijacker

complex_simple.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Cooley-Tukey_FFT_algorithm_(C)?oldid=19032
14 */
15 
16 #include "complex_simple.h"
17 #include <math.h>
18 
19 complex complex_from_polar(double r, double theta_radians) {
20     complex result;
21     result.re = r * cos(theta_radians);
22     result.im = r * sin(theta_radians);
23     return result;
24 }
25 
26 double complex_magnitude(complex c) {
27     return sqrt(c.re*c.re + c.im*c.im);
28 }
29 
30 complex complex_add(complex left, complex right) {
31     complex result;
32     result.re = left.re + right.re;
33     result.im = left.im + right.im;
34     return result;
35 }
36 
37 complex complex_sub(complex left, complex right) {
38     complex result;
39     result.re = left.re - right.re;
40     result.im = left.im - right.im;
41     return result;
42 }
43 
44 complex complex_mult(complex left, complex right) {
45     complex result;
46     result.re = left.re*right.re - left.im*right.im;
47     result.im = left.re*right.im + left.im*right.re;
48     return result;
49 }
50 


hijacker
hijacker
hijacker
hijacker

fft.h

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Cooley-Tukey_FFT_algorithm_(C)?oldid=19032
14 */
15 
16 #ifndef _FFT_H_
17 #define _FFT_H_
18 
19 #include "complex_simple.h"
20 
21 complex* DFT_naive_1(complex* x, int N);
22 complex* DFT_naive_2(complex* x, int N);
23 complex* FFT_simple(complex* x, int N /* must be a power of 2 */);
24 
25 #endif /* #ifndef _FFT_H_ */
26 


hijacker
hijacker
hijacker
hijacker

complex_simple.h

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Cooley-Tukey_FFT_algorithm_(C)?oldid=19032
14 */
15 
16 #ifndef _COMPLEX_SIMPLE_H_
17 #define _COMPLEX_SIMPLE_H_
18 
19 typedef struct complex_t {
20     double re;
21     double im;
22 } complex;
23 
24 complex complex_from_polar(double r, double theta_radians);
25 double  complex_magnitude(complex c);
26 complex complex_add(complex left, complex right);
27 complex complex_sub(complex left, complex right);
28 complex complex_mult(complex left, complex right);
29 
30 
31 #endif /* #ifndef _COMPLEX_SIMPLE_H_ */
32 


hijacker
hijacker
hijacker
hijacker