-
Notifications
You must be signed in to change notification settings - Fork 0
/
fft.cpp
66 lines (54 loc) · 1.11 KB
/
fft.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
@@ -0,0 +1,64 @@
#include <bits/stdc++.h>
using namespace std;
//for sorting complex values of nth roots
//of unity we use complex<double>
typedef complex<double> cd;
//recursive function of FFT
vector<cd> fft(vector<cd>& a)
{
int n=a.size();
//if input contains just one element
if(n==1)
return vector<cd>(1,a[0]);
//
vector<cd> w(n);
for(int i=0;i<n;i++)
{
double alpha=2*M_PI*i/n;
w[i]=cd(cos(alpha),sin(alpha));
}
vector<cd> A0(n/2),A1(n/2);
for(int i=0;i<n/2;i++)
{
//even indexed coefficients
A0[i]=a[i*2];
//odd indexed coefficients
A1[i]=a[i*2+1];
}
//recursive call
vector<cd> y0=fft(A0);
vector<cd> y1=fft(A1);
//for storing values of y0,y1,...yn-1
vector<cd> y(n);
for(int k=0;k<n/2;k++)
{
y[k]=y0[k]+w[k]*y1[k];
y[k+n/2]=y0[k]-w[k]*y1[k];
}
return y;
}
//driver code
int main()
{
vector<cd> a(1000);
for(int i=0;i<1000;i++)
a[i]=i+1;
vector<cd> b=fft(a);
for(int i=0;i<1000;i++)
{
cout<<b[i]<<endl;
}
return 0;
}
//1