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.

cmProc5.h 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. } cmGoldSig_t;
  66. cmGoldSig_t* cmGoldSigAlloc( cmCtx* ctx, cmGoldSig_t* p, const cmGoldSigArg_t* a );
  67. cmRC_t cmGoldSigFree( cmGoldSig_t** pp );
  68. cmRC_t cmGoldSigInit( cmGoldSig_t* p, const cmGoldSigArg_t* a );
  69. cmRC_t cmGoldSigFinal( cmGoldSig_t* p );
  70. cmRC_t cmGoldSigWrite( cmCtx* ctx, cmGoldSig_t* p, const char* fn );
  71. // Generate a signal consisting of underlying white noise with
  72. // bsiN repeated copies of the id signal associated with
  73. // channel 'chIdx'. Each discrete id signal copy is separated by 'dsN' samples.
  74. // The signal will be prefixed with 'prefixN' samples of silence (noise).
  75. // On return sets 'yVRef' to point to the generated signal and 'yNRef'
  76. // to the count of samples in 'yVRef'.
  77. // On error sets yVRef to NULL and yNRef to zero.
  78. // The vector returned in 'yVRef' should be freed via atMemFree().
  79. // On return sets bsiV[bsiN] to the onset sample index of each id signal copy.
  80. // The background noise signal is limited to the range -noiseGain to noiseGain.
  81. cmRC_t cmGoldSigGen(
  82. cmGoldSig_t* p,
  83. unsigned chIdx,
  84. unsigned prefixN,
  85. unsigned dsN,
  86. unsigned *bsiV,
  87. unsigned bsiN,
  88. double noiseGain,
  89. cmSample_t** yVRef,
  90. unsigned* yNRef );
  91. cmRC_t cmGoldSigTest( cmCtx* ctx );
  92. //=======================================================================================================================
  93. // Phase aligned transform generalized cross correlator
  94. //
  95. // Flags for use with the 'flags' argument to cmPhatAlloc()
  96. enum
  97. {
  98. kNoFlagsAtPhatFl= 0x00,
  99. kDebugAtPhatFl = 0x01, // generate debugging file
  100. kHannAtPhatFl = 0x02 // apply a hann window function to the id/audio signals prior to correlation.
  101. };
  102. typedef struct
  103. {
  104. cmObj obj;
  105. cmFftSR fft;
  106. cmIFftRS ifft;
  107. float alpha;
  108. unsigned flags;
  109. cmComplexR_t* fhM; // fhM[fhN,chN] FT of each id signal stored in complex form
  110. float* mhM; // mhM[binN,chN] magnitude of each fhM column
  111. unsigned chN; // count of id signals
  112. unsigned fhN; // length of each FT id signal (fft->xN)
  113. unsigned binN; // length of each mhM column (fft->xN/2);
  114. unsigned hN; // length of each time domain id signal (hN<=fhN/2)
  115. unsigned absIdx; // abs. sample index of p->di
  116. cmSample_t* dV; // dV[fhN] delay line
  117. unsigned di; // next input into delay line
  118. cmSample_t* xV; // xV[fhN] linear delay buffer
  119. cmComplexR_t* t0V; // t0V[fhN]
  120. cmComplexR_t* t1V; // t1V[fhN]
  121. cmSample_t* wndV;
  122. cmVectArray_t* ftVa;
  123. } cmPhat_t;
  124. // Allocate a PHAT based multi-channel correlator.
  125. // 'chN' is the maximum count of id signals to be set via cmPhatSetId().
  126. // 'hN' is the the length of the id signal in samples.
  127. // 'alpha' weight used to emphasize the frequencies where the id signal contains energy.
  128. // 'mult' * 'hN' is the correlation length (fhN)
  129. // 'flags' See kDebugAtPhatFl and kWndAtPhatFl.
  130. cmPhat_t* cmPhatAlloc( cmCtx* ctx, cmPhat_t* p, unsigned chN, unsigned hN, float alpha, unsigned mult, unsigned flags );
  131. cmRC_t cmPhatFree( cmPhat_t** pp );
  132. cmRC_t cmPhatInit( cmPhat_t* p, unsigned chN, unsigned hN, float alpha, unsigned mult, unsigned flags );
  133. cmRC_t cmPhatFinal( cmPhat_t* p );
  134. // Zero the audio delay line and reset the current input sample (di)
  135. // and absolute time index (absIdx) to 0.
  136. cmRC_t cmPhatReset( cmPhat_t* p );
  137. // Register an id signal with the correlator.
  138. cmRC_t cmPhatSetId( cmPhat_t* p, unsigned chIdx, const cmSample_t* hV, unsigned hN );
  139. // Update the correlators internal delay buffer.
  140. cmRC_t cmPhatExec( cmPhat_t* p, const cmSample_t* xV, unsigned xN );
  141. // Set p->xV[0:fhN-1] to the correlation function based on
  142. // correlation between the current audio delay line d[] and
  143. // the id signal in fhM[:,chIdx].
  144. // 'sessionId' and 'roleId' are only used to label the
  145. // data stored in the debug file and may be set to any
  146. // arbitrary value if the debug files are not being generated.
  147. void cmPhatChExec(
  148. cmPhat_t* p,
  149. unsigned chIdx,
  150. unsigned sessionId,
  151. unsigned roleId);
  152. cmRC_t cmPhatWrite( cmPhat_t* p, const char* dirStr );
  153. //=======================================================================================================================
  154. //
  155. //
  156. typedef struct
  157. {
  158. cmObj obj;
  159. cmGoldSig_t* gs;
  160. unsigned xi; // index into gs->ch[0].mdV[] of the next sample to output
  161. bool zeroFl;
  162. cmPhat_t* phat;
  163. cmVectArray_t* va;
  164. } cmReflectCalc_t;
  165. cmReflectCalc_t* cmReflectCalcAlloc( cmCtx* ctx, cmReflectCalc_t* p, const cmGoldSigArg_t* gsa, float phat_alpha, unsigned phat_mult );
  166. cmRC_t cmReflectCalcFree( cmReflectCalc_t** pp );
  167. cmRC_t cmReflectCalcInit( cmReflectCalc_t* p, const cmGoldSigArg_t* gsa, float phat_alpha, unsigned phat_mult );
  168. cmRC_t cmReflectCalcFinal( cmReflectCalc_t* p );
  169. cmRC_t cmReflectCalcExec( cmReflectCalc_t* p, const cmSample_t xV, cmSample_t* yV, unsigned xyN );
  170. #ifdef __cplusplus
  171. }
  172. #endif
  173. #endif