FFTPlan.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*******************************************************************************
  2. * Copyright 2015-2016 Juan Francisco Crespo Galán
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. ******************************************************************************/
  16. #pragma once
  17. /**
  18. * @file FFTPlan.h
  19. * @ingroup util
  20. * The FFTPlan class.
  21. */
  22. #include <complex>
  23. #include <fftw3.h>
  24. #include "Audaspace.h"
  25. #include <memory>
  26. #include <vector>
  27. /**Default FFT size.*/
  28. #define DEFAULT_N 4096
  29. AUD_NAMESPACE_BEGIN
  30. /**
  31. * Thas class represents an plan object that allows to calculate FFTs and IFFTs.
  32. */
  33. class AUD_API FFTPlan
  34. {
  35. private:
  36. /**
  37. * The size of the FFT plan.
  38. */
  39. int m_N;
  40. /**
  41. * The plan to transform the input to the frequency domain.
  42. */
  43. fftwf_plan m_fftPlanR2C;
  44. /**
  45. * The plan to transform the input to the time domain again.
  46. */
  47. fftwf_plan m_fftPlanC2R;
  48. /**
  49. * The size of a buffer for its use with the FFT plan (in bytes).
  50. */
  51. unsigned int m_bufferSize;
  52. // delete copy constructor and operator=
  53. FFTPlan(const FFTPlan&) = delete;
  54. FFTPlan& operator=(const FFTPlan&) = delete;
  55. public:
  56. /**
  57. * Creates a new FFTPlan object with DEFAULT_N size (4096).
  58. * \param measureTime The aproximate amount of seconds that FFTW will spend searching for the optimal plan,
  59. * which means faster FFTs and IFFTs while using this plan. If measureTime is negative, it will take all the time it needs.
  60. */
  61. FFTPlan(double measureTime = 0);
  62. /**
  63. * Creates a new FFTPlan object with a custom size.
  64. * \param n The size of the FFT plan. Values that are a power of two are faster.
  65. * The useful range usually is between 2048 and 8192, but bigger values can be useful
  66. * in certain situations (when using the StreamBuffer class per example).
  67. * Generally, low values use more CPU power and are a bit faster than large ones,
  68. * there is also a huge decrease in efficiency when n is lower than 2048.
  69. * \param measureTime The aproximate amount of seconds that FFTW will spend searching for the optimal plan,
  70. * which means faster FFTs while using this plan. If measureTime is negative, it will take all the time it needs.
  71. */
  72. FFTPlan(int n, double measureTime = 0);
  73. ~FFTPlan();
  74. /**
  75. * Retrieves the size of the FFT plan.
  76. * \return The size of the plan.
  77. */
  78. int getSize();
  79. /**
  80. * Calculates the FFT of an input buffer with the current plan.
  81. * \param[in,out] buffer A buffer with the input data an in which the output data will be written.
  82. */
  83. void FFT(void* buffer);
  84. /**
  85. * Calculates the IFFT of an input buffer with the current plan.
  86. * \param[in,out] buffer A buffer with the input data an in which the output data will be written.
  87. */
  88. void IFFT(void* buffer);
  89. /**
  90. * Reserves memory for a buffer that can be used for inplace transformations with this plan.
  91. * \return A pointer to a buffer of size ((N/2)+1)*2*sizeof(fftwf_complex).
  92. * \warning The returned buffer must be freed with the freeBuffer method of this class.
  93. */
  94. void* getBuffer();
  95. /**
  96. * Frees one of the buffers reserved with the getRealOnlyBuffer(), getComplexOnlyBuffer() or getInplaceBuffer() method.
  97. * \param buffer A pointer to the buufer taht must be freed.
  98. */
  99. void freeBuffer(void* buffer);
  100. };
  101. AUD_NAMESPACE_END