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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#include "FloatArray.h"
#include "basicmaths.h"
#include "message.h"
#include <string.h>
void FloatArray::getMin(float* value, int* index){
/// @note When built for ARM Cortex-M processor series, this method uses the optimized <a href="http://www.keil.com/pack/doc/CMSIS/General/html/index.html">CMSIS library</a>
#ifdef ARM_CORTEX
uint32_t idx;
arm_min_f32(data, size, value, &idx);
*index = (int)idx;
#else
*value=data[0];
*index=0;
for(size_t n=1; n<size; n++){
float currentValue=data[n];
if(currentValue<*value){
*value=currentValue;
*index=n;
}
}
#endif
}
float FloatArray::getMinValue(){
float value;
int index;
/// @note When built for ARM Cortex-M processor series, this method uses the optimized <a href="http://www.keil.com/pack/doc/CMSIS/General/html/index.html">CMSIS library</a>
getMin(&value, &index);
return value;
}
int FloatArray::getMinIndex(){
float value;
int index;
/// @note When built for ARM Cortex-M processor series, this method uses the optimized <a href="http://www.keil.com/pack/doc/CMSIS/General/html/index.html">CMSIS library</a>
getMin(&value, &index);
return index;
}
void FloatArray::getMax(float* value, int* index){
ASSERT(size>0, "Wrong size");
/// @note When built for ARM Cortex-M processor series, this method uses the optimized <a href="http://www.keil.com/pack/doc/CMSIS/General/html/index.html">CMSIS library</a>
#ifdef ARM_CORTEX
uint32_t idx;
arm_max_f32(data, size, value, &idx);
*index = (int)idx;
#else
*value=data[0];
*index=0;
for(size_t n=1; n<size; n++){
float currentValue=data[n];
if(currentValue>*value){
*value=currentValue;
*index=n;
}
}
#endif
}
float FloatArray::getMaxValue(){
float value;
int index;
/// @note When built for ARM Cortex-M processor series, this method uses the optimized <a href="http://www.keil.com/pack/doc/CMSIS/General/html/index.html">CMSIS library</a>
getMax(&value, &index);
return value;
}
int FloatArray::getMaxIndex(){
float value;
int index;
/// @note When built for ARM Cortex-M processor series, this method uses the optimized <a href="http://www.keil.com/pack/doc/CMSIS/General/html/index.html">CMSIS library</a>
getMax(&value, &index);
return index;
}
void FloatArray::rectify(FloatArray& destination){ //this is actually "copy data with rectifify"
/// @note When built for ARM Cortex-M processor series, this method uses the optimized <a href="http://www.keil.com/pack/doc/CMSIS/General/html/index.html">CMSIS library</a>
#ifdef ARM_CORTEX
arm_abs_f32(data, destination.getData(), size);
#else
size_t minSize= min(size,destination.getSize()); //TODO: shall we take this out and allow it to segfault?
for(size_t n=0; n<minSize; n++){
destination[n] = fabsf(data[n]);
}
#endif
}
void FloatArray::reverse(FloatArray& destination){ //this is actually "copy data with reverse"
if(destination==*this){ //make sure it is not called "in-place"
reverse();
return;
}
for(size_t n=0; n<size; n++){
destination[n]=data[size-n-1];
}
}
void FloatArray::reverse(){//in place
for(size_t n=0; n<size/2; n++){
float temp=data[n];
data[n]=data[size-n-1];
data[size-n-1]=temp;
}
}
void FloatArray::reciprocal(FloatArray& destination){
float* data = getData();
for(size_t n=0; n<getSize(); n++)
destination[n] = 1.0f/data[n];
}
float FloatArray::getRms(){
float result;
/// @note When built for ARM Cortex-M processor series, this method uses the optimized <a href="http://www.keil.com/pack/doc/CMSIS/General/html/index.html">CMSIS library</a>
#ifdef ARM_CORTEX
arm_rms_f32 (data, size, &result);
#else
result=0;
float *pSrc= data;
for(size_t n=0; n<size; n++){
result += pSrc[n]*pSrc[n];
}
result=sqrtf(result/size);
#endif
return result;
}
float FloatArray::getSum(){
float result = 0;
for(size_t n=0; n<size; n++)
result += data[n];
return result;
}
float FloatArray::getMean(){
float result;
/// @note When built for ARM Cortex-M processor series, this method uses the optimized <a href="http://www.keil.com/pack/doc/CMSIS/General/html/index.html">CMSIS library</a>
#ifdef ARM_CORTEX
arm_mean_f32 (data, size, &result);
#else
result=0;
for(size_t n=0; n<size; n++){
result+=data[n];
}
result=result/size;
#endif
return result;
}
float FloatArray::getPower(){
float result;
/// @note When built for ARM Cortex-M processor series, this method uses the optimized <a href="http://www.keil.com/pack/doc/CMSIS/General/html/index.html">CMSIS library</a>
#ifdef ARM_CORTEX
arm_power_f32 (data, size, &result);
#else
result=0;
float *pSrc = data;
for(size_t n=0; n<size; n++){
result += pSrc[n]*pSrc[n];
}
#endif
return result;
}
float FloatArray::getStandardDeviation(){
float result;
/// @note When built for ARM Cortex-M processor series, this method uses the optimized <a href="http://www.keil.com/pack/doc/CMSIS/General/html/index.html">CMSIS library</a>
#ifdef ARM_CORTEX
arm_std_f32 (data, size, &result);
#else
result=sqrtf(getVariance());
#endif
return result;
}
float FloatArray::getVariance(){
float result;
/// @note When built for ARM Cortex-M processor series, this method uses the optimized <a href="http://www.keil.com/pack/doc/CMSIS/General/html/index.html">CMSIS library</a>
#ifdef ARM_CORTEX
arm_var_f32(data, size, &result);
#else
float sumOfSquares=getPower();
float sum=0;
for(size_t n=0; n<size; n++){
sum+=data[n];
}
result=(sumOfSquares - sum*sum/size) / (size - 1);
#endif
return result;
}
void FloatArray::clip(){
clip(1);
}
void FloatArray::clip(float max){
for(size_t n=0; n<size; n++){
if(data[n]>max)
data[n]=max;
else if(data[n]<-max)
data[n]=-max;
}
}
void FloatArray::clip(float min, float max){
for(size_t n=0; n<size; n++){
if(data[n]>max)
data[n]=max;
else if(data[n]<min)
data[n]=min;
}
}
FloatArray FloatArray::subArray(int offset, size_t length){
ASSERT(size >= offset+length, "Array too small");
return FloatArray(data+offset, length);
}
void FloatArray::setAll(float value){
/// @note When built for ARM Cortex-M processor series, this method uses the optimized <a href="http://www.keil.com/pack/doc/CMSIS/General/html/index.html">CMSIS library</a>
#ifdef ARM_CORTEX
arm_fill_f32(value, data, size);
#else
for(size_t n=0; n<size; n++){
data[n]=value;
}
#endif /* ARM_CORTEX */
}
void FloatArray::add(FloatArray operand2, FloatArray destination){ //allows in-place
ASSERT(operand2.size >= size && destination.size<=size, "Arrays must be matching size");
/// @note When built for ARM Cortex-M processor series, this method uses the optimized <a href="http://www.keil.com/pack/doc/CMSIS/General/html/index.html">CMSIS library</a>
#ifdef ARM_CORTEX
/* despite not explicitely documented in the CMSIS documentation,
this has been tested to behave properly even when pSrcA==pDst
void arm_add_f32 (float32_t *pSrcA, float32_t *pSrcB, float32_t *pDst, uint32_t blockSize)
*/
arm_add_f32(data, operand2.data, destination.data, size);
#else
for(size_t n=0; n<size; n++){
destination[n]=data[n]+operand2[n];
}
#endif /* ARM_CORTEX */
}
void FloatArray::add(FloatArray operand2){ //in-place
/// @note When built for ARM Cortex-M processor series, this method uses the optimized <a href="http://www.keil.com/pack/doc/CMSIS/General/html/index.html">CMSIS library</a>
add(operand2, *this);
}
void FloatArray::add(float scalar){
for(size_t n=0; n<size; n++){
data[n] += scalar;
}
}
void FloatArray::add(float scalar, FloatArray destination){
for(size_t n=0; n<size; n++)
destination[n] = data[n]+scalar;
}
void FloatArray::subtract(FloatArray operand2, FloatArray destination){ //allows in-place
ASSERT(operand2.size == size && destination.size >= size, "Arrays size mismatch");
/// @note When built for ARM Cortex-M processor series, this method uses the optimized <a href="http://www.keil.com/pack/doc/CMSIS/General/html/index.html">CMSIS library</a>
#ifdef ARM_CORTEX
/* despite not explicitely documented in the CMSIS documentation,
this has been tested to behave properly even when pSrcA==pDst
void arm_sub_f32 (float32_t *pSrcA, float32_t *pSrcB, float32_t *pDst, uint32_t blockSize)
*/
arm_sub_f32(data, operand2.data, destination.data, size);
#else
for(size_t n=0; n<size; n++){
destination[n]=data[n]-operand2[n];
}
#endif /* ARM_CORTEX */
}
void FloatArray::subtract(FloatArray operand2){ //in-place
/// @note When built for ARM Cortex-M processor series, this method uses the optimized <a href="http://www.keil.com/pack/doc/CMSIS/General/html/index.html">CMSIS library</a>
subtract(operand2, *this);
}
void FloatArray::subtract(float scalar){
for(size_t n=0; n<size; n++){
data[n]-=scalar;
}
}
void FloatArray::multiply(FloatArray operand2, FloatArray destination){ //allows in-place
ASSERT(operand2.size == size && destination.size==size, "Arrays must be same size");
/// @note When built for ARM Cortex-M processor series, this method uses the optimized <a href="http://www.keil.com/pack/doc/CMSIS/General/html/index.html">CMSIS library</a>
#ifdef ARM_CORTEX
/* despite not explicitely documented in the CMSIS documentation,
this has been tested to behave properly even when pSrcA==pDst
void arm_mult_f32 (float32_t *pSrcA, float32_t *pSrcB, float32_t *pDst, uint32_t blockSize)
*/
arm_mult_f32(data, operand2.data, destination, size);
#else
for(size_t n=0; n<size; n++){
destination[n]=data[n]*operand2[n];
}
#endif /* ARM_CORTEX */
}
void FloatArray::multiply(FloatArray operand2){ //in-place
/// @note When built for ARM Cortex-M processor series, this method uses the optimized <a href="http://www.keil.com/pack/doc/CMSIS/General/html/index.html">CMSIS library</a>
multiply(operand2, *this);
}
void FloatArray::multiply(float scalar){
#ifdef ARM_CORTEX
arm_scale_f32(data, scalar, data, size);
#else
for(size_t n=0; n<size; n++)
data[n]*=scalar;
#endif
}
void FloatArray::multiply(float scalar, FloatArray destination){
#ifdef ARM_CORTEX
arm_scale_f32(data, scalar, destination, size);
#else
for(size_t n=0; n<size; n++)
destination[n] = data[n] * scalar;
#endif
}
void FloatArray::negate(FloatArray& destination){//allows in-place
#ifdef ARM_CORTEX
arm_negate_f32(data, destination.getData(), size);
#else
for(size_t n=0; n<size; n++){
destination[n]=-data[n];
}
#endif /* ARM_CORTEX */
}
void FloatArray::noise(){
noise(-1, 1);
}
void FloatArray::noise(float min, float max){
float amplitude = fabsf(max-min);
float offset = min;
for(size_t n=0; n<size; n++)
data[n] = randf() * amplitude + offset;
}
void FloatArray::convolve(FloatArray operand2, FloatArray destination){
ASSERT(destination.size >= size + operand2.size -1, "Destination array too small");
#ifdef ARM_CORTEX
arm_conv_f32(data, size, operand2.data, operand2.size, destination);
#else
size_t size2 = operand2.getSize();
for(size_t n=0; n<size+size2-1; n++){
size_t n1 = n;
destination[n] = 0;
for(size_t k=0; k<size2; k++){
if(n1>=0 && n1<size)
destination[n] += data[n1]*operand2[k];
n1--;
}
}
#endif /* ARM_CORTEX */
}
void FloatArray::convolve(FloatArray operand2, FloatArray destination, int offset, size_t samples){
ASSERT(destination.size >= size + operand2.size -1, "Destination array too small"); //TODO: change this condition to the actual size being written(will be samples+ tail)
/// @note When built for ARM Cortex-M processor series, this method uses the optimized <a href="http://www.keil.com/pack/doc/CMSIS/General/html/index.html">CMSIS library</a>
#ifdef ARM_CORTEX
//TODO: I suspect a bug in arm_conv_partial_f32
//it seems that destination[n] is left unchanged for n<offset
//and the result is actually stored from destination[offset] onwards
//that is, in the same position where they would be if a full convolution was performed.
//This requires (destination.size >= size + operand2.size -1). Ideally you would want destination to be smaller
arm_conv_partial_f32(data, size, operand2.data, operand2.size, destination.getData(), offset, samples);
#else
//this implementations reproduces the (buggy?) behaviour of arm_conv_partial (see comment above and inline comments below)
/*
This implementation is just a copy/paste/edit from the overloaded method
*/
size_t size2=operand2.getSize();
for (size_t n=offset; n<offset+samples; n++){
size_t n1=n;
destination[n] =0; //this should be [n-offset]
for(size_t k=0; k<size2; k++){
if(n1>=0 && n1<size)
destination[n]+=data[n1]*operand2[k];//this should be destination[n-offset]
n1--;
}
}
#endif /* ARM_CORTEX */
}
void FloatArray::correlate(FloatArray operand2, FloatArray destination){
destination.setAll(0);
/// @note When built for ARM Cortex-M processor series, this method uses the optimized <a href="http://www.keil.com/pack/doc/CMSIS/General/html/index.html">CMSIS library</a>
correlateInitialized(operand2, destination);
}
void FloatArray::correlateInitialized(FloatArray operand2, FloatArray destination){
ASSERT(destination.size >= size+operand2.size-1, "Destination array too small"); //TODO: change CMSIS docs, which state a different size
/// @note When built for ARM Cortex-M processor series, this method uses the optimized <a href="http://www.keil.com/pack/doc/CMSIS/General/html/index.html">CMSIS library</a>
#ifdef ARM_CORTEX
arm_correlate_f32(data, size, operand2.data, operand2.size, destination);
#else
//correlation is the same as a convolution where one of the signals is flipped in time
//so we flip in time operand2
operand2.reverse();
//and convolve it with fa to obtain the correlation
convolve(operand2, destination);
//and we flip back operand2, so that the input is not modified
operand2.reverse();
#endif /* ARM_CORTEX */
}
void FloatArray::gainToDecibel(FloatArray destination){
ASSERT(destination.getSize()>=size, "Wrong array size");
for(size_t i=0; i<size; i++)
destination[i] = log10f(data[i])*20.0;
}
void FloatArray::decibelToGain(FloatArray destination){
ASSERT(destination.getSize()>=size, "Wrong array size");
for(size_t i=0; i<size; i++)
destination[i] = exp10f(data[i]*0.05);
}
void FloatArray::ramp(float from, float to){
float step = (to-from)/size;
for(size_t i=0; i<size; i++){
data[i] = from;
from += step;
}
}
void FloatArray::scale(float from, float to, FloatArray destination){
float step = (to-from)/size;
for(size_t i=0; i<size; i++){
data[i] *= from;
from += step;
}
}
/*
* Third-order static soft-clipping function.
* ref: T. Araya and A. Suyama, “Sound effector capable of
* imparting plural sound effects like distortion and other
* effects,” US Patent 5,570,424, 29 Oct. 1996.
*/
void FloatArray::softclip(FloatArray destination){
for(size_t i=0; i<size; i++){
float x = data[i];
destination[i] = clamp((3*x/2)*(1-x*x/3), -1.0f, 1.0f);
}
}
void FloatArray::tanh(FloatArray destination){
for(size_t i=0; i<size; i++)
destination[i] = tanhf(data[i]);
}
FloatArray FloatArray::create(int size){
FloatArray fa(new float[size], size);
fa.clear();
return fa;
}
void FloatArray::destroy(FloatArray array){
delete[] array.data;
}