andrew@webrtc.org | 618ab3f | 2012-09-04 23:39:05 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "common_audio/signal_processing/include/real_fft.h" |
andrew@webrtc.org | 618ab3f | 2012-09-04 23:39:05 +0000 | [diff] [blame] | 12 | |
| 13 | #include <stdlib.h> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "common_audio/signal_processing/include/signal_processing_library.h" |
andrew@webrtc.org | 618ab3f | 2012-09-04 23:39:05 +0000 | [diff] [blame] | 16 | |
| 17 | struct RealFFT { |
| 18 | int order; |
| 19 | }; |
| 20 | |
bjornv@webrtc.org | ee43263 | 2014-12-08 16:36:22 +0000 | [diff] [blame] | 21 | struct RealFFT* WebRtcSpl_CreateRealFFT(int order) { |
andrew@webrtc.org | 618ab3f | 2012-09-04 23:39:05 +0000 | [diff] [blame] | 22 | struct RealFFT* self = NULL; |
kma@webrtc.org | f9e6cc2 | 2012-09-21 18:51:12 +0000 | [diff] [blame] | 23 | |
kma@webrtc.org | fc8aaf0 | 2013-07-24 17:38:23 +0000 | [diff] [blame] | 24 | if (order > kMaxFFTOrder || order < 0) { |
andrew@webrtc.org | 618ab3f | 2012-09-04 23:39:05 +0000 | [diff] [blame] | 25 | return NULL; |
| 26 | } |
kma@webrtc.org | f9e6cc2 | 2012-09-21 18:51:12 +0000 | [diff] [blame] | 27 | |
andrew@webrtc.org | 618ab3f | 2012-09-04 23:39:05 +0000 | [diff] [blame] | 28 | self = malloc(sizeof(struct RealFFT)); |
kma@webrtc.org | fc8aaf0 | 2013-07-24 17:38:23 +0000 | [diff] [blame] | 29 | if (self == NULL) { |
| 30 | return NULL; |
| 31 | } |
andrew@webrtc.org | 618ab3f | 2012-09-04 23:39:05 +0000 | [diff] [blame] | 32 | self->order = order; |
kma@webrtc.org | f9e6cc2 | 2012-09-21 18:51:12 +0000 | [diff] [blame] | 33 | |
andrew@webrtc.org | 618ab3f | 2012-09-04 23:39:05 +0000 | [diff] [blame] | 34 | return self; |
| 35 | } |
| 36 | |
bjornv@webrtc.org | ee43263 | 2014-12-08 16:36:22 +0000 | [diff] [blame] | 37 | void WebRtcSpl_FreeRealFFT(struct RealFFT* self) { |
kma@webrtc.org | fc8aaf0 | 2013-07-24 17:38:23 +0000 | [diff] [blame] | 38 | if (self != NULL) { |
| 39 | free(self); |
| 40 | } |
andrew@webrtc.org | 618ab3f | 2012-09-04 23:39:05 +0000 | [diff] [blame] | 41 | } |
| 42 | |
bjornv@webrtc.org | ee43263 | 2014-12-08 16:36:22 +0000 | [diff] [blame] | 43 | // The C version FFT functions (i.e. WebRtcSpl_RealForwardFFT and |
| 44 | // WebRtcSpl_RealInverseFFT) are real-valued FFT wrappers for complex-valued |
kma@webrtc.org | fc8aaf0 | 2013-07-24 17:38:23 +0000 | [diff] [blame] | 45 | // FFT implementation in SPL. |
kma@webrtc.org | f9e6cc2 | 2012-09-21 18:51:12 +0000 | [diff] [blame] | 46 | |
bjornv@webrtc.org | ee43263 | 2014-12-08 16:36:22 +0000 | [diff] [blame] | 47 | int WebRtcSpl_RealForwardFFT(struct RealFFT* self, |
| 48 | const int16_t* real_data_in, |
| 49 | int16_t* complex_data_out) { |
kma@webrtc.org | fc8aaf0 | 2013-07-24 17:38:23 +0000 | [diff] [blame] | 50 | 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.org | 618ab3f | 2012-09-04 23:39:05 +0000 | [diff] [blame] | 72 | } |
| 73 | |
bjornv@webrtc.org | ee43263 | 2014-12-08 16:36:22 +0000 | [diff] [blame] | 74 | int WebRtcSpl_RealInverseFFT(struct RealFFT* self, |
| 75 | const int16_t* complex_data_in, |
| 76 | int16_t* real_data_out) { |
kma@webrtc.org | fc8aaf0 | 2013-07-24 17:38:23 +0000 | [diff] [blame] | 77 | 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.org | 618ab3f | 2012-09-04 23:39:05 +0000 | [diff] [blame] | 102 | } |