傅里叶变换C编程Word格式.docx

上传人:b****3 文档编号:18418442 上传时间:2022-12-16 格式:DOCX 页数:11 大小:16.84KB
下载 相关 举报
傅里叶变换C编程Word格式.docx_第1页
第1页 / 共11页
傅里叶变换C编程Word格式.docx_第2页
第2页 / 共11页
傅里叶变换C编程Word格式.docx_第3页
第3页 / 共11页
傅里叶变换C编程Word格式.docx_第4页
第4页 / 共11页
傅里叶变换C编程Word格式.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

傅里叶变换C编程Word格式.docx

《傅里叶变换C编程Word格式.docx》由会员分享,可在线阅读,更多相关《傅里叶变换C编程Word格式.docx(11页珍藏版)》请在冰豆网上搜索。

傅里叶变换C编程Word格式.docx

下面为FFT.CPP文件:

//FFT.cpp:

定义控制台应用程序的入口点。

#include"

stdafx.h"

#include<

iostream>

complex>

bitset>

vector>

conio.h>

string>

fstream>

usingnamespacestd;

boolinputData(unsignedlong&

vector<

complex<

double>

>

&

);

//手工输入数据

voidFFT(unsignedlong&

//FFT变换

voiddisplay(unsignedlong&

//显示结果

boolreadDataFromFile(unsignedlong&

//从文件中读取数据

boolsaveResultToFile(unsignedlong&

//保存结果至文件中

constdoublePI=3.1415926;

int_tmain(intargc,_TCHAR*argv[])

{

vector<

vecList;

//有限长序列

unsignedlongulN=0;

//N

charchChoose='

'

;

//功能选择

//功能循环

while(chChoose!

='

Q'

&

chChoose!

q'

{

//显示选择项

cout<

<

"

\nPleasechoseafunction"

<

endl;

\t1.Inputdatamanually,press'

M'

:

"

\t2.Readdatafromfile,press'

F'

\t3.Quit,press'

Pleasechose:

//输入选择

chChoose=getch();

//判断

switch(chChoose)

case'

m'

if(inputData(ulN,vecList))

FFT(ulN,vecList);

display(ulN,vecList);

saveResultToFile(ulN,vecList);

}

break;

f'

//从文档读取数据

if(readDataFromFile(ulN,vecList))

return0;

}

boolIs2Power(unsignedlongul)//判断是否是2的整数次幂

if(ul<

2)

returnfalse;

while(ul>

1)

if(ul%2)

ul/=2;

returntrue;

ulN,vector<

vecList)

//题目

cout<

\n\n\n==============================InputData==============================="

//输入N

\nInputN:

cin>

>

ulN;

if(!

Is2Power(ulN))//验证N的有效性

Nisinvalid(Nmustlike2,4,8,.....),pleaseretry."

//输入各元素

vecList.clear();

//清空原有序列

complex<

c;

for(unsignedlongi=0;

i<

ulN;

i++)

Inputx("

):

cin>

vecList.push_back(c);

vecList)//从文件中读取数据

\n\n\n===============ReadDataFromFile=============="

//输入文件名

stringstrfilename;

Inputfilename:

;

strfilename;

//打开文件

openfile"

strfilename<

......."

endl;

ifstreamloadfile;

loadfile.open(strfilename.c_str());

loadfile)

\tfailed"

else

\tsucceed"

//读取N

loadfile>

can'

tgetN"

N="

ulN<

//读取元素

tgetenoughinfomation"

x("

)="

c<

//关闭文件

loadfile.close();

vecList)//保存结果至文件中

//询问是否需要将结果保存至文件

Doyouwanttosavetheresulttofile?

(y/n):

chChoose=_getch();

if(chChoose!

y'

Y'

\nInputfilename:

Saveresulttofile"

......"

ofstreamsavefile(strfilename.c_str());

savefile)

topenfile"

//写入N

savefile<

//写入元素

for(vector<

iteratori=vecList.begin();

vecList.end();

*i<

//写入完毕

savesucceed."

savefile.close();

//得到幂数

unsignedlongulPower=0;

//幂数

unsignedlongulN1=ulN-1;

while(ulN1>

0)

ulPower++;

ulN1/=2;

//反序

bitset<

sizeof(unsignedlong)*8>

bsIndex;

//二进制容器

unsignedlongulIndex;

//反转后的序号

unsignedlongulK;

for(unsignedlongp=0;

p<

p++)

ulIndex=0;

ulK=1;

bsIndex=bitset<

(p);

for(unsignedlongj=0;

j<

ulPower;

j++)

ulIndex+=bsIndex.test(ulPower-j-1)?

ulK:

0;

ulK*=2;

if(ulIndex>

p)

c=vecList[p];

vecList[p]=vecList[ulIndex];

vecList[ulIndex]=c;

//计算旋转因子

vecW;

ulN/2;

vecW.push_back(complex<

(cos(2*i*PI/ulN),-1*sin(2*i*PI/ulN)));

for(unsignedlongm=0;

m<

m++)

\nvW["

]="

vecW[m];

//计算FFT

unsignedlongulGroupLength=1;

//段的长度

unsignedlongulHalfLength=0;

//段长度的一半

unsignedlongulGroupCount=0;

//段的数量

cw;

//WH(x)

c1;

//G(x)+WH(x)

c2;

//G(x)-WH(x)

for(unsignedlongb=0;

b<

b++)

ulHalfLength=ulGroupLength;

ulGroupLength*=2;

j+=ulGroupLength)

for(unsignedlongk=0;

k<

ulHalfLength;

k++)

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;

\n\n===========================DisplayTheResult========================="

for(unsignedlongd=0;

d<

d++)

X("

)\t\t\t="

vecList[d]<

下面为STDAFX.H文件:

//stdafx.h:

标准系统包含文件的包含文件,

//或是常用但不常更改的项目特定的包含文件

#pragmaonce

tchar.h>

//TODO:

在此处引用程序要求的附加头文件

下面为STDAFX.CPP文件:

//stdafx.cpp:

只包括标准包含文件的源文件

//FFT.pch将成为预编译头

//stdafx.obj将包含预编译类型信息

在STDAFX.H中

//引用任何所需的附加头文件,而不是在此文件中引用

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1