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.

35 lines
866B

  1. #pragma once
  2. #include "dsp/fir.hpp"
  3. namespace rack {
  4. template<int OVERSAMPLE, int QUALITY>
  5. struct Decimator {
  6. DoubleRingBuffer<float, OVERSAMPLE*QUALITY> inBuffer;
  7. float kernel[OVERSAMPLE*QUALITY];
  8. Decimator(float cutoff = 0.9) {
  9. boxcarFIR(kernel, OVERSAMPLE*QUALITY, cutoff * 0.5 / OVERSAMPLE);
  10. blackmanHarrisWindow(kernel, OVERSAMPLE*QUALITY);
  11. // The sum of the kernel should be 1
  12. float sum = 0.0;
  13. for (int i = 0; i < OVERSAMPLE*QUALITY; i++) {
  14. sum += kernel[i];
  15. }
  16. for (int i = 0; i < OVERSAMPLE*QUALITY; i++) {
  17. kernel[i] /= sum;
  18. }
  19. }
  20. float process(float *in) {
  21. memcpy(inBuffer.endData(), in, OVERSAMPLE*sizeof(float));
  22. inBuffer.endIncr(OVERSAMPLE);
  23. float out = convolve(inBuffer.endData() + OVERSAMPLE*QUALITY, kernel, OVERSAMPLE*QUALITY);
  24. // Ignore the ring buffer's start position
  25. return out;
  26. }
  27. };
  28. } // namespace rack