annotate src/opus-1.3/silk/tests/test_unit_LPC_inv_pred_gain.c @ 81:7029a4916348

Merge build update
author Chris Cannam
date Thu, 31 Oct 2019 13:36:58 +0000
parents 7aeed7906520
children
rev   line source
Chris@69 1 /***********************************************************************
Chris@69 2 Copyright (c) 2017 Google Inc., Jean-Marc Valin
Chris@69 3 Redistribution and use in source and binary forms, with or without
Chris@69 4 modification, are permitted provided that the following conditions
Chris@69 5 are met:
Chris@69 6 - Redistributions of source code must retain the above copyright notice,
Chris@69 7 this list of conditions and the following disclaimer.
Chris@69 8 - Redistributions in binary form must reproduce the above copyright
Chris@69 9 notice, this list of conditions and the following disclaimer in the
Chris@69 10 documentation and/or other materials provided with the distribution.
Chris@69 11 - Neither the name of Internet Society, IETF or IETF Trust, nor the
Chris@69 12 names of specific contributors, may be used to endorse or promote
Chris@69 13 products derived from this software without specific prior written
Chris@69 14 permission.
Chris@69 15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Chris@69 16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Chris@69 17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Chris@69 18 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Chris@69 19 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Chris@69 20 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Chris@69 21 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Chris@69 22 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Chris@69 23 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Chris@69 24 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Chris@69 25 POSSIBILITY OF SUCH DAMAGE.
Chris@69 26 ***********************************************************************/
Chris@69 27
Chris@69 28 #ifdef HAVE_CONFIG_H
Chris@69 29 #include "config.h"
Chris@69 30 #endif
Chris@69 31
Chris@69 32 #include <stdio.h>
Chris@69 33 #include <stdlib.h>
Chris@69 34 #include "celt/stack_alloc.h"
Chris@69 35 #include "cpu_support.h"
Chris@69 36 #include "SigProc_FIX.h"
Chris@69 37
Chris@69 38 /* Computes the impulse response of the filter so we
Chris@69 39 can catch filters that are definitely unstable. Some
Chris@69 40 unstable filters may be classified as stable, but not
Chris@69 41 the other way around. */
Chris@69 42 int check_stability(opus_int16 *A_Q12, int order) {
Chris@69 43 int i;
Chris@69 44 int j;
Chris@69 45 int sum_a, sum_abs_a;
Chris@69 46 sum_a = sum_abs_a = 0;
Chris@69 47 for( j = 0; j < order; j++ ) {
Chris@69 48 sum_a += A_Q12[ j ];
Chris@69 49 sum_abs_a += silk_abs( A_Q12[ j ] );
Chris@69 50 }
Chris@69 51 /* Check DC stability. */
Chris@69 52 if( sum_a >= 4096 ) {
Chris@69 53 return 0;
Chris@69 54 }
Chris@69 55 /* If the sum of absolute values is less than 1, the filter
Chris@69 56 has to be stable. */
Chris@69 57 if( sum_abs_a < 4096 ) {
Chris@69 58 return 1;
Chris@69 59 }
Chris@69 60 double y[SILK_MAX_ORDER_LPC] = {0};
Chris@69 61 y[0] = 1;
Chris@69 62 for( i = 0; i < 10000; i++ ) {
Chris@69 63 double sum = 0;
Chris@69 64 for( j = 0; j < order; j++ ) {
Chris@69 65 sum += y[ j ]*A_Q12[ j ];
Chris@69 66 }
Chris@69 67 for( j = order - 1; j > 0; j-- ) {
Chris@69 68 y[ j ] = y[ j - 1 ];
Chris@69 69 }
Chris@69 70 y[ 0 ] = sum*(1./4096);
Chris@69 71 /* If impulse response reaches +/- 10000, the filter
Chris@69 72 is definitely unstable. */
Chris@69 73 if( !(y[ 0 ] < 10000 && y[ 0 ] > -10000) ) {
Chris@69 74 return 0;
Chris@69 75 }
Chris@69 76 /* Test every 8 sample for low amplitude. */
Chris@69 77 if( ( i & 0x7 ) == 0 ) {
Chris@69 78 double amp = 0;
Chris@69 79 for( j = 0; j < order; j++ ) {
Chris@69 80 amp += fabs(y[j]);
Chris@69 81 }
Chris@69 82 if( amp < 0.00001 ) {
Chris@69 83 return 1;
Chris@69 84 }
Chris@69 85 }
Chris@69 86 }
Chris@69 87 return 1;
Chris@69 88 }
Chris@69 89
Chris@69 90 int main(void) {
Chris@69 91 const int arch = opus_select_arch();
Chris@69 92 /* Set to 10000 so all branches in C function are triggered */
Chris@69 93 const int loop_num = 10000;
Chris@69 94 int count = 0;
Chris@69 95 ALLOC_STACK;
Chris@69 96
Chris@69 97 /* FIXME: Make the seed random (with option to set it explicitly)
Chris@69 98 so we get wider coverage. */
Chris@69 99 srand(0);
Chris@69 100
Chris@69 101 printf("Testing silk_LPC_inverse_pred_gain() optimization ...\n");
Chris@69 102 for( count = 0; count < loop_num; count++ ) {
Chris@69 103 unsigned int i;
Chris@69 104 opus_int order;
Chris@69 105 unsigned int shift;
Chris@69 106 opus_int16 A_Q12[ SILK_MAX_ORDER_LPC ];
Chris@69 107 opus_int32 gain;
Chris@69 108
Chris@69 109 for( order = 2; order <= SILK_MAX_ORDER_LPC; order += 2 ) { /* order must be even. */
Chris@69 110 for( shift = 0; shift < 16; shift++ ) { /* Different dynamic range. */
Chris@69 111 for( i = 0; i < SILK_MAX_ORDER_LPC; i++ ) {
Chris@69 112 A_Q12[i] = ((opus_int16)rand()) >> shift;
Chris@69 113 }
Chris@69 114 gain = silk_LPC_inverse_pred_gain(A_Q12, order, arch);
Chris@69 115 /* Look for filters that silk_LPC_inverse_pred_gain() thinks are
Chris@69 116 stable but definitely aren't. */
Chris@69 117 if( gain != 0 && !check_stability(A_Q12, order) ) {
Chris@69 118 fprintf(stderr, "**Loop %4d failed!**\n", count);
Chris@69 119 return 1;
Chris@69 120 }
Chris@69 121 }
Chris@69 122 }
Chris@69 123 if( !(count % 500) ) {
Chris@69 124 printf("Loop %4d passed\n", count);
Chris@69 125 }
Chris@69 126 }
Chris@69 127 printf("silk_LPC_inverse_pred_gain() optimization passed\n");
Chris@69 128 return 0;
Chris@69 129 }