繁体   English   中英

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

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

我一直在Matlab中使用函数“ fft(x)”,其中“ x”是复数的向量。 我正在寻找一个易于使用的C ++函数,该函数将返回复数。

我找到了以下代码: http : //paulbourke.net/miscellaneous/dft/

如果等效,该如何使用? 感谢您的时间 !

  /*
   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);
}

替代建议:

我有同样的问题。 我从fftw使用了fft库。 http://www.fftw.org/download.html其性能类似于matlab。

乍看起来,代码看起来不错。 原始的FFT并不是很多代码。

FFT的一个特点是它是就地操作。 许多更高级别的绑定有效地掩盖了这一事实。

因此,您将实部和虚部放入x和y数组中。 执行完函数后,您将读取相同的数组以得到结果。

这种特别简单的实现方式只能将2的幂用作原始FFT。 如果输入的长度不是2的幂,则可以将信号零填充。

如果您想阅读FFT的背景知识,请在Google上下载《 Numerical Recipes和《 fft (旧版本可免费获得)。 该书中的版本与其他实现的不同之处在于,您必须提供交错的实部和虚部。

在您引用的实现中我缺少的是使用pi或三角函数。 您必须尝试一下才能与Matlab进行比较。

暂无
暂无

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

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