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

📄 mdf.c

📁 tcpmp.src.0.72RC1 优秀的多媒体播放器TCPMP的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Copyright (C) 2003-2005 Jean-Marc Valin   File: speex_echo.c   Echo cancelling 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.   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.*/#ifdef HAVE_CONFIG_H#include "config.h"#endif#include "misc.h"#include "speex/speex_echo.h"#include "smallft.h"#include <math.h>#ifndef M_PI#define M_PI 3.14159265358979323846#endif#undef BETA#define BETA .65#define min(a,b) ((a)<(b) ? (a) : (b))#define max(a,b) ((a)>(b) ? (a) : (b))/** Compute inner product of two real vectors */static inline float inner_prod(float *x, float *y, int N){   int i;   float ret=0;   for (i=0;i<N;i++)      ret += x[i]*y[i];   return ret;}/** Compute power spectrum of a half-complex (packed) vector */static inline void power_spectrum(float *X, float *ps, int N){   int i, j;   ps[0]=X[0]*X[0];   for (i=1,j=1;i<N-1;i+=2,j++)   {      ps[j] =  X[i]*X[i] + X[i+1]*X[i+1];   }   ps[j]=X[i]*X[i];}/** Compute cross-power spectrum of a half-complex (packed) vectors and add to acc */static inline void spectral_mul_accum(float *X, float *Y, float *acc, int N){   int i;   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];}/** Compute cross-power spectrum of a half-complex (packed) vector with conjugate */static inline void spectral_mul_conj(float *X, float *Y, float *prod, int N){   int i;   prod[0] = X[0]*Y[0];   for (i=1;i<N-1;i+=2)   {      prod[i] = (X[i]*Y[i] + X[i+1]*Y[i+1]);      prod[i+1] = (-X[i+1]*Y[i] + X[i]*Y[i+1]);   }   prod[i] = X[i]*Y[i];}/** Compute weighted cross-power spectrum of a half-complex (packed) vector with conjugate */static inline void weighted_spectral_mul_conj(float *w, float *X, float *Y, float *prod, int N){   int i, j;   prod[0] = w[0]*X[0]*Y[0];   for (i=1,j=1;i<N-1;i+=2,j++)   {      prod[i] = w[j]*(X[i]*Y[i] + X[i+1]*Y[i+1]);      prod[i+1] = w[j]*(-X[i+1]*Y[i] + X[i]*Y[i+1]);   }   prod[i] = w[j]*X[i]*Y[i];}/** Creates a new echo canceller state */SpeexEchoState *speex_echo_state_init(int frame_size, int filter_length){   int i,j,N,M;   SpeexEchoState *st = (SpeexEchoState *)speex_alloc(sizeof(SpeexEchoState));   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->adapt_rate = .01f;   st->sum_adapt = 0;   st->Sey = 0;   st->Syy = 0;   st->See = 0;            st->fft_lookup = (struct drft_lookup*)speex_alloc(sizeof(struct drft_lookup));   spx_drft_init(st->fft_lookup, N);      st->x = (float*)speex_alloc(N*sizeof(float));   st->d = (float*)speex_alloc(N*sizeof(float));   st->y = (float*)speex_alloc(N*sizeof(float));   st->y2 = (float*)speex_alloc(N*sizeof(float));   st->Yps = (float*)speex_alloc(N*sizeof(float));   st->last_y = (float*)speex_alloc(N*sizeof(float));   st->Yf = (float*)speex_alloc((st->frame_size+1)*sizeof(float));   st->Rf = (float*)speex_alloc((st->frame_size+1)*sizeof(float));   st->Xf = (float*)speex_alloc((st->frame_size+1)*sizeof(float));   st->fratio = (float*)speex_alloc((st->frame_size+1)*sizeof(float));   st->regul = (float*)speex_alloc(N*sizeof(float));   st->X = (float*)speex_alloc(M*N*sizeof(float));   st->D = (float*)speex_alloc(N*sizeof(float));   st->Y = (float*)speex_alloc(N*sizeof(float));   st->Y2 = (float*)speex_alloc(N*sizeof(float));   st->E = (float*)speex_alloc(N*sizeof(float));   st->W = (float*)speex_alloc(M*N*sizeof(float));   st->PHI = (float*)speex_alloc(M*N*sizeof(float));   st->power = (float*)speex_alloc((frame_size+1)*sizeof(float));   st->power_1 = (float*)speex_alloc((frame_size+1)*sizeof(float));   st->grad = (float*)speex_alloc(N*M*sizeof(float));      for (i=0;i<N*M;i++)   {      st->W[i] = st->PHI[i] = 0;   }      st->regul[0] = (.01+(10.)/((4.)*(4.)))/M;   for (i=1,j=1;i<N-1;i+=2,j++)   {      st->regul[i] = .01+((10.)/((j+4.)*(j+4.)))/M;      st->regul[i+1] = .01+((10.)/((j+4.)*(j+4.)))/M;   }   st->regul[i] = .01+((10.)/((j+4.)*(j+4.)))/M;            st->adapted = 0;   return st;}/** Resets echo canceller state */void speex_echo_state_reset(SpeexEchoState *st){   int i, M, N;   st->cancel_count=0;   st->adapt_rate = .01f;   N = st->window_size;   M = st->M;   for (i=0;i<N*M;i++)   {      st->W[i] = 0;      st->X[i] = 0;   }   for (i=0;i<=st->frame_size;i++)      st->power[i] = 0;      st->adapted = 0;   st->adapt_rate = .01f;   st->sum_adapt = 0;   st->Sey = 0;   st->Syy = 0;   st->See = 0;}/** Destroys an echo canceller state */void speex_echo_state_destroy(SpeexEchoState *st){   spx_drft_clear(st->fft_lookup);   speex_free(st->fft_lookup);   speex_free(st->x);   speex_free(st->d);   speex_free(st->y);   speex_free(st->last_y);   speex_free(st->Yps);   speex_free(st->Yf);   speex_free(st->Rf);   speex_free(st->Xf);   speex_free(st->fratio);   speex_free(st->regul);   speex_free(st->X);   speex_free(st->D);   speex_free(st->Y);   speex_free(st->E);   speex_free(st->W);   speex_free(st->PHI);   speex_free(st->power);   speex_free(st->power_1);   speex_free(st->grad);   speex_free(st);}      /** Performs echo cancellation on a frame */void speex_echo_cancel(SpeexEchoState *st, short *ref, short *echo, short *out, float *Yout){   int i,j,m;   int N,M;   float scale;   float ESR;   float SER;   float Sry=0,Srr=0,Syy=0,Sey=0,See=0,Sxx=0;   float leak_estimate;      /*leak_estimate = .1+(.9/(1+2*st->sum_adapt));*/   leak_estimate = (.1 + (1-st->sum_adapt)*(1-st->sum_adapt))/(.9*st->sum_adapt*st->sum_adapt + .1);            N = st->window_size;   M = st->M;   scale = 1.0f/N;   st->cancel_count++;   /* Copy input data to buffer */   for (i=0;i<st->frame_size;i++)   {      st->x[i] = st->x[i+st->frame_size];      st->x[i+st->frame_size] = echo[i];      st->d[i] = st->d[i+st->frame_size];      st->d[i+st->frame_size] = ref[i];   }   /* Shift memory: this could be optimized eventually*/   for (i=0;i<N*(M-1);i++)      st->X[i]=st->X[i+N];   /* Copy new echo frame */   for (i=0;i<N;i++)      st->X[(M-1)*N+i]=st->x[i];

⌨️ 快捷键说明

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