michael@0: /*********************************************************************** michael@0: Copyright (c) 2006-2011, Skype Limited. All rights reserved. michael@0: Redistribution and use in source and binary forms, with or without michael@0: modification, are permitted provided that the following conditions michael@0: are met: michael@0: - Redistributions of source code must retain the above copyright notice, michael@0: this list of conditions and the following disclaimer. michael@0: - Redistributions in binary form must reproduce the above copyright michael@0: notice, this list of conditions and the following disclaimer in the michael@0: documentation and/or other materials provided with the distribution. michael@0: - Neither the name of Internet Society, IETF or IETF Trust, nor the michael@0: names of specific contributors, may be used to endorse or promote michael@0: products derived from this software without specific prior written michael@0: permission. michael@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" michael@0: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE michael@0: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR michael@0: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF michael@0: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS michael@0: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN michael@0: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) michael@0: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE michael@0: POSSIBILITY OF SUCH DAMAGE. michael@0: ***********************************************************************/ michael@0: michael@0: #ifdef HAVE_CONFIG_H michael@0: #include "config.h" michael@0: #endif michael@0: michael@0: #include "main_FLP.h" michael@0: #include "tuning_parameters.h" michael@0: michael@0: void silk_find_LTP_FLP( michael@0: silk_float b[ MAX_NB_SUBFR * LTP_ORDER ], /* O LTP coefs */ michael@0: silk_float WLTP[ MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER ], /* O Weight for LTP quantization */ michael@0: silk_float *LTPredCodGain, /* O LTP coding gain */ michael@0: const silk_float r_lpc[], /* I LPC residual */ michael@0: const opus_int lag[ MAX_NB_SUBFR ], /* I LTP lags */ michael@0: const silk_float Wght[ MAX_NB_SUBFR ], /* I Weights */ michael@0: const opus_int subfr_length, /* I Subframe length */ michael@0: const opus_int nb_subfr, /* I number of subframes */ michael@0: const opus_int mem_offset /* I Number of samples in LTP memory */ michael@0: ) michael@0: { michael@0: opus_int i, k; michael@0: silk_float *b_ptr, temp, *WLTP_ptr; michael@0: silk_float LPC_res_nrg, LPC_LTP_res_nrg; michael@0: silk_float d[ MAX_NB_SUBFR ], m, g, delta_b[ LTP_ORDER ]; michael@0: silk_float w[ MAX_NB_SUBFR ], nrg[ MAX_NB_SUBFR ], regu; michael@0: silk_float Rr[ LTP_ORDER ], rr[ MAX_NB_SUBFR ]; michael@0: const silk_float *r_ptr, *lag_ptr; michael@0: michael@0: b_ptr = b; michael@0: WLTP_ptr = WLTP; michael@0: r_ptr = &r_lpc[ mem_offset ]; michael@0: for( k = 0; k < nb_subfr; k++ ) { michael@0: lag_ptr = r_ptr - ( lag[ k ] + LTP_ORDER / 2 ); michael@0: michael@0: silk_corrMatrix_FLP( lag_ptr, subfr_length, LTP_ORDER, WLTP_ptr ); michael@0: silk_corrVector_FLP( lag_ptr, r_ptr, subfr_length, LTP_ORDER, Rr ); michael@0: michael@0: rr[ k ] = ( silk_float )silk_energy_FLP( r_ptr, subfr_length ); michael@0: regu = 1.0f + rr[ k ] + michael@0: matrix_ptr( WLTP_ptr, 0, 0, LTP_ORDER ) + michael@0: matrix_ptr( WLTP_ptr, LTP_ORDER-1, LTP_ORDER-1, LTP_ORDER ); michael@0: regu *= LTP_DAMPING / 3; michael@0: silk_regularize_correlations_FLP( WLTP_ptr, &rr[ k ], regu, LTP_ORDER ); michael@0: silk_solve_LDL_FLP( WLTP_ptr, LTP_ORDER, Rr, b_ptr ); michael@0: michael@0: /* Calculate residual energy */ michael@0: nrg[ k ] = silk_residual_energy_covar_FLP( b_ptr, WLTP_ptr, Rr, rr[ k ], LTP_ORDER ); michael@0: michael@0: temp = Wght[ k ] / ( nrg[ k ] * Wght[ k ] + 0.01f * subfr_length ); michael@0: silk_scale_vector_FLP( WLTP_ptr, temp, LTP_ORDER * LTP_ORDER ); michael@0: w[ k ] = matrix_ptr( WLTP_ptr, LTP_ORDER / 2, LTP_ORDER / 2, LTP_ORDER ); michael@0: michael@0: r_ptr += subfr_length; michael@0: b_ptr += LTP_ORDER; michael@0: WLTP_ptr += LTP_ORDER * LTP_ORDER; michael@0: } michael@0: michael@0: /* Compute LTP coding gain */ michael@0: if( LTPredCodGain != NULL ) { michael@0: LPC_LTP_res_nrg = 1e-6f; michael@0: LPC_res_nrg = 0.0f; michael@0: for( k = 0; k < nb_subfr; k++ ) { michael@0: LPC_res_nrg += rr[ k ] * Wght[ k ]; michael@0: LPC_LTP_res_nrg += nrg[ k ] * Wght[ k ]; michael@0: } michael@0: michael@0: silk_assert( LPC_LTP_res_nrg > 0 ); michael@0: *LTPredCodGain = 3.0f * silk_log2( LPC_res_nrg / LPC_LTP_res_nrg ); michael@0: } michael@0: michael@0: /* Smoothing */ michael@0: /* d = sum( B, 1 ); */ michael@0: b_ptr = b; michael@0: for( k = 0; k < nb_subfr; k++ ) { michael@0: d[ k ] = 0; michael@0: for( i = 0; i < LTP_ORDER; i++ ) { michael@0: d[ k ] += b_ptr[ i ]; michael@0: } michael@0: b_ptr += LTP_ORDER; michael@0: } michael@0: /* m = ( w * d' ) / ( sum( w ) + 1e-3 ); */ michael@0: temp = 1e-3f; michael@0: for( k = 0; k < nb_subfr; k++ ) { michael@0: temp += w[ k ]; michael@0: } michael@0: m = 0; michael@0: for( k = 0; k < nb_subfr; k++ ) { michael@0: m += d[ k ] * w[ k ]; michael@0: } michael@0: m = m / temp; michael@0: michael@0: b_ptr = b; michael@0: for( k = 0; k < nb_subfr; k++ ) { michael@0: g = LTP_SMOOTHING / ( LTP_SMOOTHING + w[ k ] ) * ( m - d[ k ] ); michael@0: temp = 0; michael@0: for( i = 0; i < LTP_ORDER; i++ ) { michael@0: delta_b[ i ] = silk_max_float( b_ptr[ i ], 0.1f ); michael@0: temp += delta_b[ i ]; michael@0: } michael@0: temp = g / temp; michael@0: for( i = 0; i < LTP_ORDER; i++ ) { michael@0: b_ptr[ i ] = b_ptr[ i ] + delta_b[ i ] * temp; michael@0: } michael@0: b_ptr += LTP_ORDER; michael@0: } michael@0: }