Line data Source code
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 :
13 : #include <stdlib.h>
14 :
15 : #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
16 :
17 : struct RealFFT {
18 : int order;
19 : };
20 :
21 0 : struct RealFFT* WebRtcSpl_CreateRealFFT(int order) {
22 0 : struct RealFFT* self = NULL;
23 :
24 0 : if (order > kMaxFFTOrder || order < 0) {
25 0 : return NULL;
26 : }
27 :
28 0 : self = malloc(sizeof(struct RealFFT));
29 0 : if (self == NULL) {
30 0 : return NULL;
31 : }
32 0 : self->order = order;
33 :
34 0 : return self;
35 : }
36 :
37 0 : void WebRtcSpl_FreeRealFFT(struct RealFFT* self) {
38 0 : if (self != NULL) {
39 0 : free(self);
40 : }
41 0 : }
42 :
43 : // The C version FFT functions (i.e. WebRtcSpl_RealForwardFFT and
44 : // WebRtcSpl_RealInverseFFT) are real-valued FFT wrappers for complex-valued
45 : // FFT implementation in SPL.
46 :
47 0 : int WebRtcSpl_RealForwardFFT(struct RealFFT* self,
48 : const int16_t* real_data_in,
49 : int16_t* complex_data_out) {
50 0 : int i = 0;
51 0 : int j = 0;
52 0 : int result = 0;
53 0 : int n = 1 << self->order;
54 : // The complex-value FFT implementation needs a buffer to hold 2^order
55 : // 16-bit COMPLEX numbers, for both time and frequency data.
56 : int16_t complex_buffer[2 << kMaxFFTOrder];
57 :
58 : // Insert zeros to the imaginary parts for complex forward FFT input.
59 0 : for (i = 0, j = 0; i < n; i += 1, j += 2) {
60 0 : complex_buffer[j] = real_data_in[i];
61 0 : complex_buffer[j + 1] = 0;
62 : };
63 :
64 0 : WebRtcSpl_ComplexBitReverse(complex_buffer, self->order);
65 0 : result = WebRtcSpl_ComplexFFT(complex_buffer, self->order, 1);
66 :
67 : // For real FFT output, use only the first N + 2 elements from
68 : // complex forward FFT.
69 0 : memcpy(complex_data_out, complex_buffer, sizeof(int16_t) * (n + 2));
70 :
71 0 : return result;
72 : }
73 :
74 0 : int WebRtcSpl_RealInverseFFT(struct RealFFT* self,
75 : const int16_t* complex_data_in,
76 : int16_t* real_data_out) {
77 0 : int i = 0;
78 0 : int j = 0;
79 0 : int result = 0;
80 0 : int n = 1 << self->order;
81 : // Create the buffer specific to complex-valued FFT implementation.
82 : int16_t complex_buffer[2 << kMaxFFTOrder];
83 :
84 : // For n-point FFT, first copy the first n + 2 elements into complex
85 : // FFT, then construct the remaining n - 2 elements by real FFT's
86 : // conjugate-symmetric properties.
87 0 : memcpy(complex_buffer, complex_data_in, sizeof(int16_t) * (n + 2));
88 0 : for (i = n + 2; i < 2 * n; i += 2) {
89 0 : complex_buffer[i] = complex_data_in[2 * n - i];
90 0 : complex_buffer[i + 1] = -complex_data_in[2 * n - i + 1];
91 : }
92 :
93 0 : WebRtcSpl_ComplexBitReverse(complex_buffer, self->order);
94 0 : result = WebRtcSpl_ComplexIFFT(complex_buffer, self->order, 1);
95 :
96 : // Strip out the imaginary parts of the complex inverse FFT output.
97 0 : for (i = 0, j = 0; i < n; i += 1, j += 2) {
98 0 : real_data_out[i] = complex_buffer[j];
99 : }
100 :
101 0 : return result;
102 : }
|