solve_LS_FLP.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #include "tuning_parameters.h"
  32. /**********************************************************************
  33. * LDL Factorisation. Finds the upper triangular matrix L and the diagonal
  34. * Matrix D (only the diagonal elements returned in a vector)such that
  35. * the symmetric matric A is given by A = L*D*L'.
  36. **********************************************************************/
  37. static OPUS_INLINE void silk_LDL_FLP(
  38. silk_float *A, /* I/O Pointer to Symetric Square Matrix */
  39. opus_int M, /* I Size of Matrix */
  40. silk_float *L, /* I/O Pointer to Square Upper triangular Matrix */
  41. silk_float *Dinv /* I/O Pointer to vector holding the inverse diagonal elements of D */
  42. );
  43. /**********************************************************************
  44. * Function to solve linear equation Ax = b, when A is a MxM lower
  45. * triangular matrix, with ones on the diagonal.
  46. **********************************************************************/
  47. static OPUS_INLINE void silk_SolveWithLowerTriangularWdiagOnes_FLP(
  48. const silk_float *L, /* I Pointer to Lower Triangular Matrix */
  49. opus_int M, /* I Dim of Matrix equation */
  50. const silk_float *b, /* I b Vector */
  51. silk_float *x /* O x Vector */
  52. );
  53. /**********************************************************************
  54. * Function to solve linear equation (A^T)x = b, when A is a MxM lower
  55. * triangular, with ones on the diagonal. (ie then A^T is upper triangular)
  56. **********************************************************************/
  57. static OPUS_INLINE void silk_SolveWithUpperTriangularFromLowerWdiagOnes_FLP(
  58. const silk_float *L, /* I Pointer to Lower Triangular Matrix */
  59. opus_int M, /* I Dim of Matrix equation */
  60. const silk_float *b, /* I b Vector */
  61. silk_float *x /* O x Vector */
  62. );
  63. /**********************************************************************
  64. * Function to solve linear equation Ax = b, when A is a MxM
  65. * symmetric square matrix - using LDL factorisation
  66. **********************************************************************/
  67. void silk_solve_LDL_FLP(
  68. silk_float *A, /* I/O Symmetric square matrix, out: reg. */
  69. const opus_int M, /* I Size of matrix */
  70. const silk_float *b, /* I Pointer to b vector */
  71. silk_float *x /* O Pointer to x solution vector */
  72. )
  73. {
  74. opus_int i;
  75. silk_float L[ MAX_MATRIX_SIZE ][ MAX_MATRIX_SIZE ];
  76. silk_float T[ MAX_MATRIX_SIZE ];
  77. silk_float Dinv[ MAX_MATRIX_SIZE ]; /* inverse diagonal elements of D*/
  78. silk_assert( M <= MAX_MATRIX_SIZE );
  79. /***************************************************
  80. Factorize A by LDL such that A = L*D*(L^T),
  81. where L is lower triangular with ones on diagonal
  82. ****************************************************/
  83. silk_LDL_FLP( A, M, &L[ 0 ][ 0 ], Dinv );
  84. /****************************************************
  85. * substitute D*(L^T) = T. ie:
  86. L*D*(L^T)*x = b => L*T = b <=> T = inv(L)*b
  87. ******************************************************/
  88. silk_SolveWithLowerTriangularWdiagOnes_FLP( &L[ 0 ][ 0 ], M, b, T );
  89. /****************************************************
  90. D*(L^T)*x = T <=> (L^T)*x = inv(D)*T, because D is
  91. diagonal just multiply with 1/d_i
  92. ****************************************************/
  93. for( i = 0; i < M; i++ ) {
  94. T[ i ] = T[ i ] * Dinv[ i ];
  95. }
  96. /****************************************************
  97. x = inv(L') * inv(D) * T
  98. *****************************************************/
  99. silk_SolveWithUpperTriangularFromLowerWdiagOnes_FLP( &L[ 0 ][ 0 ], M, T, x );
  100. }
  101. static OPUS_INLINE void silk_SolveWithUpperTriangularFromLowerWdiagOnes_FLP(
  102. const silk_float *L, /* I Pointer to Lower Triangular Matrix */
  103. opus_int M, /* I Dim of Matrix equation */
  104. const silk_float *b, /* I b Vector */
  105. silk_float *x /* O x Vector */
  106. )
  107. {
  108. opus_int i, j;
  109. silk_float temp;
  110. const silk_float *ptr1;
  111. for( i = M - 1; i >= 0; i-- ) {
  112. ptr1 = matrix_adr( L, 0, i, M );
  113. temp = 0;
  114. for( j = M - 1; j > i ; j-- ) {
  115. temp += ptr1[ j * M ] * x[ j ];
  116. }
  117. temp = b[ i ] - temp;
  118. x[ i ] = temp;
  119. }
  120. }
  121. static OPUS_INLINE void silk_SolveWithLowerTriangularWdiagOnes_FLP(
  122. const silk_float *L, /* I Pointer to Lower Triangular Matrix */
  123. opus_int M, /* I Dim of Matrix equation */
  124. const silk_float *b, /* I b Vector */
  125. silk_float *x /* O x Vector */
  126. )
  127. {
  128. opus_int i, j;
  129. silk_float temp;
  130. const silk_float *ptr1;
  131. for( i = 0; i < M; i++ ) {
  132. ptr1 = matrix_adr( L, i, 0, M );
  133. temp = 0;
  134. for( j = 0; j < i; j++ ) {
  135. temp += ptr1[ j ] * x[ j ];
  136. }
  137. temp = b[ i ] - temp;
  138. x[ i ] = temp;
  139. }
  140. }
  141. static OPUS_INLINE void silk_LDL_FLP(
  142. silk_float *A, /* I/O Pointer to Symetric Square Matrix */
  143. opus_int M, /* I Size of Matrix */
  144. silk_float *L, /* I/O Pointer to Square Upper triangular Matrix */
  145. silk_float *Dinv /* I/O Pointer to vector holding the inverse diagonal elements of D */
  146. )
  147. {
  148. opus_int i, j, k, loop_count, err = 1;
  149. silk_float *ptr1, *ptr2;
  150. double temp, diag_min_value;
  151. silk_float v[ MAX_MATRIX_SIZE ], D[ MAX_MATRIX_SIZE ]; /* temp arrays*/
  152. silk_assert( M <= MAX_MATRIX_SIZE );
  153. diag_min_value = FIND_LTP_COND_FAC * 0.5f * ( A[ 0 ] + A[ M * M - 1 ] );
  154. for( loop_count = 0; loop_count < M && err == 1; loop_count++ ) {
  155. err = 0;
  156. for( j = 0; j < M; j++ ) {
  157. ptr1 = matrix_adr( L, j, 0, M );
  158. temp = matrix_ptr( A, j, j, M ); /* element in row j column j*/
  159. for( i = 0; i < j; i++ ) {
  160. v[ i ] = ptr1[ i ] * D[ i ];
  161. temp -= ptr1[ i ] * v[ i ];
  162. }
  163. if( temp < diag_min_value ) {
  164. /* Badly conditioned matrix: add white noise and run again */
  165. temp = ( loop_count + 1 ) * diag_min_value - temp;
  166. for( i = 0; i < M; i++ ) {
  167. matrix_ptr( A, i, i, M ) += ( silk_float )temp;
  168. }
  169. err = 1;
  170. break;
  171. }
  172. D[ j ] = ( silk_float )temp;
  173. Dinv[ j ] = ( silk_float )( 1.0f / temp );
  174. matrix_ptr( L, j, j, M ) = 1.0f;
  175. ptr1 = matrix_adr( A, j, 0, M );
  176. ptr2 = matrix_adr( L, j + 1, 0, M);
  177. for( i = j + 1; i < M; i++ ) {
  178. temp = 0.0;
  179. for( k = 0; k < j; k++ ) {
  180. temp += ptr2[ k ] * v[ k ];
  181. }
  182. matrix_ptr( L, i, j, M ) = ( silk_float )( ( ptr1[ i ] - temp ) * Dinv[ j ] );
  183. ptr2 += M; /* go to next column*/
  184. }
  185. }
  186. }
  187. silk_assert( err == 0 );
  188. }