00001
00002
00003
00004
00005
00006
00007 #ifndef __IM_COMPLEX_H
00008 #define __IM_COMPLEX_H
00009
00010 #include "im_math.h"
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 class imcfloat
00027 {
00028 public:
00029 float real;
00030 float imag;
00031
00032
00033 imcfloat():real(0), imag(0) {}
00034
00035
00036 imcfloat(const float& r, const float& i):real(r),imag(i) {}
00037
00038
00039 imcfloat(const float& r):real(r),imag(0) {}
00040 };
00041
00042
00043
00044
00045
00046
00047 inline int operator <= (const imcfloat& C1, const imcfloat& C2)
00048 {
00049 return ((C1.real <= C2.real) && (C1.imag <= C2.imag));
00050 }
00051
00052 inline int operator <= (const imcfloat& C, const float& F)
00053 {
00054 return ((F <= C.real) && (0 <= C.imag));
00055 }
00056
00057 inline int operator < (const imcfloat& C1, const imcfloat& C2)
00058 {
00059 return ((C1.real < C2.real) && (C1.imag < C2.imag));
00060 }
00061
00062 inline int operator < (const imcfloat& C, const float& F)
00063 {
00064 return ((F < C.real) && (0 < C.imag));
00065 }
00066
00067 inline int operator > (const imcfloat& C1, const imcfloat& C2)
00068 {
00069 return ((C1.real > C2.real) && (C1.imag > C2.imag));
00070 }
00071
00072 inline int operator > (const imcfloat& C, const float& F)
00073 {
00074 return ((F > C.real) && (0 > C.imag));
00075 }
00076
00077 inline imcfloat operator + (const imcfloat& C1, const imcfloat& C2)
00078 {
00079 return imcfloat(C1.real + C2.real, C1.imag + C2.imag);
00080 }
00081
00082 inline imcfloat operator += (const imcfloat& C1, const imcfloat& C2)
00083 {
00084 return imcfloat(C1.real + C2.real, C1.imag + C2.imag);
00085 }
00086
00087 inline imcfloat operator - (const imcfloat& C1, const imcfloat& C2)
00088 {
00089 return imcfloat(C1.real - C2.real, C1.imag - C2.imag);
00090 }
00091
00092 inline imcfloat operator * (const imcfloat& C1, const imcfloat& C2)
00093 {
00094 return imcfloat(C1.real * C2.real - C1.imag * C2.imag,
00095 C1.imag * C2.real + C1.real * C2.imag);
00096 }
00097
00098 inline imcfloat operator / (const imcfloat& C1, const imcfloat& C2)
00099 {
00100 float den = C2.real * C2.real - C2.imag * C2.imag;
00101 return imcfloat((C1.real * C2.real + C1.imag * C2.imag) / den,
00102 (C1.imag * C2.real - C1.real * C2.imag) / den);
00103 }
00104
00105 inline imcfloat operator / (const imcfloat& C, const float& R)
00106 {
00107 return imcfloat(C.real / R, C.imag / R);
00108 }
00109
00110 inline imcfloat operator /= (const imcfloat& C, const float& R)
00111 {
00112 return imcfloat(C.real / R, C.imag / R);
00113 }
00114
00115 inline imcfloat operator * (const imcfloat& C, const float& R)
00116 {
00117 return imcfloat(C.real * R, C.imag * R);
00118 }
00119
00120 inline int operator == (const imcfloat& C1, const imcfloat& C2)
00121 {
00122 return ((C1.real == C2.real) && (C1.imag == C2.imag));
00123 }
00124
00125 inline float cpxreal(const imcfloat& C)
00126 {
00127 return C.real;
00128 }
00129
00130 inline float cpximag(const imcfloat& C)
00131 {
00132 return C.imag;
00133 }
00134
00135 inline float cpxmag(const imcfloat& C)
00136 {
00137 return sqrtf(C.real*C.real + C.imag*C.imag);
00138 }
00139
00140 inline float cpxphase(const imcfloat& C)
00141 {
00142 return atan2f(C.real, C.imag);
00143 }
00144
00145 inline imcfloat cpxconj(const imcfloat& C)
00146 {
00147 return imcfloat(C.real, -C.imag);
00148 }
00149
00150 inline imcfloat log(const imcfloat& C)
00151 {
00152 return imcfloat(logf(cpxmag(C)), atan2f(C.real, C.imag));
00153 }
00154
00155 inline imcfloat exp(const imcfloat& C)
00156 {
00157 float mag = expf(C.real);
00158 return imcfloat(mag * cosf(C.imag), mag * sinf(C.imag));
00159 }
00160
00161 inline imcfloat pow(const imcfloat& C1, const imcfloat& C2)
00162 {
00163 return exp(C1 * log(C2));
00164 }
00165
00166 inline imcfloat sqrt(const imcfloat& C)
00167 {
00168 float mag = sqrtf(sqrtf(C.real*C.real + C.imag*C.imag));
00169 float phase = atan2f(C.real, C.imag) / 2;
00170 return imcfloat(mag * cosf(phase), mag * sinf(phase));
00171 }
00172
00173 inline imcfloat cpxpolar(const float& mag, const float& phase)
00174 {
00175 return imcfloat(mag * cosf(phase), mag * sinf(phase));
00176 }
00177
00178
00179
00180 #endif