-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFFT.cpp
More file actions
61 lines (58 loc) · 1.24 KB
/
FFT.cpp
File metadata and controls
61 lines (58 loc) · 1.24 KB
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
#include <bits/stdc++.h>
using namespace std;
typedef complex<double> comp;
vector<comp> fft(vector<comp>& a)
{
int n = a.size();
if (n == 1)
return vector<comp>(1, a[0]);
vector<comp> w(n);
for (int i = 0; i < n; i++) {
double angle = -2 * M_PI * i / n;
w[i] = comp(cos(angle), sin(angle));
}
vector<comp> P0(n / 2), P1(n / 2);
for (int i = 0; i < n / 2; i++) {
P0[i] = a[i * 2];
P1[i] = a[i * 2 + 1];
}
vector<comp> y0 = fft(P0);
vector<comp> y1 = fft(P1);
vector<comp> 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;
}
int main()
{
int n;
cout<<"Give signal length\n";
cin>>n;
bool isvalid=false;
for(int i=0;i<20;i++)
{
int mask=1<<i;
if(n==mask)
isvalid=true;
}
if(!isvalid)
{
cout<<"Invalid input\n";
return 0;
}
cout<<"Give signal values\n";
vector<comp> a(n);
for(int i=0;i<n;i++)
{
int x;
cin>>x;
a[i]=x;
}
vector<comp> b = fft(a);
cout<<"Output:\n";
for (int i = 0; i < n; i++)
cout << b[i] << endl;
return 0;
}