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