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.

92 lines
1.8KB

  1. #pragma once
  2. #include <complex>
  3. #include <vector>
  4. #include <assert.h>
  5. class FFT;
  6. /**
  7. * Our wrapper api uses std::complex, so we don't need to expose kiss_fft_cpx
  8. * outside. Our implementation assumes the two are equivalent, and that a
  9. * reinterpret_cast can bridge them.
  10. */
  11. using cpx = std::complex<float>;
  12. template <typename T>
  13. class FFTData
  14. {
  15. public:
  16. friend FFT;
  17. FFTData(int numBins);
  18. ~FFTData();
  19. T get(int bin) const;
  20. void set(int bin, T value);
  21. int size() const
  22. {
  23. return (int) buffer.size();
  24. }
  25. T * data()
  26. {
  27. return buffer.data();
  28. }
  29. float getAbs(int bin) const
  30. {
  31. return std::abs(buffer[bin]);
  32. }
  33. static int _count;
  34. private:
  35. std::vector<T> buffer;
  36. /**
  37. * we store this without type so that clients don't need
  38. * to pull in the kiss_fft headers. It's mutable so it can
  39. * be lazy created by FFT functions.
  40. * Note that the cfg has a "direction" baked into it. For
  41. * now we assume that all FFT with complex input will be inverse FFTs.
  42. */
  43. mutable void * kiss_cfg = 0;
  44. };
  45. using FFTDataReal = FFTData<float>;
  46. using FFTDataCpx = FFTData<cpx>;
  47. //int FFTDataCpx::_count = 0;
  48. template <typename T>
  49. inline FFTData<T>::FFTData(int numBins) :
  50. buffer(numBins)
  51. {
  52. ++_count;
  53. }
  54. template <typename T>
  55. inline FFTData<T>::~FFTData()
  56. {
  57. // We need to manually delete the cfg, since only "we" know
  58. // what type it is.
  59. if (kiss_cfg) {
  60. free(kiss_cfg);
  61. }
  62. --_count;
  63. }
  64. template <typename T>
  65. inline T FFTData<T>::get(int index) const
  66. {
  67. assert(index < (int) buffer.size() && index >= 0);
  68. return buffer[index];
  69. }
  70. template <typename T>
  71. inline void FFTData<T>::set(int index, T value)
  72. {
  73. assert(index < (int) buffer.size() && index >= 0);
  74. buffer[index] = value;
  75. }