[問題] 請教一下FFT的code這樣子對嗎?

看板C_and_CPP作者 (999就是久久久!)時間13年前 (2010/11/08 12:37), 編輯推噓1(101)
留言2則, 2人參與, 最新討論串1/1
遇到的問題: (題意請描述清楚)一個測試FFT.c是否正確? #include "complex.h" #include "fft.c" #include <math.h> #include <stdlib.h> #include <stdio.h> extern void fft(); void main (void){ complex X[16]; /*Declare input array*/ int i; /*loop index*/ int M=9; /*log2(512)*/ double pi=3.141592653589; int N1=512; /*Number of FFT points*/ for(i=0;i<N1;i++) { (X[i]).real=cos(i*5*2.0*pi/N1); (X[i]).imag=0.0; } fft(X,M); /*Display results on screen*/ for(i=0;i<N1;i++) printf("%4d %15.5f\t%15.5f\n",i,(X[i]).real,(X[i]).imag); } 希望得到的正確結果: 程式跑出來的錯誤結果: main.c Error[Pe101]: "cmpx" has already been declared in the current scope (at line 3) Error[Pe101]: "complex" has already been declared in the current scope (at line 9) Error while running C/C++ compiler Done. 2 error(s), 0 warning(s) 開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux)IAR 有問題的code: (請善用置底文標色功能)沒問題,但是無解? #include "complex.h" #include <math.h> #include <stdlib.h> #include <stdio.h> #include <msp430x54x.h> void fft(complex *X, int M) { complex temp1; complex W; /*e^(-j2 pi/N)*/ complex U; /*Twiddle factor W^k*/ int i,j,k; int id; int N1=1<<M; /*Number of points for FFT*/ int N2=N1/2; int L; /*FFT stage*/ int LE; int LE1; double pi=3.1415926535897; j=0; for(i=1;i<(N1-1);i++) { k=N2; while(k<=j) { j=j-k; k=k/2; } j=j+k; if(i<j) { temp1.real=(X[j]).real; temp1.imag=(X[j]).imag; (X[j]).real=(X[i]).real; (X[j]).imag=(X[i]).imag; (X[i]).real=temp1.real; (X[i]).imag=temp1.imag; } } for(L=1;L<=M;L++) { LE=1<<L; LE1=LE/2; U.real=1.0; U.imag=0.0; W.real=cos(pi/LE1); W.imag=-sin(pi/LE1); for(j=0;j<LE1;j++) { /*Compute butterflies that use same W**k*/ for(i=j;i<N1;i+=LE) { id=i+LE1; temp1.real=(X[id]).real*U.real-(X[id]).imag*U.imag; temp1.imag=(X[id]).imag*U.real+(X[id]).real*U.imag; (X[id]).real=(X[i]).real-temp1.real; (X[id]).imag=(X[i]).imag-temp1.imag; (X[i]).real=(X[i]).real+temp1.real; (X[i]).imag=(X[i]).imag+temp1.imag; } /*Recursively compute W**k as W*W**(k-1)=W*U*/ temp1.real=U.real*W.real-U.imag*W.imag; U.imag=U.real*W.imag+U.imag*W.real; U.real=temp1.real; } } } 補充說明: complex.h struct cmpx .... Error { double real; double imag; }; typedef struct cmpx complex; .... Error -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 120.110.10.109 ※ 編輯: han999 來自: 120.110.10.109 (11/08 12:42)

11/08 18:53, , 1F
請看我的文章 #1C3n2OvA 的後半段 你犯了那個錯誤
11/08 18:53, 1F

11/08 19:49, , 2F
c自己有complex http://ppt.cc/ll;8
11/08 19:49, 2F
文章代碼(AID): #1Crtw4TW (C_and_CPP)