c语言编写傅里叶变换函数 c语言傅立叶变换函数

求FFT的c语言程序快速傅里叶变换 要用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 complex
#include bitset
#include vector
#include conio.h
#include string
#include fstream
using namespace std;
bool inputData(unsigned long , vectorcomplexdouble );//手工输入数据
void FFT(unsigned long , vectorcomplexdouble );//FFT变换
void display(unsigned long , vectorcomplexdouble );//显示结果
bool readDataFromFile(unsigned long , vectorcomplexdouble );//从文件中读取数据
bool saveResultToFile(unsigned long , vectorcomplexdouble );//保存结果至文件中
const double PI = 3.1415926;
int _tmain(int argc, _TCHAR* argv[])
{
vectorcomplexdoublevecList;//有限长序列
unsigned long ulN = 0;//N
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(ul2)
return false;
while( ul1 )
{
if( ul % 2 )
return false;
ul /= 2;
}
return true;
}
bool inputData(unsigned longulN, vectorcomplexdoublevecList)
{
//题目
cout "\n\n\n==============================Input Datahttps://www.04ip.com/post/==============================="endl;
//输入N
cout "\nInput N:";
cinulN;
if(!Is2Power(ulN))//验证N的有效性
{
cout "N is invalid (N must like 2, 4, 8, .....), please retry."endl;
return false;
}
//输入各元素
vecList.clear();//清空原有序列
complexdouble c;
for(unsigned long i = 0; iulN; i++)
{
cout"Input x("i"):";
cinc;
vecList.push_back(c);
}
return true;
}
bool readDataFromFile(unsigned longulN, vectorcomplexdoublevecList)//从文件中读取数据
{
//题目
cout "\n\n\n===============Read Data From File=============="endl;
//输入文件名
string strfilename;
cout"Input filename:" ;
cinstrfilename;
//打开文件
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
loadfileulN;
if(!loadfile)

推荐阅读