libcm is a C development framework with an emphasis on audio signal processing applications.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

cmProc5.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #ifndef cmProc5_h
  2. #define cmProc5_h
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. //( { file_desc:"Process Library 5", kw:[proclib]}
  7. //)
  8. //( { label:cmGoertzel file_desc:"Goertzel tone detection filter." kw:[proc]}
  9. typedef struct
  10. {
  11. double s0;
  12. double s1;
  13. double s2;
  14. double coeff;
  15. double hz;
  16. } cmGoertzelCh;
  17. struct cmShiftBuf_str;
  18. typedef struct cmGoertzel_str
  19. {
  20. cmObj obj;
  21. cmGoertzelCh* ch;
  22. unsigned chCnt;
  23. double srate;
  24. struct cmShiftBuf_str* shb;
  25. cmSample_t* wnd;
  26. } cmGoertzel;
  27. cmGoertzel* cmGoertzelAlloc( cmCtx* c, cmGoertzel* p, double srate, const double* fcHzV, unsigned chCnt, unsigned procSmpCnt, unsigned hopSmpCnt, unsigned wndSmpCnt );
  28. cmRC_t cmGoertzelFree( cmGoertzel** pp );
  29. cmRC_t cmGoertzelInit( cmGoertzel* p, double srate, const double* fcHzV, unsigned chCnt, unsigned procSmpCnt, unsigned hopSmpCnt, unsigned wndSmpCnt );
  30. cmRC_t cmGoertzelFinal( cmGoertzel* p );
  31. cmRC_t cmGoertzelSetFcHz( cmGoertzel* p, unsigned chIdx, double hz );
  32. cmRC_t cmGoertzelExec( cmGoertzel* p, const cmSample_t* in, unsigned procSmpCnt, double* outV, unsigned chCnt );
  33. //------------------------------------------------------------------------------------------------------------
  34. //)
  35. //( { label:cmGoldCode file_desc:"Gold code random generator." kw:[proc]}
  36. typedef struct
  37. {
  38. unsigned chN; // count of channels (each channel has a unique id)
  39. double srate; // system sample rate (samples/second)
  40. unsigned lfsrN; // linear feedback shift register (LFSR) length used to form Gold codes
  41. unsigned mlsCoeff0; // LFSR coeff. 0
  42. unsigned mlsCoeff1; // LFSR coeff. 1
  43. unsigned samplesPerChip; // samples per spreading code bit
  44. double rcosBeta; // raised cosine impulse response beta coeff.
  45. unsigned rcosOSFact; // raised cosine impulse response oversample factor
  46. double carrierHz; // carrier frequency
  47. double envMs; // attack/decay envelope duration
  48. } cmGoldSigArg_t;
  49. typedef struct
  50. {
  51. int* pnV; // pnV[ mlsN ] spread code (aliased from pnM[:,i])
  52. cmSample_t* bbV; // bbV[ sigN ] baseband signal at audio rate
  53. cmSample_t* mdV; // mdV[ sigN ] modulated signal at audio rate
  54. } cmGoldSigCh_t;
  55. typedef struct
  56. {
  57. cmObj obj; //
  58. cmGoldSigArg_t a; // argument record
  59. cmGoldSigCh_t* ch; // ch[ chN ] channel array
  60. int* pnM; // pnM[mlsN,chN] (aliased to ch[].pnV)
  61. cmSample_t* rcosV; // rcosV[rcosN] raised cosine impulse response
  62. unsigned rcosN; // length of raised cosine impulse response
  63. unsigned mlsN; // length of Gold codes (Maximum length sequence length)
  64. unsigned sigN; // length of channel signals bbV[] and mdV[]
  65. cmFIR* fir;
  66. } cmGoldSig_t;
  67. cmGoldSig_t* cmGoldSigAlloc( cmCtx* ctx, cmGoldSig_t* p, const cmGoldSigArg_t* a );
  68. cmRC_t cmGoldSigFree( cmGoldSig_t** pp );
  69. cmRC_t cmGoldSigInit( cmGoldSig_t* p, const cmGoldSigArg_t* a );
  70. cmRC_t cmGoldSigFinal( cmGoldSig_t* p );
  71. cmRC_t cmGoldSigWrite( cmCtx* ctx, cmGoldSig_t* p, const char* fn );
  72. // Generate a signal consisting of underlying white noise with
  73. // bsiN repeated copies of the id signal associated with
  74. // channel 'chIdx'. Each discrete id signal copy is separated by 'dsN' samples.
  75. // The signal will be prefixed with 'prefixN' samples of silence (noise).
  76. // On return sets 'yVRef' to point to the generated signal and 'yNRef'
  77. // to the count of samples in 'yVRef'.
  78. // On error sets yVRef to NULL and yNRef to zero.
  79. // The vector returned in 'yVRef' should be freed via atMemFree().
  80. // On return sets bsiV[bsiN] to the onset sample index of each id signal copy.
  81. // The background noise signal is limited to the range -noiseGain to noiseGain.
  82. cmRC_t cmGoldSigGen(
  83. cmGoldSig_t* p,
  84. unsigned chIdx,
  85. unsigned prefixN,
  86. unsigned dsN,
  87. unsigned *bsiV,
  88. unsigned bsiN,
  89. double noiseGain,
  90. cmSample_t** yVRef,
  91. unsigned* yNRef );
  92. cmRC_t cmGoldSigTest( cmCtx* ctx );
  93. //------------------------------------------------------------------------------------------------------------
  94. //)
  95. //( { label:cmPhat file_desc:"Phase-aligned transform for generalized cross correlator." kw:[proc]}
  96. // Flags for use with the 'flags' argument to cmPhatAlloc()
  97. enum
  98. {
  99. kNoFlagsAtPhatFl= 0x00,
  100. kDebugAtPhatFl = 0x01, // generate debugging file
  101. kHannAtPhatFl = 0x02 // apply a hann window function to the id/audio signals prior to correlation.
  102. };
  103. typedef struct
  104. {
  105. cmObj obj;
  106. cmFftSR fft;
  107. cmIFftRS ifft;
  108. float alpha;
  109. unsigned flags;
  110. cmComplexR_t* fhM; // fhM[fhN,chN] FT of each id signal stored in complex form
  111. float* mhM; // mhM[binN,chN] magnitude of each fhM column
  112. unsigned chN; // count of id signals
  113. unsigned fhN; // length of each FT id signal (fft->xN)
  114. unsigned binN; // length of each mhM column (fft->xN/2);
  115. unsigned hN; // length of each time domain id signal (hN<=fhN/2)
  116. unsigned absIdx; // abs. sample index of p->di
  117. cmSample_t* dV; // dV[fhN] delay line
  118. unsigned di; // next input into delay line
  119. cmSample_t* xV; // xV[fhN] linear delay buffer
  120. cmComplexR_t* t0V; // t0V[fhN]
  121. cmComplexR_t* t1V; // t1V[fhN]
  122. cmSample_t* wndV;
  123. cmVectArray_t* ftVa;
  124. } cmPhat_t;
  125. // Allocate a PHAT based multi-channel correlator.
  126. // 'chN' is the maximum count of id signals to be set via cmPhatSetId().
  127. // 'hN' is the the length of the id signal in samples.
  128. // 'alpha' weight used to emphasize the frequencies where the id signal contains energy.
  129. // 'mult' * 'hN' is the correlation length (fhN)
  130. // 'flags' See kDebugAtPhatFl and kWndAtPhatFl.
  131. cmPhat_t* cmPhatAlloc( cmCtx* ctx, cmPhat_t* p, unsigned chN, unsigned hN, float alpha, unsigned mult, unsigned flags );
  132. cmRC_t cmPhatFree( cmPhat_t** pp );
  133. cmRC_t cmPhatInit( cmPhat_t* p, unsigned chN, unsigned hN, float alpha, unsigned mult, unsigned flags );
  134. cmRC_t cmPhatFinal( cmPhat_t* p );
  135. // Zero the audio delay line and reset the current input sample (di)
  136. // and absolute time index (absIdx) to 0.
  137. cmRC_t cmPhatReset( cmPhat_t* p );
  138. // Register an id signal with the correlator.
  139. cmRC_t cmPhatSetId( cmPhat_t* p, unsigned chIdx, const cmSample_t* hV, unsigned hN );
  140. // Update the correlators internal delay buffer.
  141. cmRC_t cmPhatExec( cmPhat_t* p, const cmSample_t* xV, unsigned xN );
  142. // Set p->xV[0:fhN-1] to the correlation function based on
  143. // correlation between the current audio delay line d[] and
  144. // the id signal in fhM[:,chIdx].
  145. // 'sessionId' and 'roleId' are only used to label the
  146. // data stored in the debug file and may be set to any
  147. // arbitrary value if the debug files are not being generated.
  148. void cmPhatChExec(
  149. cmPhat_t* p,
  150. unsigned chIdx,
  151. unsigned sessionId,
  152. unsigned roleId);
  153. cmRC_t cmPhatWrite( cmPhat_t* p, const char* dirStr );
  154. //------------------------------------------------------------------------------------------------------------
  155. //)
  156. //( { label:cmReflectCal file_desc:"Calculate the time of flight of Gold code acoustic reflections." kw:[proc]}
  157. typedef struct
  158. {
  159. cmObj obj;
  160. cmGoldSig_t* gs;
  161. cmPhat_t* phat;
  162. unsigned xi; // index into xV[] of the next sample to output
  163. unsigned t;
  164. unsigned* t0V; // t0V[tN] - last tN signal start times
  165. unsigned* t1V; // t1V[tN] - last tN signal detection times
  166. unsigned tN;
  167. unsigned ti;
  168. cmVectArray_t* phVa;
  169. cmVectArray_t* xVa;
  170. cmVectArray_t* yVa;
  171. } cmReflectCalc_t;
  172. cmReflectCalc_t* cmReflectCalcAlloc( cmCtx* ctx, cmReflectCalc_t* p, const cmGoldSigArg_t* gsa, float phat_alpha, unsigned phat_mult );
  173. cmRC_t cmReflectCalcFree( cmReflectCalc_t** pp );
  174. cmRC_t cmReflectCalcInit( cmReflectCalc_t* p, const cmGoldSigArg_t* gsa, float phat_alpha, unsigned phat_mult );
  175. cmRC_t cmReflectCalcFinal( cmReflectCalc_t* p );
  176. cmRC_t cmReflectCalcExec( cmReflectCalc_t* p, const cmSample_t* xV, cmSample_t* yV, unsigned xyN );
  177. cmRC_t cmReflectCalcWrite( cmReflectCalc_t* p, const char* dirStr );
  178. //------------------------------------------------------------------------------------------------------------
  179. //)
  180. //( { label:cmNlms file_desc:"Normalized least mean squares echo canceller." kw:[proc]}
  181. typedef struct
  182. {
  183. cmObj obj;
  184. float mu; // LMS step rate
  185. unsigned hN; // filter length
  186. unsigned delayN; // fixed delay to apply to align xV with fV.
  187. unsigned dN; // max length of the fixed delay
  188. cmSample_t* delayV; // delayV[ dN ] fixed delay buffer[]
  189. unsigned di; // delay index
  190. double* wV; // wV[hN] filter weights
  191. double* hV; // hV[hN] filter delay line
  192. unsigned w0i; // The index into hV[] of the start of the delay line.
  193. cmVectArray_t* uVa;
  194. cmVectArray_t* fVa;
  195. cmVectArray_t* eVa;
  196. } cmNlmsEc_t;
  197. cmNlmsEc_t* cmNlmsEcAlloc( cmCtx* ctx, cmNlmsEc_t* p, double srate, float mu, unsigned hN, unsigned delayN );
  198. cmRC_t cmNlmsEcFree( cmNlmsEc_t** pp );
  199. cmRC_t cmNlmsEcInit( cmNlmsEc_t* p, double srate, float mu, unsigned hN, unsigned delayN );
  200. cmRC_t cmNlmsEcFinal( cmNlmsEc_t* p );
  201. // xV[] unfiltered reference signal (direct from xform output)
  202. // fV[] filtered reference signal (from mic)
  203. // yV[] echo-canelled signal
  204. cmRC_t cmNlmsEcExec( cmNlmsEc_t* p, const cmSample_t* xV, const cmSample_t* fV, cmSample_t* yV, unsigned xyN );
  205. cmRC_t cmNlmsEcWrite( cmNlmsEc_t* p, const cmChar_t* dir );
  206. void cmNlmsEcSetMu( cmNlmsEc_t* p, float mu );
  207. void cmNlmsEcSetDelayN( cmNlmsEc_t* p, unsigned delayN );
  208. void cmNlmsEcSetIrN( cmNlmsEc_t* p, unsigned irN );
  209. //------------------------------------------------------------------------------------------------------------
  210. //)
  211. //( { label:cmSeqAlign file_desc:"Align two sequences where each sequence is composed of records with varying numbers of features." kw:[proc]}
  212. typedef struct cmSeqAlignLoc_str
  213. {
  214. unsigned id; // location id
  215. unsigned vN; //
  216. unsigned* vV; // vV[vN]
  217. struct cmSeqAlignLoc_str* link;
  218. } cmSeqAlignLoc_t;
  219. typedef struct cmSeqAlignSeq_str
  220. {
  221. unsigned id;
  222. cmSeqAlignLoc_t* locL;
  223. struct cmSeqAlignSeq_str* link;
  224. } cmSeqAlignSeq_t;
  225. typedef struct
  226. {
  227. cmObj obj;
  228. cmSeqAlignSeq_t* seqL;
  229. } cmSeqAlign_t;
  230. cmSeqAlign_t* cmSeqAlignAlloc( cmCtx* ctx, cmSeqAlign_t* p );
  231. cmRC_t cmSeqAlignFree( cmSeqAlign_t** p );
  232. cmRC_t cmSeqAlignInit( cmSeqAlign_t* p );
  233. cmRC_t cmSeqAlignFinal( cmSeqAlign_t* p );
  234. cmRC_t cmSeqAlignInsert( cmSeqAlign_t* p, unsigned seqId, unsigned locId, unsigned value );
  235. cmRC_t cmSeqAlignExec( cmSeqAlign_t* p );
  236. void cmSeqAlignReport( cmSeqAlign_t* p, cmRpt_t* rpt );
  237. //------------------------------------------------------------------------------------------------------------
  238. //)
  239. #ifdef __cplusplus
  240. }
  241. #endif
  242. #endif