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.

cmProc5.h 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #ifndef cmProc5_h
  2. #define cmProc5_h
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. //=======================================================================================================================
  7. // Goertzel Filter
  8. //
  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. // Gold Code Signal Generator
  35. //
  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. // Phase aligned transform generalized cross correlator
  95. //
  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. //
  157. typedef struct
  158. {
  159. cmObj obj;
  160. cmGoldSig_t* gs;
  161. unsigned xi; // index into gs->ch[0].mdV[] of the next sample to output
  162. bool zeroFl;
  163. cmPhat_t* phat;
  164. cmVectArray_t* phVa;
  165. cmVectArray_t* xVa;
  166. cmVectArray_t* yVa;
  167. } cmReflectCalc_t;
  168. cmReflectCalc_t* cmReflectCalcAlloc( cmCtx* ctx, cmReflectCalc_t* p, const cmGoldSigArg_t* gsa, float phat_alpha, unsigned phat_mult );
  169. cmRC_t cmReflectCalcFree( cmReflectCalc_t** pp );
  170. cmRC_t cmReflectCalcInit( cmReflectCalc_t* p, const cmGoldSigArg_t* gsa, float phat_alpha, unsigned phat_mult );
  171. cmRC_t cmReflectCalcFinal( cmReflectCalc_t* p );
  172. cmRC_t cmReflectCalcExec( cmReflectCalc_t* p, const cmSample_t* xV, cmSample_t* yV, unsigned xyN );
  173. cmRC_t cmReflectCalcWrite( cmReflectCalc_t* p, const char* dirStr );
  174. //=======================================================================================================================
  175. //
  176. //
  177. typedef struct
  178. {
  179. cmObj obj;
  180. float mu; // LMS step rate
  181. unsigned hN; // filter length
  182. unsigned delayN; // fixed delay to apply to align xV with fV.
  183. cmSample_t* dV; // delay line
  184. cmSample_t* wV; // wV[hN] filter weights
  185. cmSample_t* hV; // hV[hN] filter delay line
  186. unsigned w0i;
  187. cmVectArray_t* eVa;
  188. } cmNlmsEc_t;
  189. cmNlmsEc_t* cmNlmsEcAlloc( cmCtx* ctx, cmNlmsEc_t* p, float mu, unsigned hN, unsigned delayN );
  190. cmRC_t cmNlmsEcFree( cmNlmsEc_t** pp );
  191. cmRC_t cmNlmsEcInit( cmNlmsEc_t* p, float mu, unsigned hN, unsigned delayN );
  192. cmRC_t cmNlmsEcFinal( cmNlmsEc_t* p );
  193. // xV[] unfiltered reference signal (direct from xform output)
  194. // fV[] filtered reference signal (from mic)
  195. // yV[] echo-canelled signal
  196. cmRC_t cmNlmsEcExec( cmNlmsEc_t* p, const cmSample_t* xV, const cmSample_t* fV, cmSample_t* yV, unsigned xyN );
  197. cmRC_t cmNlmsEcWrite( cmNlmsEc_t* p, const cmChar_t* dir );
  198. #ifdef __cplusplus
  199. }
  200. #endif
  201. #endif