A2NLSF.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /***********************************************************************
  2. Copyright (c) 2006-2011, Skype Limited. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. - Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. - Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. - Neither the name of Internet Society, IETF or IETF Trust, nor the
  12. names of specific contributors, may be used to endorse or promote
  13. products derived from this software without specific prior written
  14. permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. ***********************************************************************/
  27. /* Conversion between prediction filter coefficients and NLSFs */
  28. /* Requires the order to be an even number */
  29. /* A piecewise linear approximation maps LSF <-> cos(LSF) */
  30. /* Therefore the result is not accurate NLSFs, but the two */
  31. /* functions are accurate inverses of each other */
  32. #ifdef HAVE_CONFIG_H
  33. #include "config.h"
  34. #endif
  35. #include "SigProc_FIX.h"
  36. #include "tables.h"
  37. /* Number of binary divisions, when not in low complexity mode */
  38. #define BIN_DIV_STEPS_A2NLSF_FIX 3 /* must be no higher than 16 - log2( LSF_COS_TAB_SZ_FIX ) */
  39. #define MAX_ITERATIONS_A2NLSF_FIX 30
  40. /* Helper function for A2NLSF(..) */
  41. /* Transforms polynomials from cos(n*f) to cos(f)^n */
  42. static OPUS_INLINE void silk_A2NLSF_trans_poly(
  43. opus_int32 *p, /* I/O Polynomial */
  44. const opus_int dd /* I Polynomial order (= filter order / 2 ) */
  45. )
  46. {
  47. opus_int k, n;
  48. for( k = 2; k <= dd; k++ ) {
  49. for( n = dd; n > k; n-- ) {
  50. p[ n - 2 ] -= p[ n ];
  51. }
  52. p[ k - 2 ] -= silk_LSHIFT( p[ k ], 1 );
  53. }
  54. }
  55. /* Helper function for A2NLSF(..) */
  56. /* Polynomial evaluation */
  57. static OPUS_INLINE opus_int32 silk_A2NLSF_eval_poly( /* return the polynomial evaluation, in Q16 */
  58. opus_int32 *p, /* I Polynomial, Q16 */
  59. const opus_int32 x, /* I Evaluation point, Q12 */
  60. const opus_int dd /* I Order */
  61. )
  62. {
  63. opus_int n;
  64. opus_int32 x_Q16, y32;
  65. y32 = p[ dd ]; /* Q16 */
  66. x_Q16 = silk_LSHIFT( x, 4 );
  67. if ( opus_likely( 8 == dd ) )
  68. {
  69. y32 = silk_SMLAWW( p[ 7 ], y32, x_Q16 );
  70. y32 = silk_SMLAWW( p[ 6 ], y32, x_Q16 );
  71. y32 = silk_SMLAWW( p[ 5 ], y32, x_Q16 );
  72. y32 = silk_SMLAWW( p[ 4 ], y32, x_Q16 );
  73. y32 = silk_SMLAWW( p[ 3 ], y32, x_Q16 );
  74. y32 = silk_SMLAWW( p[ 2 ], y32, x_Q16 );
  75. y32 = silk_SMLAWW( p[ 1 ], y32, x_Q16 );
  76. y32 = silk_SMLAWW( p[ 0 ], y32, x_Q16 );
  77. }
  78. else
  79. {
  80. for( n = dd - 1; n >= 0; n-- ) {
  81. y32 = silk_SMLAWW( p[ n ], y32, x_Q16 ); /* Q16 */
  82. }
  83. }
  84. return y32;
  85. }
  86. static OPUS_INLINE void silk_A2NLSF_init(
  87. const opus_int32 *a_Q16,
  88. opus_int32 *P,
  89. opus_int32 *Q,
  90. const opus_int dd
  91. )
  92. {
  93. opus_int k;
  94. /* Convert filter coefs to even and odd polynomials */
  95. P[dd] = silk_LSHIFT( 1, 16 );
  96. Q[dd] = silk_LSHIFT( 1, 16 );
  97. for( k = 0; k < dd; k++ ) {
  98. P[ k ] = -a_Q16[ dd - k - 1 ] - a_Q16[ dd + k ]; /* Q16 */
  99. Q[ k ] = -a_Q16[ dd - k - 1 ] + a_Q16[ dd + k ]; /* Q16 */
  100. }
  101. /* Divide out zeros as we have that for even filter orders, */
  102. /* z = 1 is always a root in Q, and */
  103. /* z = -1 is always a root in P */
  104. for( k = dd; k > 0; k-- ) {
  105. P[ k - 1 ] -= P[ k ];
  106. Q[ k - 1 ] += Q[ k ];
  107. }
  108. /* Transform polynomials from cos(n*f) to cos(f)^n */
  109. silk_A2NLSF_trans_poly( P, dd );
  110. silk_A2NLSF_trans_poly( Q, dd );
  111. }
  112. /* Compute Normalized Line Spectral Frequencies (NLSFs) from whitening filter coefficients */
  113. /* If not all roots are found, the a_Q16 coefficients are bandwidth expanded until convergence. */
  114. void silk_A2NLSF(
  115. opus_int16 *NLSF, /* O Normalized Line Spectral Frequencies in Q15 (0..2^15-1) [d] */
  116. opus_int32 *a_Q16, /* I/O Monic whitening filter coefficients in Q16 [d] */
  117. const opus_int d /* I Filter order (must be even) */
  118. )
  119. {
  120. opus_int i, k, m, dd, root_ix, ffrac;
  121. opus_int32 xlo, xhi, xmid;
  122. opus_int32 ylo, yhi, ymid, thr;
  123. opus_int32 nom, den;
  124. opus_int32 P[ SILK_MAX_ORDER_LPC / 2 + 1 ];
  125. opus_int32 Q[ SILK_MAX_ORDER_LPC / 2 + 1 ];
  126. opus_int32 *PQ[ 2 ];
  127. opus_int32 *p;
  128. /* Store pointers to array */
  129. PQ[ 0 ] = P;
  130. PQ[ 1 ] = Q;
  131. dd = silk_RSHIFT( d, 1 );
  132. silk_A2NLSF_init( a_Q16, P, Q, dd );
  133. /* Find roots, alternating between P and Q */
  134. p = P; /* Pointer to polynomial */
  135. xlo = silk_LSFCosTab_FIX_Q12[ 0 ]; /* Q12*/
  136. ylo = silk_A2NLSF_eval_poly( p, xlo, dd );
  137. if( ylo < 0 ) {
  138. /* Set the first NLSF to zero and move on to the next */
  139. NLSF[ 0 ] = 0;
  140. p = Q; /* Pointer to polynomial */
  141. ylo = silk_A2NLSF_eval_poly( p, xlo, dd );
  142. root_ix = 1; /* Index of current root */
  143. } else {
  144. root_ix = 0; /* Index of current root */
  145. }
  146. k = 1; /* Loop counter */
  147. i = 0; /* Counter for bandwidth expansions applied */
  148. thr = 0;
  149. while( 1 ) {
  150. /* Evaluate polynomial */
  151. xhi = silk_LSFCosTab_FIX_Q12[ k ]; /* Q12 */
  152. yhi = silk_A2NLSF_eval_poly( p, xhi, dd );
  153. /* Detect zero crossing */
  154. if( ( ylo <= 0 && yhi >= thr ) || ( ylo >= 0 && yhi <= -thr ) ) {
  155. if( yhi == 0 ) {
  156. /* If the root lies exactly at the end of the current */
  157. /* interval, look for the next root in the next interval */
  158. thr = 1;
  159. } else {
  160. thr = 0;
  161. }
  162. /* Binary division */
  163. ffrac = -256;
  164. for( m = 0; m < BIN_DIV_STEPS_A2NLSF_FIX; m++ ) {
  165. /* Evaluate polynomial */
  166. xmid = silk_RSHIFT_ROUND( xlo + xhi, 1 );
  167. ymid = silk_A2NLSF_eval_poly( p, xmid, dd );
  168. /* Detect zero crossing */
  169. if( ( ylo <= 0 && ymid >= 0 ) || ( ylo >= 0 && ymid <= 0 ) ) {
  170. /* Reduce frequency */
  171. xhi = xmid;
  172. yhi = ymid;
  173. } else {
  174. /* Increase frequency */
  175. xlo = xmid;
  176. ylo = ymid;
  177. ffrac = silk_ADD_RSHIFT( ffrac, 128, m );
  178. }
  179. }
  180. /* Interpolate */
  181. if( silk_abs( ylo ) < 65536 ) {
  182. /* Avoid dividing by zero */
  183. den = ylo - yhi;
  184. nom = silk_LSHIFT( ylo, 8 - BIN_DIV_STEPS_A2NLSF_FIX ) + silk_RSHIFT( den, 1 );
  185. if( den != 0 ) {
  186. ffrac += silk_DIV32( nom, den );
  187. }
  188. } else {
  189. /* No risk of dividing by zero because abs(ylo - yhi) >= abs(ylo) >= 65536 */
  190. ffrac += silk_DIV32( ylo, silk_RSHIFT( ylo - yhi, 8 - BIN_DIV_STEPS_A2NLSF_FIX ) );
  191. }
  192. NLSF[ root_ix ] = (opus_int16)silk_min_32( silk_LSHIFT( (opus_int32)k, 8 ) + ffrac, silk_int16_MAX );
  193. silk_assert( NLSF[ root_ix ] >= 0 );
  194. root_ix++; /* Next root */
  195. if( root_ix >= d ) {
  196. /* Found all roots */
  197. break;
  198. }
  199. /* Alternate pointer to polynomial */
  200. p = PQ[ root_ix & 1 ];
  201. /* Evaluate polynomial */
  202. xlo = silk_LSFCosTab_FIX_Q12[ k - 1 ]; /* Q12*/
  203. ylo = silk_LSHIFT( 1 - ( root_ix & 2 ), 12 );
  204. } else {
  205. /* Increment loop counter */
  206. k++;
  207. xlo = xhi;
  208. ylo = yhi;
  209. thr = 0;
  210. if( k > LSF_COS_TAB_SZ_FIX ) {
  211. i++;
  212. if( i > MAX_ITERATIONS_A2NLSF_FIX ) {
  213. /* Set NLSFs to white spectrum and exit */
  214. NLSF[ 0 ] = (opus_int16)silk_DIV32_16( 1 << 15, d + 1 );
  215. for( k = 1; k < d; k++ ) {
  216. NLSF[ k ] = (opus_int16)silk_SMULBB( k + 1, NLSF[ 0 ] );
  217. }
  218. return;
  219. }
  220. /* Error: Apply progressively more bandwidth expansion and run again */
  221. silk_bwexpander_32( a_Q16, d, 65536 - silk_SMULBB( 10 + i, i ) ); /* 10_Q16 = 0.00015*/
  222. silk_A2NLSF_init( a_Q16, P, Q, dd );
  223. p = P; /* Pointer to polynomial */
  224. xlo = silk_LSFCosTab_FIX_Q12[ 0 ]; /* Q12*/
  225. ylo = silk_A2NLSF_eval_poly( p, xlo, dd );
  226. if( ylo < 0 ) {
  227. /* Set the first NLSF to zero and move on to the next */
  228. NLSF[ 0 ] = 0;
  229. p = Q; /* Pointer to polynomial */
  230. ylo = silk_A2NLSF_eval_poly( p, xlo, dd );
  231. root_ix = 1; /* Index of current root */
  232. } else {
  233. root_ix = 0; /* Index of current root */
  234. }
  235. k = 1; /* Reset loop counter */
  236. }
  237. }
  238. }
  239. }