标签:数字信号(78)
C语言实现数字信号处理算法
附录A1 BC下复数类型的实现
1、利用BC提供的复数支持
//BC中使用复数类型使用示例(ComplexUse.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中的复数类型调用时可能不是非常好用,可自己定义复数类(ComplexUse.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;
};// Operator overloaded using a member function
Complex::Complex(float re,float im){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,y);
};