corrMatrix_FIX.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. /**********************************************************************
  31. * Correlation Matrix Computations for LS estimate.
  32. **********************************************************************/
  33. #include "main_FIX.h"
  34. /* Calculates correlation vector X'*t */
  35. void silk_corrVector_FIX(
  36. const opus_int16 *x, /* I x vector [L + order - 1] used to form data matrix X */
  37. const opus_int16 *t, /* I Target vector [L] */
  38. const opus_int L, /* I Length of vectors */
  39. const opus_int order, /* I Max lag for correlation */
  40. opus_int32 *Xt, /* O Pointer to X'*t correlation vector [order] */
  41. const opus_int rshifts, /* I Right shifts of correlations */
  42. int arch /* I Run-time architecture */
  43. )
  44. {
  45. opus_int lag, i;
  46. const opus_int16 *ptr1, *ptr2;
  47. opus_int32 inner_prod;
  48. ptr1 = &x[ order - 1 ]; /* Points to first sample of column 0 of X: X[:,0] */
  49. ptr2 = t;
  50. /* Calculate X'*t */
  51. if( rshifts > 0 ) {
  52. /* Right shifting used */
  53. for( lag = 0; lag < order; lag++ ) {
  54. inner_prod = 0;
  55. for( i = 0; i < L; i++ ) {
  56. inner_prod = silk_ADD_RSHIFT32( inner_prod, silk_SMULBB( ptr1[ i ], ptr2[i] ), rshifts );
  57. }
  58. Xt[ lag ] = inner_prod; /* X[:,lag]'*t */
  59. ptr1--; /* Go to next column of X */
  60. }
  61. } else {
  62. silk_assert( rshifts == 0 );
  63. for( lag = 0; lag < order; lag++ ) {
  64. Xt[ lag ] = silk_inner_prod_aligned( ptr1, ptr2, L, arch ); /* X[:,lag]'*t */
  65. ptr1--; /* Go to next column of X */
  66. }
  67. }
  68. }
  69. /* Calculates correlation matrix X'*X */
  70. void silk_corrMatrix_FIX(
  71. const opus_int16 *x, /* I x vector [L + order - 1] used to form data matrix X */
  72. const opus_int L, /* I Length of vectors */
  73. const opus_int order, /* I Max lag for correlation */
  74. opus_int32 *XX, /* O Pointer to X'*X correlation matrix [ order x order ] */
  75. opus_int32 *nrg, /* O Energy of x vector */
  76. opus_int *rshifts, /* O Right shifts of correlations and energy */
  77. int arch /* I Run-time architecture */
  78. )
  79. {
  80. opus_int i, j, lag;
  81. opus_int32 energy;
  82. const opus_int16 *ptr1, *ptr2;
  83. /* Calculate energy to find shift used to fit in 32 bits */
  84. silk_sum_sqr_shift( nrg, rshifts, x, L + order - 1 );
  85. energy = *nrg;
  86. /* Calculate energy of first column (0) of X: X[:,0]'*X[:,0] */
  87. /* Remove contribution of first order - 1 samples */
  88. for( i = 0; i < order - 1; i++ ) {
  89. energy -= silk_RSHIFT32( silk_SMULBB( x[ i ], x[ i ] ), *rshifts );
  90. }
  91. /* Calculate energy of remaining columns of X: X[:,j]'*X[:,j] */
  92. /* Fill out the diagonal of the correlation matrix */
  93. matrix_ptr( XX, 0, 0, order ) = energy;
  94. silk_assert( energy >= 0 );
  95. ptr1 = &x[ order - 1 ]; /* First sample of column 0 of X */
  96. for( j = 1; j < order; j++ ) {
  97. energy = silk_SUB32( energy, silk_RSHIFT32( silk_SMULBB( ptr1[ L - j ], ptr1[ L - j ] ), *rshifts ) );
  98. energy = silk_ADD32( energy, silk_RSHIFT32( silk_SMULBB( ptr1[ -j ], ptr1[ -j ] ), *rshifts ) );
  99. matrix_ptr( XX, j, j, order ) = energy;
  100. silk_assert( energy >= 0 );
  101. }
  102. ptr2 = &x[ order - 2 ]; /* First sample of column 1 of X */
  103. /* Calculate the remaining elements of the correlation matrix */
  104. if( *rshifts > 0 ) {
  105. /* Right shifting used */
  106. for( lag = 1; lag < order; lag++ ) {
  107. /* Inner product of column 0 and column lag: X[:,0]'*X[:,lag] */
  108. energy = 0;
  109. for( i = 0; i < L; i++ ) {
  110. energy += silk_RSHIFT32( silk_SMULBB( ptr1[ i ], ptr2[i] ), *rshifts );
  111. }
  112. /* Calculate remaining off diagonal: X[:,j]'*X[:,j + lag] */
  113. matrix_ptr( XX, lag, 0, order ) = energy;
  114. matrix_ptr( XX, 0, lag, order ) = energy;
  115. for( j = 1; j < ( order - lag ); j++ ) {
  116. energy = silk_SUB32( energy, silk_RSHIFT32( silk_SMULBB( ptr1[ L - j ], ptr2[ L - j ] ), *rshifts ) );
  117. energy = silk_ADD32( energy, silk_RSHIFT32( silk_SMULBB( ptr1[ -j ], ptr2[ -j ] ), *rshifts ) );
  118. matrix_ptr( XX, lag + j, j, order ) = energy;
  119. matrix_ptr( XX, j, lag + j, order ) = energy;
  120. }
  121. ptr2--; /* Update pointer to first sample of next column (lag) in X */
  122. }
  123. } else {
  124. for( lag = 1; lag < order; lag++ ) {
  125. /* Inner product of column 0 and column lag: X[:,0]'*X[:,lag] */
  126. energy = silk_inner_prod_aligned( ptr1, ptr2, L, arch );
  127. matrix_ptr( XX, lag, 0, order ) = energy;
  128. matrix_ptr( XX, 0, lag, order ) = energy;
  129. /* Calculate remaining off diagonal: X[:,j]'*X[:,j + lag] */
  130. for( j = 1; j < ( order - lag ); j++ ) {
  131. energy = silk_SUB32( energy, silk_SMULBB( ptr1[ L - j ], ptr2[ L - j ] ) );
  132. energy = silk_SMLABB( energy, ptr1[ -j ], ptr2[ -j ] );
  133. matrix_ptr( XX, lag + j, j, order ) = energy;
  134. matrix_ptr( XX, j, lag + j, order ) = energy;
  135. }
  136. ptr2--;/* Update pointer to first sample of next column (lag) in X */
  137. }
  138. }
  139. }