OpenWareLaboratory
ComplexFloatArray.h
Go to the documentation of this file.
1 #ifndef __ComplexFloatArray_h__
2 #define __ComplexFloatArray_h__
3 
4 #include "FloatArray.h"
5 #include "basicmaths.h"
6 
10 struct 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 
18  float re;
19 
23  float im;
24 
30  float getMagnitude() const{
31  return sqrtf(re*re+im*im);
32  }
33 
40  float getMagnitudeSquared() const{
41  return re*re+im*im;
42  }
43 
49  float getPhase() const{
50  return atan2f(im,re);
51  }
52 
58  void setPhase(float phase){
59  float magnitude = getMagnitude();
60  setPolar(magnitude, phase);
61  }
62 
68  void setMagnitude(float magnitude){
69  float phase = getPhase();
70  setPolar(magnitude, phase);
71  }
72 
78  void setPolar(float magnitude, float phase){
79  re = magnitude*cosf(phase);
80  im = magnitude*sinf(phase);
81  }
82 
87  return ComplexFloat {re, -im};
88  }
89 
94  return ComplexFloat {re * other.re - im * other.im, re * other.im + im * other.re};
95  }
96 
97  bool operator<(const ComplexFloat& other) const {
98  return getMagnitudeSquared() < other.getMagnitudeSquared();
99  }
100 
101  bool operator>(const ComplexFloat& other) const {
102  return getMagnitudeSquared() > other.getMagnitudeSquared();
103  }
104 
105  bool operator<=(const ComplexFloat& other) const {
106  return getMagnitudeSquared() <= other.getMagnitudeSquared();
107  }
108 
109  bool operator>=(const ComplexFloat& other) const {
110  return getMagnitudeSquared() >= other.getMagnitudeSquared();
111  }
112 
113  bool operator==(const ComplexFloat& other) const {
114  return re == other.re && im == other.im;
115  }
116 
117  bool operator!=(const ComplexFloat& other) const {
118  return re != other.re || im != other.im;
119  }
120 
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 
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 
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 
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 
203 };
204 
205 class ComplexFloatArray : public SimpleArray<ComplexFloat> {
206 public:
209  SimpleArray(data, size) {}
210 
216  float re(const int i){
217  return data[i].re;
218  }
219 
225  float im(const int i){
226  return data[i].im;
227  }
228 
229  void clear(){
230  setAll(0);
231  }
232 
238  float mag(const int i);
239 
244  void getMagnitudeValues(FloatArray destination);
245 
251  float mag2(const int i);
252 
257  void getMagnitudeSquaredValues(FloatArray destination);
258 
264 
270  void complexDotProduct(ComplexFloatArray operand2, ComplexFloat& result);
271 
278 
285 
292  void add(ComplexFloatArray operand2, ComplexFloatArray destination);
293 
299  void add(ComplexFloatArray operand2);
300 
307  void subtract(ComplexFloatArray operand2, ComplexFloatArray destination);
308 
314  void subtract(ComplexFloatArray operand2);
315 
320  float getMaxMagnitudeValue();
321 
326  int getMaxMagnitudeIndex();
327 
339  ComplexFloatArray subArray(int offset, size_t length);
340 
344  void getRealValues(FloatArray buf);
345 
349  void getImaginaryValues(FloatArray buf);
350 
354  void scale(float factor);
355 
363  static ComplexFloatArray create(size_t size);
364 
371  static void destroy(ComplexFloatArray);
372 
377  void fromFloat(FloatArray source);
378 
384  void toFloat(FloatArray destination);
385 
390  void setAll(ComplexFloat value);
391 
396  void setAll(float value);
397 
403  void setAll(float valueRe, float valueIm);
404 
410  void getPolar(FloatArray magnitude, FloatArray phase);
411 
417  void setPolar(FloatArray magnitude, FloatArray phase);
418 
426  void setPolar(FloatArray magnitude, FloatArray phase, int offset, size_t count);
427 
432  void getPhaseValues(FloatArray destination);
433 
438  void setPhase(FloatArray phase);
439 
446  void setPhase(FloatArray phase, int offset, size_t count);
447 
453  void setPhase(FloatArray phase, ComplexFloatArray destination);
454 
465  void setPhase(FloatArray phase, int offset, size_t count, ComplexFloatArray destination);
466 
467 
472  void setMagnitude(FloatArray magnitude);
473 
480  void setMagnitude(FloatArray magnitude, int offset, size_t count);
481 
487  void setMagnitude(FloatArray magnitude, ComplexFloatArray destination);
488 
499  void setMagnitude(FloatArray magnitude, int offset, size_t count, ComplexFloatArray destination);
500 
509  void copyFrom(FloatArray real, FloatArray imag);
510 
517  void copyTo(FloatArray real, FloatArray imag);
518 };
519 
520 #endif // __ComplexFloatArray_h__
void getPhaseValues(FloatArray destination)
The phases of the elements of the array.
float im(const int i)
Get the imaginary part of an element of the array.
void getImaginaryValues(FloatArray buf)
Get the imaginary part of the elements of the array.
void getComplexConjugateValues(ComplexFloatArray destination)
The complex conjugate values of the element of the array.
void setPolar(FloatArray magnitude, FloatArray phase)
Set all the elements in the array using polar coordinates.
void setPhase(FloatArray phase)
Set the phase of the elements of the array, leaving the magnitude unchanged.
void getMagnitudeSquaredValues(FloatArray destination)
The squared magnitudes of the elements of the array.
void getRealValues(FloatArray buf)
Get the real part of the elements of the array.
void add(ComplexFloatArray operand2, ComplexFloatArray destination)
Element-wise sum between complex arrays.
static void destroy(ComplexFloatArray)
Destroys a ComplexFloatArray created with the create() method.
void setMagnitude(FloatArray magnitude)
Set the magnitude of the elements of the array, leaving the phase unchanged.
void complexDotProduct(ComplexFloatArray operand2, ComplexFloat &result)
Complex dot product between arrays.
ComplexFloatArray subArray(int offset, size_t length)
A subset of the array.
ComplexFloatArray(ComplexFloat *data, size_t size)
void copyTo(FloatArray real, FloatArray imag)
Split complex data into two channels of audio containing real and imaginary axis data.
void complexByComplexMultiplication(ComplexFloatArray operand2, ComplexFloatArray result)
Complex by complex multiplication between arrays.
void getPolar(FloatArray magnitude, FloatArray phase)
Get polar coordinates for all the elements in the array.
void getMagnitudeValues(FloatArray destination)
The magnitudes of the elements of the array.
void setAll(ComplexFloat value)
Set all the elements in the array.
float mag2(const int i)
The magnitude squared of an element of the array.
void scale(float factor)
Array by scalar multiplication.
static ComplexFloatArray create(size_t size)
Creates a new ComplexFloatArray.
int getMaxMagnitudeIndex()
The index of the element with the maximum magnitude in the array.
void copyFrom(FloatArray real, FloatArray imag)
Merge two channels of audio containing real and imaginary axis data into this array.
float re(const int i)
Get the real part of an element of the array.
float getMaxMagnitudeValue()
The value of the element with the maximum magnitude in the array.
void subtract(ComplexFloatArray operand2, ComplexFloatArray destination)
Element-wise difference between complex arrays.
float mag(const int i)
Get the magnitude of an element of the array.
void toFloat(FloatArray destination)
Copies real and imaginary values of the ComplexFloatArray into a FloatArray.
void complexByRealMultiplication(FloatArray operand2, ComplexFloatArray result)
Complex by real multiplication between arrays.
void fromFloat(FloatArray source)
Copies real values from a FloatArray, sets imaginary values to 0.
This class contains useful methods for manipulating arrays of floats.
Definition: FloatArray.h:12
SimpleArray holds a pointer to an array and the array size, and is designed to be passed by value.
Definition: SimpleArray.h:14
A structure defining a floating point complex number as two members of type float.
float im
The imaginary part of the complex number.
friend const ComplexFloat operator+(const ComplexFloat &lhs, const ComplexFloat &rhs)
friend const ComplexFloat operator+(const ComplexFloat &lhs, float rhs)
ComplexFloat & operator*=(const ComplexFloat &other)
constexpr ComplexFloat(float x)
bool operator>=(const ComplexFloat &other) const
friend const ComplexFloat operator-(const ComplexFloat &lhs, float rhs)
friend const ComplexFloat operator/(const ComplexFloat &lhs, float rhs)
void setMagnitude(float magnitude)
Set the magnitude of the complex number.
ComplexFloat & operator*=(float other)
float re
The real part of the complex number.
constexpr ComplexFloat()
float getMagnitude() const
Get the magnitude of the complex number.
ComplexFloat & operator+=(const ComplexFloat &other)
float getPhase() const
Get the phase of the complex number.
ComplexFloat getDotProduct(ComplexFloat other) const
Returns dot product with another complex float value.
ComplexFloat getComplexConjugate() const
Returns complex conjugate - a copy of current number with imaginary part inverted.
void setPhase(float phase)
Set the phase of the complex number.
friend const ComplexFloat operator*(const ComplexFloat &lhs, float rhs)
void setPolar(float magnitude, float phase)
Set magnitude and phase of the complex number.
bool operator!=(const ComplexFloat &other) const
ComplexFloat & operator+=(float other)
friend const ComplexFloat operator*(const ComplexFloat &lhs, const ComplexFloat &rhs)
float getMagnitudeSquared() const
Get the squared magnitude of the complex number.
ComplexFloat & operator-=(float other)
bool operator<=(const ComplexFloat &other) const
bool operator>(const ComplexFloat &other) const
ComplexFloat & operator-=(const ComplexFloat &other)
bool operator==(const ComplexFloat &other) const
bool operator<(const ComplexFloat &other) const
constexpr ComplexFloat(float re, float im)
friend const ComplexFloat operator-(const ComplexFloat &lhs, const ComplexFloat &rhs)
ComplexFloat & operator/=(float other)