Formalized Real 16-bit FFT for APM.

It also prepares for introducing Real 16-bit FFT Neon code from Openmax to SPL. CL https://webrtc-codereview.appspot.com/1819004/ takes care of that, but this CL is a prerequisite of that one.
Tested audioproc with an offline file. Bit exact.

R=andrew@webrtc.org, rtoy@google.com

Review URL: https://webrtc-codereview.appspot.com/1830004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@4390 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
kma@webrtc.org
2013-07-24 17:38:23 +00:00
parent b63c29f48c
commit fc8aaf02e1
10 changed files with 285 additions and 473 deletions

View File

@ -17,9 +17,17 @@
namespace webrtc {
namespace {
const int kOrder = 4;
const int kLength = 1 << (kOrder + 1); // +1 to hold complex data.
const int16_t kRefData[kLength] = {
// FFT order.
const int kOrder = 5;
// Lengths for real FFT's time and frequency bufffers.
// For N-point FFT, the length requirements from API are N and N+2 respectively.
const int kTimeDataLength = 1 << kOrder;
const int kFreqDataLength = (1 << kOrder) + 2;
// For complex FFT's time and freq buffer. The implementation requires
// 2*N 16-bit words.
const int kComplexFftDataLength = 2 << kOrder;
// Reference data for time signal.
const int16_t kRefData[kTimeDataLength] = {
11739, 6848, -8688, 31980, -30295, 25242, 27085, 19410,
-26299, 15607, -10791, 11778, -23819, 14498, -25772, 10076,
1173, 6848, -8688, 31980, -30295, 2522, 27085, 19410,
@ -40,36 +48,58 @@ TEST_F(RealFFTTest, CreateFailsOnBadInput) {
EXPECT_TRUE(fft == NULL);
}
// TODO(andrew): This won't always be the case, but verifies the current code
// at least.
TEST_F(RealFFTTest, RealAndComplexAreIdentical) {
int16_t real_data[kLength] = {0};
int16_t real_data_out[kLength] = {0};
int16_t complex_data[kLength] = {0};
memcpy(real_data, kRefData, sizeof(kRefData));
memcpy(complex_data, kRefData, sizeof(kRefData));
TEST_F(RealFFTTest, RealAndComplexMatch) {
int i = 0;
int j = 0;
int16_t real_fft_time[kTimeDataLength] = {0};
int16_t real_fft_freq[kFreqDataLength] = {0};
// One common buffer for complex FFT's time and frequency data.
int16_t complex_fft_buff[kComplexFftDataLength] = {0};
// Prepare the inputs to forward FFT's.
memcpy(real_fft_time, kRefData, sizeof(kRefData));
for (i = 0, j = 0; i < kTimeDataLength; i += 1, j += 2) {
complex_fft_buff[j] = kRefData[i];
complex_fft_buff[j + 1] = 0; // Insert zero's to imaginary parts.
};
// Create and run real forward FFT.
RealFFT* fft = WebRtcSpl_CreateRealFFT(kOrder);
EXPECT_TRUE(fft != NULL);
EXPECT_EQ(0, WebRtcSpl_RealForwardFFT(fft, real_fft_time, real_fft_freq));
EXPECT_EQ(0, WebRtcSpl_RealForwardFFT(fft, real_data, real_data_out));
WebRtcSpl_ComplexBitReverse(complex_data, kOrder);
EXPECT_EQ(0, WebRtcSpl_ComplexFFT(complex_data, kOrder, 1));
// Run complex forward FFT.
WebRtcSpl_ComplexBitReverse(complex_fft_buff, kOrder);
EXPECT_EQ(0, WebRtcSpl_ComplexFFT(complex_fft_buff, kOrder, 1));
for (int i = 0; i < kLength; i++) {
EXPECT_EQ(real_data_out[i], complex_data[i]);
// Verify the results between complex and real forward FFT.
for (i = 0; i < kFreqDataLength; i++) {
EXPECT_EQ(real_fft_freq[i], complex_fft_buff[i]);
}
memcpy(complex_data, kRefData, sizeof(kRefData));
// Prepare the inputs to inverse real FFT.
// We use whatever data in complex_fft_buff[] since we don't care
// about data contents. Only kFreqDataLength 16-bit words are copied
// from complex_fft_buff to real_fft_freq since remaining words (2nd half)
// are conjugate-symmetric to the first half in theory.
memcpy(real_fft_freq, complex_fft_buff, sizeof(real_fft_freq));
int real_scale = WebRtcSpl_RealInverseFFT(fft, real_data, real_data_out);
// Run real inverse FFT.
int real_scale = WebRtcSpl_RealInverseFFT(fft, real_fft_freq, real_fft_time);
EXPECT_GE(real_scale, 0);
WebRtcSpl_ComplexBitReverse(complex_data, kOrder);
int complex_scale = WebRtcSpl_ComplexIFFT(complex_data, kOrder, 1);
// Run complex inverse FFT.
WebRtcSpl_ComplexBitReverse(complex_fft_buff, kOrder);
int complex_scale = WebRtcSpl_ComplexIFFT(complex_fft_buff, kOrder, 1);
// Verify the results between complex and real inverse FFT.
// They are not bit-exact, since complex IFFT doesn't produce
// exactly conjugate-symmetric data (between first and second half).
EXPECT_EQ(real_scale, complex_scale);
for (int i = 0; i < kLength; i++) {
EXPECT_EQ(real_data_out[i], complex_data[i]);
for (i = 0, j = 0; i < kTimeDataLength; i += 1, j += 2) {
EXPECT_LE(abs(real_fft_time[i] - complex_fft_buff[j]), 1);
}
WebRtcSpl_FreeRealFFT(fft);
}