fft2.cpp
上传用户:alisonmail
上传日期:2013-02-28
资源大小:500k
文件大小:4k
- // FFT.cpp: implementation of the CFFT class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "Dsp.h"
- #include "FFT.h"
- #include <math.h>
- #include "FFTINPUTDLG.h"
- #include "FFTINPUTDLG2.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CFFT::CFFT()
- {
- //m=(int)(log10(lengthoftrans)/log10(2.0));
- }
- CFFT::CFFT(int len)
- {
- m=(int)(log10(len)/log10(2.0));
- length = len;
- wI = new double[len];
- wR = new double[len];
- xI = new double[len];
- xR = new double[len];
- transofx = new double[len];
- bridge = new double[len];
- L = new int[len];
- }
- CFFT::~CFFT()
- {
- delete wI;
- delete wR;
- delete xI;
- delete xR;
- delete L;
- delete bridge;
- delete transofx;
- }
- void CFFT::FftTrans(int M)
- {
- int ip,k,kk,l,incr,iter,j,i;
- double Tr,Ti;
- ip=1;
- kk= (M>>1);
- incr= 2;
- AdjustSignalOrder(M);// adjust the order of the signal
- for (iter=0;iter<m;iter++)
- {
- for (j=0;j<M;j+=incr)
- {
- i= j+ip;
- Tr= xR[i];
- Ti= xI[i];
- xR[i]= xR[j]-Tr;
- xI[i]= xI[j]-Ti;
- xR[j]= xR[j]+Tr;
- xI[j]= xI[j]+Ti;
- }
- if (iter!=0)
- {
- for (k=1;k<ip;k++)
- {
- l= k*kk-1;
- for (j=k;j<M;j+=incr)
- {
- i= j+ip;
- Tr= xR[i]*wR[l]-xI[i]*wI[l];
- Ti= xR[i]*wI[l]+xI[i]*wR[l];
-
- xR[i]= xR[j]-Tr;
- xI[i]= xI[j]-Ti;
- xR[j]= xR[j]+Tr;
- xI[j]= xI[j]+Ti;
- }
- }
- }
- kk>>=1;
- ip<<=1;
- incr<<=1;
- }
- }
- void CFFT::IFftTrans(int M)
- {
- }
- void CFFT::BitReversal(int M)
- {
- unsigned int MASK,C,A,j,k,i;
- for (k=0;k<M;k++)
- {
- MASK =1;
- C=0;
- for (i=0,j=m-1;i<m;i++,j--)
- { A= (k&MASK)>>i;
- A<<=j;
- C|=A;
- MASK= MASK<<1;
- }
- L[k]=C;
- }
- }
- void CFFT::IniWvalue(int M,int flag)//flag=1 for fft ,flag=0 for ifft
- {
- int n2;
- double theta;
- n2=(M>>1)-1;
- theta= 2.0*3.1415926/M;
- for (int i=0;i<=n2;i++)
- {
- wR[i]= cos((i+1)*theta);
- wI[i]= sin((i+1)*theta);
- if (flag==1)
- wI[i]= -wI[i];
- }
- }
- void CFFT::IniSignals(int type, int M)
- {
-
- ///////////////////////////////////////////////////////
- IniWvalue(M,1);/////////// ini the value of w[i]
- BitReversal(M);
- ///////////////////////////////////////////////
- if (type==1)
- {
- double p,q;
- CFFTINPUTDLG dlg;
- dlg.DoModal();
- p= dlg.m_p;
- q= dlg.m_q;
- for (int n=0;n<M;n++)
- {
- xI[n]= 0.0;
- xR[n]= exp(-(n-p)*(n-p)/q);
- }
- }
- else if (type==2)
- {
- double a,f;
- CFFTINPUTDLG2 dlg;
- dlg.DoModal();
- a= dlg.m_a;
- f= dlg.m_f;
- for (int n=0;n<M;n++)
- {
- xI[n]= 0.0;
- xR[n]= exp(-a*n)*sin(2*3.1415926*f*n);
- }
- }
- else if (type==3)
- {
- for (int n=0;n<M;n++)
- {
- xI[n]=0.0;
- xR[n]=0.0;
- if (n>=0 && n<=3)
- xR[n]= n+1;
- if (n<=7 && n>=4)
- xR[n]= 8-n;
- }
- /* double a=0.02;A=1.0;W=0.3;f=1;
- for (int n=0;n<lengthoftrans;n++)
- {
- xI[n]=0.0;
- xR[n]= A*exp(-a*n/f)*sin(W*n/f);
- }*/
- }
- else if (type==4)
- {
- for (int n=0;n<M;n++)
- {
- xI[n]=0.0;
- xR[n]=0.0;
- if (n>=0 && n<=3)
- xR[n]= 4-n;
- if (n<=7 && n>=4)
- xR[n]= n-3;
- }
- }
- }
- void CFFT::AdjustSignalOrder(int M)
- {
- for (int n=0;n<M;n++)
- {
- bridge[n] = xR[n];
- }
- for (int i=0;i<M;i++)
- {
- xR[i]= bridge[L[i]];
- }
- }
- /*void CFFT::DrawSignal(CClientDC *mDC,int startx, int starty, int len,double signal)
- {
- double max= GetMax(signal,lengthoftrans);
- for (int n=0;n<len;n++)
- {
- mDC->MoveTo(n*5+startx,y);
- mDC->LineTo(n*5+startx,int(starty-90/max*signals[n]));
- }
- }
- */
- void CFFT::GetAmp(int M)
- {
- for (int k=0;k<M;k++)
- {
- transofx[k]= sqrt(xR[k]*xR[k]+xI[k]*xI[k]);
- }
- }