首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >单个C文件中的FFT

单个C文件中的FFT
EN

Stack Overflow用户
提问于 2012-01-10 17:42:56
回答 4查看 122.9K关注 0票数 38

我在寻找一个C语言的FFT实现。然而,我并不是在寻找一个巨大的库(比如FFTW),而是为了一个易于使用的C文件实现。不幸的是,我还没能找到这样的东西。

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

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-01-10 18:01:29

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

票数 34
EN

Stack Overflow用户

发布于 2016-06-09 23:02:01

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

代码语言:javascript
复制
#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;
}
票数 27
EN

Stack Overflow用户

发布于 2012-01-10 17:48:23

你可以开始将this java snippet转换成C,作者说他已经根据你在网上找到的numerical recipies这本书把它从C转换成了C!here

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8801158

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档