-
Notifications
You must be signed in to change notification settings - Fork 4
/
Complex.cpp
231 lines (185 loc) · 5.32 KB
/
Complex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// speaker.cpp : implementation of the CDataSetArray classes
//
#include "stdafx.h"
#include "audtest.h"
#include "Complex.h"
#include "math.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// ZComplex
/////////////////////////////////////////////////////////////////////////////
ZComplex::ZComplex( )
{
m_dX = m_dY = 0.0;
}
ZComplex::ZComplex(const ZComplex &cIn )
{
m_dX = cIn.m_dX;
m_dY = cIn.m_dY;
}
ZComplex::ZComplex(double dX, double dY)
{
m_dX = dX;
m_dY = dY;
}
ZComplex::~ZComplex()
{
}
/////////////////////////////////////////////////////////////////////////////
// ZComplex Operations
/////////////////////////////////////////////////////////////////////////////
double ZComplex::Amplitude()
{
return sqrt( m_dX * m_dX + m_dY * m_dY);
}
double ZComplex::Phase()
{
return (180 * atan2( m_dY, m_dX) / ONEPI );
}
/////////////////////////////////////////////////////////////////////////////
// ZComplex Operators
/////////////////////////////////////////////////////////////////////////////
void ZComplex::SetRectangular( double dX, double dY)
{
m_dX = dX;
m_dY = dY;
}
void ZComplex::SetPolar( double dAmp, double dPhase)
{
m_dX = dAmp * cos( ONEPI * dPhase / 180);
m_dY = dAmp * sin( ONEPI * dPhase / 180);
}
// -------------------------- assignment
const ZComplex& ZComplex::operator=(const ZComplex& cIn)
{
m_dX = cIn.m_dX;
m_dY = cIn.m_dY;
return *this;
}
// -------------------------- + assignment
const ZComplex& ZComplex::operator+=(const ZComplex& cIn)
{
m_dX += cIn.m_dX;
m_dY += cIn.m_dY;
return *this;
}
// -------------------------- - assignment
const ZComplex& ZComplex::operator-=(const ZComplex& cIn)
{
m_dX -= cIn.m_dX;
m_dY -= cIn.m_dY;
return *this;
}
// -------------------------- * assignment
const ZComplex& ZComplex::operator*=(const ZComplex& cIn)
{
*this = (*this)*cIn;
return *this;
}
// -------------------------- * assignment by double
const ZComplex& ZComplex::operator*=(const double& dMult)
{
m_dX *= dMult;
m_dY *= dMult;
return *this;
}
// -------------------------- / assignment
const ZComplex& ZComplex::operator/=(const ZComplex& cIn)
{
*this = (*this)/cIn;
return *this;
}
// -------------------------- * assignment by double
const ZComplex& ZComplex::operator/=(const double& dDiv)
{
m_dX /= dDiv;
m_dY /= dDiv;
return *this;
}
// -------------------------- unary -
ZComplex AFXAPI operator- (const ZComplex &cIn)
{
ZComplex zout( -cIn.m_dX, -cIn.m_dY);
return zout;
}
/////////////////////////////////////////////////////////////////////////////
// -------------------------- add
/////////////////////////////////////////////////////////////////////////////
ZComplex AFXAPI operator+ (const ZComplex &cIn, const ZComplex &cSrc)
{
ZComplex zout;
zout.m_dX = cIn.m_dX + cSrc.m_dX;
zout.m_dY = cIn.m_dY + cSrc.m_dY;
return zout;
}
/////////////////////////////////////////////////////////////////////////////
// -------------------------- subtract
/////////////////////////////////////////////////////////////////////////////
ZComplex AFXAPI operator- (const ZComplex &cIn, const ZComplex &cSrc)
{
ZComplex zout;
zout.m_dX = cIn.m_dX - cSrc.m_dX;
zout.m_dY = cIn.m_dY - cSrc.m_dY;
return zout;
}
/////////////////////////////////////////////////////////////////////////////
// -------------------------- multiply
/////////////////////////////////////////////////////////////////////////////
ZComplex AFXAPI operator* (const ZComplex &cIn, const ZComplex &cSrc)
{
ZComplex zout;
zout.m_dX = cIn.m_dX * cSrc.m_dX - cIn.m_dY * cSrc.m_dY;
zout.m_dY = cIn.m_dX * cSrc.m_dY + cIn.m_dY * cSrc.m_dX;
return zout;
}
ZComplex AFXAPI operator* (const ZComplex &cIn, const double &dMult)
{
ZComplex zout;
zout.m_dX = cIn.m_dX * dMult;
zout.m_dY = cIn.m_dY * dMult;
return zout;
}
/////////////////////////////////////////////////////////////////////////////
// -------------------------- divide
/////////////////////////////////////////////////////////////////////////////
ZComplex AFXAPI operator/ (const ZComplex &cIn, const ZComplex &cSrc)
{
ZComplex zout;
double damp = cSrc.m_dX * cSrc.m_dX + cSrc.m_dY * cSrc.m_dY;
zout.m_dX = (cIn.m_dX * cSrc.m_dX + cIn.m_dY * cSrc.m_dY) / damp;
zout.m_dY = (cIn.m_dY * cSrc.m_dX - cIn.m_dX * cSrc.m_dY) / damp;
return zout;
}
ZComplex AFXAPI operator/ (const ZComplex &cIn, const double &dDiv)
{
ZComplex zout;
zout.m_dX = cIn.m_dX / dDiv;
zout.m_dY = cIn.m_dY / dDiv;
return zout;
}
/////////////////////////////////////////////////////////////////////////////
// -------------------------- parallel (operator | )
/////////////////////////////////////////////////////////////////////////////
ZComplex AFXAPI operator| (const ZComplex &cIn, const ZComplex &cSrc)
{
double sx, sy; // sum coefficients
ZComplex zsum;
sx = cIn.XValue() + cSrc.XValue();
sy = cIn.YValue() + cSrc.YValue();
if ( sx || sy)
{ // do this to avoid building more zcomplex structures
double rl = cIn.XValue() * cSrc.XValue() - cIn.YValue() * cSrc.YValue();
double cp = cIn.XValue() * cSrc.YValue() + cIn.YValue() * cSrc.XValue();
double x = rl * sx + cp * sy;
double y = cp * sx - rl * sy;
rl = sx * sx + sy * sy;
zsum.SetRectangular( x / rl, y / rl);
}
else
zsum.SetRectangular( 1000000, 1000000);
// return ( cIn * cSrc / zsum);
return zsum;
}