TDFII.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. TDFII.h
  3. Copyright 2006-7
  4. David Yeh <dtyeh@ccrma.stanford.edu> (implementation)
  5. Tim Goetze <tim@quitte.de> (cosmetics)
  6. transposed Direct Form II digital filter.
  7. Assumes order of b = order of a.
  8. Assumes a0 = 1.
  9. */
  10. /*
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  22. 02111-1307, USA or point your web browser to http://www.gnu.org.
  23. */
  24. #ifndef _DSP_TDFII_H_
  25. #define _DSP_TDFII_H_
  26. namespace DSP {
  27. // ORDER is the highest power of s in the transfer function
  28. template <int Order>
  29. class TDFII
  30. {
  31. public:
  32. double a[Order + 1];
  33. double b[Order + 1];
  34. double h[Order + 1];
  35. void reset()
  36. {
  37. for (int i = 0; i <= Order; ++i)
  38. h[i] = 0; // zero state
  39. }
  40. void init (double fs)
  41. {
  42. reset();
  43. clear();
  44. }
  45. void clear()
  46. {
  47. for (int i=0; i<= Order; i++)
  48. a[i] = b[i] = 1;
  49. }
  50. /* per-band recursion:
  51. * y = 2 * (a * (x - x[-2]) + c * y[-1] - b * y[-2])
  52. */
  53. sample_t process (sample_t s)
  54. {
  55. double y = h[0] + b[0] * s;
  56. for (int i = 1; i < Order; ++i)
  57. h[i - 1] = h[i] + b[i] * s - a[i] * y;
  58. h[Order - 1] = b[Order] * s - a[Order] * y;
  59. return (sample_t) y;
  60. }
  61. };
  62. } /* namespace DSP */
  63. #endif /* _DSP_TDFII_H_ */