Skip to content

Commit 3881ea2

Browse files
authored
Merge pull request thuva4#384 from syam3526/patch-5
fast fourier transform using c
2 parents 1c1204c + 40fdb13 commit 3881ea2

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

FastFourierTransform/C/fft_c.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <stdio.h>
2+
#include <math.h>
3+
#include <complex.h>
4+
5+
double PI;
6+
typedef double complex cplx;
7+
8+
void _fft(cplx buf[], cplx out[], int n, int step)
9+
{
10+
if (step < n) {
11+
_fft(out, buf, n, step * 2);
12+
_fft(out + step, buf + step, n, step * 2);
13+
14+
for (int i = 0; i < n; i += 2 * step) {
15+
cplx t = cexp(-I * PI * i / n) * out[i + step];
16+
buf[i / 2] = out[i] + t;
17+
buf[(i + n)/2] = out[i] - t;
18+
}
19+
}
20+
}
21+
22+
void fft(cplx buf[], int n)
23+
{
24+
cplx out[n];
25+
for (int i = 0; i < n; i++) out[i] = buf[i];
26+
27+
_fft(buf, out, n, 1);
28+
}
29+
30+
31+
void show(const char * s, cplx buf[]) {
32+
printf("%s", s);
33+
for (int i = 0; i < 8; i++)
34+
if (!cimag(buf[i]))
35+
printf("%g ", creal(buf[i]));
36+
else
37+
printf("(%g, %g) ", creal(buf[i]), cimag(buf[i]));
38+
}
39+
40+
int main()
41+
{
42+
PI = atan2(1, 1) * 4;
43+
cplx buf[] = {1, 1, 1, 1, 0, 0, 0, 0};
44+
45+
show("Data: ", buf);
46+
fft(buf, 8);
47+
show("\nFFT : ", buf);
48+
49+
return 0;
50+
}
51+

0 commit comments

Comments
 (0)