test-rfft.cc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * Copyright 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 "gtest/gtest.h"
  19. #include "rfft.h"
  20. namespace knf {
  21. #if 0
  22. >>> import torch
  23. >>> a = torch.tensor([1., -1, 3, 8, 20, 6, 0, 2])
  24. >>> torch.fft.rfft(a)
  25. tensor([ 39.0000+0.0000j, -28.1924-2.2929j, 18.0000+5.0000j, -9.8076+3.7071j,
  26. 9.0000+0.0000j])
  27. #endif
  28. TEST(Rfft, TestRfft) {
  29. knf::Rfft fft(8);
  30. for (int32_t i = 0; i != 10; ++i) {
  31. std::vector<float> d = {1, -1, 3, 8, 20, 6, 0, 2};
  32. fft.Compute(d.data());
  33. EXPECT_EQ(d[0], 39);
  34. EXPECT_EQ(d[1], 9);
  35. EXPECT_NEAR(d[2], -28.1924, 1e-3);
  36. EXPECT_NEAR(-d[3], -2.2929, 1e-3);
  37. EXPECT_NEAR(d[4], 18, 1e-3);
  38. EXPECT_NEAR(-d[5], 5, 1e-3);
  39. EXPECT_NEAR(d[6], -9.8076, 1e-3);
  40. EXPECT_NEAR(-d[7], 3.7071, 1e-3);
  41. }
  42. }
  43. } // namespace knf