简体   繁体   English

这个C ++ FFT函数是否等效于“ fft” matlab函数?

[英]Is this C++ FFT function equivalent to “fft” matlab function?

I have always used the function "fft(x)" in matlab where "x" is a vector of complex numbers. 我一直在Matlab中使用函数“ fft(x)”,其中“ x”是复数的向量。 I am looking for an easy to use function in C++ that would return complex numbers. 我正在寻找一个易于使用的C ++函数,该函数将返回复数。

I have found this code : http://paulbourke.net/miscellaneous/dft/ 我找到了以下代码: http : //paulbourke.net/miscellaneous/dft/

If it is equivalent, how can I use it ? 如果等效,该如何使用? Thank you for your time ! 感谢您的时间 !

  /*
   This computes an in-place complex-to-complex FFT 
   x and y are the real and imaginary arrays of 2^m points.
   dir =  1 gives forward transform
   dir = -1 gives reverse transform 
*/
short FFT(short int dir,long m,double *x,double *y)
{
   long n,i,i1,j,k,i2,l,l1,l2;
   double c1,c2,tx,ty,t1,t2,u1,u2,z;

   /* Calculate the number of points */
   n = 1;
   for (i=0;i<m;i++) 
      n *= 2;

   /* Do the bit reversal */
   i2 = n >> 1;
   j = 0;
   for (i=0;i<n-1;i++) {
      if (i < j) {
         tx = x[i];
         ty = y[i];
         x[i] = x[j];
         y[i] = y[j];
         x[j] = tx;
         y[j] = ty;
      }
      k = i2;
      while (k <= j) {
         j -= k;
         k >>= 1;
      }
      j += k;
   }

   /* Compute the FFT */
   c1 = -1.0; 
   c2 = 0.0;
   l2 = 1;
   for (l=0;l<m;l++) {
      l1 = l2;
      l2 <<= 1;
      u1 = 1.0; 
      u2 = 0.0;
      for (j=0;j<l1;j++) {
         for (i=j;i<n;i+=l2) {
            i1 = i + l1;
            t1 = u1 * x[i1] - u2 * y[i1];
            t2 = u1 * y[i1] + u2 * x[i1];
            x[i1] = x[i] - t1; 
            y[i1] = y[i] - t2;
            x[i] += t1;
            y[i] += t2;
         }
         z =  u1 * c1 - u2 * c2;
         u2 = u1 * c2 + u2 * c1;
         u1 = z;
      }
      c2 = sqrt((1.0 - c1) / 2.0);
      if (dir == 1) 
         c2 = -c2;
      c1 = sqrt((1.0 + c1) / 2.0);
   }

   /* Scaling for forward transform */
   if (dir == 1) {
      for (i=0;i<n;i++) {
         x[i] /= n;
         y[i] /= n;
      }
   }

   return(TRUE);
}

Alternative Suggestion: 替代建议:

I had same problem. 我有同样的问题。 I used fft library from fftw. 我从fftw使用了fft库。 http://www.fftw.org/download.html Its performance is similar to matlab. http://www.fftw.org/download.html其性能类似于matlab。

The code looks fine at first glance. 乍看起来,代码看起来不错。 The original FFT is not a lot of code. 原始的FFT并不是很多代码。

One feature of the FFT is that it is an inplace operation. FFT的一个特点是它是就地操作。 Many higher level bindings effectively hide that fact. 许多更高级别的绑定有效地掩盖了这一事实。

So you put your real and imaginary parts into the x and y arrays. 因此,您将实部和虚部放入x和y数组中。 After the executing the function you read those same arrays for your result. 执行完函数后,您将读取相同的数组以得到结果。

This particularly simple implementation will only work with powers of 2 as the original FFT. 这种特别简单的实现方式只能将2的幂用作原始FFT。 If you have inputs of lengths other than powers of 2 you could zero-pad your signal. 如果输入的长度不是2的幂,则可以将信号零填充。

Google the book Numerical Recipes and fft (older versions are freely available) if you want to read up on the background of the FFT. 如果您想阅读FFT的背景知识,请在Google上下载《 Numerical Recipes和《 fft (旧版本可免费获得)。 The version in that book is different from other implementations in that you have to feed in the real and imaginary parts interleaved. 该书中的版本与其他实现的不同之处在于,您必须提供交错的实部和虚部。

What I'm missing on the implementation that you are quoting is the use of pi or trigonometric functions. 在您引用的实现中我缺少的是使用pi或三角函数。 You'll have to try it out to compare against Matlab. 您必须尝试一下才能与Matlab进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM