簡體   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