1/*
2 *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11/******************************************************************
12
13 iLBC Speech Coder ANSI-C Source Code
14
15 WebRtcIlbcfix_Lsp2Lsf.c
16
17******************************************************************/
18
19#include "defines.h"
20#include "constants.h"
21
22/*----------------------------------------------------------------*
23 *  conversion from LSP coefficients to LSF coefficients
24 *---------------------------------------------------------------*/
25
26void WebRtcIlbcfix_Lsp2Lsf(
27    int16_t *lsp, /* (i) lsp vector -1...+1 in Q15 */
28    int16_t *lsf, /* (o) Lsf vector 0...Pi in Q13
29                           (ordered, so that lsf[i]<lsf[i+1]) */
30    int16_t m  /* (i) Number of coefficients */
31                           )
32{
33  int16_t i, k;
34  int16_t diff; /* diff between table value and desired value (Q15) */
35  int16_t freq; /* lsf/(2*pi) (Q16) */
36  int16_t *lspPtr, *lsfPtr, *cosTblPtr;
37  int16_t tmp;
38
39  /* set the index to maximum index value in WebRtcIlbcfix_kCos */
40  k = 63;
41
42  /*
43     Start with the highest LSP and then work the way down
44     For each LSP the lsf is calculated by first order approximation
45     of the acos(x) function
46  */
47  lspPtr = &lsp[9];
48  lsfPtr = &lsf[9];
49  cosTblPtr=(int16_t*)&WebRtcIlbcfix_kCos[k];
50  for(i=m-1; i>=0; i--)
51  {
52    /*
53       locate value in the table, which is just above lsp[i],
54       basically an approximation to acos(x)
55    */
56    while( (((int32_t)(*cosTblPtr)-(*lspPtr)) < 0)&&(k>0) )
57    {
58      k-=1;
59      cosTblPtr--;
60    }
61
62    /* Calculate diff, which is used in the linear approximation of acos(x) */
63    diff = (*lspPtr)-(*cosTblPtr);
64
65    /*
66       The linear approximation of acos(lsp[i]) :
67       acos(lsp[i])= k*512 + (WebRtcIlbcfix_kAcosDerivative[ind]*offset >> 11)
68    */
69
70    /* tmp (linear offset) in Q16 */
71    tmp = (int16_t)WEBRTC_SPL_MUL_16_16_RSFT(WebRtcIlbcfix_kAcosDerivative[k],diff, 11);
72
73    /* freq in Q16 */
74    freq = (int16_t)WEBRTC_SPL_LSHIFT_W16(k,9)+tmp;
75
76    /* lsf = freq*2*pi */
77    (*lsfPtr) = (int16_t)(((int32_t)freq*25736)>>15);
78
79    lsfPtr--;
80    lspPtr--;
81  }
82
83  return;
84}
85