mdf.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /* Copyright (C) 2003-2006 Jean-Marc Valin
  2. File: mdf.c
  3. Echo canceller based on the MDF algorithm (see below)
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. 1. Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. 3. The name of the author may not be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  18. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  20. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  22. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  23. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /*
  27. The echo canceller is based on the MDF algorithm described in:
  28. J. S. Soo, K. K. Pang Multidelay block frequency adaptive filter,
  29. IEEE Trans. Acoust. Speech Signal Process., Vol. ASSP-38, No. 2,
  30. February 1990.
  31. We use the Alternatively Updated MDF (AUMDF) variant. Robustness to
  32. double-talk is achieved using a variable learning rate as described in:
  33. Valin, J.-M., On Adjusting the Learning Rate in Frequency Domain Echo
  34. Cancellation With Double-Talk. Submitted to IEEE Transactions on Speech
  35. and Audio Processing, 2006.
  36. There is no explicit double-talk detection, but a continuous variation
  37. in the learning rate based on residual echo, double-talk and background
  38. noise.
  39. About the fixed-point version:
  40. All the signals are represented with 16-bit words. The filter weights
  41. are represented with 32-bit words, but only the top 16 bits are used
  42. in most cases. The lower 16 bits are completely unreliable (due to the
  43. fact that the update is done only on the top bits), but help in the
  44. adaptation -- probably by removing a "threshold effect" due to
  45. quantization (rounding going to zero) when the gradient is small.
  46. Another kludge that seems to work good: when performing the weight
  47. update, we only move half the way toward the "goal" this seems to
  48. reduce the effect of quantization noise in the update phase. This
  49. can be seen as applying a gradient descent on a "soft constraint"
  50. instead of having a hard constraint.
  51. */
  52. #ifdef HAVE_CONFIG_H
  53. #include "config.h"
  54. #endif
  55. #include "misc.h"
  56. #include "speex/speex_echo.h"
  57. #include "fftwrap.h"
  58. #include "pseudofloat.h"
  59. #include "math_approx.h"
  60. #ifndef M_PI
  61. #define M_PI 3.14159265358979323846
  62. #endif
  63. #define min(a,b) ((a)<(b) ? (a) : (b))
  64. #define max(a,b) ((a)>(b) ? (a) : (b))
  65. #ifdef FIXED_POINT
  66. #define WEIGHT_SHIFT 11
  67. #define NORMALIZE_SCALEDOWN 5
  68. #define NORMALIZE_SCALEUP 3
  69. #else
  70. #define WEIGHT_SHIFT 0
  71. #endif
  72. #ifdef FIXED_POINT
  73. static const spx_float_t MIN_LEAK = ((spx_float_t){16777, -24});
  74. #define TOP16(x) ((x)>>16)
  75. #else
  76. static const spx_float_t MIN_LEAK = .001f;
  77. #define TOP16(x) (x)
  78. #endif
  79. /** Speex echo cancellation state. */
  80. struct SpeexEchoState_ {
  81. int frame_size; /**< Number of samples processed each time */
  82. int window_size;
  83. int M;
  84. int cancel_count;
  85. int adapted;
  86. spx_int32_t sampling_rate;
  87. spx_word16_t spec_average;
  88. spx_word16_t beta0;
  89. spx_word16_t beta_max;
  90. spx_word32_t sum_adapt;
  91. spx_word16_t *e;
  92. spx_word16_t *x;
  93. spx_word16_t *X;
  94. spx_word16_t *d;
  95. spx_word16_t *y;
  96. spx_word16_t *last_y;
  97. spx_word32_t *Yps;
  98. spx_word16_t *Y;
  99. spx_word16_t *E;
  100. spx_word32_t *PHI;
  101. spx_word32_t *W;
  102. spx_word32_t *power;
  103. spx_float_t *power_1;
  104. spx_word16_t *wtmp;
  105. #ifdef FIXED_POINT
  106. spx_word16_t *wtmp2;
  107. #endif
  108. spx_word32_t *Rf;
  109. spx_word32_t *Yf;
  110. spx_word32_t *Xf;
  111. spx_word32_t *Eh;
  112. spx_word32_t *Yh;
  113. spx_float_t Pey;
  114. spx_float_t Pyy;
  115. spx_word16_t *window;
  116. void *fft_table;
  117. spx_word16_t memX, memD, memE;
  118. spx_word16_t preemph;
  119. spx_word16_t notch_radius;
  120. spx_mem_t notch_mem[2];
  121. };
  122. static inline void filter_dc_notch16(spx_int16_t *in, spx_word16_t radius, spx_word16_t *out, int len, spx_mem_t *mem)
  123. {
  124. int i;
  125. spx_word16_t den2;
  126. #ifdef FIXED_POINT
  127. den2 = MULT16_16_Q15(radius,radius) + MULT16_16_Q15(QCONST16(.7,15),MULT16_16_Q15(32767-radius,32767-radius));
  128. #else
  129. den2 = radius*radius + .7*(1-radius)*(1-radius);
  130. #endif
  131. /*printf ("%d %d %d %d %d %d\n", num[0], num[1], num[2], den[0], den[1], den[2]);*/
  132. for (i=0;i<len;i++)
  133. {
  134. spx_word16_t vin = in[i];
  135. spx_word32_t vout = mem[0] + SHL32(EXTEND32(vin),15);
  136. #ifdef FIXED_POINT
  137. mem[0] = mem[1] + SHL32(SHL32(-EXTEND32(vin),15) + MULT16_32_Q15(radius,vout),1);
  138. #else
  139. mem[0] = mem[1] + 2*(-vin + radius*vout);
  140. #endif
  141. mem[1] = SHL32(EXTEND32(vin),15) - MULT16_32_Q15(den2,vout);
  142. out[i] = SATURATE32(PSHR32(MULT16_32_Q15(radius,vout),15),32767);
  143. }
  144. }
  145. static inline spx_word32_t inner_prod(const spx_word16_t *x, const spx_word16_t *y, int len)
  146. {
  147. spx_word32_t sum=0;
  148. len >>= 2;
  149. while(len--)
  150. {
  151. spx_word32_t part=0;
  152. part = MAC16_16(part,*x++,*y++);
  153. part = MAC16_16(part,*x++,*y++);
  154. part = MAC16_16(part,*x++,*y++);
  155. part = MAC16_16(part,*x++,*y++);
  156. /* HINT: If you had a 40-bit accumulator, you could shift only at the end */
  157. sum = ADD32(sum,SHR32(part,6));
  158. }
  159. return sum;
  160. }
  161. /** Compute power spectrum of a half-complex (packed) vector */
  162. static inline void power_spectrum(spx_word16_t *X, spx_word32_t *ps, int N)
  163. {
  164. int i, j;
  165. ps[0]=MULT16_16(X[0],X[0]);
  166. for (i=1,j=1;i<N-1;i+=2,j++)
  167. {
  168. ps[j] = MULT16_16(X[i],X[i]) + MULT16_16(X[i+1],X[i+1]);
  169. }
  170. ps[j]=MULT16_16(X[i],X[i]);
  171. }
  172. /** Compute cross-power spectrum of a half-complex (packed) vectors and add to acc */
  173. #ifdef FIXED_POINT
  174. static inline void spectral_mul_accum(spx_word16_t *X, spx_word32_t *Y, spx_word16_t *acc, int N, int M)
  175. {
  176. int i,j;
  177. spx_word32_t tmp1=0,tmp2=0;
  178. for (j=0;j<M;j++)
  179. {
  180. tmp1 = MAC16_16(tmp1, X[j*N],TOP16(Y[j*N]));
  181. }
  182. acc[0] = PSHR32(tmp1,WEIGHT_SHIFT);
  183. for (i=1;i<N-1;i+=2)
  184. {
  185. tmp1 = tmp2 = 0;
  186. for (j=0;j<M;j++)
  187. {
  188. tmp1 = SUB32(MAC16_16(tmp1, X[j*N+i],TOP16(Y[j*N+i])), MULT16_16(X[j*N+i+1],TOP16(Y[j*N+i+1])));
  189. tmp2 = MAC16_16(MAC16_16(tmp2, X[j*N+i+1],TOP16(Y[j*N+i])), X[j*N+i], TOP16(Y[j*N+i+1]));
  190. }
  191. acc[i] = PSHR32(tmp1,WEIGHT_SHIFT);
  192. acc[i+1] = PSHR32(tmp2,WEIGHT_SHIFT);
  193. }
  194. tmp1 = tmp2 = 0;
  195. for (j=0;j<M;j++)
  196. {
  197. tmp1 = MAC16_16(tmp1, X[(j+1)*N-1],TOP16(Y[(j+1)*N-1]));
  198. }
  199. acc[N-1] = PSHR32(tmp1,WEIGHT_SHIFT);
  200. }
  201. #else
  202. static inline void spectral_mul_accum(spx_word16_t *X, spx_word32_t *Y, spx_word16_t *acc, int N, int M)
  203. {
  204. int i,j;
  205. for (i=0;i<N;i++)
  206. acc[i] = 0;
  207. for (j=0;j<M;j++)
  208. {
  209. acc[0] += X[0]*Y[0];
  210. for (i=1;i<N-1;i+=2)
  211. {
  212. acc[i] += (X[i]*Y[i] - X[i+1]*Y[i+1]);
  213. acc[i+1] += (X[i+1]*Y[i] + X[i]*Y[i+1]);
  214. }
  215. acc[i] += X[i]*Y[i];
  216. X += N;
  217. Y += N;
  218. }
  219. }
  220. #endif
  221. /** Compute weighted cross-power spectrum of a half-complex (packed) vector with conjugate */
  222. static inline void weighted_spectral_mul_conj(spx_float_t *w, spx_word16_t *X, spx_word16_t *Y, spx_word32_t *prod, int N)
  223. {
  224. int i, j;
  225. prod[0] = FLOAT_MUL32(w[0],MULT16_16(X[0],Y[0]));
  226. for (i=1,j=1;i<N-1;i+=2,j++)
  227. {
  228. prod[i] = FLOAT_MUL32(w[j],MAC16_16(MULT16_16(X[i],Y[i]), X[i+1],Y[i+1]));
  229. prod[i+1] = FLOAT_MUL32(w[j],MAC16_16(MULT16_16(-X[i+1],Y[i]), X[i],Y[i+1]));
  230. }
  231. prod[i] = FLOAT_MUL32(w[j],MULT16_16(X[i],Y[i]));
  232. }
  233. /** Creates a new echo canceller state */
  234. SpeexEchoState *speex_echo_state_init(int frame_size, int filter_length)
  235. {
  236. int i,N,M;
  237. SpeexEchoState *st = (SpeexEchoState *)speex_alloc(sizeof(SpeexEchoState));
  238. st->frame_size = frame_size;
  239. st->window_size = 2*frame_size;
  240. N = st->window_size;
  241. M = st->M = (filter_length+st->frame_size-1)/frame_size;
  242. st->cancel_count=0;
  243. st->sum_adapt = 0;
  244. /* FIXME: Make that an init option (new API call?) */
  245. st->sampling_rate = 8000;
  246. st->spec_average = DIV32_16(SHL32(st->frame_size, 15), st->sampling_rate);
  247. #ifdef FIXED_POINT
  248. st->beta0 = DIV32_16(SHL32(st->frame_size, 16), st->sampling_rate);
  249. st->beta_max = DIV32_16(SHL32(st->frame_size, 14), st->sampling_rate);
  250. #else
  251. st->beta0 = (2.0f*st->frame_size)/st->sampling_rate;
  252. st->beta_max = (.5f*st->frame_size)/st->sampling_rate;
  253. #endif
  254. st->fft_table = spx_fft_init(N);
  255. st->e = (spx_word16_t*)speex_alloc(N*sizeof(spx_word16_t));
  256. st->x = (spx_word16_t*)speex_alloc(N*sizeof(spx_word16_t));
  257. st->d = (spx_word16_t*)speex_alloc(N*sizeof(spx_word16_t));
  258. st->y = (spx_word16_t*)speex_alloc(N*sizeof(spx_word16_t));
  259. st->Yps = (spx_word32_t*)speex_alloc(N*sizeof(spx_word32_t));
  260. st->last_y = (spx_word16_t*)speex_alloc(N*sizeof(spx_word16_t));
  261. st->Yf = (spx_word32_t*)speex_alloc((st->frame_size+1)*sizeof(spx_word32_t));
  262. st->Rf = (spx_word32_t*)speex_alloc((st->frame_size+1)*sizeof(spx_word32_t));
  263. st->Xf = (spx_word32_t*)speex_alloc((st->frame_size+1)*sizeof(spx_word32_t));
  264. st->Yh = (spx_word32_t*)speex_alloc((st->frame_size+1)*sizeof(spx_word32_t));
  265. st->Eh = (spx_word32_t*)speex_alloc((st->frame_size+1)*sizeof(spx_word32_t));
  266. st->X = (spx_word16_t*)speex_alloc(M*N*sizeof(spx_word16_t));
  267. st->Y = (spx_word16_t*)speex_alloc(N*sizeof(spx_word16_t));
  268. st->E = (spx_word16_t*)speex_alloc(N*sizeof(spx_word16_t));
  269. st->W = (spx_word32_t*)speex_alloc(M*N*sizeof(spx_word32_t));
  270. st->PHI = (spx_word32_t*)speex_alloc(M*N*sizeof(spx_word32_t));
  271. st->power = (spx_word32_t*)speex_alloc((frame_size+1)*sizeof(spx_word32_t));
  272. st->power_1 = (spx_float_t*)speex_alloc((frame_size+1)*sizeof(spx_float_t));
  273. st->window = (spx_word16_t*)speex_alloc(N*sizeof(spx_word16_t));
  274. st->wtmp = (spx_word16_t*)speex_alloc(N*sizeof(spx_word16_t));
  275. #ifdef FIXED_POINT
  276. st->wtmp2 = (spx_word16_t*)speex_alloc(N*sizeof(spx_word16_t));
  277. for (i=0;i<N>>1;i++)
  278. {
  279. st->window[i] = (16383-SHL16(spx_cos(DIV32_16(MULT16_16(25736,i<<1),N)),1));
  280. st->window[N-i-1] = st->window[i];
  281. }
  282. #else
  283. for (i=0;i<N;i++)
  284. st->window[i] = .5-.5*cos(2*M_PI*i/N);
  285. #endif
  286. for (i=0;i<N*M;i++)
  287. {
  288. st->W[i] = st->PHI[i] = 0;
  289. }
  290. st->memX=st->memD=st->memE=0;
  291. st->preemph = QCONST16(.9,15);
  292. if (st->sampling_rate<12000)
  293. st->notch_radius = QCONST16(.9, 15);
  294. else if (st->sampling_rate<24000)
  295. st->notch_radius = QCONST16(.982, 15);
  296. else
  297. st->notch_radius = QCONST16(.992, 15);
  298. st->notch_mem[0] = st->notch_mem[1] = 0;
  299. st->adapted = 0;
  300. st->Pey = st->Pyy = FLOAT_ONE;
  301. return st;
  302. }
  303. /** Resets echo canceller state */
  304. void speex_echo_state_reset(SpeexEchoState *st)
  305. {
  306. int i, M, N;
  307. st->cancel_count=0;
  308. N = st->window_size;
  309. M = st->M;
  310. for (i=0;i<N*M;i++)
  311. {
  312. st->W[i] = 0;
  313. st->X[i] = 0;
  314. }
  315. for (i=0;i<=st->frame_size;i++)
  316. st->power[i] = 0;
  317. st->adapted = 0;
  318. st->sum_adapt = 0;
  319. st->Pey = st->Pyy = FLOAT_ONE;
  320. }
  321. /** Destroys an echo canceller state */
  322. void speex_echo_state_destroy(SpeexEchoState *st)
  323. {
  324. spx_fft_destroy(st->fft_table);
  325. speex_free(st->e);
  326. speex_free(st->x);
  327. speex_free(st->d);
  328. speex_free(st->y);
  329. speex_free(st->last_y);
  330. speex_free(st->Yps);
  331. speex_free(st->Yf);
  332. speex_free(st->Rf);
  333. speex_free(st->Xf);
  334. speex_free(st->Yh);
  335. speex_free(st->Eh);
  336. speex_free(st->X);
  337. speex_free(st->Y);
  338. speex_free(st->E);
  339. speex_free(st->W);
  340. speex_free(st->PHI);
  341. speex_free(st->power);
  342. speex_free(st->power_1);
  343. speex_free(st->window);
  344. speex_free(st->wtmp);
  345. #ifdef FIXED_POINT
  346. speex_free(st->wtmp2);
  347. #endif
  348. speex_free(st);
  349. }
  350. extern int fixed_point;
  351. /** Performs echo cancellation on a frame */
  352. void speex_echo_cancel(SpeexEchoState *st, short *ref, short *echo, short *out, spx_int32_t *Yout)
  353. {
  354. int i,j;
  355. int N,M;
  356. spx_word32_t Syy,See;
  357. spx_word16_t leak_estimate;
  358. spx_word16_t ss, ss_1;
  359. spx_float_t Pey = FLOAT_ONE, Pyy=FLOAT_ONE;
  360. spx_float_t alpha, alpha_1;
  361. spx_word16_t RER;
  362. spx_word32_t tmp32;
  363. spx_word16_t M_1;
  364. N = st->window_size;
  365. M = st->M;
  366. st->cancel_count++;
  367. #ifdef FIXED_POINT
  368. ss=DIV32_16(11469,M);
  369. ss_1 = SUB16(32767,ss);
  370. M_1 = DIV32_16(32767,M);
  371. #else
  372. ss=.35/M;
  373. ss_1 = 1-ss;
  374. M_1 = 1.f/M;
  375. #endif
  376. filter_dc_notch16(ref, st->notch_radius, st->d, st->frame_size, st->notch_mem);
  377. /* Copy input data to buffer */
  378. for (i=0;i<st->frame_size;i++)
  379. {
  380. spx_word16_t tmp;
  381. st->x[i] = st->x[i+st->frame_size];
  382. st->x[i+st->frame_size] = SUB16(echo[i], MULT16_16_P15(st->preemph, st->memX));
  383. st->memX = echo[i];
  384. tmp = st->d[i];
  385. st->d[i] = st->d[i+st->frame_size];
  386. st->d[i+st->frame_size] = SUB16(tmp, MULT16_16_P15(st->preemph, st->memD));
  387. st->memD = tmp;
  388. }
  389. /* Shift memory: this could be optimized eventually*/
  390. for (i=0;i<N*(M-1);i++)
  391. st->X[i]=st->X[i+N];
  392. /* Convert x (echo input) to frequency domain */
  393. spx_fft(st->fft_table, st->x, &st->X[(M-1)*N]);
  394. /* Compute filter response Y */
  395. spectral_mul_accum(st->X, st->W, st->Y, N, M);
  396. spx_ifft(st->fft_table, st->Y, st->y);
  397. #if 1
  398. spectral_mul_accum(st->X, st->PHI, st->Y, N, M);
  399. spx_ifft(st->fft_table, st->Y, st->e);
  400. #endif
  401. /* Compute error signal (for the output with de-emphasis) */
  402. for (i=0;i<st->frame_size;i++)
  403. {
  404. spx_word32_t tmp_out;
  405. #if 1
  406. spx_word16_t y = MULT16_16_Q15(st->window[i+st->frame_size],st->e[i+st->frame_size]) + MULT16_16_Q15(st->window[i],st->y[i+st->frame_size]);
  407. tmp_out = SUB32(EXTEND32(st->d[i+st->frame_size]), EXTEND32(y));
  408. #else
  409. tmp_out = SUB32(EXTEND32(st->d[i+st->frame_size]), EXTEND32(st->y[i+st->frame_size]));
  410. #endif
  411. /* Saturation */
  412. if (tmp_out>32767)
  413. tmp_out = 32767;
  414. else if (tmp_out<-32768)
  415. tmp_out = -32768;
  416. tmp_out = ADD32(tmp_out, EXTEND32(MULT16_16_P15(st->preemph, st->memE)));
  417. out[i] = tmp_out;
  418. st->memE = tmp_out;
  419. }
  420. /* Compute error signal (filter update version) */
  421. for (i=0;i<st->frame_size;i++)
  422. {
  423. st->e[i] = 0;
  424. st->e[i+st->frame_size] = st->d[i+st->frame_size] - st->y[i+st->frame_size];
  425. }
  426. /* Compute a bunch of correlations */
  427. See = inner_prod(st->e+st->frame_size, st->e+st->frame_size, st->frame_size);
  428. See = ADD32(See, SHR32(10000,6));
  429. Syy = inner_prod(st->y+st->frame_size, st->y+st->frame_size, st->frame_size);
  430. /* Convert error to frequency domain */
  431. spx_fft(st->fft_table, st->e, st->E);
  432. for (i=0;i<st->frame_size;i++)
  433. st->y[i] = 0;
  434. spx_fft(st->fft_table, st->y, st->Y);
  435. /* Compute power spectrum of echo (X), error (E) and filter response (Y) */
  436. power_spectrum(st->E, st->Rf, N);
  437. power_spectrum(st->Y, st->Yf, N);
  438. power_spectrum(&st->X[(M-1)*N], st->Xf, N);
  439. /* Smooth echo energy estimate over time */
  440. for (j=0;j<=st->frame_size;j++)
  441. st->power[j] = MULT16_32_Q15(ss_1,st->power[j]) + 1 + MULT16_32_Q15(ss,st->Xf[j]);
  442. /* Enable this to compute the power based only on the tail (would need to compute more
  443. efficiently to make this really useful */
  444. if (0)
  445. {
  446. float scale2 = .5f/M;
  447. for (j=0;j<=st->frame_size;j++)
  448. st->power[j] = 0;
  449. for (i=0;i<M;i++)
  450. {
  451. power_spectrum(&st->X[i*N], st->Xf, N);
  452. for (j=0;j<=st->frame_size;j++)
  453. st->power[j] += scale2*st->Xf[j];
  454. }
  455. }
  456. /* Compute filtered spectra and (cross-)correlations */
  457. for (j=st->frame_size;j>=0;j--)
  458. {
  459. spx_float_t Eh, Yh;
  460. Eh = PSEUDOFLOAT(st->Rf[j] - st->Eh[j]);
  461. Yh = PSEUDOFLOAT(st->Yf[j] - st->Yh[j]);
  462. Pey = FLOAT_ADD(Pey,FLOAT_MULT(Eh,Yh));
  463. Pyy = FLOAT_ADD(Pyy,FLOAT_MULT(Yh,Yh));
  464. #ifdef FIXED_POINT
  465. st->Eh[j] = MAC16_32_Q15(MULT16_32_Q15(SUB16(32767,st->spec_average),st->Eh[j]), st->spec_average, st->Rf[j]);
  466. st->Yh[j] = MAC16_32_Q15(MULT16_32_Q15(SUB16(32767,st->spec_average),st->Yh[j]), st->spec_average, st->Yf[j]);
  467. #else
  468. st->Eh[j] = (1-st->spec_average)*st->Eh[j] + st->spec_average*st->Rf[j];
  469. st->Yh[j] = (1-st->spec_average)*st->Yh[j] + st->spec_average*st->Yf[j];
  470. #endif
  471. }
  472. /* Compute correlation updatete rate */
  473. tmp32 = MULT16_32_Q15(st->beta0,Syy);
  474. if (tmp32 > MULT16_32_Q15(st->beta_max,See))
  475. tmp32 = MULT16_32_Q15(st->beta_max,See);
  476. alpha = FLOAT_DIV32(tmp32, See);
  477. alpha_1 = FLOAT_SUB(FLOAT_ONE, alpha);
  478. /* Update correlations (recursive average) */
  479. st->Pey = FLOAT_ADD(FLOAT_MULT(alpha_1,st->Pey) , FLOAT_MULT(alpha,Pey));
  480. st->Pyy = FLOAT_ADD(FLOAT_MULT(alpha_1,st->Pyy) , FLOAT_MULT(alpha,Pyy));
  481. if (FLOAT_LT(st->Pyy, FLOAT_ONE))
  482. st->Pyy = FLOAT_ONE;
  483. /* We don't really hope to get better than 33 dB (MIN_LEAK-3dB) attenuation anyway */
  484. if (FLOAT_LT(st->Pey, FLOAT_MULT(MIN_LEAK,st->Pyy)))
  485. st->Pey = FLOAT_MULT(MIN_LEAK,st->Pyy);
  486. if (FLOAT_GT(st->Pey, st->Pyy))
  487. st->Pey = st->Pyy;
  488. /* leak_estimate is the limear regression result */
  489. leak_estimate = FLOAT_EXTRACT16(FLOAT_SHL(FLOAT_DIVU(st->Pey, st->Pyy),14));
  490. if (leak_estimate > 16383)
  491. leak_estimate = 32767;
  492. else
  493. leak_estimate = SHL16(leak_estimate,1);
  494. /*printf ("%f\n", leak_estimate);*/
  495. /* Compute Residual to Error Ratio */
  496. #ifdef FIXED_POINT
  497. tmp32 = MULT16_32_Q15(leak_estimate,Syy);
  498. tmp32 = ADD32(tmp32, SHL32(tmp32,1));
  499. if (tmp32 > SHR32(See,1))
  500. tmp32 = SHR32(See,1);
  501. RER = FLOAT_EXTRACT16(FLOAT_SHL(FLOAT_DIV32(tmp32,See),15));
  502. #else
  503. RER = 3.*MULT16_32_Q15(leak_estimate,Syy) / See;
  504. if (RER > .5)
  505. RER = .5;
  506. #endif
  507. /* We consider that the filter has had minimal adaptation if the following is true*/
  508. if (!st->adapted && st->sum_adapt > QCONST32(1,15))
  509. {
  510. st->adapted = 1;
  511. }
  512. if (st->adapted)
  513. {
  514. for (i=0;i<=st->frame_size;i++)
  515. {
  516. spx_word32_t r, e;
  517. /* Compute frequency-domain adaptation mask */
  518. r = MULT16_32_Q15(leak_estimate,SHL32(st->Yf[i],3));
  519. e = SHL32(st->Rf[i],3)+1;
  520. #ifdef FIXED_POINT
  521. if (r>SHR32(e,1))
  522. r = SHR32(e,1);
  523. #else
  524. if (r>.5*e)
  525. r = .5*e;
  526. #endif
  527. r = MULT16_32_Q15(QCONST16(.8,15),r) + MULT16_32_Q15(QCONST16(.2,15),(spx_word32_t)(MULT16_32_Q15(RER,e)));
  528. /*st->power_1[i] = adapt_rate*r/(e*(1+st->power[i]));*/
  529. st->power_1[i] = FLOAT_SHL(FLOAT_DIV32_FLOAT(MULT16_32_Q15(M_1,r),FLOAT_MUL32U(e,st->power[i]+10)),WEIGHT_SHIFT+16);
  530. }
  531. } else {
  532. spx_word32_t Sxx;
  533. spx_word16_t adapt_rate=0;
  534. Sxx = inner_prod(st->x+st->frame_size, st->x+st->frame_size, st->frame_size);
  535. /* Temporary adaption rate if filter is not adapted correctly */
  536. tmp32 = MULT16_32_Q15(QCONST16(.15f, 15), Sxx);
  537. #ifdef FIXED_POINT
  538. if (Sxx > SHR32(See,2))
  539. Sxx = SHR32(See,2);
  540. #else
  541. if (Sxx > .25*See)
  542. Sxx = .25*See;
  543. #endif
  544. adapt_rate = FLOAT_EXTRACT16(FLOAT_SHL(FLOAT_DIV32(MULT16_32_Q15(M_1,Sxx), See),15));
  545. for (i=0;i<=st->frame_size;i++)
  546. st->power_1[i] = FLOAT_SHL(FLOAT_DIV32(EXTEND32(adapt_rate),ADD32(st->power[i],10)),WEIGHT_SHIFT+1);
  547. /* How much have we adapted so far? */
  548. st->sum_adapt = ADD32(st->sum_adapt,adapt_rate);
  549. }
  550. /* Compute weight gradient */
  551. for (j=0;j<M;j++)
  552. {
  553. weighted_spectral_mul_conj(st->power_1, &st->X[j*N], st->E, st->PHI+N*j, N);
  554. }
  555. /* Gradient descent */
  556. for (i=0;i<M*N;i++)
  557. {
  558. st->W[i] += st->PHI[i];
  559. /* Old value of W in PHI */
  560. st->PHI[i] = st->W[i] - st->PHI[i];
  561. }
  562. /* Update weight to prevent circular convolution (MDF / AUMDF) */
  563. for (j=0;j<M;j++)
  564. {
  565. /* This is a variant of the Alternatively Updated MDF (AUMDF) */
  566. /* Remove the "if" to make this an MDF filter */
  567. if (j==M-1 || st->cancel_count%(M-1) == j)
  568. {
  569. #ifdef FIXED_POINT
  570. for (i=0;i<N;i++)
  571. st->wtmp2[i] = PSHR32(st->W[j*N+i],NORMALIZE_SCALEDOWN+16);
  572. spx_ifft(st->fft_table, st->wtmp2, st->wtmp);
  573. for (i=0;i<st->frame_size;i++)
  574. {
  575. st->wtmp[i]=0;
  576. }
  577. for (i=st->frame_size;i<N;i++)
  578. {
  579. st->wtmp[i]=SHL(st->wtmp[i],NORMALIZE_SCALEUP);
  580. }
  581. spx_fft(st->fft_table, st->wtmp, st->wtmp2);
  582. /* The "-1" in the shift is a sort of kludge that trades less efficient update speed for decrease noise */
  583. for (i=0;i<N;i++)
  584. st->W[j*N+i] -= SHL32(st->wtmp2[i],16+NORMALIZE_SCALEDOWN-NORMALIZE_SCALEUP-1);
  585. #else
  586. spx_ifft(st->fft_table, &st->W[j*N], st->wtmp);
  587. for (i=st->frame_size;i<N;i++)
  588. {
  589. st->wtmp[i]=0;
  590. }
  591. spx_fft(st->fft_table, st->wtmp, &st->W[j*N]);
  592. #endif
  593. }
  594. }
  595. /* Compute spectrum of estimated echo for use in an echo post-filter (if necessary)*/
  596. if (Yout)
  597. {
  598. spx_word16_t leak2;
  599. if (st->adapted)
  600. {
  601. /* If the filter is adapted, take the filtered echo */
  602. for (i=0;i<st->frame_size;i++)
  603. st->last_y[i] = st->last_y[st->frame_size+i];
  604. for (i=0;i<st->frame_size;i++)
  605. st->last_y[st->frame_size+i] = ref[i]-out[i];
  606. } else {
  607. /* If filter isn't adapted yet, all we can do is take the echo signal directly */
  608. for (i=0;i<N;i++)
  609. st->last_y[i] = st->x[i];
  610. }
  611. /* Apply hanning window (should pre-compute it)*/
  612. for (i=0;i<N;i++)
  613. st->y[i] = MULT16_16_Q15(st->window[i],st->last_y[i]);
  614. /* Compute power spectrum of the echo */
  615. spx_fft(st->fft_table, st->y, st->Y);
  616. power_spectrum(st->Y, st->Yps, N);
  617. #ifdef FIXED_POINT
  618. if (leak_estimate > 16383)
  619. leak2 = 32767;
  620. else
  621. leak2 = SHL16(leak_estimate, 1);
  622. #else
  623. if (leak_estimate>.5)
  624. leak2 = 1;
  625. else
  626. leak2 = 2*leak_estimate;
  627. #endif
  628. /* Estimate residual echo */
  629. for (i=0;i<=st->frame_size;i++)
  630. Yout[i] = MULT16_32_Q15(leak2,st->Yps[i]);
  631. }
  632. }
  633. int speex_echo_ctl(SpeexEchoState *st, int request, void *ptr)
  634. {
  635. switch(request)
  636. {
  637. case SPEEX_ECHO_GET_FRAME_SIZE:
  638. (*(int*)ptr) = st->frame_size;
  639. break;
  640. case SPEEX_ECHO_SET_SAMPLING_RATE:
  641. st->sampling_rate = (*(int*)ptr);
  642. st->spec_average = DIV32_16(SHL32(st->frame_size, 15), st->sampling_rate);
  643. #ifdef FIXED_POINT
  644. st->beta0 = DIV32_16(SHL32(st->frame_size, 16), st->sampling_rate);
  645. st->beta_max = DIV32_16(SHL32(st->frame_size, 14), st->sampling_rate);
  646. #else
  647. st->beta0 = (2.0f*st->frame_size)/st->sampling_rate;
  648. st->beta_max = (.5f*st->frame_size)/st->sampling_rate;
  649. #endif
  650. if (st->sampling_rate<12000)
  651. st->notch_radius = QCONST16(.9, 15);
  652. else if (st->sampling_rate<24000)
  653. st->notch_radius = QCONST16(.982, 15);
  654. else
  655. st->notch_radius = QCONST16(.992, 15);
  656. break;
  657. case SPEEX_ECHO_GET_SAMPLING_RATE:
  658. (*(int*)ptr) = st->sampling_rate;
  659. break;
  660. default:
  661. speex_warning_int("Unknown speex_echo_ctl request: ", request);
  662. return -1;
  663. }
  664. return 0;
  665. }
粤ICP备19079148号