post_pro.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 : post_pro.c
  11. * Purpose : Postprocessing of output speech.
  12. *
  13. * - 2nd order high pass filtering with cut
  14. * off frequency at 60 Hz.
  15. * - Multiplication of output by two.
  16. *
  17. ********************************************************************************
  18. */
  19. /*
  20. ********************************************************************************
  21. * MODULE INCLUDE FILE AND VERSION ID
  22. ********************************************************************************
  23. */
  24. #include "post_pro.h"
  25. const char post_pro_id[] = "@(#)$Id $" post_pro_h;
  26. /*
  27. ********************************************************************************
  28. * INCLUDE FILES
  29. ********************************************************************************
  30. */
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include "typedef.h"
  34. #include "basic_op.h"
  35. #include "oper_32b.h"
  36. #include "count.h"
  37. /*
  38. ********************************************************************************
  39. * LOCAL VARIABLES AND TABLES
  40. ********************************************************************************
  41. */
  42. /* filter coefficients (fc = 60 Hz) */
  43. static const Word16 b[3] = {7699, -15398, 7699};
  44. static const Word16 a[3] = {8192, 15836, -7667};
  45. /*
  46. ********************************************************************************
  47. * PUBLIC PROGRAM CODE
  48. ********************************************************************************
  49. */
  50. /*************************************************************************
  51. *
  52. * Function: Post_Process_init
  53. * Purpose: Allocates state memory and initializes state memory
  54. *
  55. **************************************************************************
  56. */
  57. int Post_Process_init (Post_ProcessState **state)
  58. {
  59. Post_ProcessState* s;
  60. if (state == (Post_ProcessState **) NULL){
  61. wfprintf(stderr, "Post_Process_init: invalid parameter\n");
  62. return -1;
  63. }
  64. *state = NULL;
  65. /* allocate memory */
  66. if ((s= (Post_ProcessState *) wmalloc(sizeof(Post_ProcessState))) == NULL){
  67. wfprintf(stderr, "Post_Process_init: can not malloc state structure\n");
  68. return -1;
  69. }
  70. Post_Process_reset(s);
  71. *state = s;
  72. return 0;
  73. }
  74. /*************************************************************************
  75. *
  76. * Function: Post_Process_reset
  77. * Purpose: Initializes state memory to zero
  78. *
  79. **************************************************************************
  80. */
  81. int Post_Process_reset (Post_ProcessState *state)
  82. {
  83. if (state == (Post_ProcessState *) NULL){
  84. wfprintf(stderr, "Post_Process_reset: invalid parameter\n");
  85. return -1;
  86. }
  87. state->y2_hi = 0;
  88. state->y2_lo = 0;
  89. state->y1_hi = 0;
  90. state->y1_lo = 0;
  91. state->x0 = 0;
  92. state->x1 = 0;
  93. return 0;
  94. }
  95. /*************************************************************************
  96. *
  97. * Function: Post_Process_exit
  98. * Purpose: The memory used for state memory is freed
  99. *
  100. **************************************************************************
  101. */
  102. void Post_Process_exit (Post_ProcessState **state)
  103. {
  104. if (state == NULL || *state == NULL)
  105. return;
  106. /* deallocate memory */
  107. wfree(*state);
  108. *state = NULL;
  109. return;
  110. }
  111. /*************************************************************************
  112. *
  113. * FUNCTION: Post_Process()
  114. *
  115. * PURPOSE: Postprocessing of input speech.
  116. *
  117. * DESCRIPTION:
  118. * - 2nd order high pass filtering with cut off frequency at 60 Hz.
  119. * - Multiplication of output by two.
  120. *
  121. * Algorithm:
  122. *
  123. * y[i] = b[0]*x[i]*2 + b[1]*x[i-1]*2 + b[2]*x[i-2]*2
  124. * + a[1]*y[i-1] + a[2]*y[i-2];
  125. *
  126. *
  127. *************************************************************************/
  128. int Post_Process (
  129. Post_ProcessState *st, /* i/o : post process state */
  130. Word16 signal[], /* i/o : signal */
  131. Word16 lg /* i : length of signal */
  132. )
  133. {
  134. Word16 i, x2;
  135. Word32 L_tmp;
  136. test (); test ();
  137. for (i = 0; i < lg; i++)
  138. {
  139. x2 = st->x1; move16 ();
  140. st->x1 = st->x0; move16 ();
  141. st->x0 = signal[i]; move16 ();
  142. /* y[i] = b[0]*x[i]*2 + b[1]*x[i-1]*2 + b140[2]*x[i-2]/2 */
  143. /* + a[1]*y[i-1] + a[2] * y[i-2]; */
  144. L_tmp = Mpy_32_16 (st->y1_hi, st->y1_lo, a[1]);
  145. L_tmp = L_add_ex (L_tmp, Mpy_32_16 (st->y2_hi, st->y2_lo, a[2]));
  146. L_tmp = L_mac_ex (L_tmp, st->x0, b[0]);
  147. L_tmp = L_mac_ex (L_tmp, st->x1, b[1]);
  148. L_tmp = L_mac_ex (L_tmp, x2, b[2]);
  149. L_tmp = L_shl_ex (L_tmp, 2);
  150. /* Multiplication by two of output speech with saturation. */
  151. signal[i] = round_ex(L_shl_ex(L_tmp, 1)); move16 ();
  152. st->y2_hi = st->y1_hi; move16 ();
  153. st->y2_lo = st->y1_lo; move16 ();
  154. L_Extract (L_tmp, &st->y1_hi, &st->y1_lo);
  155. }
  156. return 0;
  157. }