blob: 92daae4d38fbaeac37fdfda8d21ce1a4aa2111b3 [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
pbos@webrtc.orgaa30bb72013-05-27 09:49:58 +000011#include "webrtc/common_audio/signal_processing/include/real_fft.h"
andrew@webrtc.org618ab3f2012-09-04 23:39:05 +000012
13#include <stdlib.h>
14
pbos@webrtc.orgaa30bb72013-05-27 09:49:58 +000015#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
andrew@webrtc.org618ab3f2012-09-04 23:39:05 +000016
17struct RealFFT {
18 int order;
19};
20
bjornv@webrtc.orgee432632014-12-08 16:36:22 +000021struct RealFFT* WebRtcSpl_CreateRealFFT(int order) {
andrew@webrtc.org618ab3f2012-09-04 23:39:05 +000022 struct RealFFT* self = NULL;
kma@webrtc.orgf9e6cc22012-09-21 18:51:12 +000023
kma@webrtc.orgfc8aaf02013-07-24 17:38:23 +000024 if (order > kMaxFFTOrder || order < 0) {
andrew@webrtc.org618ab3f2012-09-04 23:39:05 +000025 return NULL;
26 }
kma@webrtc.orgf9e6cc22012-09-21 18:51:12 +000027
andrew@webrtc.org618ab3f2012-09-04 23:39:05 +000028 self = malloc(sizeof(struct RealFFT));
kma@webrtc.orgfc8aaf02013-07-24 17:38:23 +000029 if (self == NULL) {
30 return NULL;
31 }
andrew@webrtc.org618ab3f2012-09-04 23:39:05 +000032 self->order = order;
kma@webrtc.orgf9e6cc22012-09-21 18:51:12 +000033
andrew@webrtc.org618ab3f2012-09-04 23:39:05 +000034 return self;
35}
36
bjornv@webrtc.orgee432632014-12-08 16:36:22 +000037void WebRtcSpl_FreeRealFFT(struct RealFFT* self) {
kma@webrtc.orgfc8aaf02013-07-24 17:38:23 +000038 if (self != NULL) {
39 free(self);
40 }
andrew@webrtc.org618ab3f2012-09-04 23:39:05 +000041}
42
bjornv@webrtc.orgee432632014-12-08 16:36:22 +000043// The C version FFT functions (i.e. WebRtcSpl_RealForwardFFT and
44// WebRtcSpl_RealInverseFFT) are real-valued FFT wrappers for complex-valued
kma@webrtc.orgfc8aaf02013-07-24 17:38:23 +000045// FFT implementation in SPL.
kma@webrtc.orgf9e6cc22012-09-21 18:51:12 +000046
bjornv@webrtc.orgee432632014-12-08 16:36:22 +000047int WebRtcSpl_RealForwardFFT(struct RealFFT* self,
48 const int16_t* real_data_in,
49 int16_t* complex_data_out) {
kma@webrtc.orgfc8aaf02013-07-24 17:38:23 +000050 int i = 0;
51 int j = 0;
52 int result = 0;
53 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 for (i = 0, j = 0; i < n; i += 1, j += 2) {
60 complex_buffer[j] = real_data_in[i];
61 complex_buffer[j + 1] = 0;
62 };
63
64 WebRtcSpl_ComplexBitReverse(complex_buffer, self->order);
65 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 memcpy(complex_data_out, complex_buffer, sizeof(int16_t) * (n + 2));
70
71 return result;
andrew@webrtc.org618ab3f2012-09-04 23:39:05 +000072}
73
bjornv@webrtc.orgee432632014-12-08 16:36:22 +000074int WebRtcSpl_RealInverseFFT(struct RealFFT* self,
75 const int16_t* complex_data_in,
76 int16_t* real_data_out) {
kma@webrtc.orgfc8aaf02013-07-24 17:38:23 +000077 int i = 0;
78 int j = 0;
79 int result = 0;
80 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 memcpy(complex_buffer, complex_data_in, sizeof(int16_t) * (n + 2));
88 for (i = n + 2; i < 2 * n; i += 2) {
89 complex_buffer[i] = complex_data_in[2 * n - i];
90 complex_buffer[i + 1] = -complex_data_in[2 * n - i + 1];
91 }
92
93 WebRtcSpl_ComplexBitReverse(complex_buffer, self->order);
94 result = WebRtcSpl_ComplexIFFT(complex_buffer, self->order, 1);
95
96 // Strip out the imaginary parts of the complex inverse FFT output.
97 for (i = 0, j = 0; i < n; i += 1, j += 2) {
98 real_data_out[i] = complex_buffer[j];
99 }
100
101 return result;
andrew@webrtc.org618ab3f2012-09-04 23:39:05 +0000102}