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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
#include "ShortArray.h"
#include "basicmaths.h"
#include "message.h"
#include <string.h>
#include <limits.h>
#ifndef ARM_CORTEX
static int16_t saturateTo16(int64_t value){
if(value > SHRT_MAX)
value = SHRT_MAX;
else if(value < SHRT_MIN)
value = SHRT_MIN;
return value;
}
#endif
void ShortArray::getMin(int16_t* 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_q15(data, size, value, &idx);
*index = (int)idx;
#else
*value=data[0];
*index=0;
for(size_t n=1; n<size; n++){
int16_t currentValue=data[n];
if(currentValue<*value){
*value=currentValue;
*index=n;
}
}
#endif
}
int16_t ShortArray::getMinValue(){
int16_t 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 ShortArray::getMinIndex(){
int16_t 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 ShortArray::getMax(int16_t* 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_q15(data, size, value, &idx);
*index = (int)idx;
#else
*value=data[0];
*index=0;
for(size_t n=1; n<size; n++){
int16_t currentValue=data[n];
if(currentValue>*value){
*value=currentValue;
*index=n;
}
}
#endif
}
int16_t ShortArray::getMaxValue(){
int16_t 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 ShortArray::getMaxIndex(){
int16_t 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 ShortArray::rectify(ShortArray& 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_q15(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] = fabs(data[n]);
}
#endif
}
void ShortArray::rectify(){//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>
rectify(*this);
}
void ShortArray::reverse(ShortArray& 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 ShortArray::reverse(){//in place
for(size_t n=0; n<size/2; n++){
int16_t temp=data[n];
data[n]=data[size-n-1];
data[size-n-1]=temp;
}
}
void ShortArray::reciprocal(ShortArray& destination){
int16_t* data = getData();
for(size_t n=0; n<getSize(); n++)
destination[n] = (int16_t)(0.5 + 1.0f/data[n]);
}
void ShortArray::reciprocal(){//in place
reciprocal(*this);
}
int16_t ShortArray::getRms(){
int16_t result;
#ifdef ARM_CORTEX
/// @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>
arm_rms_q15 (data, size, &result);
#else
int64_t value = 0;
for(size_t n=0; n < size; ++n){
value += data[n] * data[n];
}
value = sqrtf(value / size);
result = saturateTo16(value);
#endif
return result;
}
int16_t ShortArray::getMean(){
int16_t 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_q15 (data, size, &result);
#else
int32_t value = 0;
for(size_t n=0; n < size; n++){
value += data[n];
}
value = value / size;
result = saturateTo16(value);
#endif
return result;
}
int64_t ShortArray::getPower(){
int64_t 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_q15 (data, size, &result);
#else
result=0;
int16_t *pSrc = data;
for(size_t n=0; n < size; n++){
int32_t value = (int32_t)pSrc[n]*pSrc[n];
result += saturateTo16(value >> 15);
}
#endif
return result;
}
int16_t ShortArray::getStandardDeviation(){
int16_t 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_q15 (data, size, &result);
#else
result=sqrtf(getVariance());
#endif
return result;
}
int16_t ShortArray::getVariance(){
int16_t 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_q15(data, size, &result);
#else
int16_t sumOfSquares=getPower();
int16_t sum=0;
for(size_t n=0; n<size; n++){
sum+=data[n];
}
result=(sumOfSquares - sum*sum/size) / (size - 1);
#endif
return result;
}
void ShortArray::clip(int16_t 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 ShortArray::clip(int16_t min, int16_t max){
for(size_t n=0; n<size; n++){
if(data[n]>max)
data[n]=max;
else if(data[n]<min)
data[n]=min;
}
}
ShortArray ShortArray::subArray(int offset, size_t length){
ASSERT(size >= offset+length, "Array too small");
return ShortArray(data+offset, length);
}
void ShortArray::setAll(int16_t 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_q15(value, data, size);
#else
for(size_t n=0; n<size; n++){
data[n]=value;
}
#endif /* ARM_CORTEX */
}
void ShortArray::add(ShortArray operand2, ShortArray 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_q15 (int16_t32_t *pSrcA, int16_t32_t *pSrcB, int16_t32_t *pDst, uint32_t blockSize)
*/
arm_add_q15(data, operand2.data, destination.data, size);
#else
for(size_t n=0; n<size; n++){
int32_t value = data[n] + operand2[n];
destination[n] = saturateTo16(value);
}
#endif /* ARM_CORTEX */
}
void ShortArray::add(ShortArray 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 ShortArray::add(int16_t scalar){
#ifdef ARM_CORTEX
int16_t doubleScalar[2] = {scalar, scalar};
q15_t * pSrcA = data;
q15_t * pSrcB = (int16_t*)&doubleScalar;
q15_t * pDst = data;
uint32_t blockSize = size;
uint32_t blkCnt; /* loop counter */
/* Run the below code for Cortex-M4 and Cortex-M3 */
q31_t inA1, inA2, inB1, inB2;
/*loop Unrolling */
blkCnt = blockSize >> 2u;
/* First part of the processing with loop unrolling. Compute 4 outputs at a time.
** a second loop below computes the remaining 1 to 3 samples. */
while(blkCnt > 0u)
{
/* C = A + B */
/* Add and then store the results in the destination buffer. */
inA1 = *__SIMD32(pSrcA)++;
inA2 = *__SIMD32(pSrcA)++;
inB1 = *__SIMD32(pSrcB); //HERE: do not increment pSrcB
inB2 = *__SIMD32(pSrcB); //HERE: do not increment pSrcB
*__SIMD32(pDst)++ = __QADD16(inA1, inB1);
*__SIMD32(pDst)++ = __QADD16(inA2, inB2);
/* Decrement the loop counter */
blkCnt--;
}
/* If the blockSize is not a multiple of 4, compute any remaining output samples here.
** No loop unrolling is used. */
blkCnt = blockSize % 0x4u;
while(blkCnt > 0u)
{
/* C = A + B */
/* Add and then store the results in the destination buffer. */
*pDst++ = (q15_t) __QADD16(*pSrcA++, *pSrcB); //HERE: do not increment pSrcB
/* Decrement the loop counter */
blkCnt--;
}
#else
for(size_t n=0; n < size; ++n){
int32_t value = data[n] + scalar;
data[n] = saturateTo16(value);
}
#endif
}
void ShortArray::subtract(ShortArray operand2, ShortArray destination){ //allows in-place
ASSERT(operand2.size == size && destination.size >= size, "Arrays size mismatch");
#ifdef ARM_CORTEX
/// @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>
/* despite not explicitely documented in the CMSIS documentation,
this has been tested to behave properly even when pSrcA==pDst
void arm_sub_q15 (int16_t32_t *pSrcA, int16_t32_t *pSrcB, int16_t32_t *pDst, uint32_t blockSize)
*/
arm_sub_q15(data, operand2.data, destination.data, size);
#else
for(size_t n=0; n < size; ++n){
int32_t value = data[n] - operand2[n];
destination[n] = saturateTo16(value);
}
#endif /* ARM_CORTEX */
}
void ShortArray::subtract(ShortArray 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 ShortArray::subtract(int16_t scalar)
{
#ifdef ARM_CORTEX
// this method is modelled on arm_sub_q15
// from <a href="http://www.keil.com/pack/doc/CMSIS/General/html/index.html">CMSIS library</a>
// just avoid incrementing the pSrcB pointer(see below)
int16_t *pSrcA = data;
int16_t scalarDouble[2] = {scalar, scalar};
int16_t *pSrcB = scalarDouble;
int16_t *pDst = data;
uint32_t blockSize = size;
uint32_t blkCnt; /* loop counter */
/* Run the below code for Cortex-M4 and Cortex-M3 */
q31_t inA1, inA2;
q31_t inB1, inB2;
/*loop Unrolling */
blkCnt = blockSize >> 2u;
/* First part of the processing with loop unrolling. Compute 4 outputs at a time.
** a second loop below computes the remaining 1 to 3 samples. */
while(blkCnt > 0u)
{
/* C = A - B */
/* Subtract and then store the results in the destination buffer two samples at a time. */
inA1 = *__SIMD32(pSrcA)++;
inA2 = *__SIMD32(pSrcA)++;
inB1 = *__SIMD32(pSrcB); //HERE: not incrementing pSrcB
inB2 = *__SIMD32(pSrcB); //HERE: not incrementing pSrcB
*__SIMD32(pDst)++ = __QSUB16(inA1, inB1);
*__SIMD32(pDst)++ = __QSUB16(inA2, inB2);
/* Decrement the loop counter */
blkCnt--;
}
/* If the blockSize is not a multiple of 4, compute any remaining output samples here.
** No loop unrolling is used. */
blkCnt = blockSize % 0x4u;
while(blkCnt > 0u)
{
/* C = A - B */
/* Subtract and then store the result in the destination buffer. */
*pDst++ = (q15_t) __QSUB16(*pSrcA++, *pSrcB); //HERE: not incrementing pSrcB
/* Decrement the loop counter */
blkCnt--;
}
#else
for(size_t n = 0; n < size; ++n){
int32_t value = data[n] - scalar;
data[n] = saturateTo16(value);
}
#endif
}
void ShortArray::multiply(ShortArray operand2, ShortArray 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_q15 (int16_t32_t *pSrcA, int16_t32_t *pSrcB, int16_t32_t *pDst, uint32_t blockSize)
*/
arm_mult_q15(data, operand2.data, destination, size);
#else
for(size_t n=0; n<size; n++){
int32_t value = data[n] * operand2[n];
destination[n] = saturateTo16(value >> 15);
}
#endif /* ARM_CORTEX */
}
void ShortArray::multiply(ShortArray 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 ShortArray::multiply(int16_t scalar){
// this method is modelled on arm_mult_q15
// from <a href="http://www.keil.com/pack/doc/CMSIS/General/html/index.html">CMSIS library</a>
// just avoid incrementing the pSrcB pointer(see below)
#ifdef ARM_CORTEX
arm_scale_q15(data, scalar, 0, data, size);
#else
for(size_t n = 0; n < size; ++n){
int32_t value = data[n] * scalar;
data[n] = saturateTo16(value >> 15);
}
#endif
}
void ShortArray::negate(ShortArray& destination){//allows 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>
#ifdef ARM_CORTEX
arm_negate_q15(data, destination.getData(), size);
#else
for(size_t n=0; n<size; n++){
destination[n]=-data[n];
}
#endif /* ARM_CORTEX */
}
void ShortArray::negate(){
/// @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>
negate(*this);
}
void ShortArray::noise(){
noise(-32768, 32767);
}
void ShortArray::noise(int16_t min, int16_t max){
uint16_t amplitude = abs((int32_t)max-(int32_t)min);
int16_t offset = min;
for(size_t n=0; n<size; n++){
data[n]=(rand()/((float)RAND_MAX)) * amplitude + offset;
}
}
void ShortArray::convolve(ShortArray operand2, ShortArray destination){
ASSERT(destination.size >= size + operand2.size -1, "Destination array too small");
/// @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_conv_q15(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 ShortArray::convolve(ShortArray operand2, ShortArray 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_q15
//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_q15(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 ShortArray::correlate(ShortArray operand2, ShortArray 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 ShortArray::correlateInitialized(ShortArray operand2, ShortArray 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_q15(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 ShortArray::shift(int shiftValue){
#ifdef ARM_CORTEX
arm_shift_q15(data, shiftValue, data, size);
#else
if(shiftValue >= 0){
for(size_t n=0; n<size; n++)
data[n] = data[n] << shiftValue;
}else{
shiftValue = -shiftValue;
for(size_t n=0; n<size; n++)
data[n] = data[n] >> shiftValue;
}
#endif
}
void ShortArray::setFloatValue(uint32_t n, float value){
data[n] = value * -SHRT_MIN;
}
float ShortArray::getFloatValue(uint32_t n){
return data[n] / (float)-SHRT_MIN;
}
void ShortArray::fromFloat(FloatArray source){
ASSERT(source.getSize() == size, "Size does not match");
#ifdef ARM_CORTEX
/// @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>
arm_float_to_q15((float*)source, data, size);
#else
for(size_t n = 0; n < size; ++n){
setFloatValue(n, source[n]);
}
#endif
}
void ShortArray::toFloat(FloatArray destination){
ASSERT(destination.getSize() == size, "Size does not match");
#ifdef ARM_CORTEX
/// @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>
arm_q15_to_float(data, (float*)destination, size);
#else
for(size_t n = 0; n < size; ++n){
destination[n] = getFloatValue(n);
}
#endif
}
ShortArray ShortArray::create(int size){
ShortArray fa(new int16_t[size], size);
fa.clear();
return fa;
}
void ShortArray::destroy(ShortArray array){
delete[] array.data;
}