filter.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Xytronic LF-1600
  3. * Low pass filter
  4. *
  5. * Copyright (c) 2015 Michael Buesch <m@bues.ch>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. #include "filter.h"
  22. uint16_t lp_filter_u16_run(struct lp_filter_u16 *lp, uint16_t in,
  23. uint8_t filter_shift)
  24. {
  25. uint24_t buf, out;
  26. buf = lp->filter_buf;
  27. buf -= buf >> filter_shift;
  28. buf += in;
  29. lp->filter_buf = buf;
  30. out = buf >> filter_shift;
  31. return (uint16_t)min(out, (uint24_t)UINT16_MAX);
  32. }
  33. fixpt_t lp_filter_fixpt_run(struct lp_filter_fixpt *lp, fixpt_t in,
  34. fixpt_t div)
  35. {
  36. fixpt_t buf, out;
  37. buf = lp->filter_buf;
  38. buf = fixpt_sub(buf, fixpt_div(buf, div));
  39. buf = fixpt_add(buf, in);
  40. lp->filter_buf = buf;
  41. out = fixpt_div(buf, div);
  42. return out;
  43. }