Skip to content

Commit 4e3ac16

Browse files
Gathrosleios
authored andcommitted
C adding FFT test (#571)
* adding fftw3 test to fft * updating cooley_tukey.md
1 parent 1048a07 commit 4e3ac16

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

contents/cooley_tukey/code/c/fft.c

+25-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@
44
#include <stdlib.h>
55
#include <string.h>
66
#include <time.h>
7+
#include <fftw3.h>
8+
9+
void fft(double complex *x, int n) {
10+
double complex y[n];
11+
memset(y, 0, sizeof(y));
12+
fftw_plan p;
13+
14+
p = fftw_plan_dft_1d(n, (fftw_complex*)x, (fftw_complex*)y,
15+
FFTW_FORWARD, FFTW_ESTIMATE);
16+
17+
fftw_execute(p);
18+
fftw_destroy_plan(p);
19+
20+
for (size_t i = 0; i < n; ++i) {
21+
x[i] = y[i] / sqrt((double)n);
22+
}
23+
}
724

825
void dft(double complex *X, const size_t N) {
926
double complex tmp[N];
@@ -79,8 +96,12 @@ void iterative_cooley_tukey(double complex *X, size_t N) {
7996

8097
void approx(double complex *X, double complex *Y, size_t N) {
8198
for (size_t i = 0; i < N; ++i) {
82-
printf("%f\n", cabs(X[i]) - cabs(Y[i]));
99+
if (cabs(X[i]) - cabs(Y[i]) > 1E-5) {
100+
printf("This is not approximate.\n");
101+
return;
102+
}
83103
}
104+
printf("This is approximate.\n");
84105
}
85106

86107
int main() {
@@ -92,10 +113,12 @@ int main() {
92113
z[i] = x[i];
93114
}
94115

116+
fft(x, 64);
95117
cooley_tukey(y, 64);
96118
iterative_cooley_tukey(z, 64);
97119

98-
approx(y, z, 64);
120+
approx(x, y, 64);
121+
approx(x, z, 64);
99122

100123
return 0;
101124
}

contents/cooley_tukey/cooley_tukey.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ For some reason, though, putting code to this transformation really helped me fi
7272
{% sample lang="jl" %}
7373
[import:4-13, lang:"julia"](code/julia/fft.jl)
7474
{% sample lang="c" %}
75-
[import:8-19, lang:"c"](code/c/fft.c)
75+
[import:25-35, lang:"c"](code/c/fft.c)
7676
{% sample lang="clj" %}
7777
[import:15-30, lang:"clojure"](code/clojure/fft.clj)
7878
{% sample lang="cpp" %}
@@ -121,7 +121,7 @@ In the end, the code looks like:
121121
{% sample lang="jl" %}
122122
[import:16-32, lang:"julia"](code/julia/fft.jl)
123123
{% sample lang="c" %}
124-
[import:20-39, lang:"c"](code/c/fft.c)
124+
[import:37-56, lang:"c"](code/c/fft.c)
125125
{% sample lang="clj" %}
126126
[import:31-58, lang:"clojure"](code/clojure/fft.clj)
127127
{% sample lang="cpp" %}

0 commit comments

Comments
 (0)