preemph.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 : preemph.c
  11. * Purpose : Preemphasis filtering
  12. * Description : Filtering through 1 - g z^-1
  13. *
  14. ********************************************************************************
  15. */
  16. /*
  17. ********************************************************************************
  18. * MODULE INCLUDE FILE AND VERSION ID
  19. ********************************************************************************
  20. */
  21. #include "preemph.h"
  22. const char preemph_id[] = "@(#)$Id $" preemph_h;
  23. /*
  24. ********************************************************************************
  25. * INCLUDE FILES
  26. ********************************************************************************
  27. */
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include "typedef.h"
  31. #include "basic_op.h"
  32. #include "count.h"
  33. /*
  34. ********************************************************************************
  35. * LOCAL VARIABLES AND TABLES
  36. ********************************************************************************
  37. */
  38. /*
  39. ********************************************************************************
  40. * PUBLIC PROGRAM CODE
  41. ********************************************************************************
  42. */
  43. /*************************************************************************
  44. *
  45. * Function: Post_Filter_init
  46. * Purpose: Allocates memory for filter structure and initializes
  47. * state memory
  48. *
  49. **************************************************************************
  50. */
  51. int preemphasis_init (preemphasisState **state)
  52. {
  53. preemphasisState* s;
  54. if (state == (preemphasisState **) NULL){
  55. wfprintf(stderr, "preemphasis_init: invalid parameter\n");
  56. return -1;
  57. }
  58. *state = NULL;
  59. /* allocate memory */
  60. if ((s= (preemphasisState *) wmalloc(sizeof(preemphasisState))) == NULL){
  61. wfprintf(stderr, "preemphasis_init: can not malloc state structure\n");
  62. return -1;
  63. }
  64. preemphasis_reset(s);
  65. *state = s;
  66. return 0;
  67. }
  68. /*************************************************************************
  69. *
  70. * Function: preemphasis_reset
  71. * Purpose: Initializes state memory to zero
  72. *
  73. **************************************************************************
  74. */
  75. int preemphasis_reset (preemphasisState *state)
  76. {
  77. if (state == (preemphasisState *) NULL){
  78. wfprintf(stderr, "preemphasis_reset: invalid parameter\n");
  79. return -1;
  80. }
  81. state->mem_pre = 0;
  82. return 0;
  83. }
  84. /*************************************************************************
  85. *
  86. * Function: preemphasis_exit
  87. * Purpose: The memory used for state memory is freed
  88. *
  89. **************************************************************************
  90. */
  91. void preemphasis_exit (preemphasisState **state)
  92. {
  93. if (state == NULL || *state == NULL)
  94. return;
  95. /* deallocate memory */
  96. wfree(*state);
  97. *state = NULL;
  98. return;
  99. }
  100. /*
  101. **************************************************************************
  102. * Function: preemphasis
  103. * Purpose: Filtering through 1 - g z^-1
  104. *
  105. **************************************************************************
  106. */
  107. int preemphasis (
  108. preemphasisState *st, /* (i/o) : preemphasis filter state */
  109. Word16 *signal, /* (i/o) : input signal overwritten by the output */
  110. Word16 g, /* (i) : preemphasis coefficient */
  111. Word16 L /* (i) : size of filtering */
  112. )
  113. {
  114. Word16 *p1, *p2, temp, i;
  115. p1 = signal + L - 1; move16 ();
  116. p2 = p1 - 1; move16 ();
  117. temp = *p1; move16 ();
  118. for (i = 0; i <= L - 2; i++)
  119. {
  120. *p1 = sub_ex (*p1, mult_ex (g, *p2--)); move16 ();
  121. p1--;
  122. }
  123. *p1 = sub_ex (*p1, mult_ex (g, st->mem_pre)); move16 ();
  124. st->mem_pre = temp; move16 ();
  125. return 0;
  126. }