热度 6
2015-5-12 16:05
1904 次阅读|
1 个评论
#includestdio.h #includemath.h classcomplex// 定义一个类 , 实现复数的所有操作 { doubleReal,Image;// 实部与虚部 public: complex(doubler="0",doublei="0"){Real=r;Image=i;} doubleGetR(){returnReal;}// 取出实部 doubleGetI(){returnImage;}// 取出虚部 complexoperator+(complex);// 复数加法 complexoperator-(complex);// 复数减法 complexoperator*(complex);// 复数乘法 voidoperator=(complex);// 复数赋值 }; complexcomplex::operator+(complexc)// 复数加法 { complext; t.Real=Real+c.Real; t.Image=Image+c.Image; returnt; } complexcomplex::operator-(complexc)// 复数减法 { complext; t.Real=Real-c.Real; t.Image=Image-c.Image; returnt; } complexcomplex::operator*(complexc)// 复数乘法 { complext; t.Real=Real*c.Real-Image*c.Image; t.Image=Real*c.Image+Image*c.Real; returnt; } voidcomplex::operator=(complexc)// 复数赋值 { Real=c.Real; Image=c.Image; } voidfft(complexa ; a =t; } } for(m=1;m=jishu;m++)// 第 m 级蝶形运算 , 总级数为 jishu { kind=(int)pow(2,m-1);// 第 m 级有 2^(m-1) 种蝶形运算 distance=2*kind;// 同种蝶形结相邻距离为 2^m u=complex(1,0);// 旋转因子初始值为 1 tmp=PI/kind; Wn=complex(cos(tmp),-sin(tmp));// 旋转因子 Wn for(j=0;jkind;j++)// 每种蝶形运算的起始点为 j, 共有 kind 种 { for(i=j;ilength;i+=distance)// 同种蝶形运算 { other=i+kind;// 蝶形运算的两个因子对应单元下标的距离为 2^(m-1) t=a *u;// 蝶形运算的乘积项 a =a-t;// 蝶形运算 a=a+t;// 蝶形运算 } u="u"*Wn;// 修改旋转因子 , 多乘一个基本 DFT 因子 WN } } } voidmain(void) { doublea,b; complexx ;// 此程序以 8 点序列测试 printf("8 点序列 :\n"); for(inti="0";i8;i++)// 初始化并输出原始序列 { x=complex(i,i+1); printf("x(%d)=%lf+%lfi\n",i+1,x.GetR(),x.GetI()); } fft(x,8,3);// 调用 fft 函数 printf("fft 变换的结果为 :\n"); for(i=0;i8;i++)// 输出结果 printf("X(%d)=%lf+%lfi\n",i+1,x.GetR(),x.GetI()); }