real_fourier.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef COMMON_AUDIO_REAL_FOURIER_H_
  11. #define COMMON_AUDIO_REAL_FOURIER_H_
  12. #include <stddef.h>
  13. #include <complex>
  14. #include <memory>
  15. #include "rtc_base/memory/aligned_malloc.h"
  16. // Uniform interface class for the real DFT and its inverse, for power-of-2
  17. // input lengths. Also contains helper functions for buffer allocation, taking
  18. // care of any memory alignment requirements the underlying library might have.
  19. namespace webrtc {
  20. class RealFourier {
  21. public:
  22. // Shorthand typenames for the scopers used by the buffer allocation helpers.
  23. typedef std::unique_ptr<float[], AlignedFreeDeleter> fft_real_scoper;
  24. typedef std::unique_ptr<std::complex<float>[], AlignedFreeDeleter>
  25. fft_cplx_scoper;
  26. // The alignment required for all input and output buffers, in bytes.
  27. static const size_t kFftBufferAlignment;
  28. // Construct a wrapper instance for the given input order, which must be
  29. // between 1 and kMaxFftOrder, inclusively.
  30. static std::unique_ptr<RealFourier> Create(int fft_order);
  31. virtual ~RealFourier() {}
  32. // Helper to compute the smallest FFT order (a power of 2) which will contain
  33. // the given input length.
  34. static int FftOrder(size_t length);
  35. // Helper to compute the input length from the FFT order.
  36. static size_t FftLength(int order);
  37. // Helper to compute the exact length, in complex floats, of the transform
  38. // output (i.e. |2^order / 2 + 1|).
  39. static size_t ComplexLength(int order);
  40. // Buffer allocation helpers. The buffers are large enough to hold |count|
  41. // floats/complexes and suitably aligned for use by the implementation.
  42. // The returned scopers are set up with proper deleters; the caller owns
  43. // the allocated memory.
  44. static fft_real_scoper AllocRealBuffer(int count);
  45. static fft_cplx_scoper AllocCplxBuffer(int count);
  46. // Main forward transform interface. The output array need only be big
  47. // enough for |2^order / 2 + 1| elements - the conjugate pairs are not
  48. // returned. Input and output must be properly aligned (e.g. through
  49. // AllocRealBuffer and AllocCplxBuffer) and input length must be
  50. // |2^order| (same as given at construction time).
  51. virtual void Forward(const float* src, std::complex<float>* dest) const = 0;
  52. // Inverse transform. Same input format as output above, conjugate pairs
  53. // not needed.
  54. virtual void Inverse(const std::complex<float>* src, float* dest) const = 0;
  55. virtual int order() const = 0;
  56. };
  57. } // namespace webrtc
  58. #endif // COMMON_AUDIO_REAL_FOURIER_H_