快速傅立葉變換(FFT) C_第1頁
快速傅立葉變換(FFT) C_第2頁
快速傅立葉變換(FFT) C_第3頁
快速傅立葉變換(FFT) C_第4頁
快速傅立葉變換(FFT) C_第5頁
已閱讀5頁,還剩1頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、快速傅立葉變換(FFT)的C#代碼這個代碼是從快速傅立葉變換(FFT)的C+實現(xiàn)與Matlab實驗這篇文章里的源代碼轉(zhuǎn)換而來,請注意查看原文。在這里自己轉(zhuǎn)換成了C#代碼,并作了一些改動,主要是對N值的確定,原文的N值為常量1024,自己通過對輸入的數(shù)組的長度來確定N值,N值的確定符合2的n次方,函數(shù)返回N值。通過作者提供的測試變量進行測試,能得到相應的結(jié)果。代碼如下:FFT代碼:namespace ConsoleApplication1    / <summary>    / 快速傅立葉變換(Fast Fourier Tr

2、ansform)。     / </summary>    public class TWFFT            private TWFFT()                        priva

3、te static void bitrp(float xreal, float ximag, int n)                    / 位反轉(zhuǎn)置換 Bit-reversal Permutation            int i, j, a, b, p;   &#

4、160;        for (i = 1, p = 0; i < n; i *= 2)                            p+;         &#

5、160;              for (i = 0; i < n; i+)                            a = i;     

6、60;          b = 0;                for (j = 0; j < p; j+)                     

7、;               b = b * 2 + a % 2;                    a = a / 2;            

8、;                    if (b > i)                              &

9、#160;     float t = xreali;                    xreali = xrealb;                    xrealb

10、= t;                    t = ximagi;                    ximagi = ximagb;      

11、60;             ximagb = t;                                      

12、      public static int FFT(float xreal, float ximag)                    /n值為2的N次方            int n = 2;    &#

13、160;       while (n <= xreal.Length)                            n *= 2;           

14、             n /= 2;            / 快速傅立葉變換,將復數(shù) x 變換后仍保存在 x 中,xreal, ximag 分別是 x 的實部和虛部            float wreal = new floatn / 2; 

15、60;          float wimag = new floatn / 2;            float treal, timag, ureal, uimag, arg;            int m, k, j, t, index1, index2; 

16、0;          bitrp(xreal, ximag, n);            / 計算 1 的前 n / 2 個 n 次方根的共軛復數(shù) W'j = wreal j + i * wimag j , j = 0, 1, . , n / 2 - 1           

17、arg = (float)(-2 * Math.PI / n);            treal = (float)Math.Cos(arg);            timag = (float)Math.Sin(arg);            wreal0 =

18、1.0f;            wimag0 = 0.0f;            for (j = 1; j < n / 2; j+)                    

19、60;       wrealj = wrealj - 1 * treal - wimagj - 1 * timag;                wimagj = wrealj - 1 * timag + wimagj - 1 * treal;            

20、0;           for (m = 2; m <= n; m *= 2)                            for (k = 0; k < n; k += m)   &#

21、160;                                for (j = 0; j < m / 2; j+)              &

22、#160;                             index1 = k + j;                   &#

23、160;    index2 = index1 + m / 2;                        t = n * j / m;    / 旋轉(zhuǎn)因子 w 的實部在 wreal 中的下標為 t         

24、               treal = wrealt * xrealindex2 - wimagt * ximagindex2;                        timag = wrealt * ximagin

25、dex2 + wimagt * xrealindex2;                        ureal = xrealindex1;                   

26、;     uimag = ximagindex1;                        xrealindex1 = ureal + treal;              

27、60;         ximagindex1 = uimag + timag;                        xrealindex2 = ureal - treal;         

28、;               ximagindex2 = uimag - timag;                                

29、;                            return n;                public static int IFFT(float xreal, float ximag)

30、60;                   /n值為2的N次方            int n = 2;            while (n <= xreal.Length)  

31、                          n *= 2;                        n /= 2; 

32、           / 快速傅立葉逆變換            float wreal = new floatn / 2;            float wimag = new floatn / 2;      &

33、#160;     float treal, timag, ureal, uimag, arg;            int m, k, j, t, index1, index2;            bitrp(xreal, ximag, n);       &#

34、160;    / 計算 1 的前 n / 2 個 n 次方根 Wj = wreal j + i * wimag j , j = 0, 1, . , n / 2 - 1            arg = (float)(2 * Math.PI / n);            treal = (float)(Math.Cos(arg); 

35、           timag = (float)(Math.Sin(arg);            wreal0 = 1.0f;            wimag0 = 0.0f;        

36、;    for (j = 1; j < n / 2; j+)                            wrealj = wrealj - 1 * treal - wimagj - 1 * timag;       &#

37、160;        wimagj = wrealj - 1 * timag + wimagj - 1 * treal;                        for (m = 2; m <= n; m *= 2)      

38、0;                     for (k = 0; k < n; k += m)                          &#

39、160;         for (j = 0; j < m / 2; j+)                                     &

40、#160;      index1 = k + j;                        index2 = index1 + m / 2;              &#

41、160;         t = n * j / m;    / 旋轉(zhuǎn)因子 w 的實部在 wreal 中的下標為 t                        treal = wrealt * xrealindex2 - wimagt * ximagind

42、ex2;                        timag = wrealt * ximagindex2 + wimagt * xrealindex2;                 

43、60;      ureal = xrealindex1;                        uimag = ximagindex1;              

44、60;         xrealindex1 = ureal + treal;                        ximagindex1 = uimag + timag;         

45、;               xrealindex2 = ureal - treal;                        ximagindex2 = uimag - timag;   &

46、#160;                                                   

47、0;    for (j = 0; j < n; j+)                            xrealj /= n;              

48、  ximagj /= n;                        return n;            這里只給出了函數(shù)FFT()的,函數(shù)IFFT()可以進行相應測試。測試代碼:namespace ConsoleApplication1 

49、0;  class Program            static void Main(string args)                    float a =                 0.5751f,0.4514f,0.0439f,

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論