filter.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Low pass filter
  3. *
  4. * Copyright (c) 2020 Michael Buesch <m@bues.ch>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "compat.h"
  21. #include "debug.h"
  22. #include "filter.h"
  23. #include "util.h"
  24. #include "arithmetic.h"
  25. uint16_t lp_filter_run(struct lp_filter *lp,
  26. uint8_t shift,
  27. uint16_t in)
  28. {
  29. uint32_t buf;
  30. buf = lp->filter_buf;
  31. if (lp->initialized)
  32. buf = add_sat_u32((buf - (buf >> shift)), in);
  33. else
  34. buf = (uint32_t)in << shift;
  35. lp->filter_buf = buf;
  36. lp->initialized = true;
  37. return lim_u16(buf >> shift);
  38. }
  39. void lp_filter_set(struct lp_filter *lp,
  40. uint8_t shift,
  41. uint16_t in)
  42. {
  43. lp->filter_buf = (uint32_t)in << shift;
  44. lp->initialized = true;
  45. }
  46. void lp_filter_reset(struct lp_filter *lp)
  47. {
  48. lp->filter_buf = 0u;
  49. lp->initialized = false;
  50. }