⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mdf.c

📁 mediastreamer2是开源的网络传输媒体流的库
💻 C
📖 第 1 页 / 共 3 页
字号:
/* Copyright (C) 2003-2008 Jean-Marc Valin   File: mdf.c   Echo canceller based on the MDF algorithm (see below)   Redistribution and use in source and binary forms, with or without   modification, are permitted provided that the following conditions are   met:   1. Redistributions of source code must retain the above copyright notice,   this list of conditions and the following disclaimer.   2. Redistributions in binary form must reproduce the above copyright   notice, this list of conditions and the following disclaimer in the   documentation and/or other materials provided with the distribution.   3. The name of the author may not be used to endorse or promote products   derived from this software without specific prior written permission.   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE   DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE   POSSIBILITY OF SUCH DAMAGE.*//*   The echo canceller is based on the MDF algorithm described in:   J. S. Soo, K. K. Pang Multidelay block frequency adaptive filter,    IEEE Trans. Acoust. Speech Signal Process., Vol. ASSP-38, No. 2,    February 1990.      We use the Alternatively Updated MDF (AUMDF) variant. Robustness to    double-talk is achieved using a variable learning rate as described in:      Valin, J.-M., On Adjusting the Learning Rate in Frequency Domain Echo    Cancellation With Double-Talk. IEEE Transactions on Audio,   Speech and Language Processing, Vol. 15, No. 3, pp. 1030-1034, 2007.   http://people.xiph.org/~jm/papers/valin_taslp2006.pdf      There is no explicit double-talk detection, but a continuous variation   in the learning rate based on residual echo, double-talk and background   noise.      About the fixed-point version:   All the signals are represented with 16-bit words. The filter weights    are represented with 32-bit words, but only the top 16 bits are used   in most cases. The lower 16 bits are completely unreliable (due to the   fact that the update is done only on the top bits), but help in the   adaptation -- probably by removing a "threshold effect" due to   quantization (rounding going to zero) when the gradient is small.      Another kludge that seems to work good: when performing the weight   update, we only move half the way toward the "goal" this seems to   reduce the effect of quantization noise in the update phase. This   can be seen as applying a gradient descent on a "soft constraint"   instead of having a hard constraint.   */#ifdef HAVE_CONFIG_H#include "config.h"#endif#include "arch.h"#include "speex/speex_echo.h"#include "fftwrap.h"#include "pseudofloat.h"#include "math_approx.h"#include "os_support.h"#ifndef M_PI#define M_PI 3.14159265358979323846#endif#ifdef FIXED_POINT#define WEIGHT_SHIFT 11#define NORMALIZE_SCALEDOWN 5#define NORMALIZE_SCALEUP 3#else#define WEIGHT_SHIFT 0#endif#ifdef FIXED_POINT#define WORD2INT(x) ((x) < -32767 ? -32768 : ((x) > 32766 ? 32767 : (x)))  #else#define WORD2INT(x) ((x) < -32767.5f ? -32768 : ((x) > 32766.5f ? 32767 : floor(.5+(x))))  #endif/* If enabled, the AEC will use a foreground filter and a background filter to be more robust to double-talk   and difficult signals in general. The cost is an extra FFT and a matrix-vector multiply */#define TWO_PATH#ifdef FIXED_POINTstatic const spx_float_t MIN_LEAK = {20972, -22};/* Constants for the two-path filter */static const spx_float_t VAR1_SMOOTH = {23593, -16};static const spx_float_t VAR2_SMOOTH = {23675, -15};static const spx_float_t VAR1_UPDATE = {16384, -15};static const spx_float_t VAR2_UPDATE = {16384, -16};static const spx_float_t VAR_BACKTRACK = {16384, -12};#define TOP16(x) ((x)>>16)#elsestatic const spx_float_t MIN_LEAK = .005f;/* Constants for the two-path filter */static const spx_float_t VAR1_SMOOTH = .36f;static const spx_float_t VAR2_SMOOTH = .7225f;static const spx_float_t VAR1_UPDATE = .5f;static const spx_float_t VAR2_UPDATE = .25f;static const spx_float_t VAR_BACKTRACK = 4.f;#define TOP16(x) (x)#endif#define PLAYBACK_DELAY 2void speex_echo_get_residual(SpeexEchoState *st, spx_word32_t *Yout, int len);/** Speex echo cancellation state. */struct SpeexEchoState_ {   int frame_size;           /**< Number of samples processed each time */   int window_size;   int M;   int cancel_count;   int adapted;   int saturated;   int screwed_up;   int C;                    /** Number of input channels (microphones) */   int K;                    /** Number of output channels (loudspeakers) */   spx_int32_t sampling_rate;   spx_word16_t spec_average;   spx_word16_t beta0;   spx_word16_t beta_max;   spx_word32_t sum_adapt;   spx_word16_t leak_estimate;      spx_word16_t *e;      /* scratch */   spx_word16_t *x;      /* Far-end input buffer (2N) */   spx_word16_t *X;      /* Far-end buffer (M+1 frames) in frequency domain */   spx_word16_t *input;  /* scratch */   spx_word16_t *y;      /* scratch */   spx_word16_t *last_y;   spx_word16_t *Y;      /* scratch */   spx_word16_t *E;   spx_word32_t *PHI;    /* scratch */   spx_word32_t *W;      /* (Background) filter weights */#ifdef TWO_PATH   spx_word16_t *foreground; /* Foreground filter weights */   spx_word32_t  Davg1;  /* 1st recursive average of the residual power difference */   spx_word32_t  Davg2;  /* 2nd recursive average of the residual power difference */   spx_float_t   Dvar1;  /* Estimated variance of 1st estimator */   spx_float_t   Dvar2;  /* Estimated variance of 2nd estimator */#endif   spx_word32_t *power;  /* Power of the far-end signal */   spx_float_t  *power_1;/* Inverse power of far-end */   spx_word16_t *wtmp;   /* scratch */#ifdef FIXED_POINT   spx_word16_t *wtmp2;  /* scratch */#endif   spx_word32_t *Rf;     /* scratch */   spx_word32_t *Yf;     /* scratch */   spx_word32_t *Xf;     /* scratch */   spx_word32_t *Eh;   spx_word32_t *Yh;   spx_float_t   Pey;   spx_float_t   Pyy;   spx_word16_t *window;   spx_word16_t *prop;   void *fft_table;   spx_word16_t *memX, *memD, *memE;   spx_word16_t preemph;   spx_word16_t notch_radius;   spx_mem_t *notch_mem;   /* NOTE: If you only use speex_echo_cancel() and want to save some memory, remove this */   spx_int16_t *play_buf;   int play_buf_pos;   int play_buf_started;};static inline void filter_dc_notch16(const spx_int16_t *in, spx_word16_t radius, spx_word16_t *out, int len, spx_mem_t *mem, int stride){   int i;   spx_word16_t den2;#ifdef FIXED_POINT   den2 = MULT16_16_Q15(radius,radius) + MULT16_16_Q15(QCONST16(.7,15),MULT16_16_Q15(32767-radius,32767-radius));#else   den2 = radius*radius + .7*(1-radius)*(1-radius);#endif      /*printf ("%d %d %d %d %d %d\n", num[0], num[1], num[2], den[0], den[1], den[2]);*/   for (i=0;i<len;i++)   {      spx_word16_t vin = in[i*stride];      spx_word32_t vout = mem[0] + SHL32(EXTEND32(vin),15);#ifdef FIXED_POINT      mem[0] = mem[1] + SHL32(SHL32(-EXTEND32(vin),15) + MULT16_32_Q15(radius,vout),1);#else      mem[0] = mem[1] + 2*(-vin + radius*vout);#endif      mem[1] = SHL32(EXTEND32(vin),15) - MULT16_32_Q15(den2,vout);      out[i] = SATURATE32(PSHR32(MULT16_32_Q15(radius,vout),15),32767);   }}/* This inner product is slightly different from the codec version because of fixed-point */static inline spx_word32_t mdf_inner_prod(const spx_word16_t *x, const spx_word16_t *y, int len){   spx_word32_t sum=0;   len >>= 1;   while(len--)   {      spx_word32_t part=0;      part = MAC16_16(part,*x++,*y++);      part = MAC16_16(part,*x++,*y++);      /* HINT: If you had a 40-bit accumulator, you could shift only at the end */      sum = ADD32(sum,SHR32(part,6));   }   return sum;}/** Compute power spectrum of a half-complex (packed) vector */static inline void power_spectrum(const spx_word16_t *X, spx_word32_t *ps, int N){   int i, j;   ps[0]=MULT16_16(X[0],X[0]);   for (i=1,j=1;i<N-1;i+=2,j++)   {      ps[j] =  MULT16_16(X[i],X[i]) + MULT16_16(X[i+1],X[i+1]);   }   ps[j]=MULT16_16(X[i],X[i]);}/** Compute power spectrum of a half-complex (packed) vector and accumulate */static inline void power_spectrum_accum(const spx_word16_t *X, spx_word32_t *ps, int N){   int i, j;   ps[0]+=MULT16_16(X[0],X[0]);   for (i=1,j=1;i<N-1;i+=2,j++)   {      ps[j] +=  MULT16_16(X[i],X[i]) + MULT16_16(X[i+1],X[i+1]);   }   ps[j]+=MULT16_16(X[i],X[i]);}/** Compute cross-power spectrum of a half-complex (packed) vectors and add to acc */#ifdef FIXED_POINTstatic inline void spectral_mul_accum(const spx_word16_t *X, const spx_word32_t *Y, spx_word16_t *acc, int N, int M){   int i,j;   spx_word32_t tmp1=0,tmp2=0;   for (j=0;j<M;j++)   {      tmp1 = MAC16_16(tmp1, X[j*N],TOP16(Y[j*N]));   }   acc[0] = PSHR32(tmp1,WEIGHT_SHIFT);   for (i=1;i<N-1;i+=2)   {      tmp1 = tmp2 = 0;      for (j=0;j<M;j++)      {         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])));         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]));      }      acc[i] = PSHR32(tmp1,WEIGHT_SHIFT);      acc[i+1] = PSHR32(tmp2,WEIGHT_SHIFT);   }   tmp1 = tmp2 = 0;   for (j=0;j<M;j++)   {      tmp1 = MAC16_16(tmp1, X[(j+1)*N-1],TOP16(Y[(j+1)*N-1]));   }   acc[N-1] = PSHR32(tmp1,WEIGHT_SHIFT);}static inline void spectral_mul_accum16(const spx_word16_t *X, const spx_word16_t *Y, spx_word16_t *acc, int N, int M){   int i,j;   spx_word32_t tmp1=0,tmp2=0;   for (j=0;j<M;j++)   {      tmp1 = MAC16_16(tmp1, X[j*N],Y[j*N]);   }   acc[0] = PSHR32(tmp1,WEIGHT_SHIFT);   for (i=1;i<N-1;i+=2)   {      tmp1 = tmp2 = 0;      for (j=0;j<M;j++)      {         tmp1 = SUB32(MAC16_16(tmp1, X[j*N+i],Y[j*N+i]), MULT16_16(X[j*N+i+1],Y[j*N+i+1]));         tmp2 = MAC16_16(MAC16_16(tmp2, X[j*N+i+1],Y[j*N+i]), X[j*N+i], Y[j*N+i+1]);      }      acc[i] = PSHR32(tmp1,WEIGHT_SHIFT);      acc[i+1] = PSHR32(tmp2,WEIGHT_SHIFT);   }   tmp1 = tmp2 = 0;   for (j=0;j<M;j++)   {      tmp1 = MAC16_16(tmp1, X[(j+1)*N-1],Y[(j+1)*N-1]);   }   acc[N-1] = PSHR32(tmp1,WEIGHT_SHIFT);}#elsestatic inline void spectral_mul_accum(const spx_word16_t *X, const spx_word32_t *Y, spx_word16_t *acc, int N, int M){   int i,j;   for (i=0;i<N;i++)      acc[i] = 0;   for (j=0;j<M;j++)   {      acc[0] += X[0]*Y[0];      for (i=1;i<N-1;i+=2)      {         acc[i] += (X[i]*Y[i] - X[i+1]*Y[i+1]);         acc[i+1] += (X[i+1]*Y[i] + X[i]*Y[i+1]);      }      acc[i] += X[i]*Y[i];      X += N;      Y += N;   }}#define spectral_mul_accum16 spectral_mul_accum#endif/** Compute weighted cross-power spectrum of a half-complex (packed) vector with conjugate */static inline void weighted_spectral_mul_conj(const spx_float_t *w, const spx_float_t p, const spx_word16_t *X, const spx_word16_t *Y, spx_word32_t *prod, int N){   int i, j;   spx_float_t W;   W = FLOAT_AMULT(p, w[0]);   prod[0] = FLOAT_MUL32(W,MULT16_16(X[0],Y[0]));   for (i=1,j=1;i<N-1;i+=2,j++)   {      W = FLOAT_AMULT(p, w[j]);      prod[i] = FLOAT_MUL32(W,MAC16_16(MULT16_16(X[i],Y[i]), X[i+1],Y[i+1]));      prod[i+1] = FLOAT_MUL32(W,MAC16_16(MULT16_16(-X[i+1],Y[i]), X[i],Y[i+1]));   }   W = FLOAT_AMULT(p, w[j]);   prod[i] = FLOAT_MUL32(W,MULT16_16(X[i],Y[i]));}static inline void mdf_adjust_prop(const spx_word32_t *W, int N, int M, int P, spx_word16_t *prop){   int i, j, p;   spx_word16_t max_sum = 1;   spx_word32_t prop_sum = 1;   for (i=0;i<M;i++)   {      spx_word32_t tmp = 1;      for (p=0;p<P;p++)         for (j=0;j<N;j++)            tmp += MULT16_16(EXTRACT16(SHR32(W[p*N*M + i*N+j],18)), EXTRACT16(SHR32(W[p*N*M + i*N+j],18)));#ifdef FIXED_POINT      /* Just a security in case an overflow were to occur */      tmp = MIN32(ABS32(tmp), 536870912);#endif      prop[i] = spx_sqrt(tmp);      if (prop[i] > max_sum)         max_sum = prop[i];   }   for (i=0;i<M;i++)   {      prop[i] += MULT16_16_Q15(QCONST16(.1f,15),max_sum);      prop_sum += EXTEND32(prop[i]);   }   for (i=0;i<M;i++)   {      prop[i] = DIV32(MULT16_16(QCONST16(.99f,15), prop[i]),prop_sum);      /*printf ("%f ", prop[i]);*/   }   /*printf ("\n");*/}#ifdef DUMP_ECHO_CANCEL_DATA#include <stdio.h>static FILE *rFile=NULL, *pFile=NULL, *oFile=NULL;static void dump_audio(const spx_int16_t *rec, const spx_int16_t *play, const spx_int16_t *out, int len){   if (!(rFile && pFile && oFile))   {      speex_fatal("Dump files not open");   }   fwrite(rec, sizeof(spx_int16_t), len, rFile);   fwrite(play, sizeof(spx_int16_t), len, pFile);   fwrite(out, sizeof(spx_int16_t), len, oFile);}#endif/** Creates a new echo canceller state */EXPORT SpeexEchoState *speex_echo_state_init(int frame_size, int filter_length){   return speex_echo_state_init_mc(frame_size, filter_length, 1, 1);}EXPORT SpeexEchoState *speex_echo_state_init_mc(int frame_size, int filter_length, int nb_mic, int nb_speakers){   int i,N,M, C, K;   SpeexEchoState *st = (SpeexEchoState *)speex_alloc(sizeof(SpeexEchoState));   st->K = nb_speakers;   st->C = nb_mic;   C=st->C;   K=st->K;#ifdef DUMP_ECHO_CANCEL_DATA   if (rFile || pFile || oFile)      speex_fatal("Opening dump files twice");   rFile = fopen("aec_rec.sw", "wb");   pFile = fopen("aec_play.sw", "wb");   oFile = fopen("aec_out.sw", "wb");#endif      st->frame_size = frame_size;   st->window_size = 2*frame_size;   N = st->window_size;   M = st->M = (filter_length+st->frame_size-1)/frame_size;   st->cancel_count=0;   st->sum_adapt = 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -