📄 filters.c
字号:
/* Copyright (C) 2002 Jean-Marc Valin
File: filters.c
Various analysis/synthesis filters
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- 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.
- Neither the name of the Xiph.org Foundation nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``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 FOUNDATION OR
CONTRIBUTORS 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 "filters.h"
#include "stack_alloc.h"
#include "misc.h"
#include "math_approx.h"
#include "ltp.h"
#include <math.h>
#ifdef _USE_SSE
#include "filters_sse.h"
#elif defined (ARM4_ASM) || defined(ARM5E_ASM)
#include "filters_arm4.h"
#elif defined (BFIN_ASM)
#include "filters_bfin.h"
#endif
void bw_lpc(spx_word16_t gamma, const spx_coef_t *lpc_in, spx_coef_t *lpc_out, int order)
{
int i;
spx_word16_t tmp=gamma;
for (i=0;i<order;i++)
{
lpc_out[i] = MULT16_16_P15(tmp,lpc_in[i]);
tmp = MULT16_16_P15(tmp, gamma);
}
}
#ifdef FIXED_POINT
/* FIXME: These functions are ugly and probably introduce too much error */
void signal_mul(const spx_sig_t *x, spx_sig_t *y, spx_word32_t scale, int len)
{
int i;
for (i=0;i<len;i++)
{
y[i] = SHL32(MULT16_32_Q14(EXTRACT16(SHR32(x[i],7)),scale),7);
}
}
void signal_div(const spx_sig_t *x, spx_sig_t *y, spx_word32_t scale, int len)
{
int i;
if (scale > SHL32(EXTEND32(SIG_SCALING), 8))
{
spx_word16_t scale_1;
scale = PSHR32(scale, SIG_SHIFT);
scale_1 = EXTRACT16(DIV32_16(SHL32(EXTEND32(SIG_SCALING),7),scale));
for (i=0;i<len;i++)
{
y[i] = SHR32(MULT16_16(scale_1, EXTRACT16(SHR32(x[i],SIG_SHIFT))),7);
}
} else {
spx_word16_t scale_1;
scale = PSHR32(scale, SIG_SHIFT-5);
scale_1 = DIV32_16(SHL32(EXTEND32(SIG_SCALING),3),scale);
for (i=0;i<len;i++)
{
y[i] = MULT16_16(scale_1, EXTRACT16(SHR32(x[i],SIG_SHIFT-2)));
}
}
}
#else
void signal_mul(const spx_sig_t *x, spx_sig_t *y, spx_word32_t scale, int len)
{
int i;
for (i=0;i<len;i++)
y[i] = scale*x[i];
}
void signal_div(const spx_sig_t *x, spx_sig_t *y, spx_word32_t scale, int len)
{
int i;
float scale_1 = 1/scale;
for (i=0;i<len;i++)
y[i] = scale_1*x[i];
}
#endif
#ifdef FIXED_POINT
spx_word16_t compute_rms(const spx_sig_t *x, int len)
{
int i;
spx_word32_t sum=0;
spx_sig_t max_val=1;
int sig_shift;
for (i=0;i<len;i++)
{
spx_sig_t tmp = x[i];
if (tmp<0)
tmp = -tmp;
if (tmp > max_val)
max_val = tmp;
}
sig_shift=0;
while (max_val>16383)
{
sig_shift++;
max_val >>= 1;
}
for (i=0;i<len;i+=4)
{
spx_word32_t sum2=0;
spx_word16_t tmp;
tmp = EXTRACT16(SHR32(x[i],sig_shift));
sum2 = MAC16_16(sum2,tmp,tmp);
tmp = EXTRACT16(SHR32(x[i+1],sig_shift));
sum2 = MAC16_16(sum2,tmp,tmp);
tmp = EXTRACT16(SHR32(x[i+2],sig_shift));
sum2 = MAC16_16(sum2,tmp,tmp);
tmp = EXTRACT16(SHR32(x[i+3],sig_shift));
sum2 = MAC16_16(sum2,tmp,tmp);
sum = ADD32(sum,SHR32(sum2,6));
}
return EXTRACT16(SHR32(SHL32(EXTEND32(spx_sqrt(1+DIV32(sum,len))),(sig_shift+3)),SIG_SHIFT));
}
#ifndef OVERRIDE_NORMALIZE16
int normalize16(const spx_sig_t *x, spx_word16_t *y, spx_sig_t max_scale, int len)
{
int i;
spx_sig_t max_val=1;
int sig_shift;
for (i=0;i<len;i++)
{
spx_sig_t tmp = x[i];
if (tmp<0)
tmp = NEG32(tmp);
if (tmp >= max_val)
max_val = tmp;
}
sig_shift=0;
while (max_val>max_scale)
{
sig_shift++;
max_val >>= 1;
}
for (i=0;i<len;i++)
y[i] = EXTRACT16(SHR32(x[i], sig_shift));
return sig_shift;
}
#endif
#else
spx_word16_t compute_rms(const spx_sig_t *x, int len)
{
int i;
float sum=0;
for (i=0;i<len;i++)
{
sum += x[i]*x[i];
}
return sqrt(.1+sum/len);
}
#endif
#ifndef OVERRIDE_FILTER_MEM2
#ifdef PRECISION16
void filter_mem2(const spx_sig_t *x, const spx_coef_t *num, const spx_coef_t *den, spx_sig_t *y, int N, int ord, spx_mem_t *mem)
{
int i,j;
spx_word16_t xi,yi,nyi;
for (i=0;i<N;i++)
{
xi= EXTRACT16(PSHR32(SATURATE(x[i],536870911),SIG_SHIFT));
yi = EXTRACT16(PSHR32(SATURATE(ADD32(x[i], SHL32(mem[0],1)),536870911),SIG_SHIFT));
nyi = NEG16(yi);
for (j=0;j<ord-1;j++)
{
mem[j] = MAC16_16(MAC16_16(mem[j+1], num[j],xi), den[j],nyi);
}
mem[ord-1] = ADD32(MULT16_16(num[ord-1],xi), MULT16_16(den[ord-1],nyi));
y[i] = SHL32(EXTEND32(yi),SIG_SHIFT);
}
}
#else
void filter_mem2(const spx_sig_t *x, const spx_coef_t *num, const spx_coef_t *den, spx_sig_t *y, int N, int ord, spx_mem_t *mem)
{
int i,j;
spx_sig_t xi,yi,nyi;
for (i=0;i<N;i++)
{
xi=SATURATE(x[i],805306368);
yi = SATURATE(ADD32(xi, SHL32(mem[0],2)),805306368);
nyi = NEG32(yi);
for (j=0;j<ord-1;j++)
{
mem[j] = MAC16_32_Q15(MAC16_32_Q15(mem[j+1], num[j],xi), den[j],nyi);
}
mem[ord-1] = SUB32(MULT16_32_Q15(num[ord-1],xi), MULT16_32_Q15(den[ord-1],yi));
y[i] = yi;
}
}
#endif
#endif
#ifndef OVERRIDE_IIR_MEM2
#ifdef PRECISION16
void iir_mem2(const spx_sig_t *x, const spx_coef_t *den, spx_sig_t *y, int N, int ord, spx_mem_t *mem)
{
int i,j;
spx_word16_t yi,nyi;
for (i=0;i<N;i++)
{
yi = EXTRACT16(PSHR32(SATURATE(x[i] + SHL32(mem[0],1),536870911),SIG_SHIFT));
nyi = NEG16(yi);
for (j=0;j<ord-1;j++)
{
mem[j] = MAC16_16(mem[j+1],den[j],nyi);
}
mem[ord-1] = MULT16_16(den[ord-1],nyi);
y[i] = SHL32(EXTEND32(yi),SIG_SHIFT);
}
}
#else
void iir_mem2(const spx_sig_t *x, const spx_coef_t *den, spx_sig_t *y, int N, int ord, spx_mem_t *mem)
{
int i,j;
spx_word32_t xi,yi,nyi;
for (i=0;i<N;i++)
{
xi=SATURATE(x[i],805306368);
yi = SATURATE(xi + SHL32(mem[0],2),805306368);
nyi = NEG32(yi);
for (j=0;j<ord-1;j++)
{
mem[j] = MAC16_32_Q15(mem[j+1],den[j],nyi);
}
mem[ord-1] = MULT16_32_Q15(den[ord-1],nyi);
y[i] = yi;
}
}
#endif
#endif
#ifndef OVERRIDE_FIR_MEM2
#ifdef PRECISION16
void fir_mem2(const spx_sig_t *x, const spx_coef_t *num, spx_sig_t *y, int N, int ord, spx_mem_t *mem)
{
int i,j;
spx_word16_t xi,yi;
for (i=0;i<N;i++)
{
xi= EXTRACT16(PSHR32(SATURATE(x[i],536870911),SIG_SHIFT));
yi = EXTRACT16(PSHR32(SATURATE(x[i] + SHL32(mem[0],1),536870911),SIG_SHIFT));
for (j=0;j<ord-1;j++)
{
mem[j] = MAC16_16(mem[j+1], num[j],xi);
}
mem[ord-1] = MULT16_16(num[ord-1],xi);
y[i] = SHL32(EXTEND32(yi),SIG_SHIFT);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -