filter.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /******************************************************************
  2. iLBC Speech Coder ANSI-C Source Code
  3. filter.c
  4. Copyright (c) 2001,
  5. Global IP Sound AB.
  6. All rights reserved.
  7. ******************************************************************/
  8. #include "iLBC_define.h"
  9. #include "filter.h"
  10. /*----------------------------------------------------------------*
  11. * all-pole filter
  12. *---------------------------------------------------------------*/
  13. void AllPoleFilter(
  14. float *InOut, /* (i/o) on entrance InOut[-orderCoef] to
  15. InOut[-1] contain the state of the filter
  16. (delayed samples). InOut[0] to
  17. InOut[lengthInOut-1] contain the filter
  18. input, on en exit InOut[-orderCoef] to
  19. InOut[-1] is unchanged and InOut[0] to
  20. InOut[lengthInOut-1] contain filtered
  21. samples */
  22. float *Coef,/* (i) filter coefficients, Coef[0] is assumed to
  23. be 1.0 */
  24. int lengthInOut,/* (i) number of input/output samples */
  25. int orderCoef /* (i) number of filter coefficients */
  26. ){
  27. int n,k;
  28. for(n=0;n<lengthInOut;n++){
  29. for(k=1;k<=orderCoef;k++){
  30. *InOut -= Coef[k]*InOut[-k];
  31. }
  32. InOut++;
  33. }
  34. }
  35. /*----------------------------------------------------------------*
  36. * all-zero filter
  37. *---------------------------------------------------------------*/
  38. void AllZeroFilter(
  39. float *In, /* (i) In[0] to In[lengthInOut-1] contain filter
  40. input samples */
  41. float *Coef,/* (i) filter coefficients (Coef[0] is assumed
  42. to be 1.0) */
  43. int lengthInOut,/* (i) number of input/output samples */
  44. int orderCoef, /* (i) number of filter coefficients */
  45. float *Out /* (i/o) on entrance Out[-orderCoef] to Out[-1]
  46. contain the filter state, on exit Out[0]
  47. to Out[lengthInOut-1] contain filtered
  48. samples */
  49. ){
  50. int n,k;
  51. for(n=0;n<lengthInOut;n++){
  52. *Out = Coef[0]*In[0];
  53. for(k=1;k<=orderCoef;k++){
  54. *Out += Coef[k]*In[-k];
  55. }
  56. Out++;
  57. In++;
  58. }
  59. }
  60. /*----------------------------------------------------------------*
  61. * pole-zero filter
  62. *---------------------------------------------------------------*/
  63. void ZeroPoleFilter(
  64. float *In, /* (i) In[0] to In[lengthInOut-1] contain filter
  65. input samples In[-orderCoef] to In[-1]
  66. contain state of all-zero section */
  67. float *ZeroCoef,/* (i) filter coefficients for all-zero
  68. section (ZeroCoef[0] is assumed to
  69. be 1.0) */
  70. float *PoleCoef,/* (i) filter coefficients for all-pole section
  71. (ZeroCoef[0] is assumed to be 1.0) */
  72. int lengthInOut,/* (i) number of input/output samples */
  73. int orderCoef, /* (i) number of filter coefficients */
  74. float *Out /* (i/o) on entrance Out[-orderCoef] to Out[-1]
  75. contain state of all-pole section. On exit
  76. Out[0] to Out[lengthInOut-1] contain
  77. filtered samples */
  78. ){
  79. AllZeroFilter(In,ZeroCoef,lengthInOut,orderCoef,Out);
  80. AllPoleFilter(Out,PoleCoef,lengthInOut,orderCoef);
  81. }
  82. /*----------------------------------------------------------------*
  83. * downsample (LP filter and decimation)
  84. *---------------------------------------------------------------*/
  85. void DownSample (
  86. float *In, /* (i) input samples */
  87. float *Coef, /* (i) filter coefficients */
  88. int lengthIn, /* (i) number of input samples */
  89. float *state, /* (i) filter state */
  90. float *Out /* (o) downsampled output */
  91. ){
  92. float o;
  93. float *Out_ptr = Out;
  94. float *Coef_ptr, *In_ptr;
  95. float *state_ptr;
  96. int i, j, stop;
  97. /* LP filter and decimate at the same time */
  98. for (i = DELAY_DS; i < lengthIn; i+=FACTOR_DS)
  99. {
  100. Coef_ptr = &Coef[0];
  101. In_ptr = &In[i];
  102. state_ptr = &state[FILTERORDER_DS-2];
  103. o = (float)0.0;
  104. stop = (i < FILTERORDER_DS) ? i + 1 : FILTERORDER_DS;
  105. for (j = 0;j < stop; j++)
  106. {
  107. o += *Coef_ptr++ * (*In_ptr--);
  108. }
  109. for (j = i + 1; j < FILTERORDER_DS; j++)
  110. {
  111. o += *Coef_ptr++ * (*state_ptr--);
  112. }
  113. *Out_ptr++ = o;
  114. }
  115. /* Get the last part (use zeros as input for the future) */
  116. for (i=(lengthIn+FACTOR_DS); i<(lengthIn+DELAY_DS);
  117. i+=FACTOR_DS) {
  118. o=(float)0.0;
  119. if (i<lengthIn) {
  120. Coef_ptr = &Coef[0];
  121. In_ptr = &In[i];
  122. for (j=0;j<FILTERORDER_DS;j++) {
  123. o += *Coef_ptr++ * (*Out_ptr--);
  124. }
  125. } else {
  126. Coef_ptr = &Coef[i-lengthIn];
  127. In_ptr = &In[lengthIn-1];
  128. for (j=0;j<FILTERORDER_DS-(i-lengthIn);j++) {
  129. o += *Coef_ptr++ * (*In_ptr--);
  130. }
  131. }
  132. *Out_ptr++ = o;
  133. }
  134. }