C語言數(shù)據(jù)結(jié)構(gòu)算法之實(shí)現(xiàn)快速傅立葉變換
成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供東陽企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計(jì)制作、成都做網(wǎng)站、H5技術(shù)、小程序制作等業(yè)務(wù)。10年已為東陽眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。本實(shí)例將實(shí)現(xiàn)二維快速傅立葉變換,同時也將借此實(shí)例學(xué)習(xí)用c語言實(shí)現(xiàn)矩陣的基本操作、復(fù)數(shù)的基本掾作,復(fù)習(xí)所學(xué)過的動態(tài)內(nèi)存分配、文件操作、結(jié)構(gòu)指針的函數(shù)調(diào)用等內(nèi)容。
很久以來,傅立葉變換一直是許多領(lǐng)域,如線性系統(tǒng)、光學(xué)、概率論、量子物理、天線、數(shù)字圖像處理和信號分析等的一個基本分析工具,但是即便使用計(jì)算速度驚人的計(jì)算機(jī)計(jì)算離散傅立葉變換所花費(fèi)的時間也常常是難以接受的,因此導(dǎo)致了快速傅立葉變換(FFT)的產(chǎn)生。
本實(shí)例將對一個二維數(shù)組進(jìn)行正、反快速傅立葉變換。正傅立葉變換時dfft()函數(shù)先調(diào)用fft()按行對數(shù)組進(jìn)行變換,再對結(jié)果調(diào)用fft()按列進(jìn)行變換,此時完成了快速傅立葉變換,再調(diào)用rdfft()函數(shù)進(jìn)行傅立葉逆變換。如果程序設(shè)計(jì)正確的話,變換的結(jié)果應(yīng)與原數(shù)組相同。
實(shí)例代碼:
#include#include #include #define PI 3.14159265358979323846 struct COMPLEX { float re; float im; } cplx , * Hfield , * S , * R , * w; int n , m; int ln , lm; void initiate (); void dfft (); void rdfft (); void showresult (); void fft (int l , int k); int reverse (int t , int k); void W (int l); int loop (int l); void conjugate (); void add (struct COMPLEX * x , struct COMPLEX * y , struct COMPLEX * z); void sub (struct COMPLEX * x , struct COMPLEX * y , struct COMPLEX * z); void mul (struct COMPLEX * x , struct COMPLEX * y , struct COMPLEX * z); struct COMPLEX * Hread(int i , int j); void Hwrite (int i , int j , struct COMPLEX x); void main () { initiate (); printf("\n原始數(shù)據(jù):\n"); showresult(); getchar (); dfft (); printf("\n快速復(fù)利葉變換后的結(jié)果:\n"); showresult (); getchar (); rdfft (); printf("\n快速復(fù)利葉逆變換后的結(jié)果:\n"); showresult (); getchar (); free (Hfield); } void initiate () {//程序初始化操作,包括分配內(nèi)存、讀入要處理的數(shù)據(jù)、進(jìn)行顯示等 FILE * df; df = fopen ("data.txt" , "r"); fscanf (df , "%5d" , &n); fscanf (df , "%5d" , &m); if ((ln = loop (n)) == -1) { printf (" 列數(shù)不是2的整數(shù)次冪 "); exit (1); } if ((lm = loop (m)) == -1) { printf (" 行數(shù)不是2的整數(shù)次冪 "); exit (1); } Hfield = (struct COMPLEX *) malloc (n * m * sizeof (cplx)); if (fread (Hfield , sizeof (cplx) , m * n , df) != (unsigned) (m * n)) { if (feof (df)) printf (" Premature end of file "); else printf (" File read error "); } fclose (df); } void dfft () {//進(jìn)行二維快速復(fù)利葉變換 int i , j; int l , k; l = n; k = ln; w = (struct COMPLEX *) calloc (l , sizeof (cplx)); R = (struct COMPLEX *) calloc (l , sizeof (cplx)); S = (struct COMPLEX *) calloc (l , sizeof(cplx)); W (l); for ( i = 0 ; i < m ; i++ ) {//按行進(jìn)行快速復(fù)利葉變換 for (j = 0 ; j < n ; j++) { S[j].re = Hread (i , j)->re; S[j].im = Hread (i , j)->im; } fft(l , k); for (j = 0 ; j < n ; j++) Hwrite (i , j , R[j]); } free (R); free (S); free (w); l = m; k = lm; w = (struct COMPLEX *) calloc (l , sizeof (cplx)); R = (struct COMPLEX *) calloc (l , sizeof (cplx)); S = (struct COMPLEX *) calloc (l , sizeof (cplx)); W (l); for (i = 0 ; i < n ; i++) {//按列進(jìn)行快速復(fù)利葉變換 for(j = 0 ; j < m ; j++) { S[j].re = Hread(j , i)->re; S[j].im = Hread(j , i)->im; } fft(l , k); for (j = 0 ; j < m ; j++) Hwrite (j , i , R[j]); } free (R); free (S); free (w); } void rdfft () { conjugate (); dfft (); conjugate (); } void showresult () { int i , j; for (i = 0 ; i < m ; i++) { printf ( " \n第%d行\(zhòng)n " , i); for (j = 0 ; j < n ; j++) { if (j % 4 == 0) printf (" \n "); printf(" (%5.2f,%5.2fi) " , Hread (i , j)->re , Hread (i , j)->im); } } } void fft (int l , int k) { int i , j , s , nv , t; float c; struct COMPLEX mp , r; nv = l; c = (float) l; c = pow (c , 0.5); for (i = 0 ; i < k ; i++) { for (t = 0 ; t < l ; t += nv) { for (j = 0 ; j < nv / 2 ; j++) { s = (t + j) >> (k - i -1); s = reverse(s , k); r.re = S[t + j].re; r.im = S[t + j].im; mul (&w[s] , &S[t + j + nv / 2] , &mp);/////////講解傳遞結(jié)構(gòu)指針和結(jié)構(gòu)本身的區(qū)別 add (&r , &mp , &S[t + j]); sub (&r , &mp , &S[t + j + nv / 2]); } } nv = nv >> 1; } for (i = 0 ; i < l ; i++) { j = reverse(i , k); R[j].re = S[i].re / c; R[j].im = S[i].im / c; } } int reverse (int t , int k) { int i , x , y; y = 0; for (i = 0 ; i < k ; i++) { x = t & 1; t = t >> 1; y = (y << 1) + x; } return y; } void W (int l) { int i; float c , a; c = (float) l; c = 2 * PI / c; for (i = 0 ; i < l ; i++) { a = (float) i; w[i].re = (float) cos(a * c); w[i].im = -(float) sin(a * c); } } int loop (int l) {//檢驗(yàn)輸入數(shù)據(jù)是否為2的整數(shù)次冪,如果是返回用2進(jìn)制表示時的位數(shù) int i , m; if (l != 0) { for (i = 1 ; i < 32 ; i++) { m = l >> i; if (m == 0) break; } if (l == (1 << (i - 1))) return i - 1; } return -1; } void conjugate () {//求復(fù)數(shù)矩陣的共軛矩陣 int i , j; for (i = 0 ; i < m ; i++) { for (j = 0 ; j < n ; j++) { Hread (i , j)->im *= -1; } } } struct COMPLEX * Hread (int i , int j) {//按讀矩陣方式返回Hfield中指定位置的指針 return (Hfield + i * n + j); } void Hwrite (int i , int j , struct COMPLEX x) {//按寫矩陣方式將復(fù)數(shù)結(jié)構(gòu)x寫到指定的Hfield位置上 (Hfield + i * n + j)->re = x.re; (Hfield + i * n + j)->im = x.im; } void add (struct COMPLEX * x , struct COMPLEX * y , struct COMPLEX * z) {//定義復(fù)數(shù)加法 z->re = x->re + y->re; z->im = x->im + y->im; } void sub (struct COMPLEX * x , struct COMPLEX * y , struct COMPLEX * z) {//定義復(fù)數(shù)減法 z->re = x->re - y->re; z->im = x->im - y->im; } void mul (struct COMPLEX * x , struct COMPLEX * y , struct COMPLEX * z) {//定義復(fù)數(shù)乘法 z->re = (x->re) * (y->re) - (x->im) * (y->im); z->im = (x->im) * (y->re) + (x->re) * (y->im); }
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.cdcxhl.com,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。