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.

64 lines
1.9KB

  1. #pragma once
  2. #include "BiquadParams.h"
  3. #include "BiquadState.h"
  4. #include "BiquadFilter.h"
  5. #include "ObjectCache.h"
  6. /**
  7. * A traditional decimator, using IIR filters for interpolation.
  8. * Takes a signal at a higher sample rate, and reduces it to a lower
  9. * sample rate without adding (much) aliasing.
  10. */
  11. class IIRDecimator
  12. {
  13. public:
  14. /**
  15. * Will set the oversample factor, and set the filter cutoff.
  16. * Normalized cutoff is fixed at 1 / 4 * oversample, so most of the
  17. * top octave will be filtered out.
  18. */
  19. void setup(int oversampleFactor)
  20. {
  21. if (oversampleFactor != oversample) {
  22. oversample = oversampleFactor;
  23. // Set out IIR filter to be a six pole butterworth lowpass and the magic frequency.
  24. params = ObjectCache<float>::get6PLPParams(1.f / (4.0f * oversample));
  25. }
  26. }
  27. /**
  28. * Down-sample a buffer of data.
  29. * input is just an array of floats, the size is our oversampling factor.
  30. *
  31. * return value is a single sample
  32. */
  33. float process(const float * input)
  34. {
  35. float x = 0;
  36. for (int i = 0; i < oversample; ++i) {
  37. // The key here is to filter out all the frequencies that will
  38. // be higher than the destination Nyquist frequency.
  39. x = BiquadFilter<float>::run(input[i], state, *params);
  40. }
  41. // Note that we return just the last sample, and ignore the others.
  42. // Decimator is supposed to only keep one out of 'n' samples. We could
  43. // average them all, but that would just apply a little undesired high
  44. // frequency roll-off.
  45. return x;
  46. }
  47. private:
  48. /**
  49. * This is the oversampling factor. For example,
  50. * for 4x oversample, 'oversample' will be set to 4.
  51. */
  52. int oversample = -1;
  53. /**
  54. * "standard" Squinky Labs Biquad filter data.
  55. */
  56. std::shared_ptr<BiquadParams<float, 3>> params;
  57. BiquadState<float, 3> state;
  58. };