-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFFT.c
51 lines (44 loc) · 1.28 KB
/
FFT.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <fftw3.h>
#include <time.h>
#include <stdlib.h>
int main(int argc, char* argv[]){
int N = 100;
fftw_complex *in, *out, *back_out;
fftw_plan my_plan;
fftw_plan back_plan;
unsigned int seed = 123456789;
in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*N);
out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*N);
back_out =(fftw_complex*)fftw_malloc(sizeof(fftw_complex)*N);
srand(seed);
for(int i =0; i < N; i++){
in[i][0] = (double)rand() / (double) RAND_MAX;
in[i][1] = (double)rand() / (double) RAND_MAX;
}
printf("\n");
printf("Input FFT coefficients:\n");
printf("\n");
for(int i = 0; i < N; i++){
printf("%3d %12f %12f\n", i , in[i][0], in[i][1]);
}
my_plan = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_execute(my_plan);
printf("\n");
printf("Output of FFT : \n");
for(int i = 0; i < N; i++){
printf("%3d %12f %12f\n", i, out[i][0], out[i][1]);
}
printf("Backward FFT: \n");
back_plan = fftw_plan_dft_1d(N, out, back_out, FFTW_BACKWARD, FFTW_ESTIMATE);
fftw_execute(back_plan);
for(int i = 0; i < N; i ++){
printf("%3d %12f %12f\n", i, back_out[i][0]/100, back_out[i][1]/100);
}
fftw_destroy_plan(my_plan);
fftw_destroy_plan(back_plan);
fftw_free(in);
fftw_free(out);
fftw_free(back_out);
return 0;
}