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