33
44#include " FloatArray.h"
55#include " basicmaths.h"
6+
67/* *
78* A structure defining a floating point complex number as two members of type float.
89*/
910struct ComplexFloat {
11+ constexpr ComplexFloat () : re(0 ), im(0 ) {}
12+ constexpr ComplexFloat (float x) : re(x), im(0 ) {}
13+ constexpr ComplexFloat (float re, float im) : re(re), im(im) {}
14+
1015 /* *
1116 * The real part of the complex number.
1217 */
@@ -75,6 +80,20 @@ struct ComplexFloat {
7580 im = magnitude*sinf (phase);
7681 }
7782
83+ /* *
84+ * Returns complex conjugate - a copy of current number with imaginary part inverted
85+ */
86+ ComplexFloat getComplexConjugate () const {
87+ return ComplexFloat {re, -im};
88+ }
89+
90+ /* *
91+ * Returns dot product with another complex float value
92+ */
93+ ComplexFloat getDotProduct (ComplexFloat other) const {
94+ return ComplexFloat {re * other.re - im * other.im , re * other.im + im * other.re };
95+ }
96+
7897 bool operator <(const ComplexFloat& other) const {
7998 return getMagnitudeSquared () < other.getMagnitudeSquared ();
8099 }
@@ -99,6 +118,88 @@ struct ComplexFloat {
99118 return re != other.re || im != other.im ;
100119 }
101120
121+ friend const ComplexFloat operator +(const ComplexFloat&lhs, const ComplexFloat& rhs) {
122+ ComplexFloat result = lhs;
123+ result += rhs;
124+ return result;
125+ }
126+
127+ friend const ComplexFloat operator +(const ComplexFloat&lhs, float rhs) {
128+ ComplexFloat result = lhs;
129+ result += rhs;
130+ return result;
131+ }
132+
133+ ComplexFloat& operator +=(float other) {
134+ re += other;
135+ return *this ;
136+ }
137+
138+ ComplexFloat& operator +=(const ComplexFloat& other) {
139+ re += other.re ;
140+ im += other.im ;
141+ return *this ;
142+ }
143+
144+ friend const ComplexFloat operator -(const ComplexFloat&lhs, const ComplexFloat& rhs) {
145+ ComplexFloat result = lhs;
146+ result -= rhs;
147+ return result;
148+ }
149+
150+ friend const ComplexFloat operator -(const ComplexFloat&lhs, float rhs) {
151+ ComplexFloat result = lhs;
152+ result -= rhs;
153+ return result;
154+ }
155+
156+ ComplexFloat& operator -=(float other) {
157+ re -= other;
158+ return *this ;
159+ }
160+
161+ ComplexFloat& operator -=(const ComplexFloat& other) {
162+ re -= other.re ;
163+ im -= other.im ;
164+ return *this ;
165+ }
166+
167+ friend const ComplexFloat operator *(const ComplexFloat&lhs, const ComplexFloat& rhs) {
168+ ComplexFloat result = lhs;
169+ result *= rhs;
170+ return result;
171+ }
172+
173+ friend const ComplexFloat operator *(const ComplexFloat&lhs, float rhs) {
174+ ComplexFloat result = lhs;
175+ result *= rhs;
176+ return result;
177+ }
178+
179+ ComplexFloat& operator *=(float other) {
180+ re *= other;
181+ im *= other;
182+ return *this ;
183+ }
184+
185+ ComplexFloat& operator *=(const ComplexFloat& other) {
186+ re = re * other.re - im * other.im ;
187+ im = re * other.im + im * other.re ;
188+ return *this ;
189+ }
190+
191+ friend const ComplexFloat operator /(const ComplexFloat&lhs, float rhs) {
192+ ComplexFloat result = lhs;
193+ result /= rhs;
194+ return result;
195+ }
196+
197+ ComplexFloat& operator /=(float other) {
198+ re /= other;
199+ im /= other;
200+ return *this ;
201+ }
202+
102203};
103204
104205class ComplexFloatArray : public SimpleArray <ComplexFloat> {
@@ -396,6 +497,24 @@ class ComplexFloatArray : public SimpleArray<ComplexFloat> {
396497 * @param[out] destination The destination array.
397498 */
398499 void setMagnitude (FloatArray magnitude, int offset, size_t count, ComplexFloatArray destination);
500+
501+ using SimpleArray<ComplexFloat>::copyFrom;
502+ using SimpleArray<ComplexFloat>::copyTo;
503+ /* *
504+ * Merge two channels of audio containing real and imaginary axis data into this array
505+ *
506+ * @param[in] real Real axis data
507+ * @param[in] imag Imaginary axis data
508+ */
509+ void copyFrom (FloatArray real, FloatArray imag);
510+
511+ /* *
512+ * Split complex data into two channels of audio containing real and imaginary axis data
513+ *
514+ * @param[in] real Real axis data
515+ * @param[in] imag Imaginary axis data
516+ */
517+ void copyTo (FloatArray real, FloatArray imag);
399518};
400519
401520#endif // __ComplexFloatArray_h__
0 commit comments