2012-11-15 65 views
1

你好我已經複製這個FFT實現,但它說什麼都沒有像系統。 Windows我該如何讓這段代碼工作?我已經得到了答案,我只是想編輯這篇文章,所以現在對有人來說是有用的。我怎樣才能導入System.Windows在C#

來源: http://gerrybeauregard.wordpress.com/2011/04/01/an-fft-in-c/

代碼::

using System; 
using System.Net; 


namespace FFT { 

    public class FFT2 { 
     // Element for linked list in which we store the 
     // input/output data. We use a linked list because 
     // for sequential access it's faster than array index. 
     class FFTElement { 
      public double re = 0.0;  // Real component 
      public double im = 0.0;  // Imaginary component 
      public FFTElement next;  // Next element in linked list 
      public uint revTgt;   // Target position post bit-reversal 
     } 

     private uint m_logN = 0;  // log2 of FFT size 
     private uint m_N = 0;   // FFT size 
     private FFTElement[] m_X;  // Vector of linked list elements 

     /** 
     * 
     */ 
     public FFT2() { 
     } 

     /** 
     * Initialize class to perform FFT of specified size. 
     * 
     * @param logN Log2 of FFT length. e.g. for 512 pt FFT, logN = 9. 
     */ 
     public void init(
      uint logN) { 
      m_logN = logN; 
      m_N = (uint)(1 << (int)m_logN); 

      // Allocate elements for linked list of complex numbers. 
      m_X = new FFTElement[m_N]; 
      for (uint k = 0; k < m_N; k++) 
       m_X[k] = new FFTElement(); 

      // Set up "next" pointers. 
      for (uint k = 0; k < m_N - 1; k++) 
       m_X[k].next = m_X[k + 1]; 

      // Specify target for bit reversal re-ordering. 
      for (uint k = 0; k < m_N; k++) 
       m_X[k].revTgt = BitReverse(k, logN); 
     } 

     /** 
     * Performs in-place complex FFT. 
     * 
     * @param xRe  Real part of input/output 
     * @param xIm  Imaginary part of input/output 
     * @param inverse If true, do an inverse FFT 
     */ 
     public void run(
      double[] xRe, 
      double[] xIm, 
      bool inverse = false) { 
      uint numFlies = m_N >> 1; // Number of butterflies per sub-FFT 
      uint span = m_N >> 1;  // Width of the butterfly 
      uint spacing = m_N;   // Distance between start of sub-FFTs 
      uint wIndexStep = 1;  // Increment for twiddle table index 

      // Copy data into linked complex number objects 
      // If it's an IFFT, we divide by N while we're at it 
      FFTElement x = m_X[0]; 
      uint k = 0; 
      double scale = inverse ? 1.0/m_N : 1.0; 
      while (x != null) { 
       x.re = scale * xRe[k]; 
       x.im = scale * xIm[k]; 
       x = x.next; 
       k++; 
      } 

      // For each stage of the FFT 
      for (uint stage = 0; stage < m_logN; stage++) { 
       // Compute a multiplier factor for the "twiddle factors". 
       // The twiddle factors are complex unit vectors spaced at 
       // regular angular intervals. The angle by which the twiddle 
       // factor advances depends on the FFT stage. In many FFT 
       // implementations the twiddle factors are cached, but because 
       // array lookup is relatively slow in C#, it's just 
       // as fast to compute them on the fly. 
       double wAngleInc = wIndexStep * 2.0 * Math.PI/m_N; 
       if (inverse == false) 
        wAngleInc *= -1; 
       double wMulRe = Math.Cos(wAngleInc); 
       double wMulIm = Math.Sin(wAngleInc); 

       for (uint start = 0; start < m_N; start += spacing) { 
        FFTElement xTop = m_X[start]; 
        FFTElement xBot = m_X[start + span]; 

        double wRe = 1.0; 
        double wIm = 0.0; 

        // For each butterfly in this stage 
        for (uint flyCount = 0; flyCount < numFlies; ++flyCount) { 
         // Get the top & bottom values 
         double xTopRe = xTop.re; 
         double xTopIm = xTop.im; 
         double xBotRe = xBot.re; 
         double xBotIm = xBot.im; 

         // Top branch of butterfly has addition 
         xTop.re = xTopRe + xBotRe; 
         xTop.im = xTopIm + xBotIm; 

         // Bottom branch of butterly has subtraction, 
         // followed by multiplication by twiddle factor 
         xBotRe = xTopRe - xBotRe; 
         xBotIm = xTopIm - xBotIm; 
         xBot.re = xBotRe * wRe - xBotIm * wIm; 
         xBot.im = xBotRe * wIm + xBotIm * wRe; 

         // Advance butterfly to next top & bottom positions 
         xTop = xTop.next; 
         xBot = xBot.next; 

         // Update the twiddle factor, via complex multiply 
         // by unit vector with the appropriate angle 
         // (wRe + j wIm) = (wRe + j wIm) x (wMulRe + j wMulIm) 
         double tRe = wRe; 
         wRe = wRe * wMulRe - wIm * wMulIm; 
         wIm = tRe * wMulIm + wIm * wMulRe; 
        } 
       } 

       numFlies >>= 1;  // Divide by 2 by right shift 
       span >>= 1; 
       spacing >>= 1; 
       wIndexStep <<= 1; // Multiply by 2 by left shift 
      } 

      // The algorithm leaves the result in a scrambled order. 
      // Unscramble while copying values from the complex 
      // linked list elements back to the input/output vectors. 
      x = m_X[0]; 
      while (x != null) { 
       uint target = x.revTgt; 
       xRe[target] = x.re; 
       xIm[target] = x.im; 
       x = x.next; 
      } 
     } 

     /** 
     * Do bit reversal of specified number of places of an int 
     * For example, 1101 bit-reversed is 1011 
     * 
     * @param x  Number to be bit-reverse. 
     * @param numBits Number of bits in the number. 
     */ 
     private uint BitReverse(
      uint x, 
      uint numBits) { 
      uint y = 0; 
      for (uint i = 0; i < numBits; i++) { 
       y <<= 1; 
       y |= x & 0x0001; 
       x >>= 1; 
      } 
      return y; 
     } 
    } 
} 
+0

請編輯您的問題,並在這裏發佈**相關**代碼。希望人們離開這個網站,以便弄清楚你所要求的是不合理的。這也意味着,如果其他網站由於某種原因而無法使用,那麼您的問題就沒有意義,而且未來的用戶也無法搜索到。這裏的問題應該是獨立的,幷包含所有必要的內容,以便能夠得到答案。 (只需發佈代碼的**相關**部分,而不是大量的文本。)謝謝。 –

+0

另外我建議不要如此劇烈地改變你的問題('使用System.Windows'來'我怎樣測試這個FFT實現')。如果您有單獨的問題需要處理,請提出一個單獨的問題。 –

+0

但是我有更早的海報 – user1825357

回答

2

System.Windows是爲Windows Presentation Foundation的命名空間。文章假設你有一個WPF應用程序。如果您沒有,可以從File> New Project> ...> Windows> WPF Application從Visual Studio中創建一個。

WPF是一種爲MS Windows創建圖形桌面應用程序的方法。如果您來自命令行操作系統,您可能更適合製作Windows控制檯應用程序。

如果你想學習如何創建一個WPF應用程序,你需要一個WPF tutorial