【问题标题】:FFT in a single C-file [closed]单个 C 文件中的 FFT [关闭]
【发布时间】:2012-01-10 09:42:56
【问题描述】:

我一直在寻找 C 中的 FFT 实现。但是,我不是在寻找大型库(如 FFTW),而是在寻找易于使用的单个 C 文件实现。不幸的是,我一直找不到这样的东西。

有人可以推荐一个简单的实现吗?

【问题讨论】:

  • 试试searching for 'fft' on github。但是FFTW有什么不好用的呢?您的意思是易于理解来源吗?
  • 自己写。这将是一个很好的锻炼。互联网上到处都是关于如何计算 DFT 和 FFT 的解释。使用它。
  • FFT 例程here 的代码不到一百行。该库使用时间抽取 (DIT) 和频率抽取 (DIF) 实现正向和反向快速傅里叶变换 (FFT) 算法。
  • @DaBler 这正是我正在寻找的!谢谢!

标签: c fft


【解决方案1】:

你最好的选择是KissFFT——顾名思义它是simple,但它仍然相当快,而且比FFTW轻得多。它也是免费的,而如果您想将 FFTW 包含在商业产品中,则需要支付高额的许可费。

【讨论】:

  • 仅当您重新分发它,而不是在 GPL 下发布源代码时...
【解决方案2】:

此文件可以正常工作:只需复制并粘贴到您的计算机中即可。 在网上冲浪我在维基百科页面here 上找到了这个简单的实现。该页面是意大利语的,所以我用一些翻译重新编写了代码。 Here 有几乎相同的信息,但都是英文的。尽情享受吧!

#include <iostream>
#include <complex>
#define MAX 200

using namespace std;

#define M_PI 3.1415926535897932384

int log2(int N)    /*function to calculate the log2(.) of int numbers*/
{
  int k = N, i = 0;
  while(k) {
    k >>= 1;
    i++;
  }
  return i - 1;
}

int check(int n)    //checking if the number of element is a power of 2
{
  return n > 0 && (n & (n - 1)) == 0;
}

int reverse(int N, int n)    //calculating revers number
{
  int j, p = 0;
  for(j = 1; j <= log2(N); j++) {
    if(n & (1 << (log2(N) - j)))
      p |= 1 << (j - 1);
  }
  return p;
}

void ordina(complex<double>* f1, int N) //using the reverse order in the array
{
  complex<double> f2[MAX];
  for(int i = 0; i < N; i++)
    f2[i] = f1[reverse(N, i)];
  for(int j = 0; j < N; j++)
    f1[j] = f2[j];
}

void transform(complex<double>* f, int N) //
{
  ordina(f, N);    //first: reverse order
  complex<double> *W;
  W = (complex<double> *)malloc(N / 2 * sizeof(complex<double>));
  W[1] = polar(1., -2. * M_PI / N);
  W[0] = 1;
  for(int i = 2; i < N / 2; i++)
    W[i] = pow(W[1], i);
  int n = 1;
  int a = N / 2;
  for(int j = 0; j < log2(N); j++) {
    for(int i = 0; i < N; i++) {
      if(!(i & n)) {
        complex<double> temp = f[i];
        complex<double> Temp = W[(i * a) % (n * a)] * f[i + n];
        f[i] = temp + Temp;
        f[i + n] = temp - Temp;
      }
    }
    n *= 2;
    a = a / 2;
  }
  free(W);
}

void FFT(complex<double>* f, int N, double d)
{
  transform(f, N);
  for(int i = 0; i < N; i++)
    f[i] *= d; //multiplying by step
}

int main()
{
  int n;
  do {
    cout << "specify array dimension (MUST be power of 2)" << endl;
    cin >> n;
  } while(!check(n));
  double d;
  cout << "specify sampling step" << endl; //just write 1 in order to have the same results of matlab fft(.)
  cin >> d;
  complex<double> vec[MAX];
  cout << "specify the array" << endl;
  for(int i = 0; i < n; i++) {
    cout << "specify element number: " << i << endl;
    cin >> vec[i];
  }
  FFT(vec, n, d);
  cout << "...printing the FFT of the array specified" << endl;
  for(int j = 0; j < n; j++)
    cout << vec[j] << endl;
  return 0;
}

【讨论】:

  • 这出奇的好。不是世界上最高效的代码,但它确实有效!
  • 你能解释一下你的代码吗?尤其是reverse() 函数。我正在尝试在 C 中为我的 arduino 项目实现傅立叶变换。
【解决方案3】:

您可以开始将this java snippet 转换为C,作者声明他已根据您在网上找到的书numerical recipies 将其从C 转换! here

【讨论】:

【解决方案4】:

【讨论】:

    猜你喜欢
    • 2011-12-10
    • 1970-01-01
    • 2010-12-13
    • 2013-05-22
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 2011-02-02
    相关资源
    最近更新 更多