andrew@webrtc.org | 618ab3f | 2012-09-04 23:39:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "common_audio/signal_processing/include/real_fft.h" |
| 12 | #include "common_audio/signal_processing/include/signal_processing_library.h" |
| 13 | #include "test/gtest.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 14 | #include "typedefs.h" // NOLINT(build/include) |
andrew@webrtc.org | 618ab3f | 2012-09-04 23:39:05 +0000 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | namespace { |
| 18 | |
kma@webrtc.org | fc8aaf0 | 2013-07-24 17:38:23 +0000 | [diff] [blame] | 19 | // FFT order. |
| 20 | const int kOrder = 5; |
| 21 | // Lengths for real FFT's time and frequency bufffers. |
| 22 | // For N-point FFT, the length requirements from API are N and N+2 respectively. |
| 23 | const int kTimeDataLength = 1 << kOrder; |
| 24 | const int kFreqDataLength = (1 << kOrder) + 2; |
| 25 | // For complex FFT's time and freq buffer. The implementation requires |
| 26 | // 2*N 16-bit words. |
| 27 | const int kComplexFftDataLength = 2 << kOrder; |
| 28 | // Reference data for time signal. |
| 29 | const int16_t kRefData[kTimeDataLength] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 30 | 11739, 6848, -8688, 31980, -30295, 25242, 27085, 19410, |
| 31 | -26299, 15607, -10791, 11778, -23819, 14498, -25772, 10076, |
| 32 | 1173, 6848, -8688, 31980, -30295, 2522, 27085, 19410, |
| 33 | -2629, 5607, -3, 1178, -23819, 1498, -25772, 10076}; |
andrew@webrtc.org | 618ab3f | 2012-09-04 23:39:05 +0000 | [diff] [blame] | 34 | |
| 35 | class RealFFTTest : public ::testing::Test { |
kma@webrtc.org | 0221b78 | 2012-09-08 00:09:26 +0000 | [diff] [blame] | 36 | protected: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 37 | RealFFTTest() { WebRtcSpl_Init(); } |
andrew@webrtc.org | 618ab3f | 2012-09-04 23:39:05 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | TEST_F(RealFFTTest, CreateFailsOnBadInput) { |
| 41 | RealFFT* fft = WebRtcSpl_CreateRealFFT(11); |
deadbeef | 922246a | 2017-02-26 04:18:12 -0800 | [diff] [blame] | 42 | EXPECT_TRUE(fft == nullptr); |
andrew@webrtc.org | 618ab3f | 2012-09-04 23:39:05 +0000 | [diff] [blame] | 43 | fft = WebRtcSpl_CreateRealFFT(-1); |
deadbeef | 922246a | 2017-02-26 04:18:12 -0800 | [diff] [blame] | 44 | EXPECT_TRUE(fft == nullptr); |
andrew@webrtc.org | 618ab3f | 2012-09-04 23:39:05 +0000 | [diff] [blame] | 45 | } |
| 46 | |
bjornv@webrtc.org | aca5939 | 2014-05-28 08:45:04 +0000 | [diff] [blame] | 47 | TEST_F(RealFFTTest, RealAndComplexMatch) { |
kma@webrtc.org | fc8aaf0 | 2013-07-24 17:38:23 +0000 | [diff] [blame] | 48 | int i = 0; |
| 49 | int j = 0; |
| 50 | int16_t real_fft_time[kTimeDataLength] = {0}; |
| 51 | int16_t real_fft_freq[kFreqDataLength] = {0}; |
| 52 | // One common buffer for complex FFT's time and frequency data. |
| 53 | int16_t complex_fft_buff[kComplexFftDataLength] = {0}; |
andrew@webrtc.org | 618ab3f | 2012-09-04 23:39:05 +0000 | [diff] [blame] | 54 | |
kma@webrtc.org | fc8aaf0 | 2013-07-24 17:38:23 +0000 | [diff] [blame] | 55 | // Prepare the inputs to forward FFT's. |
| 56 | memcpy(real_fft_time, kRefData, sizeof(kRefData)); |
| 57 | for (i = 0, j = 0; i < kTimeDataLength; i += 1, j += 2) { |
| 58 | complex_fft_buff[j] = kRefData[i]; |
| 59 | complex_fft_buff[j + 1] = 0; // Insert zero's to imaginary parts. |
oprypin | 67fdb80 | 2017-03-09 06:25:06 -0800 | [diff] [blame] | 60 | } |
kma@webrtc.org | fc8aaf0 | 2013-07-24 17:38:23 +0000 | [diff] [blame] | 61 | |
| 62 | // Create and run real forward FFT. |
kma@webrtc.org | f9e6cc2 | 2012-09-21 18:51:12 +0000 | [diff] [blame] | 63 | RealFFT* fft = WebRtcSpl_CreateRealFFT(kOrder); |
deadbeef | 922246a | 2017-02-26 04:18:12 -0800 | [diff] [blame] | 64 | EXPECT_TRUE(fft != nullptr); |
kma@webrtc.org | fc8aaf0 | 2013-07-24 17:38:23 +0000 | [diff] [blame] | 65 | EXPECT_EQ(0, WebRtcSpl_RealForwardFFT(fft, real_fft_time, real_fft_freq)); |
andrew@webrtc.org | 618ab3f | 2012-09-04 23:39:05 +0000 | [diff] [blame] | 66 | |
kma@webrtc.org | fc8aaf0 | 2013-07-24 17:38:23 +0000 | [diff] [blame] | 67 | // Run complex forward FFT. |
| 68 | WebRtcSpl_ComplexBitReverse(complex_fft_buff, kOrder); |
| 69 | EXPECT_EQ(0, WebRtcSpl_ComplexFFT(complex_fft_buff, kOrder, 1)); |
kma@webrtc.org | f9e6cc2 | 2012-09-21 18:51:12 +0000 | [diff] [blame] | 70 | |
kma@webrtc.org | fc8aaf0 | 2013-07-24 17:38:23 +0000 | [diff] [blame] | 71 | // Verify the results between complex and real forward FFT. |
| 72 | for (i = 0; i < kFreqDataLength; i++) { |
| 73 | EXPECT_EQ(real_fft_freq[i], complex_fft_buff[i]); |
andrew@webrtc.org | 618ab3f | 2012-09-04 23:39:05 +0000 | [diff] [blame] | 74 | } |
| 75 | |
kma@webrtc.org | fc8aaf0 | 2013-07-24 17:38:23 +0000 | [diff] [blame] | 76 | // Prepare the inputs to inverse real FFT. |
| 77 | // We use whatever data in complex_fft_buff[] since we don't care |
| 78 | // about data contents. Only kFreqDataLength 16-bit words are copied |
| 79 | // from complex_fft_buff to real_fft_freq since remaining words (2nd half) |
| 80 | // are conjugate-symmetric to the first half in theory. |
| 81 | memcpy(real_fft_freq, complex_fft_buff, sizeof(real_fft_freq)); |
kma@webrtc.org | f9e6cc2 | 2012-09-21 18:51:12 +0000 | [diff] [blame] | 82 | |
kma@webrtc.org | fc8aaf0 | 2013-07-24 17:38:23 +0000 | [diff] [blame] | 83 | // Run real inverse FFT. |
| 84 | int real_scale = WebRtcSpl_RealInverseFFT(fft, real_fft_freq, real_fft_time); |
andrew@webrtc.org | 618ab3f | 2012-09-04 23:39:05 +0000 | [diff] [blame] | 85 | EXPECT_GE(real_scale, 0); |
kma@webrtc.org | fc8aaf0 | 2013-07-24 17:38:23 +0000 | [diff] [blame] | 86 | |
| 87 | // Run complex inverse FFT. |
| 88 | WebRtcSpl_ComplexBitReverse(complex_fft_buff, kOrder); |
| 89 | int complex_scale = WebRtcSpl_ComplexIFFT(complex_fft_buff, kOrder, 1); |
| 90 | |
| 91 | // Verify the results between complex and real inverse FFT. |
| 92 | // They are not bit-exact, since complex IFFT doesn't produce |
| 93 | // exactly conjugate-symmetric data (between first and second half). |
andrew@webrtc.org | 618ab3f | 2012-09-04 23:39:05 +0000 | [diff] [blame] | 94 | EXPECT_EQ(real_scale, complex_scale); |
kma@webrtc.org | fc8aaf0 | 2013-07-24 17:38:23 +0000 | [diff] [blame] | 95 | for (i = 0, j = 0; i < kTimeDataLength; i += 1, j += 2) { |
| 96 | EXPECT_LE(abs(real_fft_time[i] - complex_fft_buff[j]), 1); |
andrew@webrtc.org | 618ab3f | 2012-09-04 23:39:05 +0000 | [diff] [blame] | 97 | } |
kma@webrtc.org | fc8aaf0 | 2013-07-24 17:38:23 +0000 | [diff] [blame] | 98 | |
andrew@webrtc.org | 618ab3f | 2012-09-04 23:39:05 +0000 | [diff] [blame] | 99 | WebRtcSpl_FreeRealFFT(fft); |
| 100 | } |
| 101 | |
| 102 | } // namespace |
| 103 | } // namespace webrtc |