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