blob: 7cabe7d9fec12e3e34373319139369cda35188cc [file] [log] [blame]
andrew@webrtc.org618ab3f2012-09-04 23:39:051/*
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 04:47:3111#include "common_audio/signal_processing/include/real_fft.h"
Jonas Olssona4d87372019-07-05 17:08:3312
Mirko Bonadei92ea95e2017-09-15 04:47:3113#include "common_audio/signal_processing/include/signal_processing_library.h"
14#include "test/gtest.h"
andrew@webrtc.org618ab3f2012-09-04 23:39:0515
16namespace webrtc {
17namespace {
18
kma@webrtc.orgfc8aaf02013-07-24 17:38:2319// FFT order.
20const 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.
23const int kTimeDataLength = 1 << kOrder;
24const int kFreqDataLength = (1 << kOrder) + 2;
25// For complex FFT's time and freq buffer. The implementation requires
26// 2*N 16-bit words.
27const int kComplexFftDataLength = 2 << kOrder;
28// Reference data for time signal.
29const int16_t kRefData[kTimeDataLength] = {
Yves Gerey665174f2018-06-19 13:03:0530 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.org618ab3f2012-09-04 23:39:0534
Karl Wiberg225842c2019-06-28 12:51:4935TEST(RealFFTTest, CreateFailsOnBadInput) {
andrew@webrtc.org618ab3f2012-09-04 23:39:0536 RealFFT* fft = WebRtcSpl_CreateRealFFT(11);
deadbeef922246a2017-02-26 12:18:1237 EXPECT_TRUE(fft == nullptr);
andrew@webrtc.org618ab3f2012-09-04 23:39:0538 fft = WebRtcSpl_CreateRealFFT(-1);
deadbeef922246a2017-02-26 12:18:1239 EXPECT_TRUE(fft == nullptr);
andrew@webrtc.org618ab3f2012-09-04 23:39:0540}
41
Karl Wiberg225842c2019-06-28 12:51:4942TEST(RealFFTTest, RealAndComplexMatch) {
kma@webrtc.orgfc8aaf02013-07-24 17:38:2343 int i = 0;
44 int j = 0;
45 int16_t real_fft_time[kTimeDataLength] = {0};
46 int16_t real_fft_freq[kFreqDataLength] = {0};
47 // One common buffer for complex FFT's time and frequency data.
48 int16_t complex_fft_buff[kComplexFftDataLength] = {0};
andrew@webrtc.org618ab3f2012-09-04 23:39:0549
kma@webrtc.orgfc8aaf02013-07-24 17:38:2350 // Prepare the inputs to forward FFT's.
51 memcpy(real_fft_time, kRefData, sizeof(kRefData));
52 for (i = 0, j = 0; i < kTimeDataLength; i += 1, j += 2) {
53 complex_fft_buff[j] = kRefData[i];
54 complex_fft_buff[j + 1] = 0; // Insert zero's to imaginary parts.
oprypin67fdb802017-03-09 14:25:0655 }
kma@webrtc.orgfc8aaf02013-07-24 17:38:2356
57 // Create and run real forward FFT.
kma@webrtc.orgf9e6cc22012-09-21 18:51:1258 RealFFT* fft = WebRtcSpl_CreateRealFFT(kOrder);
deadbeef922246a2017-02-26 12:18:1259 EXPECT_TRUE(fft != nullptr);
kma@webrtc.orgfc8aaf02013-07-24 17:38:2360 EXPECT_EQ(0, WebRtcSpl_RealForwardFFT(fft, real_fft_time, real_fft_freq));
andrew@webrtc.org618ab3f2012-09-04 23:39:0561
kma@webrtc.orgfc8aaf02013-07-24 17:38:2362 // Run complex forward FFT.
63 WebRtcSpl_ComplexBitReverse(complex_fft_buff, kOrder);
64 EXPECT_EQ(0, WebRtcSpl_ComplexFFT(complex_fft_buff, kOrder, 1));
kma@webrtc.orgf9e6cc22012-09-21 18:51:1265
kma@webrtc.orgfc8aaf02013-07-24 17:38:2366 // Verify the results between complex and real forward FFT.
67 for (i = 0; i < kFreqDataLength; i++) {
68 EXPECT_EQ(real_fft_freq[i], complex_fft_buff[i]);
andrew@webrtc.org618ab3f2012-09-04 23:39:0569 }
70
kma@webrtc.orgfc8aaf02013-07-24 17:38:2371 // Prepare the inputs to inverse real FFT.
72 // We use whatever data in complex_fft_buff[] since we don't care
73 // about data contents. Only kFreqDataLength 16-bit words are copied
74 // from complex_fft_buff to real_fft_freq since remaining words (2nd half)
75 // are conjugate-symmetric to the first half in theory.
76 memcpy(real_fft_freq, complex_fft_buff, sizeof(real_fft_freq));
kma@webrtc.orgf9e6cc22012-09-21 18:51:1277
kma@webrtc.orgfc8aaf02013-07-24 17:38:2378 // Run real inverse FFT.
79 int real_scale = WebRtcSpl_RealInverseFFT(fft, real_fft_freq, real_fft_time);
andrew@webrtc.org618ab3f2012-09-04 23:39:0580 EXPECT_GE(real_scale, 0);
kma@webrtc.orgfc8aaf02013-07-24 17:38:2381
82 // Run complex inverse FFT.
83 WebRtcSpl_ComplexBitReverse(complex_fft_buff, kOrder);
84 int complex_scale = WebRtcSpl_ComplexIFFT(complex_fft_buff, kOrder, 1);
85
86 // Verify the results between complex and real inverse FFT.
87 // They are not bit-exact, since complex IFFT doesn't produce
88 // exactly conjugate-symmetric data (between first and second half).
andrew@webrtc.org618ab3f2012-09-04 23:39:0589 EXPECT_EQ(real_scale, complex_scale);
kma@webrtc.orgfc8aaf02013-07-24 17:38:2390 for (i = 0, j = 0; i < kTimeDataLength; i += 1, j += 2) {
91 EXPECT_LE(abs(real_fft_time[i] - complex_fft_buff[j]), 1);
andrew@webrtc.org618ab3f2012-09-04 23:39:0592 }
kma@webrtc.orgfc8aaf02013-07-24 17:38:2393
andrew@webrtc.org618ab3f2012-09-04 23:39:0594 WebRtcSpl_FreeRealFFT(fft);
95}
96
97} // namespace
98} // namespace webrtc