FFT:如何修改该算法以返回系数表示
FFT: How could this algorithm be modified to return to coefficient representation?
下面是Cooley-Tukey FFT算法的base-2实现(在Rosetta Code中找到)。运行一次FFT后,数据数组将从系数表示变为点值表示。怎么转换回系数呢?
#include <complex>
#include <iostream>
#include <valarray>
const double PI = 3.141592653589793238460;
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;
// Cooley–Tukey FFT (in-place)
void fft(CArray& x)
{
const size_t N = x.size();
if (N <= 1) return;
// divide
CArray even = x[std::slice(0, N/2, 2)];
CArray odd = x[std::slice(1, N/2, 2)];
// conquer
fft(even);
fft(odd);
// combine
for (size_t k = 0; k < N/2; ++k)
{
Complex t = std::polar(1.0, -2 * PI * k / N) * odd[k];
x[k ] = even[k] + t;
x[k+N/2] = even[k] - t;
}
}
int main()
{
const Complex test[] = { 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0 };
CArray data(test, 8);
fft(data);
for (int i = 0; i < 8; ++i)
{
std::cout << data[i] << "n";
}
return 0;
}
计算FFT逆
改变-2 * PI * k / N
2 * PI * k / N
在做逆FFT之后,将输出缩放1/N
添加到Rosetta代码
// inverse fft (in-place)
void ifft(CArray& x)
{
// conjugate the complex numbers
std::transform(&x[0], &x[x.size()], &x[0], std::conj<double>);
// forward fft
fft( x );
// conjugate the complex numbers again
std::transform(&x[0], &x[x.size()], &x[0], std::conj<double>);
// scale the numbers
x /= x.size();
}
相关文章:
- 返回arg_min和可选min_val的算法
- 对于具有引用返回类型的搜索算法,默认返回值应该是什么?
- 如何使用贝尔曼-福特算法返回所有具有捆绑重量的最短路径?
- 如何检查 range:: 算法(如 find_if)是否返回了值?
- 如何检查 CNG Windows API 是否返回符合 FIPS 的算法实现
- 算法中的排序函数返回错误
- 字符串相似性算法返回错误值
- 如何从递归算法返回节点
- 使用霍尔分区方案的快速排序算法返回原始未排序列表
- C - 哈希算法代码什么都没有返回
- C 常数参考返回算法=作为非函数的声明
- 返回tictactoe的minimax算法中的bestMove
- 向量返回/加法算法的问题
- 我的快速选择算法未返回正确的值
- 从搜索算法返回的约定 - 索引或迭代器
- 为什么我的c++算法总是为一个数字的最小值和最大值返回相同的值
- 如果我想查找某些内容,但不希望它在找不到时最后返回,我应该使用哪种 STL 算法?
- STL计数算法返回不正确的值
- Dijkstra 算法返回不正确的值
- 如何简化检查搜索算法返回的迭代器是否有效