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