-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsp.h
1542 lines (1339 loc) · 82.4 KB
/
dsp.h
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* File: dsp.h
* Author: Duy Ngo
*
* Created on November 1, 2019, 8:45 PM
*/
/*********************************************************************
* *
* Software License Agreement *
* *
* The software supplied herewith by Microchip Technology *
* Incorporated (the "Company") for its dsPIC controller *
* is intended and supplied to you, the Company's customer, *
* for use solely and exclusively on Microchip dsPIC *
* products. The software is owned by the Company and/or its *
* supplier, and is protected under applicable copyright laws. All *
* rights are reserved. Any use in violation of the foregoing *
* restrictions may subject the user to criminal sanctions under *
* applicable laws, as well as to civil liability for the breach of *
* the terms and conditions of this license. *
* *
* THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO *
* WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, *
* BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND *
* FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE *
* COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, *
* INCIDENTAL OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. *
* *
* (c) Copyright 2003 Microchip Technology, All rights reserved. *
*********************************************************************/
/****************************************************************************
*
* DSP.H
* Interface to the DSP Library for the dsPIC30F.
*
****************************************************************************/
#ifndef __DSP_LIB__ /* [ */
#define __DSP_LIB__
/***************************************************************************/
/* External headers. */
#include <stdlib.h> /* malloc, NULL */
#include <math.h> /* fabs, sin, cos, atan, sqrt */
/*...........................................................................*/
/* Local defines. */
#define FLOATING -1 /* using floating type */
#define FRACTIONAL 1 /* using fractional type */
#ifndef DATA_TYPE /* [ */
#define DATA_TYPE FRACTIONAL /* default */
#endif /* ] */
/* Some constants. */
#ifndef PI /* [ */
#define PI 3.1415926535897931159979634685441851615905761718750 /* double */
#endif /* ] */
#ifndef SIN_PI_Q /* [ */
#define SIN_PI_Q 0.7071067811865474617150084668537601828575134277343750
/* sin(PI/4), (double) */
#endif /* ] */
#ifndef INV_SQRT2 /* [ */
#define INV_SQRT2 SIN_PI_Q /* 1/sqrt(2), (double) */
/* 1/sqrt(2) = sin(PI/4) */
#endif /* ] */
#define BART_0 2.0 /* Bartlett 0th factor */
#define HANN_0 0.50 /* Hanning 0th factor */
#define HANN_1 -0.50 /* Hanning 1st factor */
#define HAMM_0 0.53836 /* Hamming 0th factor */
#define HAMM_1 -0.46164 /* Hamming 1st factor */
#define BLCK_0 0.42 /* Blackman 0th factor */
#define BLCK_1 -0.50 /* Blackman 1st factor */
#define BLCK_2 0.08 /* Blackman 2nd factor */
#define COEFFS_IN_DATA 0xFF00 /* page number used for */
/* filter coefficients */
/* when allocated in X */
/* data memory */
/*...........................................................................*/
/* Local types. */
/* Type definitions. */
#ifndef fractional /* [ */
#if DATA_TYPE==FLOATING /* [ */
typedef double fractional;
#else /* ] */
typedef int fractional;
#endif /* ] */
#ifndef fractcomplex /* [ */
typedef struct {
fractional real;
fractional imag;
} fractcomplex;
#endif /* ] fractcomplex */
#endif /* ] fractional */
/*...........................................................................*/
/****************************************************************************
*
* Preliminary remarks.
*
* None of the functions provided within this API allocate memory space.
*
****************************************************************************/
/*...........................................................................*/
/****************************************************************************
*
* Interface to generic function prototypes.
*
****************************************************************************/
/* Generic function prototypes. */
#define Q15(X) \
((X < 0.0) ? (int)(32768*(X) - 0.5) : (int)(32767*(X) + 0.5)) ;
#if DATA_TYPE==FLOATING /* [ */
#define Float2Fract(aVal) (aVal) /* Identity function */
#define Fract2Float(aVal) (aVal) /* Identity function */
#else /* ] */
extern fractional Float2Fract ( /* Converts float into fractional */
float aVal /* float value in range [-1, 1) */
);
extern float Fract2Float ( /* Converts fractional into float */
fractional aVal /* fract value in range {-1, 1-2^-15} */
);
#endif /* ] */
/*...........................................................................*/
/****************************************************************************
*
* Interface to vector operations.
*
* A vector is a collection of numerical values, the vector elements,
* allocated contiguosly in memory, with the first element at the
* lowest memory address. One word of memory (two bytes) is used to
* store the value of each element, and this quantity must be interpreted
* as a fractional value in Q.15 format.
*
* A pointer addressing the first element of the vector is used as
* a handle which provides access to each of the vector values. The
* one dimensional arrangement of a vector fits with the memory
* storage model, so that the n-th element of an N-element vector
* can be accessed from the vector's base address BA as:
*
* BA + (n-1)*2,
*
* Note that because of the byte addressing capabilities of the dsPIC30F,
* the addressing of vector elements uses an increment (or decrement) size
* of 2: INC2 (or DEC2) instruction.
*
* Unary and binary operations are prototyped in this interface. The
* operand vector in a unary operation is called the source vector.
* In a binary operation the first operand is referred to as source
* one vector, and the second as source two vector. Each operation
* applies some computation to one or several elements of the source
* vector(s). Some operations result in a scalar value (also to be
* interpreted as a Q.15 fractional number), others in a vector. When
* the result is also a vector this is referred to as the destination
* vector.
*
* Some operations resulting in a vector allow computation in place;
* i.e., the results of the operations are placed back on the source
* (or source one, if binary) vector. In this case, the destination
* vector is said to (physically) replace the source (one) vector.
* When an operation can be computed in place it is indicated as such
* in the comments provided with its prototype.
*
* For some binary operations, the two operands can be the same (physical)
* source vector: the operation is applied between the source vector
* and itself. If this type of computation is possible for a given
* operation, it is indicated as such in the comments provided with
* its prototype.
*
* Some operations can be self applicable and computed in place.
*
* The operations prototyped in this interface take as an argument the
* cardinality (number of elements) of the operand vector(s). It is
* assumed that this number is in the range {1, 2, ..., (2^14)-1}, and
* that in the case of binary operations both operand vectors have the
* same cardinality. Note that no boundary checking is performed by
* the operations, and that out of range cardinalities as well as the
* use of source vectors of different sizes in binary operations may
* produce unexpected results.
*
* Additional remarks.
*
* A) Operations which return a destination vector can be nested, so that
* for instance if:
*
* a = Op1 (b, c), with b = Op2 (d), and c = Op3 (e, f), then
*
* a = Op1 (Op2 (d), Op3 (e, f))
*
* B) The vector dot product and power operations could lead to saturation
* if the sum of products is greater than 1-2^(-15) or smaller than -1.
*
* C) All the functions have been designed to operate on vectors allocated
* in default RAM memory space (X-Data or Y-Data).
*
* D) The sum of sizes of the vector(s) involved in an operation must not
* exceed the available memory in the target device.
*
****************************************************************************/
/* Vector operation prototypes. */
extern fractional VectorMax ( /* Vector (last) maximum */
/* maxVal = max{srcV[elem]} */
/* if srcV[i] = srcV[j] = maxVal */
/* and i < j, then *(maxIndex) = j */
int numElems, /* number elements in srcV */
fractional* srcV, /* ptr to source vector */
int* maxIndex /* ptr to index for maximum value */
/* maxVal returned */
);
/*...........................................................................*/
extern fractional VectorMin ( /* Vector (last) minimum */
/* minVal = min{srcV[elem]} */
/* if srcV[i] = srcV[j] = minVal */
/* and i < j, then *(minIndex) = j */
int numElems, /* number elements in srcV */
fractional* srcV, /* ptr to source vector */
int* minIndex /* ptr to index for minimum value */
/* minVal returned */
);
/*...........................................................................*/
extern fractional* VectorCopy ( /* Copy elements from source vector */
/* to an (existing) dest vector */
/* (both srcV and dstV MUST have, */
/* at least, numElems elements). */
/* dstV[n] = srcV[n], 0 <= n < N */
/* (in place capable) */
int numElems, /* number elements to copy (N) */
fractional* dstV, /* ptr to destination vector */
fractional* srcV /* ptr to source vector */
/* dstV returned */
);
/*...........................................................................*/
extern fractional* VectorZeroPad ( /* Zero pad tail of a vector */
/* dstV[n] = srcV[n], 0 <= n < N */
/* dstV[n] = 0, N <= n < N+M */
/* (srcV MUST have length >= N) */
/* (dstV MUST have length N+M) */
/* (in place capable) */
int numElems, /* number elements in srcV (N) */
int numZeros, /* number zeros to append (M) */
fractional* dstV, /* ptr to destination vector */
fractional* srcV /* ptr to source vector */
/* dstV returned */
);
/*...........................................................................*/
extern fractional* VectorNegate ( /* Vector negate */
/* dstV[n] = (-1)*srcV[n]+0, 0<=n<N */
/* (in place capable) */
int numElems, /* number elements in srcV (N) */
fractional* dstV, /* ptr to destination vector */
fractional* srcV /* ptr to source vector */
/* dstV returned */
);
/*...........................................................................*/
extern fractional* VectorScale ( /* Vector scale */
/* dstV[elem] = sclVal*srcV[elem] */
/* (in place capable) */
int numElems, /* number elements in srcV (N) */
fractional* dstV, /* ptr to destination vector */
fractional* srcV, /* ptr to source vector */
fractional sclVal /* scale value (Q.15 fractional) */
/* dstV returned */
);
/*...........................................................................*/
extern fractional* VectorAdd ( /* Vector addition */
/* dstV[elem] = */
/* = srcV1[elem] + srcV2[elem] */
/* (in place capable) */
/* (with itself capable) */
int numElems, /* number elements in srcV[1,2] (N) */
fractional* dstV, /* ptr to destination vector */
fractional* srcV1, /* ptr to source vector one */
fractional* srcV2 /* ptr to source vector two */
/* dstV returned */
);
/*...........................................................................*/
extern fractional* VectorSubtract ( /* Vector subtraction */
/* dstV[elem] = */
/* = srcV1[elem] - srcV2[elem] */
/* (in place capable) */
/* (with itself capable) */
int numElems, /* number elements in srcV[1,2] (N) */
fractional* dstV, /* ptr to destination vector */
fractional* srcV1, /* ptr to source vector one */
fractional* srcV2 /* ptr to source vector two */
/* dstV returned */
);
/*...........................................................................*/
extern fractional* VectorMultiply ( /* Vector elem-to-elem multiply */
/* dstV[elem] = */
/* = srcV1[elem] * srcV2[elem] */
/* (in place capable) */
/* (with itself capable) */
int numElems, /* number elements in srcV[1,2] (N) */
fractional* dstV, /* ptr to destination vector */
fractional* srcV1, /* ptr to source vector one */
fractional* srcV2 /* ptr to source vector two */
/* dstV returned */
);
/*...........................................................................*/
extern fractional VectorDotProduct ( /* Vector dot product */
/* dotVal = */
/* = sum(srcV1[elem]*srcV2[elem]) */
/* (with itself capable) */
int numElems, /* number elements in srcV[1,2] (N) */
fractional* srcV1, /* ptr to source vector one */
fractional* srcV2 /* ptr to source vector two */
/* dot product value returned */
);
/*...........................................................................*/
extern fractional VectorPower ( /* Vector power */
/* powVal = */
/* = sum(srcV[elem]^2) */
int numElems, /* number elements in srcV (N) */
fractional* srcV /* ptr to source vector one */
/* power value returned */
);
/*...........................................................................*/
extern fractional* VectorConvolve ( /* Vector Convolution */
/* (with itself capable) */
int numElems1, /* number elements in srcV1 */
int numElems2, /* number elements in srcV2 */
/* numElems2 <= numElems1 */
fractional* dstV, /* ptr to destination vector */
/* with numElems1+numElems2-1 elems */
fractional* srcV1, /* ptr to source vector one */
fractional* srcV2 /* ptr to source vector two */
/* dstV returned */
);
/*...........................................................................*/
extern fractional* VectorCorrelate ( /* Vector Correlation */
/* (with itself capable) */
int numElems1, /* number elements in srcV1 */
int numElems2, /* number elements in srcV2 */
/* numElems2 <= numElems1 */
fractional* dstV, /* ptr to destination vector */
/* with numElems2+numElems1-1 elems */
fractional* srcV1, /* ptr to source vector one */
fractional* srcV2 /* ptr to source vector two */
/* dstV returned */
);
/*...........................................................................*/
/****************************************************************************
*
* Interface to windowing operations.
*
* A window is a vector with a specific value distribution within its
* domain ( 0 <= n < numElems). The particular value distribution depends
* on the window being generated.
*
* Given a vector, its value distribution may be modified by applying
* a window to it. In these cases, the window must have the same number
* of elements as the vector to modify.
*
* Before a vector can be windowed, the window must be created. Window
* initialization operations are provided which generate the values of
* the window elements. For higher numerical precision, these values are
* computed in floating point arithmetic, and the resulting quantities
* stored as Q.15 fractionals.
*
* To avoid excessive overhead when applying a window operation, and since
* given a window length the values of the window elements are fixed, a
* particular window could be generated once and used many times during
* the execution of the program. Thus, it is advisable to store the window
* returned by any of the initialization operations in a permanent (static)
* vector.
*
* Additional remarks.
*
* A) All the window initialization functions have been designed to generate
* window vectors allocated in default RAM memory space (X-Data and Y-Data).
*
* B) The windowing function is designed to operate on vectors allocated
* in default RAM memory space (X-Data and Y-Data).
*
****************************************************************************/
/* Windowing operation prototypes. */
extern fractional* BartlettInit ( /* Initialize a Bartlett window */
/* computed in floating point */
/* converted to fractionals */
int numElems, /* number elements in window */
fractional* window /* ptr to window */
/* window returned */
);
/*...........................................................................*/
extern fractional* BlackmanInit ( /* Initialize a Blackman window */
/* computed in floating point */
/* converted to fractionals */
int numElems, /* number elements in window */
fractional* window /* ptr to window */
/* window returned */
);
/*...........................................................................*/
extern fractional* HammingInit ( /* Initialize a Hamming window */
/* computed in floating point */
/* converted to fractionals */
int numElems, /* number elements in window */
fractional* window /* ptr to window */
/* window returned */
);
/*...........................................................................*/
extern fractional* HanningInit ( /* Initialize a Hanning window */
/* computed in floating point */
/* converted to fractionals */
int numElems, /* number elements in window */
fractional* window /* ptr to window */
/* window returned */
);
/*...........................................................................*/
extern fractional* KaiserInit ( /* Initialize a Kaiser window */
/* computed in floating point */
/* converted to fractionals */
int numElems, /* number elements in window */
fractional* window, /* ptr to window */
float betaVal /* shape parameter */
/* window returned */
);
/*...........................................................................*/
extern fractional* VectorWindow ( /* Apply window to vector */
/* dstV[n] = srcV[n] * window[n], */
/* 0 <= n < numElems */
/* (in place capable) */
int numElems, /* number elements in srcV and window */
fractional* dstV, /* ptr to destination vector */
fractional* srcV, /* ptr to source vector */
fractional* window /* ptr to window */
/* dstV returned */
);
/*...........................................................................*/
/****************************************************************************
*
* Interface to matrix operations.
*
* A matrix is a collection of numerical values, the matrix elements,
* allocated contiguosly in memory, with the first element at the
* lowest memory address. One word of memory (two bytes) is used to
* store the value of each element, and this quantity must be interpreted
* as a fractional value in Q.15 format.
*
* A pointer addressing the first element of the matrix is used as
* a handle which provides access to each of the matrix values. The
* two dimensional arrangement of a matrix is emulated in the memory
* storage area by placing its elements organized in row major order.
* Thus, the first value in memeory is the first element of the first
* row. It is followed by the rest of the elements of the first row.
* Then, the elements of the second row are stored, and so on, until
* all the rows are in memory. This way, the element at row r and
* column c of a matrix with R rows and C columns is located from
* the matrix base address BA at:
*
* BA + ((r-1)*C + c-1)*2,
*
* Note that because of the byte addressing capabilities of the dsPIC30F,
* the addressing of a matrix element uses an increment (or decrement)
* size of 2: INC2 (or DEC2) instruction.
*
* Unary and binary operations are prototyped in this interface. The
* operand matrix in a unary operation is called the source matrix.
* In a binary operation the first operand is referred to as source
* one matrix, and the second as source two matrix. Each operation
* applies some computation to one or several elements of the source
* matrix(ces). The operations result in a matrix, referred to as the
* destination matrix.
*
* Some operations resulting in a matrix allow computation in place;
* i.e., the results of the computation are placed back on the source
* (or source one, if binary) matrix. In this case, the destination
* matrix is said to (physically) replace the source (one) matrix.
* When an operation can be computed in place it is indicated as such
* in the comments provided with its prototype.
*
* For some binary operations, the two operands can be the same (physical)
* source matrix: the operation is applied between the source matrix
* and itself. If this type of computation is possible for a given
* operation, it is indicated as such in the comments provided with
* its prototype.
*
* Some operations can be self applicable and computed in place.
*
* The operations prototyped in this interface take the number of rows
* and the number of columns in the operand matrix(ces) as arguments.
* The number of rows times that of columns must be within the range
* {1, 2, ..., (2^14)-1}. In the case of binary operations the number
* of rows and columns of the operand matrices must obey the rules of
* matrix algebra; i.e., for matrix addition and subtraction the two
* matrices must have the same number of rows and columns, while for
* matrix multiplication, the number of columns of the first operand
* must be the same as the number of rows of the second operand. The
* source matrix to the inversion operation must be square (the same
* number of rows as of columns), and non-singular (its determinat
* different than zero).
*
* NOTE: no boundary checking is performed by the operations. So forth,
* out of range number of rows or columns as well as the use of source
* matrices not adhering to the previous rules may produce unexpected
* results.
*
* Additional remarks.
*
* A) Operations which return a destination matrix can be nested, so
* that for instance if:
*
* a = Op1 (b, c), with b = Op2 (d), and c = Op3 (e, f), then
*
* a = Op1 (Op2 (d), Op3 (e, f))
*
* B) The computation of the inverse of a matrix takes as input a floating
* point valued matrix, uses floating point arithmentic, and returns a
* floating point valued matrix.
*
* C) All the functions have been designed to operate on matrices allocated
* in default RAM memory space (X-Data and Y-Data).
*
* D) The sum of sizes of the matrix(ces) involved in an operation must not
* exceed the available memory in the target device.
*
****************************************************************************/
/* Matrix operation prototypes. */
extern fractional* MatrixScale ( /* Matrix scale */
/* dstM[i][j] = sclVal*srcM[i][j] */
/* (in place capable) */
int numRows, /* number rows in srcM (R) */
int numCols, /* number columns in srcM (C) */
fractional* dstM, /* ptr to destination matrix */
fractional* srcM, /* ptr to source matrix */
fractional sclVal /* scale value (Q.15 fractional) */
/* dstM returned */
);
/*...........................................................................*/
extern fractional* MatrixTranspose ( /* Matrix transpose */
/* dstM[i][j] = srcM[j][i] */
/* (in place capable) */
int numRows, /* number rows in srcM (R) */
int numCols, /* number columns in srcM (C) */
fractional* dstM, /* ptr to destination matrix */
fractional* srcM /* ptr to source matrix */
/* dstM returned */
);
/*...........................................................................*/
float* MatrixInvert ( /* Matrix inverse */
/* dstM = srcM^(-1) */
/* (in place capable) */
int numRowsCols, /* number rows and columns in matrix */
/* matrix MUST be square */
float* dstM, /* ptr to destination matrix */
float* srcM, /* ptr to source matrix */
float* pivotFlag, /* internal use; size numRowsCols */
int* swappedRows, /* internal use; size numRowsCols */
int* swappedCols /* internal use; size numRowsCols */
/* last three vectors required from */
/* user, so that function is not */
/* responsible for memory management */
/* dstM returned (or NULL on error */
/* if source matrix is singular) */
);
/*...........................................................................*/
extern fractional* MatrixAdd ( /* Matrix addition */
/* dstM[i][j] = */
/* srcM1[i][j] + srcM2[i][j] */
/* (in place capable) */
/* (with itself capable) */
int numRows, /* number rows in srcM[1,2] (R) */
int numCols, /* number columns in srcM[1,2] (C) */
fractional* dstM, /* ptr to destination matrix */
fractional* srcM1, /* ptr to source one matrix */
fractional* srcM2 /* ptr to source two matrix */
/* dstM returned */
);
/*...........................................................................*/
extern fractional* MatrixSubtract ( /* Matrix subtraction */
/* dstM[i][j] = */
/* srcM1[i][j] - srcM2[i][j] */
/* (in place capable) */
/* (with itself capable) */
int numRows, /* number rows in srcM[1,2] (R) */
int numCols, /* number columns in srcM[1,2] (C) */
fractional* dstM, /* ptr to destination matrix */
fractional* srcM1, /* ptr to source one matrix */
fractional* srcM2 /* ptr to source two matrix */
/* dstM returned */
);
/*...........................................................................*/
extern fractional* MatrixMultiply ( /* Matrix multiplication */
/* dstM[i][j] = */
/* sum_k(srcM1[i][k]*srcM2[k][j]) */
/* i in {0, 1, ..., numRows1-1} */
/* j in {0, 1, ..., numCols2-1} */
/* k in {0, 1, ..., numCols1Rows2-1} */
/* (in place capable, only square) */
/* (with itself capable, only square) */
int numRows1, /* number rows in srcM1 */
int numCols1Rows2, /* number columns in srcM1, same as */
/* number rows in srcM2 */
int numCols2, /* number columns srcM2 */
fractional* dstM, /* ptr to destination matrix */
fractional* srcM1, /* ptr to source one matrix */
fractional* srcM2 /* ptr to source two matrix */
/* dstM returned */
);
/*...........................................................................*/
/****************************************************************************
*
* Interface to FIR filter operations.
*
* Filtering a data sequence x[n] with an FIR filter of impulse response
* b[m] (0<= m < M) is equivalent to solving the difference equation:
*
* y[n] = sum_{m = 0:M-1}(b[m]*x[n-m])
*
* In this operation it is important to know and manage the past history
* of the data sequence (x[m], -M+1 <= m < 0) which represent the initial
* condition of the filtering operation. Also, when applying an FIR filter
* to contiguous sections of a data sequence it is necessary to remember
* the final state of the previous filtering operation (x[m], N-M+1 <= m < N-1),
* and take the state into consideration for the calculations of the next
* filtering stage. Accounting for the past history and current state is
* required to perform a correct (glitch-free) filtering operation.
*
* The management of the past history and current state of the filtering
* operation is commonly implemented via an additional sequence, referred
* to as the delay. Prior to a filtering operation the delay describes the
* past history of the data sequence. After performing the FIR filtering
* the delay contains a set of the most recently filtered data samples.
* (For correct operation, it is advisable to initialize the delay values
* to zero by calling the corresponding init function.)
*
* Even though FIR filtering is a difference equation, several properties
* of FIR filters allow for computation of the operation in more effective
* ways than that of a straight difference equation. Consequently, a set
* of such implementations are hereby provided.
*
* Note that of the four main sequences involved in FIR filtering, input
* data, output data, filter coefficients and delay, the last two are
* usually thought of as making up the filter structure. All the functions
* that follow use the same FIR filter structure to manage the filtering
* operation.
*
* In the current design, the input data sequence is referred to as the
* sequence of source samples, while the resulting filtered sequence
* contains the destination samples. The filters are characterized by
* the number of coefficients or taps, and the delay properties. All of
* these data sets are stored in memory as vectors with their elements
* representing Q.15 fractional quantities. Also, the input and output
* sequences to the filtering operation ought to be allocated in default
* RAM memory (X-Data or Y-Data). The coefficients may be allocated either
* in X-Data or program memory, while the delays must be allocated solely
* in Y-Data memory.
*
****************************************************************************/
/* FIR filter operation prototypes. */
typedef struct {
int numCoeffs; /* number of coeffs in filter (M) */
/* (same as filter order if lattice) */
/* (h[m], 0 <= m < M) */
/* (if lattice, k[m], 0 <= m < M) */
fractional* coeffsBase; /* base address of filter coeffs */
/* either in X data or program memory */
/* if in X data memory, it points at */
/* h[0] (if lattice, k[0]) */
/* if in program memory, base is the */
/* offset from program page boundary */
/* to address where coeffs located */
/* (inline assembly psvoffset ()) */
/* when indicated, it must be at a */
/* 'zero' power of 2 address (since */
/* in those cases it is implemented */
/* as an increasing circular buffer) */
fractional* coeffsEnd; /* end address of filter coeffs */
/* must be an odd number */
/* if in data memory, points at */
/* last byte of coefficients buffer */
/* if in program memory, end is the */
/* offset from program page boundary */
/* to address of last byte of coeffs */
int coeffsPage; /* if in X data memory, set to */
/* defined value COEFFS_IN_DATA */
/* if in program memory, page number */
/* where coeffs are located */
/* (inline assembly psvpage ()) */
fractional* delayBase; /* base address of delay buffer, */
/* only in Y data */
/* points at d[0] of d[m], 0 <= m < M */
/* when indicated, it must be at a */
/* 'zero' power of 2 address (since */
/* in those cases it is implemented */
/* as an increasing circular buffer) */
fractional* delayEnd; /* end address of delay buffer, */
/* points at last byte of buffer */
fractional* delay; /* current value of delay pointer */
} FIRStruct;
extern void FIRStructInit ( /* Initialize FIR filter structure */
FIRStruct* FIRFilter, /* FIR filter structure */
int numCoeffs, /* number of coeffs in filter (M) */
/* (same as filter order if lattice) */
/* (h[m], 0 <= m < M) */
/* (if lattice, k[m], 0 <= m < M) */
fractional* coeffsBase, /* base address of filter coeffs */
/* either in X data or program memory */
/* if in X data memory, it points at */
/* h[0] (if lattice, k[0]) */
/* if in program memory, base is the */
/* offset from program page boundary */
/* to address where coeffs located */
/* (inline assembly psvoffset ()) */
/* when indicated, it must be at a */
/* 'zero' power of 2 address (since */
/* in those cases it is implemented */
/* as an increasing circular buffer) */
int coeffsPage, /* if in X data memory, set to */
/* defined value COEFFS_IN_DATA */
/* if in program memory, page number */
/* where coeffs are located */
/* (inline assembly psvpage ()) */
fractional* delayBase /* base address of delay buffer, */
/* only in Y data */
/* points at d[0] of d[m], 0 <= m < M */
/* when indicated, it must be at a */
/* 'zero' power of 2 address (since */
/* in those cases it is implemented */
/* as an increasing circular buffer) */
/* upon return, FIR filter structure */
/* is initialized (delay = delayBase) */
);
extern void FIRDelayInit ( /* Zero out dealy in filter structure */
FIRStruct* filter /* filter structure */
/* NOTE: FIR interpolator's delay is */
/* initialized by FIRInterpDelayInit */
);
extern void FIRInterpDelayInit ( /* Zero out dealy in filter structure */
/* for FIR interpolator */
FIRStruct* filter, /* filter structure */
int rate /* rate of interpolation (1 to) R */
);
extern fractional* FIR ( /* FIR filtering */
int numSamps, /* number of input samples (N) */
fractional* dstSamps, /* ptr to output samples */
/* (y[n], 0 <= n < N) */
fractional* srcSamps, /* ptr to input samples */
/* (x[n], 0 <= n < N) */
FIRStruct* filter /* filter structure: */
/* number of coefficients in filter */
/* same as number of delay elements */
/* (h[m], 0 <= m < M, an increasing */
/* circular buffer) */
/* (d[m], 0 <= m < M, an increasing */
/* circular buffer) */
/* returns dstSamps */
);
extern fractional* FIRDecimate ( /* Decimation by R:1 rate */
int numSamps, /* number of _output_ samples (N) */
/* N = R*p (p integer) */
fractional* dstSamps, /* ptr to output samples */
/* (y[n], 0 <= n < N) */
fractional* srcSamps, /* ptr to input samples */
/* (x[n], 0 <= n < N*R) */
FIRStruct* filter, /* filter structure: */
/* number of coefficients in filter */
/* same as number of delay elements */
/* M = R*q (q integer) */
/* (h[m], 0 <= m < M) */
/* (d[m], 0 <= m < M) */
int rate /* rate of decimation R (to 1) */
/* returns dstSamps */
);
extern fractional* FIRInterpolate ( /* Interpolation by 1:R rate */
int numSamps, /* number of input samples (N) */
/* N = R*p (p integer) */
fractional* dstSamps, /* ptr to output samples */
/* (y[n], 0 <= n < N*R) */
fractional* srcSamps, /* ptr to input samples */
/* (x[n], 0 <= n < N) */
FIRStruct* filter, /* filter structure: */
/* number of coefficients in filter */
/* M = R*q (q integer) */
/* (h[m], 0 <= m < M) */
/* (d[m], 0 <= m < M/R) */
int rate /* rate of interpolation (1 to) R */
/* returns dstSamps */
);
extern fractional* FIRLattice ( /* FIR Lattice filtering */
int numSamps, /* number of input samples (N) */
fractional* dstSamps, /* ptr to output samples */
/* (y[n], 0 <= n < N) */
fractional* srcSamps, /* ptr to input samples */
/* (x[n], 0 <= n < N) */
FIRStruct* filter /* filter structure: */
/* number of coefficients in filter */
/* (also known as kappa values) */
/* same as number of delay elements */
/* same as filter order */
/* (k[m], 0 <= m < M) */
/* (d[m], 0 <= m < M) */
/* returns dstSamps */
);
extern fractional* FIRLMS ( /* FIR Least Mean Square filtering */
/* not normalized implementation */
/* y[n] = sum_{m=0:M-1){h[n]*x[n-m]} */
/* 0 <= n < N */
/* h_m[n] = h_m[n-1] + mu*e[n]*x[n-m] */
/* 0 <= n < N, 0 <= m < M */
/* with e[n] = r[n] - y[n] */
/* NOTE: to avoid saturation while */
/* computing error, -1 <= e[n] < 1 */
/* for 0 <= n < N */
int numSamps, /* number of input samples (N) */
fractional* dstSamps, /* ptr to output samples */
/* (y[n], 0 <= n < N) */
fractional* srcSamps, /* ptr to input samples */
/* (x[n], 0 <= n < N) */
FIRStruct* filter, /* filter structure: */
/* number of coefficients in filter */
/* same as number of delay elements */
/* (h[m], 0 <= m < M, an increasing */
/* circular buffer) */
/* (d[m], 0 <= m < M, an increasing */
/* circular buffer) */
fractional* refSamps, /* ptr to reference samples */
/* (r[n], 0 <= n < N) */
fractional muVal /* mu value for correction */
/* returns dstSamps */
);
extern fractional* FIRLMSNorm ( /* FIR Least Mean Square filtering*/
/* Normalized implementation */
/* y[n] = sum_{m=0:M-1}(h[n]*x[n-m]) */
/* 0 <= n < N */
/* h_m[n] = h_m[n-1] + nu*e[n]*x[n-m] */
/* 0 <= n < N, 0 <= m < M */
/* with e[n] = r[n] - y[n], and */
/* nu[n] = mu/(mu+E[n]) */
/* E[n]=E[n-1]+(x[n])^2-(x[n-M+1])^2 */
/* is an estimate of input energy */
/* NOTE: to avoid saturation while */
/* computing error, -1 <= e[n] < 1 */
/* for 0 <= n < N */
/* NOTE: to avoid saturation while */
/* computing the estimate, the input */
/* signal values should be bound */
/* so that sum_{m=0:-M+2}(x[n+m]^2}<1 */
int numSamps, /* number of input samples (N) */
fractional* dstSamps, /* ptr to output samples */
/* (y[n], 0 <= n < N) */
fractional* srcSamps, /* ptr to input samples */
/* (x[n], 0 <= n < N) */
FIRStruct* filter, /* filter structure: */
/* number of coefficients in filter */
/* same as number of delay elements */
/* (h[m], 0 <= m < M, an increasing */
/* circular buffer) */
/* (d[m], 0 <= m < M, an increasing */
/* circular buffer) */
fractional* refSamps, /* ptr to reference samples */
/* (r[n], 0 <= n < N) */
fractional muVal, /* mu value for correction */
fractional* energyEstimate /* energy estimate for input samples */
/* E[-1] = (x[-1])^2+...+(x[-M+1])^2 */
/* on start up... (zero first time) */
/* E[N-1]=(x[N-1])^2+...+(x[N-M+1])^2 */
/* upon return */
/* returns dstSamps */
);
/* ....................................................................... */
/****************************************************************************
*
* Interface to IIR filter operations.
*
* Filtering a data sequence x[n] with an IIR filter of impulse response
* {b[m] (0<= m < M), a[p] (0 <= p < P)} is equivalent to solving the