residual_energy_FLP.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include "main_FLP.h"
  31. #define MAX_ITERATIONS_RESIDUAL_NRG 10
  32. #define REGULARIZATION_FACTOR 1e-8f
  33. /* Residual energy: nrg = wxx - 2 * wXx * c + c' * wXX * c */
  34. silk_float silk_residual_energy_covar_FLP( /* O Weighted residual energy */
  35. const silk_float *c, /* I Filter coefficients */
  36. silk_float *wXX, /* I/O Weighted correlation matrix, reg. out */
  37. const silk_float *wXx, /* I Weighted correlation vector */
  38. const silk_float wxx, /* I Weighted correlation value */
  39. const opus_int D /* I Dimension */
  40. )
  41. {
  42. opus_int i, j, k;
  43. silk_float tmp, nrg = 0.0f, regularization;
  44. /* Safety checks */
  45. silk_assert( D >= 0 );
  46. regularization = REGULARIZATION_FACTOR * ( wXX[ 0 ] + wXX[ D * D - 1 ] );
  47. for( k = 0; k < MAX_ITERATIONS_RESIDUAL_NRG; k++ ) {
  48. nrg = wxx;
  49. tmp = 0.0f;
  50. for( i = 0; i < D; i++ ) {
  51. tmp += wXx[ i ] * c[ i ];
  52. }
  53. nrg -= 2.0f * tmp;
  54. /* compute c' * wXX * c, assuming wXX is symmetric */
  55. for( i = 0; i < D; i++ ) {
  56. tmp = 0.0f;
  57. for( j = i + 1; j < D; j++ ) {
  58. tmp += matrix_c_ptr( wXX, i, j, D ) * c[ j ];
  59. }
  60. nrg += c[ i ] * ( 2.0f * tmp + matrix_c_ptr( wXX, i, i, D ) * c[ i ] );
  61. }
  62. if( nrg > 0 ) {
  63. break;
  64. } else {
  65. /* Add white noise */
  66. for( i = 0; i < D; i++ ) {
  67. matrix_c_ptr( wXX, i, i, D ) += regularization;
  68. }
  69. /* Increase noise for next run */
  70. regularization *= 2.0f;
  71. }
  72. }
  73. if( k == MAX_ITERATIONS_RESIDUAL_NRG ) {
  74. silk_assert( nrg == 0 );
  75. nrg = 1.0f;
  76. }
  77. return nrg;
  78. }
  79. /* Calculates residual energies of input subframes where all subframes have LPC_order */
  80. /* of preceding samples */
  81. void silk_residual_energy_FLP(
  82. silk_float nrgs[ MAX_NB_SUBFR ], /* O Residual energy per subframe */
  83. const silk_float x[], /* I Input signal */
  84. silk_float a[ 2 ][ MAX_LPC_ORDER ], /* I AR coefs for each frame half */
  85. const silk_float gains[], /* I Quantization gains */
  86. const opus_int subfr_length, /* I Subframe length */
  87. const opus_int nb_subfr, /* I number of subframes */
  88. const opus_int LPC_order /* I LPC order */
  89. )
  90. {
  91. opus_int shift;
  92. silk_float *LPC_res_ptr, LPC_res[ ( MAX_FRAME_LENGTH + MAX_NB_SUBFR * MAX_LPC_ORDER ) / 2 ];
  93. LPC_res_ptr = LPC_res + LPC_order;
  94. shift = LPC_order + subfr_length;
  95. /* Filter input to create the LPC residual for each frame half, and measure subframe energies */
  96. silk_LPC_analysis_filter_FLP( LPC_res, a[ 0 ], x + 0 * shift, 2 * shift, LPC_order );
  97. nrgs[ 0 ] = ( silk_float )( gains[ 0 ] * gains[ 0 ] * silk_energy_FLP( LPC_res_ptr + 0 * shift, subfr_length ) );
  98. nrgs[ 1 ] = ( silk_float )( gains[ 1 ] * gains[ 1 ] * silk_energy_FLP( LPC_res_ptr + 1 * shift, subfr_length ) );
  99. if( nb_subfr == MAX_NB_SUBFR ) {
  100. silk_LPC_analysis_filter_FLP( LPC_res, a[ 1 ], x + 2 * shift, 2 * shift, LPC_order );
  101. nrgs[ 2 ] = ( silk_float )( gains[ 2 ] * gains[ 2 ] * silk_energy_FLP( LPC_res_ptr + 0 * shift, subfr_length ) );
  102. nrgs[ 3 ] = ( silk_float )( gains[ 3 ] * gains[ 3 ] * silk_energy_FLP( LPC_res_ptr + 1 * shift, subfr_length ) );
  103. }
  104. }