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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#ifndef __ComplexShortArray_h__
#define __ComplexShortArray_h__
#include "ShortArray.h"
/**
* A structure defining a fixed point complex number as two members of type int16_t.
*/
struct ComplexShort {
/**
The real part of the complex number.
*/
int16_t re;
/**
The imaginary part of the complex number.
*/
int16_t im;
/**
Get the magnitude of the complex number.
Computes and returns the magnitude of the complex number.
@return The magnitude of the complex number.
*/
int16_t getMagnitude();
/**
Get the phase of the complex number.
Computes and returns the phase of the complex number.
@return The phase of the complex number.
*/
int16_t getPhase();
/**
Set the phase of the complex number.
Set the phase of the complex number, keeping the magnitude unaltered.
@param phase The new phase of the complex number
*/
void setPhase(int16_t phase){
int16_t magnitude = getMagnitude();
setPolar(magnitude, phase);
}
/**
Set the magnitude of the complex number.
Set the magnitude of the complex number, keeping the phase unaltered.
@param magnitude The new magnitude of the complex number
*/
void setMagnitude(int16_t magnitude){
int16_t phase = getPhase();
setPolar(magnitude, phase);
}
/**
Set magnitude and phase of the complex number.
@param magnitude The new magnitude of the complex number
@param phase The new phase of the complex number
*/
void setPolar(int16_t magnitude, int16_t phase);
bool operator==(const ComplexShort& other) const {
return re == other.re && im == other.im;
}
bool operator!=(const ComplexShort& other) const {
return re != other.re || im != other.im;
}
};
class ComplexShortArray : public SimpleArray<ComplexShort> {
public:
ComplexShortArray(){}
ComplexShortArray(ComplexShort* data, size_t size) :
SimpleArray(data, size) {}
/**
The real part of an element of the array.
@param i The index of the element
@return The real part of the element
*/
int16_t re(const int i){
return data[i].re;
}
/**
The imaginary part of an element of the array.
@param i The index of the element
@return The imaginary part of the element
*/
int16_t im(const int i){
return data[i].im;
}
void clear(){
setAll(0);
}
/**
The magnitude of an element of the array.
@param i The index of the element
@return The magnitude of the element
*/
int16_t mag(const int i);
/**
The magnitudes of the elements of the array.
@param[out] destination The array where the magnitude values will be stored.
*/
void getMagnitudeValues(ShortArray destination);
/**
The magnitude squared of an element of the array.
@param i The index of the element
@return The magnitude squared of the element
*/
int16_t mag2(const int i);
/**
The squared magnitudes of the elements of the array.
@param[out] destination The array where the magnitude squared values will be stored.
*/
void getMagnitudeSquaredValues(ShortArray destination);
/**
The complex conjugate values of the element of the array.
@param[out] destination The array where the complex conjugate values will be stored.
*/
void getComplexConjugateValues(ComplexShortArray destination);
/**
Complex dot product between arrays.
@param[in] operand2 The second operand of the dot product
@param[out] result The array where the result of the dot product is stored
*/
void complexDotProduct(ComplexShortArray operand2, ComplexShort& result);
/**
Complex by complex multiplication between arrays.
@param[in] operand2 The second operand of the multiplication
@param[out] result The array where the result of the multiplication is stored
*/
void complexByComplexMultiplication(ComplexShortArray operand2, ComplexShortArray result);
/**
Complex by real multiplication between arrays.
@param[in] operand2 The second operand of the multiplication
@param[out] result The array where the result of the multiplication is stored
*/
void complexByRealMultiplication(ShortArray operand2, ComplexShortArray result);
/**
* Element-wise sum between complex arrays.
* Sets each element in **destination** to the complex sum of the corresponding element of the array and **operand2**
* @param[in] operand2 second operand for the sum
* @param[out] destination the destination array
*/
void add(ComplexShortArray operand2, ComplexShortArray destination);
/**
* In-place element-wise sum between complex arrays.
* Adds each element of **operand2** to the corresponding element in the array.
* @param operand2 second operand for the sum
*/
void add(ComplexShortArray operand2);
/**
* Element-wise difference between complex arrays.
* Sets each element in **destination** to the complex difference between the corresponding element of the array and **operand2**
* @param[in] operand2 second operand for the subtraction
* @param[out] destination the destination array
*/
void subtract(ComplexShortArray operand2, ComplexShortArray destination);
/**
* In-place element-wise difference between complex arrays.
* Sutracts each element of **operand2** from the corresponding element in the array.
* @param operand2 second operand for the sum
*/
void subtract(ComplexShortArray operand2);
/**
The value of the element with the maximum magnitude in the array.
@return The maximum magnitude value in the array.
*/
int16_t getMaxMagnitudeValue();
/**
The index of the element with the maximum magnitude in the array.
@return The index of the element with the maximum magnitude in the array.
*/
unsigned int getMaxMagnitudeIndex();
/**
* A subset of the array.
* Returns an array that points to subset of the memory used by the original array.
* @param[in] offset the first element of the subset.
* @param[in] length the number of elments in the new ComplexShortArray.
* @return the newly created ComplexShortArray.
* @remarks no memory is allocated by this method. The memory is still shared with the original array.
* The memory should not be de-allocated elsewhere (e.g.: by calling ComplexShortArray::destroy() on the original ComplexShortArray)
* as long as the ComplexShortArray returned by this method is still in use.
* @remarks Calling ComplexShortArray::destroy() on a ComplexShortArray instance created with this method might cause an exception.
*/
ComplexShortArray subArray(unsigned int offset, unsigned int length);
/** Get the real part of the elements of the array.
* @param[out] buf The array where the real part will be stored.
*/
void getRealValues(ShortArray buf);
/** Get the imaginary part of the elements of the array.
* @param[out] buf The array where the imaginary part will be stored.
*/
void getImaginaryValues(ShortArray buf);
/** Array by scalar multiplication.
* @param factor The value by which all the elements of the array are multiplied.
*/
void scale(int16_t factor);
/**
* Creates a new ComplexShortArray.
* Allocates size*sizeof(int16_t) bytes of memory and returns a ComplexShortArray that points to it.
* @param size The size of the new ComplexShortArray.
* @return A ComplexShortArray which **data** point to the newly allocated memory and <code>size</code> is initialized to the proper value.
* @remarks A ComplexShortArray created with this method has to be destroyed invoking the ComplexShortArray::destroy() method.
*/
static ComplexShortArray create(unsigned int size);
/**
* Destroys a ComplexShortArray created with the create() method.
* @param array The ComplexShortArray to be destroyed.
* @remarks The ComplexShortArray object passed as an argument should not be used again after invoking this method.
* @remarks A ComplexShortArray object that has not been created by the ComplexShortArray::create() method might cause an exception if passed as an argument to this method.
*/
static void destroy(ComplexShortArray);
/**
* Set all the elements in the array.
* @param[in] value All the elements are set to this value.
*/
void setAll(ComplexShort value);
/**
* Set all the elements in the array.
* @param[in] value The real and imaginary parts of all the elements in the array are set to this value.
*/
void setAll(int16_t value);
/**
* Set all the elements in the array.
* @param[in] valueRe The real part of every element of the the array will be set to this value.
* @param[in] valueIm The imaginary pary of rvery element of the the array will be set to this value.
*/
void setAll(int16_t valueRe, int16_t valueIm);
/**
* Set all the elements in the array using polar coordinates.
* @param[in] magnitude An array containing the magnitudes.
* @param[in] phase An array containing the phases.
*/
void setPolar(ShortArray magnitude, ShortArray phase);
/**
* Set a range of elements in the array using polar coordinates.
* @param[in] magnitude An array containing the magnitudes.
* @param[in] phase An array containing the phases.
* @param[in] offset First element to set
* @param[in] count Number of elements to set
*/
void setPolar(ShortArray magnitude, ShortArray phase, int offset, int count);
/**
* Set the phase of the elements of the array, leaving the magnitude unchanged.
* @param[in] phase An array containing the phases.
*/
void setPhase(ShortArray phase);
/**
* Set the phase of a range of elements of the array, leaving the magnitude unchanged.
* @param[in] phase An array containing the phases.
* @param[in] offset First element to set
* @param[in] count Number of elements to set
*/
void setPhase(ShortArray phase, int offset, int count);
/**
* Set the phase of the elements of an array, using the magnitude from the current array.
* @param[in] phase An array containing the phases.
* @param[out] destination The destination array.
*/
void setPhase(ShortArray phase, ComplexShortArray destination);
/**
* Set the phase of a range of the elements of an array, using the magnitude from the current array.
*
* The <code>offset</code> and <code>count</code> specified apply to both the source and <code>destination</code> arrays.
* Elements of the <code>destination</code> array outside this range will not be affected.
* @param[in] phase An array containing the phases.
* @param[in] offset First element to set
* @param[in] count Number of elements to set
* @param[out] destination The destination array.
*/
void setPhase(ShortArray phase, int offset, int count, ComplexShortArray destination);
/**
* Set the magnitude of the elements of the array, leaving the phase unchanged.
* @param[in] magnitude An array containing the magnitudes.
*/
void setMagnitude(ShortArray magnitude);
/**
* Set the magnitude of a range of elements of the array, leaving the phase unchanged.
* @param[in] magnitude An array containing the magnitudes.
* @param[in] offset First element to set
* @param[in] count Number of elements to set
*/
void setMagnitude(ShortArray magnitude, int offset, int count);
/**
* Set the magnitude of the elements of an array, using the phase from the current array.
* @param[in] magnitude An array containing the magnitudes.
* @param[out] destination The destination array.
*/
void setMagnitude(ShortArray magnitude, ComplexShortArray destination);
/**
* Set the magnitude of a range of the elements of an array, using the phases from the current array.
*
* The <code>offset</code> and <code>count</code> specified apply to both the source and <code>destination</code> arrays.
* Elements of the <code>destination</code> array outside this range will not be affected.
* @param[in] magnitude An array containing the magnitudes.
* @param[in] offset First element to set
* @param[in] count Number of elements to set
* @param[out] destination The destination array.
*/
void setMagnitude(ShortArray magnitude, int offset, int count, ComplexShortArray destination);
};
#endif // __ComplexShortArray_h__