filter.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /******************************************************************
  2. iLBC Speech Coder ANSI-C Source Code
  3. filter.c
  4. Copyright (C) The Internet Society (2004).
  5. All Rights Reserved.
  6. ******************************************************************/
  7. #include "iLBC_define.h"
  8. #include "filter.h"
  9. /*----------------------------------------------------------------*
  10. * all-pole filter
  11. *---------------------------------------------------------------*/
  12. void AllPoleFilter(
  13. float *InOut, /* (i/o) on entrance InOut[-orderCoef] to
  14. InOut[-1] contain the state of the
  15. filter (delayed samples). InOut[0] to
  16. InOut[lengthInOut-1] contain the filter
  17. input, on en exit InOut[-orderCoef] to
  18. InOut[-1] is unchanged and InOut[0] to
  19. InOut[lengthInOut-1] contain filtered
  20. samples */
  21. float *Coef,/* (i) filter coefficients, Coef[0] is assumed
  22. to be 1.0 */
  23. int lengthInOut,/* (i) number of input/output samples */
  24. int orderCoef /* (i) number of filter coefficients */
  25. ){
  26. int n,k;
  27. for(n=0;n<lengthInOut;n++){
  28. for(k=1;k<=orderCoef;k++){
  29. *InOut -= Coef[k]*InOut[-k];
  30. }
  31. InOut++;
  32. }
  33. }
  34. /*----------------------------------------------------------------*
  35. * all-zero filter
  36. *---------------------------------------------------------------*/
  37. void AllZeroFilter(
  38. float *In, /* (i) In[0] to In[lengthInOut-1] contain
  39. filter input samples */
  40. float *Coef,/* (i) filter coefficients (Coef[0] is assumed
  41. to be 1.0) */
  42. int lengthInOut,/* (i) number of input/output samples */
  43. int orderCoef, /* (i) number of filter coefficients */
  44. float *Out /* (i/o) on entrance Out[-orderCoef] to Out[-1]
  45. contain the filter state, on exit Out[0]
  46. to Out[lengthInOut-1] contain filtered
  47. samples */
  48. ){
  49. int n,k;
  50. for(n=0;n<lengthInOut;n++){
  51. *Out = Coef[0]*In[0];
  52. for(k=1;k<=orderCoef;k++){
  53. *Out += Coef[k]*In[-k];
  54. }
  55. Out++;
  56. In++;
  57. }
  58. }
  59. /*----------------------------------------------------------------*
  60. * pole-zero filter
  61. *---------------------------------------------------------------*/
  62. void ZeroPoleFilter(
  63. float *In, /* (i) In[0] to In[lengthInOut-1] contain
  64. filter input samples In[-orderCoef] to
  65. In[-1] contain state of all-zero
  66. 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
  76. exit Out[0] to Out[lengthInOut-1]
  77. contain 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. }