gmed_n.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. ********************************************************************************
  3. *
  4. * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001
  5. * R99 Version 3.3.0
  6. * REL-4 Version 4.1.0
  7. *
  8. ********************************************************************************
  9. *
  10. * File : gmed_n.c
  11. * Purpose : calculates N-point median.
  12. *
  13. ********************************************************************************
  14. */
  15. /*
  16. ********************************************************************************
  17. * MODULE INCLUDE FILE AND VERSION ID
  18. ********************************************************************************
  19. */
  20. #include "gmed_n.h"
  21. const char gmed_n_id[] = "@(#)$Id $" gmed_n_h;
  22. /*
  23. ********************************************************************************
  24. * INCLUDE FILES
  25. ********************************************************************************
  26. */
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include "typedef.h"
  30. #include "basic_op.h"
  31. #include "count.h"
  32. /*
  33. ********************************************************************************
  34. * LOCAL VARIABLES AND TABLES
  35. ********************************************************************************
  36. */
  37. #define NMAX 9 /* largest N used in median calculation */
  38. /*
  39. ********************************************************************************
  40. * PUBLIC PROGRAM CODE
  41. ********************************************************************************
  42. */
  43. /*************************************************************************
  44. *
  45. * FUNCTION: gmed_n
  46. *
  47. * PURPOSE: calculates N-point median.
  48. *
  49. * DESCRIPTION:
  50. *
  51. *************************************************************************/
  52. Word16 gmed_n ( /* o : index of the median value (0...N-1) */
  53. Word16 ind[], /* i : Past gain values */
  54. Word16 n /* i : The number of gains; this routine */
  55. /* is only valid for a odd number of gains */
  56. /* (n <= NMAX) */
  57. )
  58. {
  59. Word16 i, j, ix = 0;
  60. Word16 max;
  61. Word16 medianIndex;
  62. Word16 tmp[NMAX];
  63. Word16 tmp2[NMAX];
  64. for (i = 0; i < n; i++)
  65. {
  66. tmp2[i] = ind[i]; move16 ();
  67. }
  68. for (i = 0; i < n; i++)
  69. {
  70. max = -32767; move16 ();
  71. for (j = 0; j < n; j++)
  72. {
  73. test ();
  74. if (sub_ex (tmp2[j], max) >= 0)
  75. {
  76. max = tmp2[j]; move16 ();
  77. ix = j; move16 ();
  78. }
  79. }
  80. tmp2[ix] = -32768; move16 ();
  81. tmp[i] = ix; move16 ();
  82. }
  83. medianIndex=tmp[ shr_ex(n,1) ]; move16 (); /* account for complex addressing */
  84. return (ind[medianIndex]);
  85. }