libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmVectOpsRIHdr.h 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //| Copyright: (C) 2009-2020 Kevin Larke <contact AT larke DOT org>
  2. //| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
  3. //( { label:"Matrix ops" desc:"Common 2D matrix operations and accessors." kw:[vop] }
  4. // 2D matrix accessors
  5. VECT_OP_TYPE* VECT_OP_FUNC(Col)( VECT_OP_TYPE* m, unsigned ci, unsigned rn, unsigned cn );
  6. VECT_OP_TYPE* VECT_OP_FUNC(Row)( VECT_OP_TYPE* m, unsigned ri, unsigned rn, unsigned cn );
  7. VECT_OP_TYPE* VECT_OP_FUNC(ElePtr)( VECT_OP_TYPE* m, unsigned ri, unsigned ci, unsigned rn, unsigned cn );
  8. VECT_OP_TYPE VECT_OP_FUNC(Ele)( VECT_OP_TYPE* m, unsigned ri, unsigned ci, unsigned rn, unsigned cn );
  9. void VECT_OP_FUNC(Set)( VECT_OP_TYPE* m, unsigned ri, unsigned ci, unsigned rn, unsigned cn, VECT_OP_TYPE v );
  10. const VECT_OP_TYPE* VECT_OP_FUNC(CCol)( const VECT_OP_TYPE* m, unsigned ci, unsigned rn, unsigned cn );
  11. const VECT_OP_TYPE* VECT_OP_FUNC(CRow)( const VECT_OP_TYPE* m, unsigned ri, unsigned rn, unsigned cn );
  12. const VECT_OP_TYPE* VECT_OP_FUNC(CElePtr)( const VECT_OP_TYPE* m, unsigned ri, unsigned ci, unsigned rn, unsigned cn );
  13. VECT_OP_TYPE VECT_OP_FUNC(CEle)( const VECT_OP_TYPE* m, unsigned ri, unsigned ci, unsigned rn, unsigned cn );
  14. // Set only the diagonal of a square mtx to sbp.
  15. VECT_OP_TYPE* VECT_OP_FUNC(Diag)( VECT_OP_TYPE* dbp, unsigned n, const VECT_OP_TYPE* sbp );
  16. // Set the diagonal of a square mtx to db to sbp and set all other values to zero.
  17. VECT_OP_TYPE* VECT_OP_FUNC(DiagZ)( VECT_OP_TYPE* dbp, unsigned n, const VECT_OP_TYPE* sbp );
  18. // Create an identity matrix (only sets 1's not zeros).
  19. VECT_OP_TYPE* VECT_OP_FUNC(Identity)( VECT_OP_TYPE* dbp, unsigned rn, unsigned cn );
  20. // Zero the matrix and then fill it as an identity matrix.
  21. VECT_OP_TYPE* VECT_OP_FUNC(IdentityZ)( VECT_OP_TYPE* dbp, unsigned rn, unsigned cn );
  22. // Transpose the matrix sbp[srn,scn] into dbp[scn,srn]
  23. VECT_OP_TYPE* VECT_OP_FUNC(Transpose)( VECT_OP_TYPE* dbp, const VECT_OP_TYPE* sbp, unsigned srn, unsigned scn );
  24. //======================================================================================================================
  25. //)
  26. //( { label:"Fill,move,copy" desc:"Basic data movement and initialization." kw:[vop] }
  27. // Fill a vector with a value. If value is 0 then the function is accellerated via memset().
  28. VECT_OP_TYPE* VECT_OP_FUNC(Fill)( VECT_OP_TYPE* dbp, unsigned dn, VECT_OP_TYPE value );
  29. // Fill a vector with zeros
  30. VECT_OP_TYPE* VECT_OP_FUNC(Zero)( VECT_OP_TYPE* dbp, unsigned dn );
  31. // Analogous to memmove()
  32. VECT_OP_TYPE* VECT_OP_FUNC(Move)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sp );
  33. // Fill the vector from various sources
  34. VECT_OP_TYPE* VECT_OP_FUNC(Copy)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sp );
  35. VECT_OP_TYPE* VECT_OP_FUNC(CopyN)( VECT_OP_TYPE* dbp, unsigned dn, unsigned d_stride, const VECT_OP_TYPE* sp, unsigned s_stride );
  36. VECT_OP_TYPE* VECT_OP_FUNC(CopyU)( VECT_OP_TYPE* dbp, unsigned dn, const unsigned* sp );
  37. VECT_OP_TYPE* VECT_OP_FUNC(CopyI)( VECT_OP_TYPE* dbp, unsigned dn, const int* sp );
  38. VECT_OP_TYPE* VECT_OP_FUNC(CopyF)( VECT_OP_TYPE* dbp, unsigned dn, const float* sp );
  39. VECT_OP_TYPE* VECT_OP_FUNC(CopyD)( VECT_OP_TYPE* dbp, unsigned dn, const double* sp );
  40. VECT_OP_TYPE* VECT_OP_FUNC(CopyS)( VECT_OP_TYPE* dbp, unsigned dn, const cmSample_t* sp );
  41. VECT_OP_TYPE* VECT_OP_FUNC(CopyR)( VECT_OP_TYPE* dbp, unsigned dn, const cmReal_t* sp );
  42. // Fill the the destination vector from a source vector where the source vector contains
  43. // srcStride interleaved elements to be ignored.
  44. VECT_OP_TYPE* VECT_OP_FUNC(CopyStride)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sp, unsigned srcStride );
  45. //======================================================================================================================
  46. //)
  47. //( { label:"Shrink/Expand/Replace" desc:"Change the size of a vector." kw:[vop] }
  48. // Shrink the elemetns of dbp[dn] by copying all elements past t+tn to t.
  49. // This operation results in overwriting the elements in the range t[tn].
  50. // t[tn] must be entirely inside dbp[dn].
  51. VECT_OP_TYPE* VECT_OP_FUNC(Shrink)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* t, unsigned tn );
  52. // Expand dbp[[dn] by shifting all elements past t to t+tn.
  53. // This produces a set of empty elements in t[tn].
  54. // t must be inside or at the end of dbp[dn].
  55. // This results in a reallocation of dbp[]. Be sure to call cmMemFree(dbp)
  56. // to release the returned pointer.
  57. VECT_OP_TYPE* VECT_OP_FUNC(Expand)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* t, unsigned tn );
  58. // Replace the elements t[tn] with the elements in u[un].
  59. // t must be inside or at the end of dbp[dn].
  60. // This operation may result in a reallocation of dbp[]. Be sure to call cmMemFree(dbp)
  61. // to release the returned pointer.
  62. // IF dbp==NULL and tn==0 then the dbp[un] is allocated and returned
  63. // with the contents of u[un].
  64. VECT_OP_TYPE* VECT_OP_FUNC(Replace)(VECT_OP_TYPE* dbp, unsigned* dn, const VECT_OP_TYPE* t, unsigned tn, const VECT_OP_TYPE* u, unsigned un );
  65. //======================================================================================================================
  66. //)
  67. //( { label:"Rotate/Shift/Flip/Sequence" desc:"Modify/generate the vector sequence." kw:[vop] }
  68. // Assuming a row vector positive shiftCnt rotates right, negative shiftCnt rotates left.
  69. VECT_OP_TYPE* VECT_OP_FUNC(Rotate)( VECT_OP_TYPE* dbp, unsigned dn, int shiftCnt );
  70. // Equivalent to Matlab circshift().
  71. VECT_OP_TYPE* VECT_OP_FUNC(RotateM)( VECT_OP_TYPE* dbp, unsigned drn, unsigned dcn, const VECT_OP_TYPE* sbp, int rShift, int cShift );
  72. // Assuming a row vector positive shiftCnt shifts right, negative shiftCnt shifts left.
  73. VECT_OP_TYPE* VECT_OP_FUNC(Shift)( VECT_OP_TYPE* dbp, unsigned dn, int shiftCnt, VECT_OP_TYPE fill );
  74. // Reverse the contents of the vector.
  75. VECT_OP_TYPE* VECT_OP_FUNC(Flip)( VECT_OP_TYPE* dbp, unsigned dn);
  76. // Fill dbp[] with a sequence of values. Returns next value.
  77. VECT_OP_TYPE VECT_OP_FUNC(Seq)( VECT_OP_TYPE* dbp, unsigned dn, VECT_OP_TYPE beg, VECT_OP_TYPE incr );
  78. //======================================================================================================================
  79. //)
  80. //( { label:"Arithmetic" desc:"Add,Sub,Mult,Divde" kw:[vop] }
  81. VECT_OP_TYPE* VECT_OP_FUNC(SubVS)( VECT_OP_TYPE* dp, unsigned dn, VECT_OP_TYPE v );
  82. VECT_OP_TYPE* VECT_OP_FUNC(SubVV)( VECT_OP_TYPE* dp, unsigned dn, const VECT_OP_TYPE* v );
  83. VECT_OP_TYPE* VECT_OP_FUNC(SubVVS)( VECT_OP_TYPE* dp, unsigned dn, const VECT_OP_TYPE* v, VECT_OP_TYPE s );
  84. VECT_OP_TYPE* VECT_OP_FUNC(SubVVNN)(VECT_OP_TYPE* dp, unsigned dn, unsigned dnn, const VECT_OP_TYPE* sp, unsigned snn );
  85. VECT_OP_TYPE* VECT_OP_FUNC(SubVVV)( VECT_OP_TYPE* dp, unsigned dn, const VECT_OP_TYPE* sb0p, const VECT_OP_TYPE* sb1p );
  86. VECT_OP_TYPE* VECT_OP_FUNC(SubVSV)( VECT_OP_TYPE* dp, unsigned dn, const VECT_OP_TYPE s0, const VECT_OP_TYPE* sb1p );
  87. VECT_OP_TYPE* VECT_OP_FUNC(AddVS)( VECT_OP_TYPE* dp, unsigned dn, VECT_OP_TYPE v );
  88. VECT_OP_TYPE* VECT_OP_FUNC(AddVV)( VECT_OP_TYPE* dp, unsigned dn, const VECT_OP_TYPE* v );
  89. VECT_OP_TYPE* VECT_OP_FUNC(AddVVS)( VECT_OP_TYPE* dp, unsigned dn, const VECT_OP_TYPE* v, VECT_OP_TYPE s );
  90. VECT_OP_TYPE* VECT_OP_FUNC(AddVVNN)(VECT_OP_TYPE* dp, unsigned dn, unsigned dnn, const VECT_OP_TYPE* sp, unsigned snn );
  91. VECT_OP_TYPE* VECT_OP_FUNC(AddVVV)( VECT_OP_TYPE* dp, unsigned dn, const VECT_OP_TYPE* sb0p, const VECT_OP_TYPE* sb1p );
  92. VECT_OP_TYPE* VECT_OP_FUNC(MultVVV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, const VECT_OP_TYPE* sb1p );
  93. VECT_OP_TYPE* VECT_OP_FUNC(MultVV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sbp );
  94. VECT_OP_TYPE* VECT_OP_FUNC(MultVVNN)(VECT_OP_TYPE* dp, unsigned dn, unsigned dnn, const VECT_OP_TYPE* sp, unsigned snn );
  95. VECT_OP_TYPE* VECT_OP_FUNC(MultVS)( VECT_OP_TYPE* dbp, unsigned dn, VECT_OP_TYPE s );
  96. VECT_OP_TYPE* VECT_OP_FUNC(MultVVS)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sbp, VECT_OP_TYPE s );
  97. VECT_OP_TYPE* VECT_OP_FUNC(MultVaVS)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sbp, VECT_OP_TYPE s );
  98. VECT_OP_TYPE* VECT_OP_FUNC(MultSumVVS)(VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sbp, VECT_OP_TYPE s );
  99. VECT_OP_TYPE* VECT_OP_FUNC(DivVVS)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, VECT_OP_TYPE sb1 );
  100. VECT_OP_TYPE* VECT_OP_FUNC(DivVVV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, const VECT_OP_TYPE* sb1p );
  101. VECT_OP_TYPE* VECT_OP_FUNC(DivVV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p );
  102. VECT_OP_TYPE* VECT_OP_FUNC(DivVVNN)(VECT_OP_TYPE* dp, unsigned dn, unsigned dnn, const VECT_OP_TYPE* sp, unsigned snn );
  103. VECT_OP_TYPE* VECT_OP_FUNC(DivVS)( VECT_OP_TYPE* dbp, unsigned dn, VECT_OP_TYPE s );
  104. VECT_OP_TYPE* VECT_OP_FUNC(DivVSV)( VECT_OP_TYPE* dp, unsigned dn, const VECT_OP_TYPE s0, const VECT_OP_TYPE* sb1p );
  105. // Set dest to 0 if denominator is 0.
  106. VECT_OP_TYPE* VECT_OP_FUNC(DivVVVZ)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, const VECT_OP_TYPE* sb1p );
  107. VECT_OP_TYPE* VECT_OP_FUNC(DivVVZ)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p );
  108. // Divide columns of dp[:,i] by each value in the source vector sp[i].
  109. VECT_OP_TYPE* VECT_OP_FUNC(DivMS)( VECT_OP_TYPE* dp, unsigned drn, unsigned dcn, const VECT_OP_TYPE* sp );
  110. //======================================================================================================================
  111. //)
  112. //( { label:"Sum vectors" desc:"Operations which take sum vector elements." kw:[vop] }
  113. VECT_OP_TYPE VECT_OP_FUNC(Sum)( const VECT_OP_TYPE* sp, unsigned sn );
  114. VECT_OP_TYPE VECT_OP_FUNC(SumN)( const VECT_OP_TYPE* sp, unsigned sn, unsigned stride );
  115. // Sum the columns of sp[srn,scn] into dp[scn].
  116. // dp[] is zeroed prior to computing the sum.
  117. VECT_OP_TYPE* VECT_OP_FUNC(SumM)( const VECT_OP_TYPE* sp, unsigned srn, unsigned scn, VECT_OP_TYPE* dp );
  118. // Sum the rows of sp[srn,scn] into dp[srn]
  119. // dp[] is zeroed prior to computing the sum.
  120. VECT_OP_TYPE* VECT_OP_FUNC(SumMN)( const VECT_OP_TYPE* sp, unsigned srn, unsigned scn, VECT_OP_TYPE* dp );
  121. //======================================================================================================================
  122. //)
  123. //( { label:"Min/max/median/mode" desc:"Simple descriptive statistics." kw:[vop] }
  124. VECT_OP_TYPE VECT_OP_FUNC(Median)( const VECT_OP_TYPE* sp, unsigned sn );
  125. unsigned VECT_OP_FUNC(MinIndex)( const VECT_OP_TYPE* sp, unsigned sn, unsigned stride );
  126. unsigned VECT_OP_FUNC(MaxIndex)( const VECT_OP_TYPE* sp, unsigned sn, unsigned stride );
  127. VECT_OP_TYPE VECT_OP_FUNC(Min)( const VECT_OP_TYPE* sp, unsigned sn, unsigned stride );
  128. VECT_OP_TYPE VECT_OP_FUNC(Max)( const VECT_OP_TYPE* sp, unsigned sn, unsigned stride );
  129. VECT_OP_TYPE* VECT_OP_FUNC(MinVV)( VECT_OP_TYPE* dp, unsigned dn, const VECT_OP_TYPE* sp );
  130. VECT_OP_TYPE* VECT_OP_FUNC(MaxVV)( VECT_OP_TYPE* dp, unsigned dn, const VECT_OP_TYPE* sp );
  131. // Return index of max/min value into dp[scn] of each column of sp[srn,scn]
  132. unsigned* VECT_OP_FUNC(MinIndexM)( unsigned* dp, const VECT_OP_TYPE* sp, unsigned srn, unsigned scn );
  133. unsigned* VECT_OP_FUNC(MaxIndexM)( unsigned* dp, const VECT_OP_TYPE* sp, unsigned srn, unsigned scn );
  134. // Return the most frequently occuring element in sp.
  135. VECT_OP_TYPE VECT_OP_FUNC(Mode)( const VECT_OP_TYPE* sp, unsigned sn );
  136. //======================================================================================================================
  137. //)
  138. //( { label:"Compare/Find" desc:"Compare, find, replace and count elements in a vector." kw:[vop] }
  139. // Return true if s0p[sn] is equal to s1p[sn]
  140. bool VECT_OP_FUNC(IsEqual)( const VECT_OP_TYPE* s0p, const VECT_OP_TYPE* s1p, unsigned sn );
  141. // Return true if all elements of s0p[sn] are within 'eps' of s1p[sn].
  142. // This function is based on cmMath.h:cmIsCloseX()
  143. bool VECT_OP_FUNC(IsClose)( const VECT_OP_TYPE* s0p, const VECT_OP_TYPE* s1p, unsigned sn, double eps );
  144. // Replace all values <= lteKeyVal with replaceVal. sp==dp is legal.
  145. VECT_OP_TYPE* VECT_OP_FUNC(ReplaceLte)( VECT_OP_TYPE* dp, unsigned dn, const VECT_OP_TYPE* sp, VECT_OP_TYPE lteKeyVal, VECT_OP_TYPE replaceVal );
  146. // Return the index of 'key' in sp[sn] or cmInvalidIdx if 'key' does not exist.
  147. unsigned VECT_OP_FUNC(Find)( const VECT_OP_TYPE* sp, unsigned sn, VECT_OP_TYPE key );
  148. // Count the number of times 'key' occurs in sp[sn].
  149. unsigned VECT_OP_FUNC(Count)(const VECT_OP_TYPE* sp, unsigned sn, VECT_OP_TYPE key );
  150. //======================================================================================================================
  151. //)
  152. //( { label:"Absolute value" desc:"Absolute value and signal rectification." kw:[vop] }
  153. VECT_OP_TYPE* VECT_OP_FUNC(Abs)( VECT_OP_TYPE* dbp, unsigned dn );
  154. // Half wave rectify the source vector.
  155. // dbp[] = sbp<0 .* sbp
  156. // Overlapping the source and dest is allowable as long as dbp <= sbp.
  157. VECT_OP_TYPE* VECT_OP_FUNC(HalfWaveRectify)(VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sp );
  158. //======================================================================================================================
  159. //)
  160. //( { label:"Filter" desc:"Apply filtering to a vector taking into account vector begin/end conditions." kw:[vop] }
  161. // Apply a median or other filter of order wndN to xV[xN] and store the result in yV[xN].
  162. // When the window goes off either side of the vector the window is shortened.
  163. // This algorithm produces the same result as the fn_thresh function in MATLAB fv codebase.
  164. void VECT_OP_FUNC(FnThresh)( const VECT_OP_TYPE* xV, unsigned xN, unsigned wndN, VECT_OP_TYPE* yV, unsigned yStride, VECT_OP_TYPE (*fnPtr)(const VECT_OP_TYPE*, unsigned) );
  165. // Apply a median filter of order wndN to xV[xN] and store the result in yV[xN].
  166. // When the window goes off either side of the vector the missing elements are considered
  167. // to be 0.
  168. // This algorithm produces the same result as the MATLAB medfilt1() function.
  169. void VECT_OP_FUNC(MedianFilt)( const VECT_OP_TYPE* xV, unsigned xN, unsigned wndN, VECT_OP_TYPE* yV, unsigned yStride );
  170. //======================================================================================================================
  171. //)
  172. //( { label:"Edit distance" desc:"Calculate the Levenshtein edit distance between vectors." kw:[vop] }
  173. // Allocate and initialize a matrix for use by LevEditDist().
  174. // This matrix can be released with a call to cmMemFree().
  175. unsigned* VECT_OP_FUNC(LevEditDistAllocMtx)(unsigned mtxMaxN);
  176. // Return the Levenshtein edit distance between two vectors.
  177. // m must point to a matrix pre-allocated by VECT_OP_FUNC(InitiLevEditDistMtx)(maxN).
  178. double VECT_OP_FUNC(LevEditDist)(unsigned mtxMaxN, unsigned* m, const VECT_OP_TYPE* s0, int n0, const VECT_OP_TYPE* s1, int n1, unsigned maxN );
  179. // Return the Levenshtein edit distance between two vectors.
  180. // Edit distance with a max cost threshold. This version of the algorithm
  181. // will run faster than LevEditDist() because it will stop execution as soon
  182. // as the distance exceeds 'maxCost'.
  183. // 'maxCost' must be between 0.0 and 1.0 or it is forced into this range.
  184. // The maximum distance returned will be 'maxCost'.
  185. // m must point to a matrix pre-allocated by VECT_OP_FUNC(InitiLevEditDistMtx)(maxN).
  186. double VECT_OP_FUNC(LevEditDistWithCostThresh)( int mtxMaxN, unsigned* m, const VECT_OP_TYPE* s0, int n0, const VECT_OP_TYPE* s1, int n1, double maxCost, unsigned maxN );
  187. //======================================================================================================================
  188. //)