You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
865B

  1. #pragma once
  2. /**
  3. * Outputs a stair-step decimation.
  4. * So maybe it's the integral or a decimation?
  5. */
  6. class Decimator
  7. {
  8. public:
  9. /**
  10. * ret the next sample from the decimator.
  11. * if (needsInput), then next call must be acceptData.
  12. */
  13. float clock(bool& needsInput)
  14. {
  15. --phaseAccumulator; // one more sample
  16. if (phaseAccumulator <= 0) {
  17. needsInput = true;
  18. phaseAccumulator += rate;
  19. } else {
  20. needsInput = false;
  21. }
  22. return memory;
  23. }
  24. void acceptData(float data)
  25. {
  26. memory = data;
  27. }
  28. /**
  29. * Rate must be > 1.
  30. * Fractional rates are fine.
  31. */
  32. void setDecimationRate(float r)
  33. {
  34. rate = r;
  35. phaseAccumulator = rate;
  36. }
  37. private:
  38. float rate=0;
  39. float memory=0;
  40. float phaseAccumulator = 0;
  41. };