libcm is a C development framework with an emphasis on audio signal processing applications.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

cmVectOpsDocOut.h 52KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. //( { file_desc:"Math vector operations." kw:[vop math] }
  2. //)
  3. //| Copyright: (C) 2009-2020 Kevin Larke <contact AT larke DOT org>
  4. //| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
  5. //( { label:misc desc:"Miscellaneous vector operations." kw:[vop] }
  6. // Compute the cummulative sum of sbp[dn]. Equivalent to Matlab cumsum().
  7. T_t* cmVOT_CumSum(T_t* dbp, unsigned dn, const T_t* sbp );
  8. // Returns true if all values in each vector are equal.
  9. bool cmVOT_Equal( const T_t* s0p, const T_t* s1p, unsigned sn );
  10. // Same as Matlab linspace() v[i] = i * (limit-1)/n
  11. T_t* cmVOT_LinSpace( T_t* dbp, unsigned dn, T_t base, T_t limit );
  12. //======================================================================================================================
  13. //)
  14. //( { label:Print desc:"Vector printing functions." kw:[vop] }
  15. // Setting fieldWidth or decPltCnt to to negative values result in fieldWidth == 10 or decPlCnt == 4
  16. //
  17. void cmVOT_Printf( cmRpt_t* rpt, unsigned rn, unsigned cn, const T_t* dbp, int fieldWidth, int decPlCnt, const char* fmt, unsigned flags );
  18. void cmVOT_Print( cmRpt_t* rpt, unsigned rn, unsigned cn, const T_t* dbp );
  19. void cmVOT_PrintE( cmRpt_t* rpt, unsigned rn, unsigned cn, const T_t* dbp );
  20. void cmVOT_PrintLf( const char* label, cmRpt_t* rpt, unsigned rn, unsigned cn, const T_t* dbp, unsigned fieldWidth, unsigned decPlCnt, const char* fmt );
  21. void cmVOT_PrintL( const char* label, cmRpt_t* rpt, unsigned rn, unsigned cn, const T_t* dbp );
  22. void cmVOT_PrintLE( const char* label, cmRpt_t* rpt, unsigned rn, unsigned cn, const T_t* dbp );
  23. //======================================================================================================================
  24. //)
  25. //( { label:Normalization desc:"Normalization and standardization functions." kw:[vop] }
  26. // Normalize the vector of proabilities by dividing through by the sum.
  27. // This leaves the relative proportions of each value unchanged while producing a total probability of 1.0.
  28. //
  29. T_t* cmVOT_NormalizeProbabilityVV(T_t* dbp, unsigned dn, const T_t* sbp);
  30. T_t* cmVOT_NormalizeProbability(T_t* dbp, unsigned dn);
  31. T_t* cmVOT_NormalizeProbabilityN(T_t* dbp, unsigned dn, unsigned stride);
  32. //
  33. // Standardize the columns of the matrix by subtracting the mean and dividing by the standard deviation.
  34. // uV[dcn] returns the mean of the data and is optional.
  35. // sdV[dcn] return the standard deviation of the data and is optional.
  36. T_t* cmVOT_StandardizeRows( T_t* dbp, unsigned drn, unsigned dcn, T_t* uV, T_t* sdV );
  37. T_t* cmVOT_StandardizeCols( T_t* dbp, unsigned drn, unsigned dcn, T_t* uV, T_t* sdV );
  38. //
  39. // Normalize by dividing through by the max. value.
  40. // dp[] ./= max(dp). Returns the index of the max value.
  41. unsigned cmVOT_NormToMax( T_t* dp, unsigned dn );
  42. //
  43. // Normalize by dividing through by the max. absolute value.
  44. // db[] .*= fact / abs(max(dp));
  45. unsigned cmVOT_NormToAbsMax( T_t* dp, unsigned dn, T_t fact );
  46. //======================================================================================================================
  47. //)
  48. //( { label:"Mean and variance" desc:"Compute mean and variance." kw:[vop] }
  49. T_t cmVOT_Mean( const T_t* sp, unsigned sn );
  50. T_t cmVOT_MeanN( const T_t* sp, unsigned sn, unsigned stride );
  51. //
  52. // Take the mean of each column/row of a matrix.
  53. // Set 'dim' to 0 to return mean of columns else return mean of rows.
  54. T_t* cmVOT_MeanM( T_t* dp, const T_t* sp, unsigned srn, unsigned scn, unsigned dim );
  55. //
  56. // Take the mean of the first 'cnt' element of each column/row of a matrix.
  57. // Set 'dim' to 0 to return mean of columns else return mean of rows.
  58. // If 'cnt' is greater than the number of elements in the column/row then 'cnt' is
  59. // reduced to the number of elements in the column/row.
  60. T_t* cmVOT_MeanM2( T_t* dp, const T_t* sp, unsigned srn, unsigned scn, unsigned dim, unsigned cnt );
  61. //
  62. // Find the mean of the data points returned by srcFuncPtr(argPtr,i) and return it in dp[dim].
  63. // 'dim' is both the size of dp[] and the length of each data point returned by srcFuncPtr().
  64. // srcFuncPtr() will be called 'cnt' times but it may return NULL on some calls if the associated
  65. // data point should not be included in the mean calculation.
  66. T_t* cmVOT_Mean2( T_t* dp, const T_t* (*srcFuncPtr)(void* arg, unsigned idx ), unsigned dim, unsigned cnt, void* argPtr );
  67. //
  68. // avgPtr is optional - set to NULL to compute the average
  69. T_t cmVOT_Variance( const T_t* sp, unsigned sn, const T_t* avgPtr );
  70. T_t cmVOT_VarianceN(const T_t* sp, unsigned sn, unsigned stride, const T_t* avgPtr );
  71. //
  72. // Set dim=0 to return variance of columns otherwise return variance or rows.
  73. T_t* cmVOT_VarianceM(T_t* dp, const T_t* sp, unsigned srn, unsigned scn, const T_t* avgPtr, unsigned dim );
  74. //======================================================================================================================
  75. //)
  76. //( { label:"Covariance" desc:"Matrix covariance" kw:[vop] }
  77. // Calculate the sample covariance matrix from a set of Gaussian distributed multidimensional data.
  78. // sp[dn,scn] is the data set.
  79. // dn is the dimensionality of the data.
  80. // scn is the count of data points
  81. // up[dn] is an optional mean vector. If up == NULL then the mean of the data is calculated internally.
  82. // selIdxV[scn] can be used to select a subset of datapoints to process.
  83. // If selIdxV[] is non-NULL then only columns where selIdxV[i]==selKey will be processed.
  84. //
  85. // dp[dn,dn] = covar( sp[dn,scn], u[dn] )
  86. void cmVOT_GaussCovariance(T_t* dp, unsigned dn, const T_t* sp, unsigned scn, const T_t* up, const unsigned* selIdxV, unsigned selKey );
  87. // Calculate the sample covariance matrix.
  88. // dp[ dn*dn ] - output matrix
  89. // dn - dimensionality of the data
  90. // srcFuncPtr - User defined function which is called to return a pointer to a data vector at index 'idx'.
  91. // The returned data vector must contain 'dn' elements. The function should return NULL
  92. // if the data point associated with 'idx' should not be included in the covariance calculation.
  93. // sn - count of data vectors
  94. // userPtr - User arg. passed to srcFuncPtr.
  95. // uV[ dn ] - mean of the data set (optional)
  96. // Note that this function computes the covariance matrix in 2 serial passes (1 if the mean vector is given)
  97. // through the 'sn' data points.
  98. // The result of this function are identical to the octave cov() function.
  99. void cmVOT_GaussCovariance2(T_t* dp, unsigned dn, const T_t* (*srcFuncPtr)(void* userPtr, unsigned idx), unsigned sn, void* userPtr, const T_t* uV, const unsigned* selIdxV, unsigned selKey );
  100. //======================================================================================================================
  101. //)
  102. //( { label:"Float point normal" desc:"Evaluate the 'normalness of floating point values." kw:[vop] }
  103. // Returns true if all values are 'normal' according the the C macro 'isnormal'.
  104. // This function will return false if any of the values are zero.
  105. bool cmVOT_IsNormal( const T_t* sp, unsigned sn );
  106. // Returns true if all values are 'normal' or zero according the the C macro 'isnormal'.
  107. // This function accepts zeros as normal.
  108. bool cmVOT_IsNormalZ(const T_t* sp, unsigned sn );
  109. // Set dp[dn] to the indexes of the non-normal numbers in sp[dn].
  110. // Returns the count of indexes stored in dp[].
  111. unsigned cmVOT_FindNonNormal( unsigned* dp, unsigned dn, const T_t* sp );
  112. unsigned cmVOT_FindNonNormalZ( unsigned* dp, unsigned dn, const T_t* sp );
  113. //======================================================================================================================
  114. //)
  115. //( { label:"Measure" desc:"Measure features of a vector." kw:[vop] }
  116. // Successive call to to ZeroCrossCount should preserve the value pointed to by delaySmpPtr.
  117. unsigned cmVOT_ZeroCrossCount( const T_t* sp, unsigned n, T_t* delaySmpPtr);
  118. // Calculuate the sum of the squares of all elements in bp[bn].
  119. T_t cmVOT_SquaredSum( const T_t* bp, unsigned bn );
  120. // sn must be <= wndSmpCnt. If sn < wndSmpCnt then sp[sn] is treated as a
  121. // a partially filled buffer padded with wndSmpCnt-sn zeros.
  122. // rms = sqrt( sum(sp[1:sn] .* sp[1:sn]) / wndSmpCnt )
  123. T_t cmVOT_RMS( const T_t* sp, unsigned sn, unsigned wndSmpCnt );
  124. // This function handles the case were sn is not an integer multiple of
  125. // wndSmpCnt or hopSmpCnt. In this case the function computes zero
  126. // padded RMS values for windows which go past the end of sp[sn].
  127. T_t* cmVOT_RmsV( T_t* dp, unsigned dn, const T_t* sp, unsigned sn, unsigned wndSmpCnt, unsigned hopSmpCnt );
  128. // Return the magnitude (Euclidean Norm) of a vector.
  129. T_t cmVOT_EuclidNorm( const T_t* sp, unsigned sn );
  130. T_t cmVOT_AlphaNorm(const T_t* sp, unsigned sn, T_t alpha );
  131. //======================================================================================================================
  132. //)
  133. //( { label:"Distance" desc:"Calculate various vector distances." kw:[vop] }
  134. // Return the Itakura-Saito distance between a modelled power spectrum (up) and another power spectrum (sp).
  135. T_t cmVOT_ItakuraDistance( const T_t* up, const T_t* sp, unsigned sn );
  136. // Return the cosine distance between two vectors.
  137. T_t cmVOT_CosineDistance( const T_t* s0P, const T_t* s1p, unsigned sn );
  138. // Return the Euclidean distance between two vectors
  139. T_t cmVOT_EuclidDistance( const T_t* s0p, const T_t* s1p, unsigned sn );
  140. // Return the Manhattan distance between two vectors
  141. T_t cmVOT_L1Distance( const T_t* s0p, const T_t* s1p, unsigned sn );
  142. // Return the Mahalanobis distance between a vector and the mean of the distribution.
  143. // The mean vector could be replaced with another vector drawn from the same distribution in which
  144. // case the returned value would reflect the distance between the two vectors.
  145. // 'sn' is the dimensionality of the data.
  146. // up[D] and invCovM[sn,sn] are the mean and inverse of the covariance matrix of the distribution from
  147. // which sp[D] is drawn.
  148. T_t cmVOT_MahalanobisDistance( const T_t* sp, unsigned sn, const T_t* up, const T_t* invCovM );
  149. // Return the KL distance between two probability distributions up[sn] and sp[sn].
  150. // Since up[] and sp[] are probability distributions they must sum to 1.0.
  151. T_t cmVOT_KL_Distance( const T_t* up, const T_t* sp, unsigned sn );
  152. // Return the KL distance between a prototype vector up[sn] and another vector sp[sn].
  153. // This function first normalizes the two vectors to sum to 1.0 before calling
  154. // cmVOT_KL_Distance(up,sp,sn);
  155. T_t cmVOT_KL_Distance2( const T_t* up, const T_t* sp, unsigned sn );
  156. // Measure the Euclidean distance between a vector and all the columns in a matrix.
  157. // If dv[scn] is no NULL then return the Euclidean distance from sv[scn] to each column of sm[srn,scn].
  158. // The function returns the index of the closest data point (column) in sm[].
  159. unsigned cmVOT_EuclidDistanceVM( T_t* dv, const T_t* sv, const T_t* sm, unsigned srn, unsigned scn );
  160. // Measure the distance between each column in s0M[ rn, s0cn ] and
  161. // each column in s1M[rn, s1cn ]. If dM is non-NULL store the
  162. // result in dM[s1cn, s0cn]. The difference between s0M[:,0] and s1M[:,0]
  163. // is stored in dM[0,0], the diff. between s0M[:,1] and s1M[:,1] is stored
  164. // in dM[1,0], etc. If mvV[s0cn] is non-NULL then minV[i] is set with
  165. // the distance from s0M[:,i] to the nearest column in s1M[]. If miV[s0cn]
  166. // is non-NULL then it is set with the column index of s1M[] which is
  167. // closest to s0M[:,i]. In other words mvV[i] gives the distance to column
  168. // miV[i] from column s0M[:,i].
  169. // In those cases where the distane from a prototype (centroid) to the data point
  170. // is not the same as from the data point to the centroid then s1M[] is considered
  171. // to hold the prototypes and s0M[] is considered to hold the data points.
  172. // The distance function returns the distance from a prototype 'cV[dimN]' to
  173. // an datapoint dV[dimN]. 'dimN' is the dimensionality of the data vector
  174. // and is threfore equal to 'rn'.
  175. void cmVOT_DistVMM(
  176. T_t* dM, // dM[s1cn,s0cn] return distance mtx (optional)
  177. T_t* mvV, // mvV[s0cn] distance to closest data point in s0M[]. (optional)
  178. unsigned* miV, // miV[s0cn] column index into s1M[] of closest data point to s0M[:,i]. (optional)
  179. unsigned rn, // dimensionality of the data and the row count for s0M[] and s1M[]
  180. const T_t* s0M, // s0M[rn,s0cn] contains one data point per column
  181. unsigned s0cn, // count of data points (count of columns in s0M[]
  182. const T_t* s1M, // s1M[rn,s1cn] contains one prototype per column
  183. unsigned s1cn, // count of prototypes (count of columns in s1m[]
  184. T_t (*distFunc)( void* userPtr, const T_t* cV, const T_t* dV, unsigned dimN ),
  185. void* userPtr );
  186. //======================================================================================================================
  187. //)
  188. //( { label:"Select columns" desc:"Select columns based on distance." kw:[vop] }
  189. // Select 'selIdxN' columns from sM[srn,scn].
  190. // dM[srn,selIdxN] receives copies of the selected columns.
  191. // selIdxV[selIdxN] receives the column indexes of the selected columns.
  192. // Both dM[] and selIdxV[] are optional.
  193. // In each case the first selected point is chosen at random.
  194. // SelectRandom() then selects the following selIdxN-1 points at random.
  195. // SelectMaxDist() selects the next selIdxN-1 points by selecting
  196. // the point whose combined distance to the previously selected points
  197. // is greatest. SelectMaxAvgDist() selectes the points whose combined
  198. // average distance is greatest relative the the previously selected
  199. // points.
  200. void cmVOT_SelectRandom( T_t* dM, unsigned* selIdxV, unsigned selIdxN, const T_t* sM, unsigned srn, unsigned scn );
  201. void cmVOT_SelectMaxDist( T_t* dM, unsigned* selIdxV, unsigned selIdxN, const T_t* sM, unsigned srn, unsigned scn, T_t (*distFunc)( void* userPtr, const T_t* s0V, const T_t* s1V, unsigned sn ), void* distUserPtr );
  202. void cmVOT_SelectMaxAvgDist( T_t* dM, unsigned* selIdxV, unsigned selIdxN, const T_t* sM, unsigned srn, unsigned scn, T_t (*distFunc)( void* userPtr, const T_t* s0V, const T_t* s1V, unsigned sn ), void* distUserPtr );
  203. //======================================================================================================================
  204. //)
  205. //( { label:"Matrix multiplication" desc:"Various matrix multiplication operations." kw:[vop] }
  206. // Return the sum of the products (dot product)
  207. T_t cmVOT_MultSumVV( const T_t* s0p, const T_t* s1p, unsigned sn );
  208. T_t cmVOT_MultSumVS( const T_t* s0p, unsigned sn, T_t s );
  209. // Number of elements in the dest vector is expected to be the same
  210. // as the number of source matrix rows.
  211. // mcn gives the number of columns in the source matrix which is
  212. // expected to match the number of elements in the source vector.
  213. // dbp[dn,1] = mp[dn,mcn] * vp[mcn,1]
  214. T_t* cmVOT_MultVMV( T_t* dbp, unsigned dn, const T_t* mp, unsigned mcn, const T_t* vp );
  215. // Multiply a row vector with a matrix to produce a row vector.
  216. // dbp[1,dn] = v[1,vn] * m[vn,dn]
  217. T_t* cmVOT_MultVVM( T_t* dbp, unsigned dn, const T_t* vp, unsigned vn, const T_t* mp );
  218. // Same as MultVMtV() except M is transposed as part of the multiply.
  219. // mrn gives the number of rows in m[] and number of elements in vp[]
  220. // dpb[dn] = mp[mrn,dn] * vp[mrn]
  221. T_t* cmVOT_MultVMtV( T_t* dbp, unsigned dn, const T_t* mp, unsigned mrn, const T_t* vp );
  222. // Same as MultVMV() but where the matrix is diagonal.
  223. T_t* cmVOT_MultDiagVMV( T_t* dbp, unsigned dn, const T_t* mp, unsigned mcn, const T_t* vp );
  224. // Generalized matrix multiply.
  225. // If transposition is selected for M0 or M1 then the given dimension represent the size of the matrix 'after' the transposion.
  226. // d[drn,dcn] = alpha * op(m0[drn,m0cn_m1rn]) * op(m1[m0cn_m1rn,dcn]) + beta * d[drn,dcn]
  227. /// See enum { kTranpsoseM0Fl=0x01, kTransposeM1Fl=0x02 } in cmVectOps for flags.
  228. T_t* cmVOT_MultMMM1(T_t* dbp, unsigned drn, unsigned dcn, T_t alpha, const T_t* m0, const T_t* m1, unsigned m0cn_m1rn, T_t beta, unsigned flags );
  229. // Same a cmVOT_MultMMM1 except allows the operation on a sub-matrix by providing the physical (memory) row count rather than the logical (matrix) row count.
  230. T_t* cmVOT_MultMMM2(T_t* dbp, unsigned drn, unsigned dcn, T_t alpha, const T_t* m0, const T_t* m1, unsigned m0cn_m1rn, T_t beta, unsigned flags, unsigned dprn, unsigned m0prn, unsigned m1prn );
  231. // d[drn,dcn] = m0[drn,m0cn] * m1[m1rn,dcn]
  232. T_t* cmVOT_MultMMM( T_t* dbp, unsigned drn, unsigned dcn, const T_t* m0, const T_t* m1, unsigned m0cn_m1rn );
  233. // same as MultMMM() except second source matrix is transposed prior to the multiply
  234. T_t* cmVOT_MultMMMt(T_t* dbp, unsigned drn, unsigned dcn, const T_t* m0, const T_t* m1, unsigned m0cn_m1rn );
  235. //======================================================================================================================
  236. //)
  237. //( { label:"Linear algebra" desc:"Miscellaneous linear algebra operations. Determinant, Inversion, Cholesky decompostion. Linear system solver." kw:[vop] }
  238. // Initialize dbp[dn,dn] as a square symetric positive definite matrix using values
  239. // from a random uniform distribution. This is useful for initializing random
  240. // covariance matrices as used by multivariate Gaussian distributions
  241. // If t is non-NULL it must point to a block of scratch memory of t[dn,dn].
  242. // If t is NULL then scratch memory is internally allocated and deallocated.
  243. T_t* cmVOT_RandSymPosDef( T_t* dbp, unsigned dn, T_t* t );
  244. // Compute the determinant of any square matrix.
  245. T_t cmVOT_DetM( const T_t* sp, unsigned srn );
  246. // Compute the determinant of a diagonal matrix.
  247. T_t cmVOT_DetDiagM( const T_t* sp, unsigned srn);
  248. // Compute the log determinant of any square matrix.
  249. T_t cmVOT_LogDetM( const T_t* sp, unsigned srn );
  250. // Compute the log determinant of a diagonal matrix.
  251. T_t cmVOT_LogDetDiagM( const T_t* sp, unsigned srn);
  252. // Compute the inverse of a square matrix. Returns NULL if the matrix is not invertable.
  253. // 'drn' is the dimensionality of the data.
  254. T_t* cmVOT_InvM( T_t* dp, unsigned drn );
  255. // Compute the inverse of a diagonal matrix. Returns NULL if the matrix is not invertable.
  256. T_t* cmVOT_InvDiagM( T_t* dp, unsigned drn );
  257. // Solve a linear system of the form AX=B where A[an,an] is square.
  258. // Since A is square B must have 'an' rows.
  259. // Result is returned in B.
  260. // Returns a pointer to B on success or NULL on fail.
  261. // NOTE: Both A and B are overwritten by this operation.
  262. T_t* cmVOT_SolveLS( T_t* A, unsigned an, T_t* B, unsigned bcn );
  263. // Perform a Cholesky decomposition of the square symetric matrix U[un,un].
  264. // The factorization has the form: A=U'TU.
  265. // If the factorization is successful A is set to U and a pointer to A is returned.
  266. // Note that the lower triangle of A is not overwritten. See CholZ().
  267. // If the factorization fails NULL is returned.
  268. T_t* cmVOT_Chol(T_t* A, unsigned an );
  269. // Same as Chol() but sets the lower triangle of U to zero.
  270. // This is equivalent ot the Matlab version.
  271. T_t* cmVOT_CholZ(T_t* U, unsigned un );
  272. // Calculate the best fit line: b0 + b1*x_i through the points x_i,y_i.
  273. // Set x to NULL if it uses sequential integers [0,1,2,3...]
  274. void cmVOT_Lsq1(const T_t* x, const T_t* y, unsigned n, T_t* b0, T_t* b1 );
  275. //======================================================================================================================
  276. //)
  277. //( { label:"Stretch/Shrink" desc:"Stretch or shrink a vector by resampling." kw:[vop] }
  278. // Return the average value of the contents of sbp[] between two fractional indexes
  279. T_t cmVOT_FracAvg( double bi, double ei, const T_t* sbp, unsigned sn );
  280. // Shrinking function - Decrease the size of sbp[] by averaging blocks of values into single values in dbp[]
  281. T_t* cmVOT_DownSampleAvg( T_t* dbp, unsigned dn, const T_t* sbp, unsigned sn );
  282. // Stretching function - linear interpolate between points in sbp[] to fill dbp[] ... where dn > sn
  283. T_t* cmVOT_UpSampleInterp( T_t* dbp, unsigned dn, const T_t* sbp, unsigned sn );
  284. // Stretch or shrink the sbp[] to fit into dbp[]
  285. T_t* cmVOT_FitToSize( T_t* dbp, unsigned dn, const T_t* sbp, unsigned sn );
  286. // Stretch or shrink sV[] to fit into dV[] using a simple linear mapping.
  287. // When stretching (sn<dn) each source element is repeated dn/sn times
  288. // and the last fraction position is interpolated. When shrinking
  289. // (sn>dn) each dest value is formed by the average of sequential segments
  290. // of sn/dn source elements. Fractional values are used at the beginning
  291. // and end of each segment.
  292. T_t* cmVOT_LinearMap(T_t* dV, unsigned dn, T_t* sV, unsigned sn );
  293. //======================================================================================================================
  294. //)
  295. //( { label:"Random number generation" desc:"Generate random numbers." kw:[vop] }
  296. // Generate a vector of uniformly distributed random numbers in the range minVal to maxVal.
  297. T_t* cmVOT_Random( T_t* dbp, unsigned dn, T_t minVal, T_t maxVal );
  298. // Generate dn random numbers integers between 0 and wn-1 based on a the relative
  299. // weights in wp[wn]. Note thtat the weights do not have to sum to 1.0.
  300. unsigned* cmVOT_WeightedRandInt( unsigned* dbp, unsigned dn, const T_t* wp, unsigned wn );
  301. // Generate a vector of normally distributed univariate random numbers
  302. T_t* cmVOT_RandomGauss( T_t* dbp, unsigned dn, T_t mean, T_t var );
  303. // Generate a vector of normally distributed univariate random numbers where each value has been drawn from a
  304. // seperately parameterized Gaussian distribution. meanV[] and varV[] must both contain dn velues.
  305. T_t* cmVOT_RandomGaussV( T_t* dbp, unsigned dn, const T_t* meanV, const T_t* varV );
  306. // Generate a matrix of multi-dimensional random values. Each column represents a single vector value and each row contains a dimension.
  307. // meanV[] and varV[] must both contain drn elements where each meanV[i],varV[i] pair parameterize one dimensions Gaussian distribution.
  308. T_t* cmVOT_RandomGaussM( T_t* dbp, unsigned drn, unsigned dcn, const T_t* meanV, const T_t* varV );
  309. T_t* cmVOT_RandomGaussDiagM( T_t* dbp, unsigned drn, unsigned dcn, const T_t* meanV, const T_t* diagCovarM );
  310. // Generate a matrix of multivariate random values drawn from a normal distribution.
  311. // The dimensionality of the values are 'drn'.
  312. // The count of returned values is 'dcn'.
  313. // meanV[drn] and covarM[drn,drn] parameterize the normal distribution.
  314. // The covariance matrix must be symetric and positive definite.
  315. // t[(drn*drn) ] points to scratch memory or is set to NULL if the function should
  316. // allocate the memory internally.
  317. // Based on octave function mvrnd.m.
  318. T_t* cmVOT_RandomGaussNonDiagM( T_t* dbp, unsigned drn, unsigned dcn, const T_t* meanV, const T_t* covarM, T_t* t );
  319. // Same as RandomGaussNonDiagM() except requires the upper trianglular
  320. // Cholesky factor of the covar matrix in 'uM'.
  321. T_t* cmVOT_RandomGaussNonDiagM2( T_t* dbp, unsigned drn, unsigned dcn, const T_t* meanV, const T_t* uM );
  322. // Generate a matrix of N*K multi-dimensional data points.
  323. // Where D is the dimensionality of the data. (D == drn).
  324. // K is the number of multi-dimensional PDF's (clusters).
  325. // N is the number of data points to generate per cluster.
  326. // dbp[ D, N*K ] contains the returned data point.
  327. // The first N columns is associated with the cluster 0,
  328. // the next N columns is associated with cluster 1, ...
  329. // meanM[ D, K ] and varM[D,K] parameterize the generating PDF.s for each cluster
  330. T_t* cmVOT_RandomGaussMM( T_t* dbp, unsigned drn, unsigned dcn, const T_t* meanM, const T_t* varM, unsigned K );
  331. // Evaluate the univariate normal distribution defined by 'mean' and 'stdDev'.
  332. T_t* cmVOT_GaussPDF( T_t* dbp, unsigned dn, const T_t* sbp, T_t mean, T_t stdDev );
  333. // Evaluate a multivariate normal distribution defined by meanV[D] and covarM[D,D]
  334. // at the data points held in the columns of xM[D,N]. Return the evaluation
  335. // results in the vector yV[N]. D is the dimensionality of the data. N is the number of
  336. // data points to evaluate and values to return in yV[N].
  337. // Set diagFl to true if covarM is diagonal.
  338. // The function fails and returns false if the covariance matrix is singular.
  339. bool cmVOT_MultVarGaussPDF( T_t* yV, const T_t* xM, const T_t* meanV, const T_t* covarM, unsigned D, unsigned N, bool diagFl );
  340. // Same as multVarGaussPDF[] except takes the inverse covar mtx invCovarM[D,D]
  341. // and log determinant of covar mtx.
  342. // Always returns yV[].
  343. T_t* cmVOT_MultVarGaussPDF2( T_t* yV, const T_t* xM, const T_t* meanV, const T_t* invCovarM, T_t logDet, unsigned D, unsigned N, bool diagFl );
  344. // Same as multVarGaussPDF[] except uses a function to obtain the data vectors.
  345. // srcFunc() can filter the data points by returning NULL if the data vector at frmIdx should
  346. // not be evaluated against the PDF. In this case yV[frmIdx] will be set to 0.
  347. T_t* cmVOT_MultVarGaussPDF3(
  348. T_t* yV,
  349. const T_t* (*srcFunc)(void* funcDataPtr, unsigned frmIdx ),
  350. void* funcDataPtr,
  351. const T_t* meanV,
  352. const T_t* invCovarM,
  353. T_t logDet,
  354. unsigned D,
  355. unsigned N,
  356. bool diagFl );
  357. //======================================================================================================================
  358. //)
  359. //( { label:"Signal generators" desc:"Generate periodic signals." kw:[vop] }
  360. // The following functions all return the phase of the next value.
  361. unsigned cmVOT_SynthSine( T_t* dbp, unsigned dn, unsigned phase, double srate, double hz );
  362. unsigned cmVOT_SynthCosine( T_t* dbp, unsigned dn, unsigned phase, double srate, double hz );
  363. unsigned cmVOT_SynthSquare( T_t* dbp, unsigned dn, unsigned phase, double srate, double hz, unsigned otCnt );
  364. unsigned cmVOT_SynthTriangle( T_t* dbp, unsigned dn, unsigned phase, double srate, double hz, unsigned otCnt );
  365. unsigned cmVOT_SynthSawtooth( T_t* dbp, unsigned dn, unsigned phase, double srate, double hz, unsigned otCnt );
  366. unsigned cmVOT_SynthPulseCos( T_t* dbp, unsigned dn, unsigned phase, double srate, double hz, unsigned otCnt );
  367. unsigned cmVOT_SynthImpulse( T_t* dbp, unsigned dn, unsigned phase, double srate, double hz );
  368. unsigned cmVOT_SynthPhasor( T_t* dbp, unsigned dn, unsigned phase, double srate, double hz );
  369. // Return value should be passed back via delaySmp on the next call.
  370. T_t cmVOT_SynthPinkNoise( T_t* dbp, unsigned dn, T_t delaySmp );
  371. //======================================================================================================================
  372. //)
  373. //( { label:"Exponential conversion" desc:"pow() and log() functions." kw:[vop] }
  374. // Raise dbp[] to the power 'expon'
  375. T_t* cmVOT_PowVS( T_t* dbp, unsigned dn, T_t expon );
  376. T_t* cmVOT_PowVVS( T_t* dbp, unsigned dn, const T_t* sp, T_t expon );
  377. // Take the natural log of all values in sbp[dn]. It is allowable for sbp point to the same array as dbp=.
  378. T_t* cmVOT_LogV( T_t* dbp, unsigned dn, const T_t* sbp );
  379. //======================================================================================================================
  380. //)
  381. //( { label:"dB Conversions" desc:"Convert vectors between dB,linear and power representations." kw:[vop] }
  382. // Convert a magnitude (amplitude) spectrum to/from decibels.
  383. // It is allowable for dbp==sbp.
  384. T_t* cmVOT_AmplToDbVV( T_t* dbp, unsigned dn, const T_t* sbp, T_t minDb );
  385. T_t* cmVOT_DbToAmplVV( T_t* dbp, unsigned dn, const T_t* sbp);
  386. T_t* cmVOT_PowToDbVV( T_t* dbp, unsigned dn, const T_t* sbp, T_t minDb );
  387. T_t* cmVOT_DbToPowVV( T_t* dbp, unsigned dn, const T_t* sbp);
  388. T_t* cmVOT_LinearToDb( T_t* dbp, unsigned dn, const T_t* sp, T_t mult );
  389. T_t* cmVOT_dBToLinear( T_t* dbp, unsigned dn, const T_t* sp, T_t mult );
  390. T_t* cmVOT_AmplitudeToDb( T_t* dbp, unsigned dn, const T_t* sp );
  391. T_t* cmVOT_PowerToDb( T_t* dbp, unsigned dn, const T_t* sp );
  392. T_t* cmVOT_dBToAmplitude( T_t* dbp, unsigned dn, const T_t* sp );
  393. T_t* cmVOT_dBToPower( T_t* dbp, unsigned dn, const T_t* sp );
  394. //======================================================================================================================
  395. //)
  396. //( { label:"DSP Windows" desc:"DSP windowing functions." kw:[vop] }
  397. T_t cmVOT_KaiserBetaFromSidelobeReject( double sidelobeRejectDb );
  398. T_t cmVOT_KaiserFreqResolutionFactor( double sidelobeRejectDb );
  399. T_t* cmVOT_Kaiser( T_t* dbp, unsigned dn, double beta );
  400. T_t* cmVOT_Gaussian(T_t* dbp, unsigned dn, double mean, double variance );
  401. T_t* cmVOT_Hamming( T_t* dbp, unsigned dn );
  402. T_t* cmVOT_Hann( T_t* dbp, unsigned dn );
  403. T_t* cmVOT_Triangle(T_t* dbp, unsigned dn );
  404. // The MATLAB equivalent Hamming and Hann windows.
  405. //T_t* cmVOT_HammingMatlab(T_t* dbp, unsigned dn );
  406. T_t* cmVOT_HannMatlab( T_t* dbp, unsigned dn );
  407. // Simulates the MATLAB GaussWin function. Set arg to 2.5 to simulate the default arg
  408. // as used by MATLAB.
  409. T_t* cmVOT_GaussWin( T_t* dbp, unsigned dn, double arg );
  410. //======================================================================================================================
  411. //)
  412. //( { label:"DSP Filters" desc:"DSP filtering functions." kw:[vop] }
  413. // Direct form II algorithm based on the MATLAB implmentation
  414. // http://www.mathworks.com/access/helpdesk/help/techdoc/ref/filter.html#f83-1015962
  415. // The only difference between this function and the equivalent MATLAB filter() function
  416. // is that the first feedforward coeff is given as a seperate value. The first b coefficient
  417. // in this function is therefore the same as the second coefficient in the MATLAB function.
  418. // and the first a[] coefficient (which is generally set to 1.0) is skipped.
  419. // Example:
  420. // Matlab: b=[.5 .4 .3] a=[1 .2 .1]
  421. // Equiv: b0 = .5 b=[ .4 .3] a=[ .2 .1];
  422. //
  423. // y[yn] - output vector
  424. // x[xn] - input vector. xn must be <= yn. if xn < yn then the end of y[] is set to zero.
  425. // b0 - signal scale. This can also be seen as b[0] (which is not included in b[])
  426. // b[dn] - feedforward coeff's b[1..dn-1]
  427. // a[dn] - feedback coeff's a[1..dn-1]
  428. // d[dn+1] - delay registers - note that this array must be one element longer than the coeff arrays.
  429. //
  430. T_t* cmVOT_Filter( T_t* y, unsigned yn, const T_t* x, unsigned xn, cmReal_t b0, const cmReal_t* b, const cmReal_t* a, cmReal_t* d, unsigned dn );
  431. struct cmFilter_str;
  432. //typedef cmRC_t (*cmVOT_FiltExecFunc_t)( struct acFilter_str* f, const T_t* x, unsigned xn, T_t* y, unsigned yn );
  433. T_t* cmVOT_FilterFilter(struct cmFilter_str* f, cmRC_t (*func)( struct cmFilter_str* f, const T_t* x, unsigned xn, T_t* y, unsigned yn ), const cmReal_t bb[], unsigned bn, const cmReal_t aa[], unsigned an, const T_t* x, unsigned xn, T_t* y, unsigned yn );
  434. // Compute the coefficients of a low/high pass FIR filter
  435. // wndV[dn] gives the window function used to truncate the ideal low-pass impulse response.
  436. // Set wndV to NULL to use a unity window.
  437. // See enum { kHighPass_LPSincFl=0x01, kNormalize_LPSincFl=0x02 } in cmVectOps.h
  438. T_t* cmVOT_LP_Sinc(T_t* dp, unsigned dn, const T_t* wndV, double srate, double fcHz, unsigned flags );
  439. //======================================================================================================================
  440. //)
  441. //( { label:"Spectral Masking" desc:"A collection of spectral masking functions." kw:[vop] }
  442. // Compute a set of filterCnt mel filter masks for wieghting magnitude spectra consisting of binCnt bins.
  443. // The spectrum is divided into bandCnt equal bands in the mel domain
  444. // Each row of the matrix contains the mask for a single filter band consisting of binCnt elements.
  445. // See enum{ kShiftMelFl=0x01, kNormalizeMelFl=0x02 } in cmVectOps.h
  446. // Set kShiftMelFl to shift the mel bands onto the nearest FFT bin.
  447. // Set kNormalizeMelFl to normalize the combined filters for unity gain.
  448. T_t* cmVOT_MelMask( T_t* maskMtx, unsigned bandCnt, unsigned binCnt, double srate, unsigned flags );
  449. // Fill binIdxV[bandCnt] and cntV[bandCnt] with a bin to band map.
  450. // binIdx[] contains the first (minimum) bin index for a given band.
  451. // cntV[] contains the count of bins for each band.
  452. // bandCnt is the number of bark bands to return
  453. // The function returns the actual number of bands mapped which will always be <= 23.
  454. unsigned cmVOT_BarkMap(unsigned* binIdxV, unsigned* cntV, unsigned bandCnt, unsigned binCnt, double srate );
  455. // Calc a set of triangle fitler masks into each row of maskMtx.
  456. // maskMtx[ bandCnt, binCnt ] - result matrix
  457. // binHz - freq resolution of the output filters.
  458. // stSpread - Semi-tone spread above and below each center frequency (stSpread*2) is the total bandwidth.
  459. // (Only used if lowHzV or uprHzV are NULL)
  460. // lowHz[ bandCnt ] - set of upper frequency limits for each band.
  461. // ctrHz[ bandCnt ] set to the center value in Hz for each band
  462. // uprHz[ bandCnt ] - set of lower frequency limits for each band.
  463. // Note if lowHz[] and uprHz[] are set to NULL then stSpread is used to set the bandwidth of each band.
  464. T_t* cmVOT_TriangleMask(T_t* maskMtx, unsigned bandCnt, unsigned binCnt, const T_t* ctrHzV, T_t binHz, T_t stSpread, const T_t* lowHzV, const T_t* uprHzV );
  465. // Calculate a set of Bark band triangle filters into maskMtx.
  466. // Each row of maskMtx contains the filter for one band.
  467. // maskMtx[ bandCnt, binCnt ]
  468. // bandCnt - the number of triangle bankds. If bandCnt is > 24 it will be reduced to 24.
  469. // binCnt - the number of bins in the filters.
  470. // binHz - the width of each bin in Hz.
  471. T_t* cmVOT_BarkMask(T_t* maskMtx, unsigned bandCnt, unsigned binCnt, double binHz );
  472. // Terhardt 1979 (Calculating virtual pitch, Hearing Research #1, pp 155-182)
  473. // See enum { kNoTtmFlags=0, kModifiedTtmFl=0x01 } in cmVectOps.h
  474. T_t* cmVOT_TerhardtThresholdMask(T_t* maskV, unsigned binCnt, double srate, unsigned flags);
  475. //Schroeder et al., 1979, JASA, Optimizing digital speech coders by exploiting masking properties of the human ear
  476. T_t* cmVOT_ShroederSpreadingFunc(T_t* m, unsigned bandCnt, double srate);
  477. //======================================================================================================================
  478. //)
  479. //( { label:"Machine learning" desc:"K-means clustering and Viterbi algorithms." kw:[vop] }
  480. // Assign each data point to one of k clusters using an expectation-maximization algorithm.
  481. // k gives the number of clusters to identify
  482. // Each column of sp[ srn, scn ] contains a multidimensional data point.
  483. // srn therefore defines the dimensionality of the data.
  484. // Each column of centroidV[ srn, k ] is set to the centroid of each of k clusters.
  485. // classIdxV[ scn ] assigns the index (0 to k-1) of a cluster to each soure data point
  486. // The function returns the number of iterations required for the EM process to converge.
  487. // selIdxV[ scn ] is optional and contains a list of id's assoc'd with each column of sM.
  488. // selKey is a integer value.
  489. // If selIdxV is non-NULL then only columns of sM[] where selIdxV[] == selKey will be clustered.
  490. // All columns of sM[] where the associated column in selIdxV[] do not match will be ignored.
  491. // Set 'initFromCentroidFl' to true if the initial centroids should be taken from centroidM[].
  492. // otherwise the initial centroids are selected from 'k' random data points in sp[].
  493. // The distance function distFunc(cV,dV,dN) is called to determine the distance from a
  494. // centroid the centroid 'cV[dN]' to a data point 'dV[dN]'. 'dN' is the dimensionality of the
  495. // feature vector and is therefore equal to 'srn'.
  496. unsigned cmVOT_Kmeans(
  497. unsigned* classIdxV,
  498. T_t* centroidM,
  499. unsigned k,
  500. const T_t* sp,
  501. unsigned srn,
  502. unsigned scn,
  503. const unsigned* selIdxV,
  504. unsigned selKey,
  505. bool initFromCentroidFl,
  506. T_t (*distFunc)( void* userPtr, const T_t* cV, const T_t* dV, unsigned dN ),
  507. void* userDistPtr );
  508. // 'srcFunc() should return NULL if the data point located at 'frmIdx' should not be included in the clustering.
  509. // Clustering is considered to be complete after 'maxIterCnt' iterations or when
  510. // 'deltaStopCnt' or fewer data points change class on a single iteration
  511. unsigned cmVOT_Kmeans2(
  512. unsigned* classIdxV, // classIdxV[scn] - data point class assignments
  513. T_t* centroidM, // centroidM[srn,K] - cluster centroids
  514. unsigned K, // count of clusters
  515. const T_t* (*srcFunc)(void* userPtr, unsigned frmIdx ),
  516. unsigned srn, // dimensionality of each data point
  517. unsigned scn, // count of data points
  518. void* userSrcPtr, // callback data for srcFunc
  519. T_t (*distFunc)( void* userPtr, const T_t* cV, const T_t* dV, unsigned dN ),
  520. void* userDistPtr, // arg. to distFunc()
  521. int iterCnt, // max. number of iterations (-1 to ignore)
  522. int deltaStopCnt); // if less than deltaStopCnt data points change classes on a given iteration then convergence occurs.
  523. // Determine the most likely state sequece stateV[timeN] given a
  524. // transition matrix a[stateN,stateN],
  525. // observation probability matrix b[stateN,timeN] and
  526. // initial state probability vector phi[stateN].
  527. // a[i,j] is the probability of transitioning from state i to state j.
  528. // b[i,t] is the probability of state i emitting the obj t.
  529. void cmVOT_DiscreteViterbi(unsigned* stateV, unsigned timeN, unsigned stateN, const T_t* phi, const T_t* a, const T_t* b );
  530. //======================================================================================================================
  531. //)
  532. //( { label:"Graphics" desc:"Graphics related algorithms." kw:[vop] }
  533. // Generate the set of coordinates which describe a circle with a center at x,y.
  534. // dbp[dn,2] must contain 2*dn elements. The first column holds the x coord and and the second holds the y coord.
  535. T_t* cmVOT_CircleCoords( T_t* dbp, unsigned dn, T_t x, T_t y, T_t varX, T_t varY );
  536. // Clip the line defined by x0,y0 to x1,y1 into the rect defined by xMin,yMin xMax,yMax.
  537. bool cmVOT_ClipLine( T_t* x0, T_t* y0, T_t* x1, T_t* y1, T_t xMin, T_t yMin, T_t xMax, T_t yMax );
  538. // Return true if the line defined by x0,y0 to x1,y1 intersects with
  539. // the rectangle formed by xMin,yMin - xMax,yMax
  540. bool cmVOT_IsLineInRect( T_t x0, T_t y0, T_t x1, T_t y1, T_t xMin, T_t yMin, T_t xMax, T_t yMax );
  541. // Return the perpendicular distance from the line formed by x0,y0 and x1,y1
  542. // and the point px,py
  543. T_t cmVOT_PtToLineDistance( T_t x0, T_t y0, T_t x1, T_t y1, T_t px, T_t py);
  544. //======================================================================================================================
  545. //)
  546. //( { label:"Miscellaneous DSP" desc:"Common DSP algorithms." kw:[vop] }
  547. // Compute the complex transient detection function from successive spectral frames.
  548. // The spectral magntidue mag0V precedes mag1V and the phase (radians) spectrum phs0V precedes the phs1V which precedes phs2V.
  549. // binCnt gives the length of each of the spectral vectors.
  550. T_t cmVOT_ComplexDetect(const T_t* mag0V, const T_t* mag1V, const T_t* phs0V, const T_t* phs1V, const T_t* phs2V, unsigned binCnt );
  551. // Compute a set of DCT-II coefficients. Result dp[ coeffCnt, filtCnt ]
  552. T_t* cmVOT_DctMatrix( T_t* dp, unsigned coeffCnt, unsigned filtCnt );
  553. // Set the indexes of local peaks greater than threshold in dbp[].
  554. // Returns the number of peaks in dbp[]
  555. // The maximum number of peaks from n source values is max(0,floor((n-1)/2)).
  556. // Note that peaks will never be found at index 0 or index sn-1.
  557. unsigned cmVOT_PeakIndexes( unsigned* dbp, unsigned dn, const T_t* sbp, unsigned sn, T_t threshold );
  558. // Return the index of the bin containing v otherwise return kInvalidIdx if v is below sbp[0] or above sbp[ n-1 ]
  559. // The bin limits are contained in sbp[].
  560. // The value in spb[] are therefore expected to be in increasing order.
  561. // The value returned will be in the range 0:sn-1.
  562. unsigned cmVOT_BinIndex( const T_t* sbp, unsigned sn, T_t v );
  563. // Given the points x0[xy0N],y0[xy0N] fill y1[i] with the interpolated value of y0[] at
  564. // x1[i]. Note that x0[] and x1[] must be increasing monotonic.
  565. // This function is similar to the octave interp1() function.
  566. void cmVOT_Interp1(T_t* y1, const T_t* x1, unsigned xy1N, const T_t* x0, const T_t* y0, unsigned xy0N );
  567. //======================================================================================================================
  568. //)
  569. //| Copyright: (C) 2009-2020 Kevin Larke <contact AT larke DOT org>
  570. //| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
  571. //( { label:"Matrix ops" desc:"Common 2D matrix operations and accessors." kw:[vop] }
  572. // 2D matrix accessors
  573. T_t* cmVOT_Col( T_t* m, unsigned ci, unsigned rn, unsigned cn );
  574. T_t* cmVOT_Row( T_t* m, unsigned ri, unsigned rn, unsigned cn );
  575. T_t* cmVOT_ElePtr( T_t* m, unsigned ri, unsigned ci, unsigned rn, unsigned cn );
  576. T_t cmVOT_Ele( T_t* m, unsigned ri, unsigned ci, unsigned rn, unsigned cn );
  577. void cmVOT_Set( T_t* m, unsigned ri, unsigned ci, unsigned rn, unsigned cn, T_t v );
  578. const T_t* cmVOT_CCol( const T_t* m, unsigned ci, unsigned rn, unsigned cn );
  579. const T_t* cmVOT_CRow( const T_t* m, unsigned ri, unsigned rn, unsigned cn );
  580. const T_t* cmVOT_CElePtr( const T_t* m, unsigned ri, unsigned ci, unsigned rn, unsigned cn );
  581. T_t cmVOT_CEle( const T_t* m, unsigned ri, unsigned ci, unsigned rn, unsigned cn );
  582. // Set only the diagonal of a square mtx to sbp.
  583. T_t* cmVOT_Diag( T_t* dbp, unsigned n, const T_t* sbp );
  584. // Set the diagonal of a square mtx to db to sbp and set all other values to zero.
  585. T_t* cmVOT_DiagZ( T_t* dbp, unsigned n, const T_t* sbp );
  586. // Create an identity matrix (only sets 1's not zeros).
  587. T_t* cmVOT_Identity( T_t* dbp, unsigned rn, unsigned cn );
  588. // Zero the matrix and then fill it as an identity matrix.
  589. T_t* cmVOT_IdentityZ( T_t* dbp, unsigned rn, unsigned cn );
  590. // Transpose the matrix sbp[srn,scn] into dbp[scn,srn]
  591. T_t* cmVOT_Transpose( T_t* dbp, const T_t* sbp, unsigned srn, unsigned scn );
  592. //======================================================================================================================
  593. //)
  594. //( { label:"Fill,move,copy" desc:"Basic data movement and initialization." kw:[vop] }
  595. // Fill a vector with a value. If value is 0 then the function is accellerated via memset().
  596. T_t* cmVOT_Fill( T_t* dbp, unsigned dn, T_t value );
  597. // Fill a vector with zeros
  598. T_t* cmVOT_Zero( T_t* dbp, unsigned dn );
  599. // Analogous to memmove()
  600. T_t* cmVOT_Move( T_t* dbp, unsigned dn, const T_t* sp );
  601. // Fill the vector from various sources
  602. T_t* cmVOT_Copy( T_t* dbp, unsigned dn, const T_t* sp );
  603. T_t* cmVOT_CopyN( T_t* dbp, unsigned dn, unsigned d_stride, const T_t* sp, unsigned s_stride );
  604. T_t* cmVOT_CopyU( T_t* dbp, unsigned dn, const unsigned* sp );
  605. T_t* cmVOT_CopyI( T_t* dbp, unsigned dn, const int* sp );
  606. T_t* cmVOT_CopyF( T_t* dbp, unsigned dn, const float* sp );
  607. T_t* cmVOT_CopyD( T_t* dbp, unsigned dn, const double* sp );
  608. T_t* cmVOT_CopyS( T_t* dbp, unsigned dn, const cmSample_t* sp );
  609. T_t* cmVOT_CopyR( T_t* dbp, unsigned dn, const cmReal_t* sp );
  610. // Fill the the destination vector from a source vector where the source vector contains
  611. // srcStride interleaved elements to be ignored.
  612. T_t* cmVOT_CopyStride( T_t* dbp, unsigned dn, const T_t* sp, unsigned srcStride );
  613. //======================================================================================================================
  614. //)
  615. //( { label:"Shrink/Expand/Replace" desc:"Change the size of a vector." kw:[vop] }
  616. // Shrink the elemetns of dbp[dn] by copying all elements past t+tn to t.
  617. // This operation results in overwriting the elements in the range t[tn].
  618. // t[tn] must be entirely inside dbp[dn].
  619. T_t* cmVOT_Shrink( T_t* dbp, unsigned dn, const T_t* t, unsigned tn );
  620. // Expand dbp[[dn] by shifting all elements past t to t+tn.
  621. // This produces a set of empty elements in t[tn].
  622. // t must be inside or at the end of dbp[dn].
  623. // This results in a reallocation of dbp[]. Be sure to call cmMemFree(dbp)
  624. // to release the returned pointer.
  625. T_t* cmVOT_Expand( T_t* dbp, unsigned dn, const T_t* t, unsigned tn );
  626. // Replace the elements t[tn] with the elements in u[un].
  627. // t must be inside or at the end of dbp[dn].
  628. // This operation may result in a reallocation of dbp[]. Be sure to call cmMemFree(dbp)
  629. // to release the returned pointer.
  630. // IF dbp==NULL and tn==0 then the dbp[un] is allocated and returned
  631. // with the contents of u[un].
  632. T_t* cmVOT_Replace(T_t* dbp, unsigned* dn, const T_t* t, unsigned tn, const T_t* u, unsigned un );
  633. //======================================================================================================================
  634. //)
  635. //( { label:"Rotate/Shift/Flip/Sequence" desc:"Modify/generate the vector sequence." kw:[vop] }
  636. // Assuming a row vector positive shiftCnt rotates right, negative shiftCnt rotates left.
  637. T_t* cmVOT_Rotate( T_t* dbp, unsigned dn, int shiftCnt );
  638. // Equivalent to Matlab circshift().
  639. T_t* cmVOT_RotateM( T_t* dbp, unsigned drn, unsigned dcn, const T_t* sbp, int rShift, int cShift );
  640. // Assuming a row vector positive shiftCnt shifts right, negative shiftCnt shifts left.
  641. T_t* cmVOT_Shift( T_t* dbp, unsigned dn, int shiftCnt, T_t fill );
  642. // Reverse the contents of the vector.
  643. T_t* cmVOT_Flip( T_t* dbp, unsigned dn);
  644. // Fill dbp[] with a sequence of values. Returns next value.
  645. T_t cmVOT_Seq( T_t* dbp, unsigned dn, T_t beg, T_t incr );
  646. //======================================================================================================================
  647. //)
  648. //( { label:"Arithmetic" desc:"Add,Sub,Mult,Divde" kw:[vop] }
  649. T_t* cmVOT_SubVS( T_t* dp, unsigned dn, T_t v );
  650. T_t* cmVOT_SubVV( T_t* dp, unsigned dn, const T_t* v );
  651. T_t* cmVOT_SubVVS( T_t* dp, unsigned dn, const T_t* v, T_t s );
  652. T_t* cmVOT_SubVVNN(T_t* dp, unsigned dn, unsigned dnn, const T_t* sp, unsigned snn );
  653. T_t* cmVOT_SubVVV( T_t* dp, unsigned dn, const T_t* sb0p, const T_t* sb1p );
  654. T_t* cmVOT_SubVSV( T_t* dp, unsigned dn, const T_t s0, const T_t* sb1p );
  655. T_t* cmVOT_AddVS( T_t* dp, unsigned dn, T_t v );
  656. T_t* cmVOT_AddVV( T_t* dp, unsigned dn, const T_t* v );
  657. T_t* cmVOT_AddVVS( T_t* dp, unsigned dn, const T_t* v, T_t s );
  658. T_t* cmVOT_AddVVNN(T_t* dp, unsigned dn, unsigned dnn, const T_t* sp, unsigned snn );
  659. T_t* cmVOT_AddVVV( T_t* dp, unsigned dn, const T_t* sb0p, const T_t* sb1p );
  660. T_t* cmVOT_MultVVV( T_t* dbp, unsigned dn, const T_t* sb0p, const T_t* sb1p );
  661. T_t* cmVOT_MultVV( T_t* dbp, unsigned dn, const T_t* sbp );
  662. T_t* cmVOT_MultVVNN(T_t* dp, unsigned dn, unsigned dnn, const T_t* sp, unsigned snn );
  663. T_t* cmVOT_MultVS( T_t* dbp, unsigned dn, T_t s );
  664. T_t* cmVOT_MultVVS( T_t* dbp, unsigned dn, const T_t* sbp, T_t s );
  665. T_t* cmVOT_MultVaVS( T_t* dbp, unsigned dn, const T_t* sbp, T_t s );
  666. T_t* cmVOT_MultSumVVS(T_t* dbp, unsigned dn, const T_t* sbp, T_t s );
  667. T_t* cmVOT_DivVVS( T_t* dbp, unsigned dn, const T_t* sb0p, T_t sb1 );
  668. T_t* cmVOT_DivVVV( T_t* dbp, unsigned dn, const T_t* sb0p, const T_t* sb1p );
  669. T_t* cmVOT_DivVV( T_t* dbp, unsigned dn, const T_t* sb0p );
  670. T_t* cmVOT_DivVVNN(T_t* dp, unsigned dn, unsigned dnn, const T_t* sp, unsigned snn );
  671. T_t* cmVOT_DivVS( T_t* dbp, unsigned dn, T_t s );
  672. T_t* cmVOT_DivVSV( T_t* dp, unsigned dn, const T_t s0, const T_t* sb1p );
  673. // Set dest to 0 if denominator is 0.
  674. T_t* cmVOT_DivVVVZ( T_t* dbp, unsigned dn, const T_t* sb0p, const T_t* sb1p );
  675. T_t* cmVOT_DivVVZ( T_t* dbp, unsigned dn, const T_t* sb0p );
  676. // Divide columns of dp[:,i] by each value in the source vector sp[i].
  677. T_t* cmVOT_DivMS( T_t* dp, unsigned drn, unsigned dcn, const T_t* sp );
  678. //======================================================================================================================
  679. //)
  680. //( { label:"Sum vectors" desc:"Operations which take sum vector elements." kw:[vop] }
  681. T_t cmVOT_Sum( const T_t* sp, unsigned sn );
  682. T_t cmVOT_SumN( const T_t* sp, unsigned sn, unsigned stride );
  683. // Sum the columns of sp[srn,scn] into dp[scn].
  684. // dp[] is zeroed prior to computing the sum.
  685. T_t* cmVOT_SumM( const T_t* sp, unsigned srn, unsigned scn, T_t* dp );
  686. // Sum the rows of sp[srn,scn] into dp[srn]
  687. // dp[] is zeroed prior to computing the sum.
  688. T_t* cmVOT_SumMN( const T_t* sp, unsigned srn, unsigned scn, T_t* dp );
  689. //======================================================================================================================
  690. //)
  691. //( { label:"Min/max/median/mode" desc:"Simple descriptive statistics." kw:[vop] }
  692. T_t cmVOT_Median( const T_t* sp, unsigned sn );
  693. unsigned cmVOT_MinIndex( const T_t* sp, unsigned sn, unsigned stride );
  694. unsigned cmVOT_MaxIndex( const T_t* sp, unsigned sn, unsigned stride );
  695. T_t cmVOT_Min( const T_t* sp, unsigned sn, unsigned stride );
  696. T_t cmVOT_Max( const T_t* sp, unsigned sn, unsigned stride );
  697. T_t* cmVOT_MinVV( T_t* dp, unsigned dn, const T_t* sp );
  698. T_t* cmVOT_MaxVV( T_t* dp, unsigned dn, const T_t* sp );
  699. // Return index of max/min value into dp[scn] of each column of sp[srn,scn]
  700. unsigned* cmVOT_MinIndexM( unsigned* dp, const T_t* sp, unsigned srn, unsigned scn );
  701. unsigned* cmVOT_MaxIndexM( unsigned* dp, const T_t* sp, unsigned srn, unsigned scn );
  702. // Return the most frequently occuring element in sp.
  703. T_t cmVOT_Mode( const T_t* sp, unsigned sn );
  704. //======================================================================================================================
  705. //)
  706. //( { label:"Compare/Find" desc:"Compare, find, replace and count elements in a vector." kw:[vop] }
  707. // Return true if s0p[sn] is equal to s1p[sn]
  708. bool cmVOT_IsEqual( const T_t* s0p, const T_t* s1p, unsigned sn );
  709. // Return true if all elements of s0p[sn] are within 'eps' of s1p[sn].
  710. // This function is based on cmMath.h:cmIsCloseX()
  711. bool cmVOT_IsClose( const T_t* s0p, const T_t* s1p, unsigned sn, double eps );
  712. // Replace all values <= lteKeyVal with replaceVal. sp==dp is legal.
  713. T_t* cmVOT_ReplaceLte( T_t* dp, unsigned dn, const T_t* sp, T_t lteKeyVal, T_t replaceVal );
  714. // Return the index of 'key' in sp[sn] or cmInvalidIdx if 'key' does not exist.
  715. unsigned cmVOT_Find( const T_t* sp, unsigned sn, T_t key );
  716. // Count the number of times 'key' occurs in sp[sn].
  717. unsigned cmVOT_Count(const T_t* sp, unsigned sn, T_t key );
  718. //======================================================================================================================
  719. //)
  720. //( { label:"Absolute value" desc:"Absolute value and signal rectification." kw:[vop] }
  721. T_t* cmVOT_Abs( T_t* dbp, unsigned dn );
  722. // Half wave rectify the source vector.
  723. // dbp[] = sbp<0 .* sbp
  724. // Overlapping the source and dest is allowable as long as dbp <= sbp.
  725. T_t* cmVOT_HalfWaveRectify(T_t* dbp, unsigned dn, const T_t* sp );
  726. //======================================================================================================================
  727. //)
  728. //( { label:"Filter" desc:"Apply filtering to a vector taking into account vector begin/end conditions." kw:[vop] }
  729. // Apply a median or other filter of order wndN to xV[xN] and store the result in yV[xN].
  730. // When the window goes off either side of the vector the window is shortened.
  731. // This algorithm produces the same result as the fn_thresh function in MATLAB fv codebase.
  732. void cmVOT_FnThresh( const T_t* xV, unsigned xN, unsigned wndN, T_t* yV, unsigned yStride, T_t (*fnPtr)(const T_t*, unsigned) );
  733. // Apply a median filter of order wndN to xV[xN] and store the result in yV[xN].
  734. // When the window goes off either side of the vector the missing elements are considered
  735. // to be 0.
  736. // This algorithm produces the same result as the MATLAB medfilt1() function.
  737. void cmVOT_MedianFilt( const T_t* xV, unsigned xN, unsigned wndN, T_t* yV, unsigned yStride );
  738. //======================================================================================================================
  739. //)
  740. //( { label:"Edit distance" desc:"Calculate the Levenshtein edit distance between vectors." kw:[vop] }
  741. // Allocate and initialize a matrix for use by LevEditDist().
  742. // This matrix can be released with a call to cmMemFree().
  743. unsigned* cmVOT_LevEditDistAllocMtx(unsigned mtxMaxN);
  744. // Return the Levenshtein edit distance between two vectors.
  745. // m must point to a matrix pre-allocated by cmVOT_InitiLevEditDistMtx(maxN).
  746. double cmVOT_LevEditDist(unsigned mtxMaxN, unsigned* m, const T_t* s0, int n0, const T_t* s1, int n1, unsigned maxN );
  747. // Return the Levenshtein edit distance between two vectors.
  748. // Edit distance with a max cost threshold. This version of the algorithm
  749. // will run faster than LevEditDist() because it will stop execution as soon
  750. // as the distance exceeds 'maxCost'.
  751. // 'maxCost' must be between 0.0 and 1.0 or it is forced into this range.
  752. // The maximum distance returned will be 'maxCost'.
  753. // m must point to a matrix pre-allocated by cmVOT_InitiLevEditDistMtx(maxN).
  754. double cmVOT_LevEditDistWithCostThresh( int mtxMaxN, unsigned* m, const T_t* s0, int n0, const T_t* s1, int n1, double maxCost, unsigned maxN );
  755. //======================================================================================================================
  756. //)