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: /* NLSF stabilizer: */ michael@0: /* */ michael@0: /* - Moves NLSFs further apart if they are too close */ michael@0: /* - Moves NLSFs away from borders if they are too close */ michael@0: /* - High effort to achieve a modification with minimum */ michael@0: /* Euclidean distance to input vector */ michael@0: /* - Output are sorted NLSF coefficients */ michael@0: /* */ michael@0: michael@0: #include "SigProc_FIX.h" michael@0: michael@0: /* Constant Definitions */ michael@0: #define MAX_LOOPS 20 michael@0: michael@0: /* NLSF stabilizer, for a single input data vector */ michael@0: void silk_NLSF_stabilize( michael@0: opus_int16 *NLSF_Q15, /* I/O Unstable/stabilized normalized LSF vector in Q15 [L] */ michael@0: const opus_int16 *NDeltaMin_Q15, /* I Min distance vector, NDeltaMin_Q15[L] must be >= 1 [L+1] */ michael@0: const opus_int L /* I Number of NLSF parameters in the input vector */ michael@0: ) michael@0: { michael@0: opus_int i, I=0, k, loops; michael@0: opus_int16 center_freq_Q15; michael@0: opus_int32 diff_Q15, min_diff_Q15, min_center_Q15, max_center_Q15; michael@0: michael@0: /* This is necessary to ensure an output within range of a opus_int16 */ michael@0: silk_assert( NDeltaMin_Q15[L] >= 1 ); michael@0: michael@0: for( loops = 0; loops < MAX_LOOPS; loops++ ) { michael@0: /**************************/ michael@0: /* Find smallest distance */ michael@0: /**************************/ michael@0: /* First element */ michael@0: min_diff_Q15 = NLSF_Q15[0] - NDeltaMin_Q15[0]; michael@0: I = 0; michael@0: /* Middle elements */ michael@0: for( i = 1; i <= L-1; i++ ) { michael@0: diff_Q15 = NLSF_Q15[i] - ( NLSF_Q15[i-1] + NDeltaMin_Q15[i] ); michael@0: if( diff_Q15 < min_diff_Q15 ) { michael@0: min_diff_Q15 = diff_Q15; michael@0: I = i; michael@0: } michael@0: } michael@0: /* Last element */ michael@0: diff_Q15 = ( 1 << 15 ) - ( NLSF_Q15[L-1] + NDeltaMin_Q15[L] ); michael@0: if( diff_Q15 < min_diff_Q15 ) { michael@0: min_diff_Q15 = diff_Q15; michael@0: I = L; michael@0: } michael@0: michael@0: /***************************************************/ michael@0: /* Now check if the smallest distance non-negative */ michael@0: /***************************************************/ michael@0: if( min_diff_Q15 >= 0 ) { michael@0: return; michael@0: } michael@0: michael@0: if( I == 0 ) { michael@0: /* Move away from lower limit */ michael@0: NLSF_Q15[0] = NDeltaMin_Q15[0]; michael@0: michael@0: } else if( I == L) { michael@0: /* Move away from higher limit */ michael@0: NLSF_Q15[L-1] = ( 1 << 15 ) - NDeltaMin_Q15[L]; michael@0: michael@0: } else { michael@0: /* Find the lower extreme for the location of the current center frequency */ michael@0: min_center_Q15 = 0; michael@0: for( k = 0; k < I; k++ ) { michael@0: min_center_Q15 += NDeltaMin_Q15[k]; michael@0: } michael@0: min_center_Q15 += silk_RSHIFT( NDeltaMin_Q15[I], 1 ); michael@0: michael@0: /* Find the upper extreme for the location of the current center frequency */ michael@0: max_center_Q15 = 1 << 15; michael@0: for( k = L; k > I; k-- ) { michael@0: max_center_Q15 -= NDeltaMin_Q15[k]; michael@0: } michael@0: max_center_Q15 -= silk_RSHIFT( NDeltaMin_Q15[I], 1 ); michael@0: michael@0: /* Move apart, sorted by value, keeping the same center frequency */ michael@0: center_freq_Q15 = (opus_int16)silk_LIMIT_32( silk_RSHIFT_ROUND( (opus_int32)NLSF_Q15[I-1] + (opus_int32)NLSF_Q15[I], 1 ), michael@0: min_center_Q15, max_center_Q15 ); michael@0: NLSF_Q15[I-1] = center_freq_Q15 - silk_RSHIFT( NDeltaMin_Q15[I], 1 ); michael@0: NLSF_Q15[I] = NLSF_Q15[I-1] + NDeltaMin_Q15[I]; michael@0: } michael@0: } michael@0: michael@0: /* Safe and simple fall back method, which is less ideal than the above */ michael@0: if( loops == MAX_LOOPS ) michael@0: { michael@0: /* Insertion sort (fast for already almost sorted arrays): */ michael@0: /* Best case: O(n) for an already sorted array */ michael@0: /* Worst case: O(n^2) for an inversely sorted array */ michael@0: silk_insertion_sort_increasing_all_values_int16( &NLSF_Q15[0], L ); michael@0: michael@0: /* First NLSF should be no less than NDeltaMin[0] */ michael@0: NLSF_Q15[0] = silk_max_int( NLSF_Q15[0], NDeltaMin_Q15[0] ); michael@0: michael@0: /* Keep delta_min distance between the NLSFs */ michael@0: for( i = 1; i < L; i++ ) michael@0: NLSF_Q15[i] = silk_max_int( NLSF_Q15[i], NLSF_Q15[i-1] + NDeltaMin_Q15[i] ); michael@0: michael@0: /* Last NLSF should be no higher than 1 - NDeltaMin[L] */ michael@0: NLSF_Q15[L-1] = silk_min_int( NLSF_Q15[L-1], (1<<15) - NDeltaMin_Q15[L] ); michael@0: michael@0: /* Keep NDeltaMin distance between the NLSFs */ michael@0: for( i = L-2; i >= 0; i-- ) michael@0: NLSF_Q15[i] = silk_min_int( NLSF_Q15[i], NLSF_Q15[i+1] - NDeltaMin_Q15[i+1] ); michael@0: } michael@0: }