1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libopus/silk/float/solve_LS_FLP.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,207 @@ 1.4 +/*********************************************************************** 1.5 +Copyright (c) 2006-2011, Skype Limited. All rights reserved. 1.6 +Redistribution and use in source and binary forms, with or without 1.7 +modification, are permitted provided that the following conditions 1.8 +are met: 1.9 +- Redistributions of source code must retain the above copyright notice, 1.10 +this list of conditions and the following disclaimer. 1.11 +- Redistributions in binary form must reproduce the above copyright 1.12 +notice, this list of conditions and the following disclaimer in the 1.13 +documentation and/or other materials provided with the distribution. 1.14 +- Neither the name of Internet Society, IETF or IETF Trust, nor the 1.15 +names of specific contributors, may be used to endorse or promote 1.16 +products derived from this software without specific prior written 1.17 +permission. 1.18 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1.19 +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.20 +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.21 +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 1.22 +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.23 +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.24 +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 1.25 +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 1.26 +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.27 +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 1.28 +POSSIBILITY OF SUCH DAMAGE. 1.29 +***********************************************************************/ 1.30 + 1.31 +#ifdef HAVE_CONFIG_H 1.32 +#include "config.h" 1.33 +#endif 1.34 + 1.35 +#include "main_FLP.h" 1.36 +#include "tuning_parameters.h" 1.37 + 1.38 +/********************************************************************** 1.39 + * LDL Factorisation. Finds the upper triangular matrix L and the diagonal 1.40 + * Matrix D (only the diagonal elements returned in a vector)such that 1.41 + * the symmetric matric A is given by A = L*D*L'. 1.42 + **********************************************************************/ 1.43 +static OPUS_INLINE void silk_LDL_FLP( 1.44 + silk_float *A, /* I/O Pointer to Symetric Square Matrix */ 1.45 + opus_int M, /* I Size of Matrix */ 1.46 + silk_float *L, /* I/O Pointer to Square Upper triangular Matrix */ 1.47 + silk_float *Dinv /* I/O Pointer to vector holding the inverse diagonal elements of D */ 1.48 +); 1.49 + 1.50 +/********************************************************************** 1.51 + * Function to solve linear equation Ax = b, when A is a MxM lower 1.52 + * triangular matrix, with ones on the diagonal. 1.53 + **********************************************************************/ 1.54 +static OPUS_INLINE void silk_SolveWithLowerTriangularWdiagOnes_FLP( 1.55 + const silk_float *L, /* I Pointer to Lower Triangular Matrix */ 1.56 + opus_int M, /* I Dim of Matrix equation */ 1.57 + const silk_float *b, /* I b Vector */ 1.58 + silk_float *x /* O x Vector */ 1.59 +); 1.60 + 1.61 +/********************************************************************** 1.62 + * Function to solve linear equation (A^T)x = b, when A is a MxM lower 1.63 + * triangular, with ones on the diagonal. (ie then A^T is upper triangular) 1.64 + **********************************************************************/ 1.65 +static OPUS_INLINE void silk_SolveWithUpperTriangularFromLowerWdiagOnes_FLP( 1.66 + const silk_float *L, /* I Pointer to Lower Triangular Matrix */ 1.67 + opus_int M, /* I Dim of Matrix equation */ 1.68 + const silk_float *b, /* I b Vector */ 1.69 + silk_float *x /* O x Vector */ 1.70 +); 1.71 + 1.72 +/********************************************************************** 1.73 + * Function to solve linear equation Ax = b, when A is a MxM 1.74 + * symmetric square matrix - using LDL factorisation 1.75 + **********************************************************************/ 1.76 +void silk_solve_LDL_FLP( 1.77 + silk_float *A, /* I/O Symmetric square matrix, out: reg. */ 1.78 + const opus_int M, /* I Size of matrix */ 1.79 + const silk_float *b, /* I Pointer to b vector */ 1.80 + silk_float *x /* O Pointer to x solution vector */ 1.81 +) 1.82 +{ 1.83 + opus_int i; 1.84 + silk_float L[ MAX_MATRIX_SIZE ][ MAX_MATRIX_SIZE ]; 1.85 + silk_float T[ MAX_MATRIX_SIZE ]; 1.86 + silk_float Dinv[ MAX_MATRIX_SIZE ]; /* inverse diagonal elements of D*/ 1.87 + 1.88 + silk_assert( M <= MAX_MATRIX_SIZE ); 1.89 + 1.90 + /*************************************************** 1.91 + Factorize A by LDL such that A = L*D*(L^T), 1.92 + where L is lower triangular with ones on diagonal 1.93 + ****************************************************/ 1.94 + silk_LDL_FLP( A, M, &L[ 0 ][ 0 ], Dinv ); 1.95 + 1.96 + /**************************************************** 1.97 + * substitute D*(L^T) = T. ie: 1.98 + L*D*(L^T)*x = b => L*T = b <=> T = inv(L)*b 1.99 + ******************************************************/ 1.100 + silk_SolveWithLowerTriangularWdiagOnes_FLP( &L[ 0 ][ 0 ], M, b, T ); 1.101 + 1.102 + /**************************************************** 1.103 + D*(L^T)*x = T <=> (L^T)*x = inv(D)*T, because D is 1.104 + diagonal just multiply with 1/d_i 1.105 + ****************************************************/ 1.106 + for( i = 0; i < M; i++ ) { 1.107 + T[ i ] = T[ i ] * Dinv[ i ]; 1.108 + } 1.109 + /**************************************************** 1.110 + x = inv(L') * inv(D) * T 1.111 + *****************************************************/ 1.112 + silk_SolveWithUpperTriangularFromLowerWdiagOnes_FLP( &L[ 0 ][ 0 ], M, T, x ); 1.113 +} 1.114 + 1.115 +static OPUS_INLINE void silk_SolveWithUpperTriangularFromLowerWdiagOnes_FLP( 1.116 + const silk_float *L, /* I Pointer to Lower Triangular Matrix */ 1.117 + opus_int M, /* I Dim of Matrix equation */ 1.118 + const silk_float *b, /* I b Vector */ 1.119 + silk_float *x /* O x Vector */ 1.120 +) 1.121 +{ 1.122 + opus_int i, j; 1.123 + silk_float temp; 1.124 + const silk_float *ptr1; 1.125 + 1.126 + for( i = M - 1; i >= 0; i-- ) { 1.127 + ptr1 = matrix_adr( L, 0, i, M ); 1.128 + temp = 0; 1.129 + for( j = M - 1; j > i ; j-- ) { 1.130 + temp += ptr1[ j * M ] * x[ j ]; 1.131 + } 1.132 + temp = b[ i ] - temp; 1.133 + x[ i ] = temp; 1.134 + } 1.135 +} 1.136 + 1.137 +static OPUS_INLINE void silk_SolveWithLowerTriangularWdiagOnes_FLP( 1.138 + const silk_float *L, /* I Pointer to Lower Triangular Matrix */ 1.139 + opus_int M, /* I Dim of Matrix equation */ 1.140 + const silk_float *b, /* I b Vector */ 1.141 + silk_float *x /* O x Vector */ 1.142 +) 1.143 +{ 1.144 + opus_int i, j; 1.145 + silk_float temp; 1.146 + const silk_float *ptr1; 1.147 + 1.148 + for( i = 0; i < M; i++ ) { 1.149 + ptr1 = matrix_adr( L, i, 0, M ); 1.150 + temp = 0; 1.151 + for( j = 0; j < i; j++ ) { 1.152 + temp += ptr1[ j ] * x[ j ]; 1.153 + } 1.154 + temp = b[ i ] - temp; 1.155 + x[ i ] = temp; 1.156 + } 1.157 +} 1.158 + 1.159 +static OPUS_INLINE void silk_LDL_FLP( 1.160 + silk_float *A, /* I/O Pointer to Symetric Square Matrix */ 1.161 + opus_int M, /* I Size of Matrix */ 1.162 + silk_float *L, /* I/O Pointer to Square Upper triangular Matrix */ 1.163 + silk_float *Dinv /* I/O Pointer to vector holding the inverse diagonal elements of D */ 1.164 +) 1.165 +{ 1.166 + opus_int i, j, k, loop_count, err = 1; 1.167 + silk_float *ptr1, *ptr2; 1.168 + double temp, diag_min_value; 1.169 + silk_float v[ MAX_MATRIX_SIZE ], D[ MAX_MATRIX_SIZE ]; /* temp arrays*/ 1.170 + 1.171 + silk_assert( M <= MAX_MATRIX_SIZE ); 1.172 + 1.173 + diag_min_value = FIND_LTP_COND_FAC * 0.5f * ( A[ 0 ] + A[ M * M - 1 ] ); 1.174 + for( loop_count = 0; loop_count < M && err == 1; loop_count++ ) { 1.175 + err = 0; 1.176 + for( j = 0; j < M; j++ ) { 1.177 + ptr1 = matrix_adr( L, j, 0, M ); 1.178 + temp = matrix_ptr( A, j, j, M ); /* element in row j column j*/ 1.179 + for( i = 0; i < j; i++ ) { 1.180 + v[ i ] = ptr1[ i ] * D[ i ]; 1.181 + temp -= ptr1[ i ] * v[ i ]; 1.182 + } 1.183 + if( temp < diag_min_value ) { 1.184 + /* Badly conditioned matrix: add white noise and run again */ 1.185 + temp = ( loop_count + 1 ) * diag_min_value - temp; 1.186 + for( i = 0; i < M; i++ ) { 1.187 + matrix_ptr( A, i, i, M ) += ( silk_float )temp; 1.188 + } 1.189 + err = 1; 1.190 + break; 1.191 + } 1.192 + D[ j ] = ( silk_float )temp; 1.193 + Dinv[ j ] = ( silk_float )( 1.0f / temp ); 1.194 + matrix_ptr( L, j, j, M ) = 1.0f; 1.195 + 1.196 + ptr1 = matrix_adr( A, j, 0, M ); 1.197 + ptr2 = matrix_adr( L, j + 1, 0, M); 1.198 + for( i = j + 1; i < M; i++ ) { 1.199 + temp = 0.0; 1.200 + for( k = 0; k < j; k++ ) { 1.201 + temp += ptr2[ k ] * v[ k ]; 1.202 + } 1.203 + matrix_ptr( L, i, j, M ) = ( silk_float )( ( ptr1[ i ] - temp ) * Dinv[ j ] ); 1.204 + ptr2 += M; /* go to next column*/ 1.205 + } 1.206 + } 1.207 + } 1.208 + silk_assert( err == 0 ); 1.209 +} 1.210 +