Skip to content

C adding FFT test #571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions contents/cooley_tukey/code/c/fft.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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() {
Expand All @@ -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;
}
4 changes: 2 additions & 2 deletions contents/cooley_tukey/cooley_tukey.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" %}
Expand Down Expand Up @@ -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" %}
Expand Down