📄 lsp.c
字号:
/*---------------------------------------------------------------------------*\Original copyright FILE........: lsp.c AUTHOR......: David Rowe DATE CREATED: 24/2/93Heavily modified by Jean-Marc Valin (c) 2002-2006 (fixed-point, optimizations, additional functions, ...) This file contains functions for converting Linear Prediction Coefficients (LPC) to Line Spectral Pair (LSP) and back. Note that the LSP coefficients are not in radians format but in the x domain of the unit circle. Speex License: 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.*//*---------------------------------------------------------------------------*\ Introduction to Line Spectrum Pairs (LSPs) ------------------------------------------ LSPs are used to encode the LPC filter coefficients {ak} for transmission over the channel. LSPs have several properties (like less sensitivity to quantisation noise) that make them superior to direct quantisation of {ak}. A(z) is a polynomial of order lpcrdr with {ak} as the coefficients. A(z) is transformed to P(z) and Q(z) (using a substitution and some algebra), to obtain something like: A(z) = 0.5[P(z)(z+z^-1) + Q(z)(z-z^-1)] (1) As you can imagine A(z) has complex zeros all over the z-plane. P(z) and Q(z) have the very neat property of only having zeros _on_ the unit circle. So to find them we take a test point z=exp(jw) and evaluate P (exp(jw)) and Q(exp(jw)) using a grid of points between 0 and pi. The zeros (roots) of P(z) also happen to alternate, which is why we swap coefficients as we find roots. So the process of finding the LSP frequencies is basically finding the roots of 5th order polynomials. The root so P(z) and Q(z) occur in symmetrical pairs at +/-w, hence the name Line Spectrum Pairs (LSPs). To convert back to ak we just evaluate (1), "clocking" an impulse thru it lpcrdr times gives us the impulse response of A(z) which is {ak}.\*---------------------------------------------------------------------------*/#ifdef HAVE_CONFIG_H#include "config.h"#endif#include <math.h>#include "lsp.h"#include "stack_alloc.h"#include "math_approx.h"#ifndef M_PI#define M_PI 3.14159265358979323846 /* pi */#endif#ifndef NULL#define NULL 0#endif#ifdef FIXED_POINT#define FREQ_SCALE 16384/*#define ANGLE2X(a) (32768*cos(((a)/8192.)))*/#define ANGLE2X(a) (SHL16(spx_cos(a),2))/*#define X2ANGLE(x) (acos(.00006103515625*(x))*LSP_SCALING)*/#define X2ANGLE(x) (spx_acos(x))#ifdef BFIN_ASM#include "lsp_bfin.h"#endif#else/*#define C1 0.99940307#define C2 -0.49558072#define C3 0.03679168*/#define FREQ_SCALE 1.#define ANGLE2X(a) (spx_cos(a))#define X2ANGLE(x) (acos(x))#endif/*---------------------------------------------------------------------------*\ FUNCTION....: cheb_poly_eva() AUTHOR......: David Rowe DATE CREATED: 24/2/93 This function evaluates a series of Chebyshev polynomials\*---------------------------------------------------------------------------*/#ifdef FIXED_POINT#ifndef OVERRIDE_CHEB_POLY_EVAstatic inline spx_word32_t cheb_poly_eva( spx_word16_t *coef, /* P or Q coefs in Q13 format */ spx_word16_t x, /* cos of freq (-1.0 to 1.0) in Q14 format */ int m, /* LPC order/2 */ char *stack){ int i; spx_word16_t b0, b1; spx_word32_t sum; /*Prevents overflows*/ if (x>16383) x = 16383; if (x<-16383) x = -16383; /* Initialise values */ b1=16384; b0=x; /* Evaluate Chebyshev series formulation usin g iterative approach */ sum = ADD32(EXTEND32(coef[m]), EXTEND32(MULT16_16_P14(coef[m-1],x))); for(i=2;i<=m;i++) { spx_word16_t tmp=b0; b0 = SUB16(MULT16_16_Q13(x,b0), b1); b1 = tmp; sum = ADD32(sum, EXTEND32(MULT16_16_P14(coef[m-i],b0))); } return sum;}#endif#elsestatic float cheb_poly_eva(spx_word32_t *coef, spx_word16_t x, int m, char *stack){ int k; float b0, b1, tmp; /* Initial conditions */ b0=0; /* b_(m+1) */ b1=0; /* b_(m+2) */ x*=2; /* Calculate the b_(k) */ for(k=m;k>0;k--) { tmp=b0; /* tmp holds the previous value of b0 */ b0=x*b0-b1+coef[m-k]; /* b0 holds its new value based on b0 and b1 */ b1=tmp; /* b1 holds the previous value of b0 */ } return(-b1+.5*x*b0+coef[m]);}#endif/*---------------------------------------------------------------------------*\ FUNCTION....: lpc_to_lsp() AUTHOR......: David Rowe DATE CREATED: 24/2/93 This function converts LPC coefficients to LSP coefficients.\*---------------------------------------------------------------------------*/#ifdef FIXED_POINT#define SIGN_CHANGE(a,b) (((a)&0x70000000)^((b)&0x70000000)||(b==0))#else#define SIGN_CHANGE(a,b) (((a)*(b))<0.0)#endifint lpc_to_lsp (spx_coef_t *a,int lpcrdr,spx_lsp_t *freq,int nb,spx_word16_t delta, char *stack)/* float *a lpc coefficients *//* int lpcrdr order of LPC coefficients (10) *//* float *freq LSP frequencies in the x domain *//* int nb number of sub-intervals (4) *//* float delta grid spacing interval (0.02) */{ spx_word16_t temp_xr,xl,xr,xm=0; spx_word32_t psuml,psumr,psumm,temp_psumr/*,temp_qsumr*/; int i,j,m,flag,k; VARDECL(spx_word32_t *Q); /* ptrs for memory allocation */ VARDECL(spx_word32_t *P); VARDECL(spx_word16_t *Q16); /* ptrs for memory allocation */ VARDECL(spx_word16_t *P16); spx_word32_t *px; /* ptrs of respective P'(z) & Q'(z) */ spx_word32_t *qx; spx_word32_t *p; spx_word32_t *q; spx_word16_t *pt; /* ptr used for cheb_poly_eval() whether P' or Q' */ int roots=0; /* DR 8/2/94: number of roots found */ flag = 1; /* program is searching for a root when, 1 else has found one */ m = lpcrdr/2; /* order of P'(z) & Q'(z) polynomials */ /* Allocate memory space for polynomials */ ALLOC(Q, (m+1), spx_word32_t); ALLOC(P, (m+1), spx_word32_t); /* determine P'(z)'s and Q'(z)'s coefficients where P'(z) = P(z)/(1 + z^(-1)) and Q'(z) = Q(z)/(1-z^(-1)) */ px = P; /* initialise ptrs */ qx = Q; p = px; q = qx;#ifdef FIXED_POINT *px++ = LPC_SCALING; *qx++ = LPC_SCALING; for(i=0;i<m;i++){ *px++ = SUB32(ADD32(EXTEND32(a[i]),EXTEND32(a[lpcrdr-i-1])), *p++); *qx++ = ADD32(SUB32(EXTEND32(a[i]),EXTEND32(a[lpcrdr-i-1])), *q++); } px = P; qx = Q; for(i=0;i<m;i++) { /*if (fabs(*px)>=32768) speex_warning_int("px", *px); if (fabs(*qx)>=32768) speex_warning_int("qx", *qx);*/ *px = PSHR32(*px,2); *qx = PSHR32(*qx,2); px++; qx++; } /* The reason for this lies in the way cheb_poly_eva() is implemented for fixed-point */ P[m] = PSHR32(P[m],3); Q[m] = PSHR32(Q[m],3);#else *px++ = LPC_SCALING; *qx++ = LPC_SCALING; for(i=0;i<m;i++){ *px++ = (a[i]+a[lpcrdr-1-i]) - *p++; *qx++ = (a[i]-a[lpcrdr-1-i]) + *q++; } px = P; qx = Q; for(i=0;i<m;i++){ *px = 2**px; *qx = 2**qx; px++; qx++; }#endif px = P; /* re-initialise ptrs */ qx = Q; /* now that we have computed P and Q convert to 16 bits to speed up cheb_poly_eval */ ALLOC(P16, m+1, spx_word16_t); ALLOC(Q16, m+1, spx_word16_t); for (i=0;i<m+1;i++) { P16[i] = P[i]; Q16[i] = Q[i]; } /* Search for a zero in P'(z) polynomial first and then alternate to Q'(z). Keep alternating between the two polynomials as each zero is found */ xr = 0; /* initialise xr to zero */ xl = FREQ_SCALE; /* start at point xl = 1 */ for(j=0;j<lpcrdr;j++){ if(j&1) /* determines whether P' or Q' is eval. */ pt = Q16; else pt = P16; psuml = cheb_poly_eva(pt,xl,m,stack); /* evals poly. at xl */ flag = 1; while(flag && (xr >= -FREQ_SCALE)){ spx_word16_t dd; /* Modified by JMV to provide smaller steps around x=+-1 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -