test_unit_LPC_inv_pred_gain.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /***********************************************************************
  2. Copyright (c) 2017 Google Inc., Jean-Marc Valin
  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. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include "celt/stack_alloc.h"
  33. #include "cpu_support.h"
  34. #include "SigProc_FIX.h"
  35. /* Computes the impulse response of the filter so we
  36. can catch filters that are definitely unstable. Some
  37. unstable filters may be classified as stable, but not
  38. the other way around. */
  39. int check_stability(opus_int16 *A_Q12, int order) {
  40. int i;
  41. int j;
  42. int sum_a, sum_abs_a;
  43. sum_a = sum_abs_a = 0;
  44. for( j = 0; j < order; j++ ) {
  45. sum_a += A_Q12[ j ];
  46. sum_abs_a += silk_abs( A_Q12[ j ] );
  47. }
  48. /* Check DC stability. */
  49. if( sum_a >= 4096 ) {
  50. return 0;
  51. }
  52. /* If the sum of absolute values is less than 1, the filter
  53. has to be stable. */
  54. if( sum_abs_a < 4096 ) {
  55. return 1;
  56. }
  57. double y[SILK_MAX_ORDER_LPC] = {0};
  58. y[0] = 1;
  59. for( i = 0; i < 10000; i++ ) {
  60. double sum = 0;
  61. for( j = 0; j < order; j++ ) {
  62. sum += y[ j ]*A_Q12[ j ];
  63. }
  64. for( j = order - 1; j > 0; j-- ) {
  65. y[ j ] = y[ j - 1 ];
  66. }
  67. y[ 0 ] = sum*(1./4096);
  68. /* If impulse response reaches +/- 10000, the filter
  69. is definitely unstable. */
  70. if( !(y[ 0 ] < 10000 && y[ 0 ] > -10000) ) {
  71. return 0;
  72. }
  73. /* Test every 8 sample for low amplitude. */
  74. if( ( i & 0x7 ) == 0 ) {
  75. double amp = 0;
  76. for( j = 0; j < order; j++ ) {
  77. amp += fabs(y[j]);
  78. }
  79. if( amp < 0.00001 ) {
  80. return 1;
  81. }
  82. }
  83. }
  84. return 1;
  85. }
  86. int main(void) {
  87. const int arch = opus_select_arch();
  88. /* Set to 10000 so all branches in C function are triggered */
  89. const int loop_num = 10000;
  90. int count = 0;
  91. ALLOC_STACK;
  92. /* FIXME: Make the seed random (with option to set it explicitly)
  93. so we get wider coverage. */
  94. srand(0);
  95. printf("Testing silk_LPC_inverse_pred_gain() optimization ...\n");
  96. for( count = 0; count < loop_num; count++ ) {
  97. unsigned int i;
  98. opus_int order;
  99. unsigned int shift;
  100. opus_int16 A_Q12[ SILK_MAX_ORDER_LPC ];
  101. opus_int32 gain;
  102. for( order = 2; order <= SILK_MAX_ORDER_LPC; order += 2 ) { /* order must be even. */
  103. for( shift = 0; shift < 16; shift++ ) { /* Different dynamic range. */
  104. for( i = 0; i < SILK_MAX_ORDER_LPC; i++ ) {
  105. A_Q12[i] = ((opus_int16)rand()) >> shift;
  106. }
  107. gain = silk_LPC_inverse_pred_gain(A_Q12, order, arch);
  108. /* Look for filters that silk_LPC_inverse_pred_gain() thinks are
  109. stable but definitely aren't. */
  110. if( gain != 0 && !check_stability(A_Q12, order) ) {
  111. fprintf(stderr, "**Loop %4d failed!**\n", count);
  112. return 1;
  113. }
  114. }
  115. }
  116. if( !(count % 500) ) {
  117. printf("Loop %4d passed\n", count);
  118. }
  119. }
  120. printf("silk_LPC_inverse_pred_gain() optimization passed\n");
  121. return 0;
  122. }