函数说明:更改主函数体中的N和length(=log2(N))既可以实现任意点数(2的幂次)的FFT/ IFFT的实现,fft函数中flag标志位控制是正变换还是逆变换。
1.复数操作类
定义复数类,重载复数四则运算符号,重载输出运算符,重载赋值运算符。
/**********预编译文件头文件complex.h********/#include"iostream"using namespace std;class complex{ double real,image;public: complex(){real=0;image=0;} complex(float i,float j){real=i;image=j;} double getr(){return real;} double geti(){return image;} void show() { if(image>=0) cout<<<"+"< <<"j"<<" "; else cout< < <<"j"<<" "; } void setvalue(double i=0,double j=0) { real=abs(i)<0.0001?0:i; image=abs(j)<0.0001?0:j; } complex operator +(complex&); complex operator-(complex&); complex operator*(complex&); complex operator /(int n); void operator+=(complex&); void operator =(complex&); friend complex W(int m,int n, int flag);};complex complex::operator +(complex& c) { complex t; t.real=real+c.real; t.image=image+c.image; return t; }complex complex::operator*(complex& c) { complex t; t.real=real*c.real-image*c.image ; t.image=real*c.image+image*c.real; return t; }complex complex::operator/ (int n) { complex temp; temp.real=real/n; temp.image=image/n; return temp; }complex complex::operator -(complex& c) { complex t; t.real=real-c.real; t.image=image-c.image; return t; }void complex::operator+=(complex&c) { real=real+c.real; image=image+c.image; }void complex::operator =(complex&c) { real=abs(c.real)<0.00001?0:(c.real); image=abs(c.image)<0.00001?0:(c.image); }
2.主函数
/*******************主函数体***********************/#include "stdafx.h"#include"iostream"#include"cmath"#include"complex.h"#define pi 3.141592657#define N 16//需要运算的fft的点数#define length 4//存储需要的二进制位,即length=log2(N)using namespace std;/*********************重新排列输入序列函数****************************/void bit_reversal(complex a[],int n){ int bitdesicion(unsigned);//用来重现排序原来的输入信号序列应该对应变换的信号的序号 complex temp; int j; for(int i=0;ivoid getW(complex w[],int len,int flag);//获得正变换(flag=1)或者逆变换系数(flag=0) void fft_temporal(complex input[],int len,int flag);//基时间的fft,flag控制为正变换或者逆变换 void fft_frequency(complex input[],int len,int flag);//基频率的fft/************************************************************/ complex input[N];
complex output[N]; for(int i=0;i
output[j]=input[j]; fft_temporal(output,N,0);//"0"控制为逆变换 cout<<"基2时域IFFT输出信号为:"<