blob: 73fcb16846641ad1451097371a9ac7c83e5383ed [file] [log] [blame]
andrew@webrtc.org618ab3f2012-09-04 23:39:05 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#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"
andrew@webrtc.org618ab3f2012-09-04 23:39:05 +000014
15namespace webrtc {
16namespace {
17
kma@webrtc.orgfc8aaf02013-07-24 17:38:23 +000018// FFT order.
19const int kOrder = 5;
20// Lengths for real FFT's time and frequency bufffers.
21// For N-point FFT, the length requirements from API are N and N+2 respectively.
22const int kTimeDataLength = 1 << kOrder;
23const int kFreqDataLength = (1 << kOrder) + 2;
24// For complex FFT's time and freq buffer. The implementation requires
25// 2*N 16-bit words.
26const int kComplexFftDataLength = 2 << kOrder;
27// Reference data for time signal.
28const int16_t kRefData[kTimeDataLength] = {
Yves Gerey665174f2018-06-19 15:03:05 +020029 11739, 6848, -8688, 31980, -30295, 25242, 27085, 19410,
30 -26299, 15607, -10791, 11778, -23819, 14498, -25772, 10076,
31 1173, 6848, -8688, 31980, -30295, 2522, 27085, 19410,
32 -2629, 5607, -3, 1178, -23819, 1498, -25772, 10076};
andrew@webrtc.org618ab3f2012-09-04 23:39:05 +000033
Karl Wiberg225842c2019-06-28 14:51:49 +020034TEST(RealFFTTest, CreateFailsOnBadInput) {
andrew@webrtc.org618ab3f2012-09-04 23:39:05 +000035 RealFFT* fft = WebRtcSpl_CreateRealFFT(11);
deadbeef922246a2017-02-26 04:18:12 -080036 EXPECT_TRUE(fft == nullptr);
andrew@webrtc.org618ab3f2012-09-04 23:39:05 +000037 fft = WebRtcSpl_CreateRealFFT(-1);
deadbeef922246a2017-02-26 04:18:12 -080038 EXPECT_TRUE(fft == nullptr);
andrew@webrtc.org618ab3f2012-09-04 23:39:05 +000039}
40
Karl Wiberg225842c2019-06-28 14:51:49 +020041TEST(RealFFTTest, RealAndComplexMatch) {
kma@webrtc.orgfc8aaf02013-07-24 17:38:23 +000042 int i = 0;
43 int j = 0;
44 int16_t real_fft_time[kTimeDataLength] = {0};
45 int16_t real_fft_freq[kFreqDataLength] = {0};
46 // One common buffer for complex FFT's time and frequency data.
47 int16_t complex_fft_buff[kComplexFftDataLength] = {0};
andrew@webrtc.org618ab3f2012-09-04 23:39:05 +000048
kma@webrtc.orgfc8aaf02013-07-24 17:38:23 +000049 // Prepare the inputs to forward FFT's.
50 memcpy(real_fft_time, kRefData, sizeof(kRefData));
51 for (i = 0, j = 0; i < kTimeDataLength; i += 1, j += 2) {
52 complex_fft_buff[j] = kRefData[i];
53 complex_fft_buff[j + 1] = 0; // Insert zero's to imaginary parts.
oprypin67fdb802017-03-09 06:25:06 -080054 }
kma@webrtc.orgfc8aaf02013-07-24 17:38:23 +000055
56 // Create and run real forward FFT.
kma@webrtc.orgf9e6cc22012-09-21 18:51:12 +000057 RealFFT* fft = WebRtcSpl_CreateRealFFT(kOrder);
deadbeef922246a2017-02-26 04:18:12 -080058 EXPECT_TRUE(fft != nullptr);
kma@webrtc.orgfc8aaf02013-07-24 17:38:23 +000059 EXPECT_EQ(0, WebRtcSpl_RealForwardFFT(fft, real_fft_time, real_fft_freq));
andrew@webrtc.org618ab3f2012-09-04 23:39:05 +000060
kma@webrtc.orgfc8aaf02013-07-24 17:38:23 +000061 // Run complex forward FFT.
62 WebRtcSpl_ComplexBitReverse(complex_fft_buff, kOrder);
63 EXPECT_EQ(0, WebRtcSpl_ComplexFFT(complex_fft_buff, kOrder, 1));
kma@webrtc.orgf9e6cc22012-09-21 18:51:12 +000064
kma@webrtc.orgfc8aaf02013-07-24 17:38:23 +000065 // Verify the results between complex and real forward FFT.
66 for (i = 0; i < kFreqDataLength; i++) {
67 EXPECT_EQ(real_fft_freq[i], complex_fft_buff[i]);
andrew@webrtc.org618ab3f2012-09-04 23:39:05 +000068 }
69
kma@webrtc.orgfc8aaf02013-07-24 17:38:23 +000070 // Prepare the inputs to inverse real FFT.
71 // We use whatever data in complex_fft_buff[] since we don't care
72 // about data contents. Only kFreqDataLength 16-bit words are copied
73 // from complex_fft_buff to real_fft_freq since remaining words (2nd half)
74 // are conjugate-symmetric to the first half in theory.
75 memcpy(real_fft_freq, complex_fft_buff, sizeof(real_fft_freq));
kma@webrtc.orgf9e6cc22012-09-21 18:51:12 +000076
kma@webrtc.orgfc8aaf02013-07-24 17:38:23 +000077 // Run real inverse FFT.
78 int real_scale = WebRtcSpl_RealInverseFFT(fft, real_fft_freq, real_fft_time);
andrew@webrtc.org618ab3f2012-09-04 23:39:05 +000079 EXPECT_GE(real_scale, 0);
kma@webrtc.orgfc8aaf02013-07-24 17:38:23 +000080
81 // Run complex inverse FFT.
82 WebRtcSpl_ComplexBitReverse(complex_fft_buff, kOrder);
83 int complex_scale = WebRtcSpl_ComplexIFFT(complex_fft_buff, kOrder, 1);
84
85 // Verify the results between complex and real inverse FFT.
86 // They are not bit-exact, since complex IFFT doesn't produce
87 // exactly conjugate-symmetric data (between first and second half).
andrew@webrtc.org618ab3f2012-09-04 23:39:05 +000088 EXPECT_EQ(real_scale, complex_scale);
kma@webrtc.orgfc8aaf02013-07-24 17:38:23 +000089 for (i = 0, j = 0; i < kTimeDataLength; i += 1, j += 2) {
90 EXPECT_LE(abs(real_fft_time[i] - complex_fft_buff[j]), 1);
andrew@webrtc.org618ab3f2012-09-04 23:39:05 +000091 }
kma@webrtc.orgfc8aaf02013-07-24 17:38:23 +000092
andrew@webrtc.org618ab3f2012-09-04 23:39:05 +000093 WebRtcSpl_FreeRealFFT(fft);
94}
95
96} // namespace
97} // namespace webrtc