From 5802e2fb1162ea137a2b37526e9bcbd5244f4dcc Mon Sep 17 00:00:00 2001 From: Gathros <6323830+Gathros@users.noreply.github.com> Date: Thu, 3 Jan 2019 11:37:02 +0000 Subject: [PATCH 1/2] adding fftw3 test to fft --- contents/cooley_tukey/code/c/fft.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/contents/cooley_tukey/code/c/fft.c b/contents/cooley_tukey/code/c/fft.c index 839507df6..90691f373 100644 --- a/contents/cooley_tukey/code/c/fft.c +++ b/contents/cooley_tukey/code/c/fft.c @@ -4,6 +4,23 @@ #include <stdlib.h> #include <string.h> #include <time.h> +#include <fftw3.h> + +void fft(double complex *x, int n) { + double complex y[n]; + memset(y, 0, sizeof(y)); + fftw_plan p; + + p = fftw_plan_dft_1d(n, (fftw_complex*)x, (fftw_complex*)y, + FFTW_FORWARD, FFTW_ESTIMATE); + + fftw_execute(p); + fftw_destroy_plan(p); + + for (size_t i = 0; i < n; ++i) { + x[i] = y[i] / sqrt((double)n); + } +} void dft(double complex *X, const size_t N) { double complex tmp[N]; @@ -79,8 +96,12 @@ void iterative_cooley_tukey(double complex *X, size_t N) { void approx(double complex *X, double complex *Y, size_t N) { for (size_t i = 0; i < N; ++i) { - printf("%f\n", cabs(X[i]) - cabs(Y[i])); + if (cabs(X[i]) - cabs(Y[i]) > 1E-5) { + printf("This is not approximate.\n"); + return; + } } + printf("This is approximate.\n"); } int main() { @@ -92,10 +113,12 @@ int main() { z[i] = x[i]; } + fft(x, 64); cooley_tukey(y, 64); iterative_cooley_tukey(z, 64); - approx(y, z, 64); + approx(x, y, 64); + approx(x, z, 64); return 0; } From 01e43f80acd1a48116646d56a88ef3acef56d0c5 Mon Sep 17 00:00:00 2001 From: Gathros <6323830+Gathros@users.noreply.github.com> Date: Thu, 3 Jan 2019 11:39:47 +0000 Subject: [PATCH 2/2] updating cooley_tukey.md --- contents/cooley_tukey/cooley_tukey.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contents/cooley_tukey/cooley_tukey.md b/contents/cooley_tukey/cooley_tukey.md index fc51aa459..27d74e6da 100644 --- a/contents/cooley_tukey/cooley_tukey.md +++ b/contents/cooley_tukey/cooley_tukey.md @@ -72,7 +72,7 @@ For some reason, though, putting code to this transformation really helped me fi {% sample lang="jl" %} [import:4-13, lang:"julia"](code/julia/fft.jl) {% sample lang="c" %} -[import:8-19, lang:"c"](code/c/fft.c) +[import:25-35, lang:"c"](code/c/fft.c) {% sample lang="clj" %} [import:15-30, lang:"clojure"](code/clojure/fft.clj) {% sample lang="cpp" %} @@ -121,7 +121,7 @@ In the end, the code looks like: {% sample lang="jl" %} [import:16-32, lang:"julia"](code/julia/fft.jl) {% sample lang="c" %} -[import:20-39, lang:"c"](code/c/fft.c) +[import:37-56, lang:"c"](code/c/fft.c) {% sample lang="clj" %} [import:31-58, lang:"clojure"](code/clojure/fft.clj) {% sample lang="cpp" %}