/* Copyright (C) 2002-2006 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 "arch.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);
}
}
void sanitize_values32(spx_word32_t *vec, spx_word32_t min_val, spx_word32_t max_val, int len)
{
int i;
for (i=0;i<len;i++)
{
/* It's important we do the test that way so we can catch NaNs, which are neither greater nor smaller */
if (!(vec[i]>=min_val && vec[i] <= max_val))
{
if (vec[i] < min_val)
vec[i] = min_val;
else if (vec[i] > max_val)
vec[i] = max_val;
else /* Has to be NaN */
vec[i] = 0;
}
}
}
void highpass(const spx_word16_t *x, spx_word16_t *y, int len, int filtID, spx_mem_t *mem)
{
int i;
#ifdef FIXED_POINT
const spx_word16_t Pcoef[5][3] = {{16384, -31313, 14991}, {16384, -31569, 15249}, {16384, -31677, 15328}, {16384, -32313, 15947}, {16384, -22446, 6537}};
const spx_word16_t Zcoef[5][3] = {{15672, -31344, 15672}, {15802, -31601, 15802}, {15847, -31694, 15847}, {16162, -32322, 16162}, {14418, -28836, 14418}};
#else
const spx_word16_t Pcoef[5][3] = {{1.00000f, -1.91120f, 0.91498f}, {1.00000f, -1.92683f, 0.93071f}, {1.00000f, -1.93338f, 0.93553f}, {1.00000f, -1.97226f, 0.97332f}, {1.00000f, -1.37000f, 0.39900f}};
const spx_word16_t Zcoef[5][3] = {{0.95654f, -1.91309f, 0.95654f}, {0.96446f, -1.92879f, 0.96446f}, {0.96723f, -1.93445f, 0.96723f}, {0.98645f, -1.97277f, 0.98645f}, {0.88000f, -1.76000f, 0.88000f}};
#endif
const spx_word16_t *den, *num;
if (filtID>4)
filtID=4;
den = Pcoef[filtID]; num = Zcoef[filtID];
/*return;*/
for (i=0;i<len;i++)
{
spx_word16_t yi;
spx_word32_t vout = ADD32(MULT16_16(num[0], x[i]),mem[0]);
yi = EXTRACT16(SATURATE(PSHR32(vout,14),32767));
mem[0] = ADD32(MAC16_16(mem[1], num[1],x[i]), SHL32(MULT16_32_Q15(-den[1],vout),1));
mem[1] = ADD32(MULT16_16(num[2],x[i]), SHL32(MULT16_32_Q15(-den[2],vout),1));
y[i] = yi;
}
}
#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_word16_t *x, spx_word16_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(PDIV32_16(SHL32(EXTEND32(SIG_SCALING),7),scale));
for (i=0;i<len;i++)
{
y[i] = MULT16_16_P15(scale_1, x[i]);
}
} else if (scale > SHR32(EXTEND32(SIG_SCALING), 2)) {
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] = PSHR32(MULT16_16(scale_1, SHL16(x[i],2)),8);
}
} else {
spx_word16_t scale_1;
scale = PSHR32(scale, SIG_SHIFT-7);
if (scale < 5)
scale = 5;
scale_1 = DIV32_16(SHL32(EXTEND32(SIG_SCALING),3),scale);
for (i=0;i<len;i++)
{
y[i] = PSHR32(MULT16_16(scale_1, SHL16(x[i],2)),6);
}
}
}
#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(PSHR32(SHL32(EXTEND32(spx_sqrt(DIV32(sum,len))),(sig_shift+3)),SIG_SHIFT));
}
spx_word16_t compute_rms16(const spx_word16_t *x, int len)
{
int i;
spx_word16_t max_val=10;
for (i=0;i<len;i++)
{
spx_sig_t tmp = x[i];
if (tmp<0)
tmp = -tmp;
if (tmp > max_val)
max_val = tmp;
}
if (max_val>16383)
{
spx_word32_t sum=0;
for (i=0;i<len;i+=4)
{
spx_word32_t sum2=0;
sum2 = MAC16_16(sum2,SHR16(x[i],1),SHR16(x[i],1));
sum2 = MAC16_16(sum2,SHR16(x[i+1],1),SHR16(x[i+1],1));
sum2 = MAC16_16(sum2,SHR16(x[i+2],1),SHR16(x[i+2],1));
sum2 = MAC16_16(sum2,SHR16(x[i+3],1),SHR16(x[i+3],1));
sum = ADD32(sum,SHR32(sum2,6));
}
return SHL16(spx_sqrt(DIV32(sum,len)),4);
} else {
spx_word32_t sum=0;
int sig_shift=0;
if (max_val < 8192)
sig_shift=1;
if (max_val < 4096)
sig_shift=2;
if (max_val < 2048)
sig_shift=3;
for (i=0;i<len;i+=4)
{
spx_word32_t sum2=0;
sum2 = MAC16_16(sum2,SHL16(x[i],sig_shift),SHL16(x[i],sig_shift));
sum2 = MAC16_16(sum2,SHL16(x[i+1],sig_shift),SHL16(x[i+1],sig_shift));
sum2 = MAC16_16(sum2,SHL16(x[i+2],sig_shift),SHL16(x[i+2],sig_shift));
sum2 = MAC16_16(sum2,SHL16(x[i+3],sig_shift),SHL16(x[i+3],sig_shift));
sum = ADD32(sum,SHR32(sum2,6));
}
return SHL16