#include <stdio.h>
#define N 8
void kkfft(double pr[], double pi[], int n, int k, double fr[], double fi[], int l, int il)
void main()
{
double xr[N],xi[N],Yr[N],Yi[N],l=0,il=0
int i,j,n=N,k=3
for(i=0i<Ni++)
{
xr[i]=i
xi[i]=0
}
printf("------FFT------\n")
l=0
kkfft(xr,xi,n,k,Yr,Yi,l,il)
for(i=0i<Ni++)
{
printf("%-11lf + j* %-11lf\n",Yr[i],Yi[i])
}
printf("-----DFFT-------\n")
l=1
kkfft(Yr,Yi,n,k,xr,xi,l,il)
for(i=0i<Ni++)
{
printf("%-11lf + j* %-11lf\n",xr[i],xi[i])
}
getch()
}
void kkfft(double pr[], double pi[], int n, int k, double fr[], double fi[], int l, int il)
{
int it,m,is,i,j,nv,l0
double p,q,s,vr,vi,poddr,poddi
for (it=0it<=n-1it++)
{
m = it
is = 0
for(i=0i<=k-1i++)
{
j = m/2
is = 2*is+(m-2*j)
m = j
}
fr[it] = pr[is]
fi[it] = pi[is]
}
pr[0] = 1.0
pi[0] = 0.0
p = 6.283185306/(1.0*n)
pr[1] = cos(p)
pi[1] = -sin(p)
if (l!=0)
pi[1]=-pi[1]
for (i=2i<=n-1i++)
{
p = pr[i-1]*pr[1]
q = pi[i-1]*pi[1]
s = (pr[i-1]+pi[i-1])*(pr[1]+pi[1])
pr[i] = p-q
pi[i] = s-p-q
}
for (it=0it<=n-2it=it+2)
{
vr = fr[it]
vi = fi[it]
fr[it] = vr+fr[it+1]
fi[it] = vi+fi[it+1]
fr[it+1] = vr-fr[it+1]
fi[it+1] = vi-fi[it+1]
}
m = n/2
nv = 2
for (l0=k-2l0>=0l0--)
{
m = m/2
nv = 2*nv
for(it=0it<=(m-1)*nvit=it+nv)
for (j=0j<=(nv/2)-1j++)
{
p = pr[m*j]*fr[it+j+nv/2]
q = pi[m*j]*fi[it+j+nv/2]
s = pr[m*j]+pi[m*j]
s = s*(fr[it+j+nv/2]+fi[it+j+nv/2])
poddr = p-q
poddi = s-p-q
fr[it+j+nv/2] = fr[it+j]-poddr
fi[it+j+nv/2] = fi[it+j]-poddi
fr[it+j] = fr[it+j]+poddr
fi[it+j] = fi[it+j]+poddi
}
}
/*逆傅立叶变换*/
if(l!=0)
{
for(i=0i<=n-1i++)
{
fr[i] = fr[i]/(1.0*n)
fi[i] = fi[i]/(1.0*n)
}
}
/*是否计算模和相角*/
if(il!=0)
{
for(i=0i<=n-1i++)
{
pr[i] = sqrt(fr[i]*fr[i]+fi[i]*fi[i])
if(fabs(fr[i])<0.000001*fabs(fi[i]))
{
if ((fi[i]*fr[i])>0)
pi[i] = 90.0
else
pi[i] = -90.0
}
else
pi[i] = atan(fi[i]/fr[i])*360.0/6.283185306
}
}
return
}
分类: 教育/科学 >>学习帮助问题描述:
追20分
解析:
快速傅里叶变换 要用C++ 才行吧 你可以用MATLAB来实现更方便点啊
此FFT 是用VC6.0编写,由FFT.CPP;STDAFX.H和STDAFX.CPP三个文件组成,编译成功。程序可以用文件输入和输出为文件。文件格式为TXT文件。测试结果如下:
输入文件:8.TXT 或手动输入
8 N
1
2
3
4
5
6
7
8
输出结果为:或保存为TXT文件。(8OUT.TXT)
8
(36,0)
(-4,9.65685)
(-4,4)
(-4,1.65685)
(-4,0)
(-4,-1.65685)
(-4,-4)
(-4,-9.65685)
下面为FFT.CPP文件:
FFT.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <iostream>
#include <plex>
#include <bitset>
#include <vector>
#include <conio.h>
#include <string>
#include <fstream>
using namespace std
bool inputData(unsigned long &, vector<plex<double>>&)手工输入数据
void FFT(unsigned long &, vector<plex<double>>&)FFT变换
void display(unsigned long &, vector<plex<double>>&)显示结果
bool readDataFromFile(unsigned long &, vector<plex<double>>&)从文件中读取数据
bool saveResultToFile(unsigned long &, vector<plex<double>>&)保存结果至文件中
const double PI = 3.1415926
int _tmain(int argc, _TCHAR* argv[])
{
vector<plex<double>>vecList有限长序列
unsigned long ulN = 0N
char chChoose = ' '功能选择
功能循环
while(chChoose != 'Q' &&chChoose != 'q')
{
显示选择项
cout <<"\nPlease chose a function" <<endl
cout <<"\t1.Input data manually, press 'M':" <<endl
cout <<"\t2.Read data from file, press 'F':" <<endl
cout <<"\t3.Quit, press 'Q'" <<endl
cout <<"Please chose:"
输入选择
chChoose = getch()
判断
switch(chChoose)
{
case 'm': 手工输入数据
case 'M':
if(inputData(ulN, vecList))
{
FFT(ulN, vecList)
display(ulN, vecList)
saveResultToFile(ulN, vecList)
}
break
case 'f': 从文档读取数据
case 'F':
if(readDataFromFile(ulN, vecList))
{
FFT(ulN, vecList)
display(ulN, vecList)
saveResultToFile(ulN, vecList)
}
break
}
}
return 0
}
bool Is2Power(unsigned long ul) 判断是否是2的整数次幂
{
if(ul <2)
return false
while( ul >1 )
{
if( ul % 2 )
return false
ul /= 2
}
return true
}
bool inputData(unsigned long &ulN, vector<plex<double>>&vecList)
{
题目
cout<<"\n\n\n==============================Input Data===============================" <<endl
输入N
cout<<"\nInput N:"
cin>>ulN
if(!Is2Power(ulN)) 验证N的有效性
{
cout<<"N is invalid (N must like 2, 4, 8, .....), please retry." <<endl
return false
}
输入各元素
vecList.clear()清空原有序列
plex<double>c
for(unsigned long i = 0i <ulNi++)
{
cout <<"Input x(" <<i <<"):"
cin >>c
vecList.push_back(c)
}
return true
}
bool readDataFromFile(unsigned long &ulN, vector<plex<double>>&vecList) 从文件中读取数据
{
题目
cout<<"\n\n\n===============Read Data From File==============" <<endl
输入文件名
string strfilename
cout <<"Input filename:"
cin >>strfilename
打开文件
cout <<"open file " <<strfilename <<"......." <<endl
ifstream loadfile
loadfile.open(strfilename.c_str())
if(!loadfile)
{
cout <<"\tfailed" <<endl
return false
}
else
{
cout <<"\tsucceed" <<endl
}
vecList.clear()
读取N
loadfile >>ulN
if(!loadfile)
{
cout <<"can't get N" <<endl
return false
}
else
{
cout <<"N = " <<ulN <<endl
}
读取元素
plex<double>c
for(unsigned long i = 0i <ulNi++)
{
loadfile >>c
if(!loadfile)
{
cout <<"can't get enough infomation" <<endl
return false
}
else
cout <<"x(" <<i <<") = " <<c <<endl
vecList.push_back(c)
}
关闭文件
loadfile.close()
return true
}
bool saveResultToFile(unsigned long &ulN, vector<plex<double>>&vecList) 保存结果至文件中
{
询问是否需要将结果保存至文件
char chChoose = ' '
cout <<"Do you want to save the result to file? (y/n):"
chChoose = _getch()
if(chChoose != 'y' &&chChoose != 'Y')
{
return true
}
输入文件名
string strfilename
cout <<"\nInput file name:"
cin >>strfilename
cout <<"Save result to file " <<strfilename <<"......" <<endl
打开文件
ofstream savefile(strfilename.c_str())
if(!savefile)
{
cout <<"can't open file" <<endl
return false
}
写入N
savefile <<ulN <<endl
写入元素
for(vector<plex<double>>::iterator i = vecList.begin()i <vecList.end()i++)
{
savefile <<*i <<endl
}
写入完毕
cout <<"save succeed." <<endl
关闭文件
savefile.close()
return true
}
void FFT(unsigned long &ulN, vector<plex<double>>&vecList)
{
得到幂数
unsigned long ulPower = 0幂数
unsigned long ulN1 = ulN - 1
while(ulN1 >0)
{
ulPower++
ulN1 /= 2
}
反序
bitset<sizeof(unsigned long) * 8>bsIndex二进制容器
unsigned long ulIndex反转后的序号
unsigned long ulK
for(unsigned long p = 0p <ulNp++)
{
ulIndex = 0
ulK = 1
bsIndex = bitset<sizeof(unsigned long) * 8>(p)
for(unsigned long j = 0j <ulPowerj++)
{
ulIndex += bsIndex.test(ulPower - j - 1) ? ulK : 0
ulK *= 2
}
if(ulIndex >p)
{
plex<double>c = vecList[p]
vecList[p] = vecList[ulIndex]
vecList[ulIndex] = c
}
}
计算旋转因子
vector<plex<double>>vecW
for(unsigned long i = 0i <ulN / 2i++)
{
vecW.push_back(plex<double>(cos(2 * i * PI / ulN) , -1 * sin(2 * i * PI / ulN)))
}
for(unsigned long m = 0m <ulN / 2m++)
{
cout<<"\nvW[" <<m <<"]=" <<vecW[m]
}
计算FFT
unsigned long ulGroupLength = 1段的长度
unsigned long ulHalfLength = 0段长度的一半
unsigned long ulGroupCount = 0段的数量
plex<double>cwWH(x)
plex<double>c1G(x) + WH(x)
plex<double>c2G(x) - WH(x)
for(unsigned long b = 0b <ulPowerb++)
{
ulHalfLength = ulGroupLength
ulGroupLength *= 2
for(unsigned long j = 0j <ulNj += ulGroupLength)
{
for(unsigned long k = 0k <ulHalfLengthk++)
{
cw = vecW[k * ulN / ulGroupLength] * vecList[j + k + ulHalfLength]
c1 = vecList[j + k] + cw
c2 = vecList[j + k] - cw
vecList[j + k] = c1
vecList[j + k + ulHalfLength] = c2
}
}
}
}
void display(unsigned long &ulN, vector<plex<double>>&vecList)
{
cout <<"\n\n===========================Display The Result=========================" <<endl
for(unsigned long d = 0d <ulNd++)
{
cout <<"X(" <<d <<")\t\t\t = " <<vecList[d] <<endl
}
}
下面为STDAFX.H文件:
stdafx.h : 标准系统包含文件的包含文件,
或是常用但不常更改的项目特定的包含文件
#pragma once
#include <iostream>
#include <tchar.h>
TODO: 在此处引用程序要求的附加头文件
下面为STDAFX.CPP文件:
stdafx.cpp : 只包括标准包含文件的源文件
FFT.pch 将成为预编译头
stdafx.obj 将包含预编译类型信息
#include "stdafx.h"
TODO: 在 STDAFX.H 中
引用任何所需的附加头文件,而不是在此文件中引用
void fft(){
int nn,n1,n2,i,j,k,l,m,s,l1
float ar[1024],ai[1024] // 实部 虚部
float a[2050]
float t1,t2,x,y
float w1,w2,u1,u2,z
float fsin[10]={0.000000,1.000000,0.707107,0.3826834,0.1950903,0.09801713,0.04906767,0.02454123,0.01227154,0.00613588,}// 优化
float fcos[10]={-1.000000,0.000000,0.7071068,0.9238796,0.9807853,0.99518472,0.99879545,0.9996988,0.9999247,0.9999812,}
nn=1024
s=10
n1=nn/2 n2=nn-1
j=1
for(i=1i<=nni++)
{
a[2*i]=ar[i-1]
a[2*i+1]=ai[i-1]
}
for(l=1l<n2l++)
{
if(l<j)
{
t1=a[2*j]
t2=a[2*j+1]
a[2*j]=a[2*l]
a[2*j+1]=a[2*l+1]
a[2*l]=t1
a[2*l+1]=t2
}
k=n1
while (k<j)
{
j=j-k
k=k/2
}
j=j+k
}
for(i=1i<=si++)
{
u1=1
u2=0
m=(1<<i)
k=m>>1
w1=fcos[i-1]
w2=-fsin[i-1]
for(j=1j<=kj++)
{
for(l=jl<nnl=l+m)
{
l1=l+k
t1=a[2*l1]*u1-a[2*l1+1]*u2
t2=a[2*l1]*u2+a[2*l1+1]*u1
a[2*l1]=a[2*l]-t1
a[2*l1+1]=a[2*l+1]-t2
a[2*l]=a[2*l]+t1
a[2*l+1]=a[2*l+1]+t2
}
z=u1*w1-u2*w2
u2=u1*w2+u2*w1
u1=z
}
}
for(i=1i<=nn/2i++)
{
ar[i]=a[2*i+2]/nn
ai[i]=-a[2*i+3]/nn
a[i]=4*sqrt(ar[i]*ar[i]+ai[i]*ai[i]) // 幅值
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)