中国科学技术大学电子工程与信息科学系 多媒体通信实验室 (Copyright 2000 )
附录A C 语言实现数字信号处理算法
附录A1 BC 下复数类型的实现
本部分内容可以在http ://3/dsp/C/ComplexUse.cpp获得。
1、利用BC 提供的复数支持
//BC 中使用复数类型使用示例(Complex Use.Cpp 文件)
#include
#include
int main(void)
{
double x = 3.1, y = 4.2;
complex z = complex(x,y);
cout << "z = "<< z << "\n";
cout << " and imaginary real part = " << imag(z) << "\n";
cout << "z has complex conjugate = " << conj(z) << " \n";
return 0;
}
2、定义复数类,填写相应成员函数
//C 中的复数类型调用时可能不是非常好用,可自己定义复数类(Complex Use.Cpp 文件)
class Complex{
public :
Complex(){}
Complex( float re, float im );
float r(){return real;};
float i(){return imag;};
float mod(){return sqrt(real*real+imag*imag);};
Complex operator+( Complex &other );
Complex operator-( Complex &other );
Complex operator*( Complex &other );
Complex operator/( Complex &other );
private:
float real, imag;
};// Op erator overloaded using a member function
Complex::Complex(float re,float im){
如果您在阅读过程中发现疏漏和错误,请您尽快和编者取得联系 network@ cxh@
中国科学技术大学电子工程与信息科学系 多媒体通信实验室 (Copyright 2000 )
real=re;
imag=im;
};
Complex Complex::operator+( Complex &other ){
return Complex( real + other.real, imag + other.imag );
};
Complex Complex::operator-( Complex &other ){
return Complex( real - other.real, imag - other.imag );
};
Complex Complex::operator*( Complex &other ){
float x,y;
x=real*other.real-imag*other.imag;
y=real*other.imag+imag*other.real;
return Complex( x,y );
};
Complex Complex::operator/( Complex &other ){
float x,y,l;
l=other.real*other.real+other.imag*other.imag;
x=real*other.real+imag*other.imag;
y=other.real*imag-real*other.imag;
x=x/l;
y=y/l;
return Complex(x