pre_proc.c 5.4 KB

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