filter.c 5.5 KB

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