# 快速傅里叶变换原理介绍及递归代码实现

O(n^2)和O(nlogn)的差距在哪里，这里的log底数是2。

# 暴力破解法

#include<cmath>
#include<iostream>
#include<complex>
#include<vector>

using namespace std;
const double pi = acos(-1.0);

vector<complex<double>> fft(vector<complex<double>> input)
{
vector<complex<double>> output;
int N = input.size(), k = 0, n = 0;
for (k = 0; k < N; k++)
{
complex<double> sum(0,0);
for (n = 0; n < N; n++)
{
complex<double> wn(polar<double>(1, -2*pi/N * n * k));
sum += input[n] * wn;
}
output.push_back(sum);
}
return output;
}

int main()
{
vector<complex<double>> in{1, 2, 3};
auto re = fft(in);
for (auto i: re)
{
cout << "real:" << i.real() << " imag:" << i.imag() << endl;
}
return 0;
}


# 递归法（自顶向下）

void FFT_recursion(vector<complex<double>> &input)
{
int n = input.size();
if(n == 1)
return;
int mid= input.size()/2;
vector<complex<double>> A0, A1;
for(int i = 0; i < n; i += 2){//拆分奇偶下标项
A0.push_back(input[i]);
A1.push_back(input[i + 1]);
}
FFT_recursion(A0);
FFT_recursion(A1);
complex<double> w0(1,0);
complex<double> wn(polar<double>(1, -2*pi/n));//单位根
for(int i=0; i < mid; i++, w0*=wn){//合并多项式
input[i] = A0[i] + w0*A1[i];
input[i + mid] = A0[i] - w0*A1[i];
}
}


# 参考资料

posted @ 2021-08-03 12:27  Binfun  阅读(91)  评论(0编辑  收藏  举报