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.

cmVectOpsDocOut.h 52KB

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