rfft.cc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * Copyright (c) 2022 Xiaomi Corporation (authors: Fangjun Kuang)
  3. *
  4. * See LICENSE for clarification regarding multiple authors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "rfft.h"
  19. #include <algorithm>
  20. #include <cmath>
  21. #include <vector>
  22. #include "log.h"
  23. // see fftsg.c
  24. #ifdef __cplusplus
  25. extern "C" void rdft(int n, int isgn, double *a, int *ip, double *w);
  26. #else
  27. void rdft(int n, int isgn, double *a, int *ip, double *w);
  28. #endif
  29. namespace knf {
  30. class Rfft::RfftImpl {
  31. public:
  32. explicit RfftImpl(int32_t n) : n_(n), ip_(2 + std::sqrt(n / 2)), w_(n / 2) {
  33. KNF_CHECK_EQ(n & (n - 1), 0);
  34. }
  35. void Compute(float *in_out) {
  36. std::vector<double> d(in_out, in_out + n_);
  37. Compute(d.data());
  38. std::copy(d.begin(), d.end(), in_out);
  39. }
  40. void Compute(double *in_out) {
  41. // 1 means forward fft
  42. rdft(n_, 1, in_out, ip_.data(), w_.data());
  43. }
  44. private:
  45. int32_t n_;
  46. std::vector<int32_t> ip_;
  47. std::vector<double> w_;
  48. };
  49. Rfft::Rfft(int32_t n) : impl_(std::make_unique<RfftImpl>(n)) {}
  50. Rfft::~Rfft() = default;
  51. void Rfft::Compute(float *in_out) { impl_->Compute(in_out); }
  52. void Rfft::Compute(double *in_out) { impl_->Compute(in_out); }
  53. } // namespace knf