Home | History | Annotate | Download | only in signal_processing
      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 struct RealFFT* WebRtcSpl_CreateRealFFTC(int order) {
     22   struct RealFFT* self = NULL;
     23 
     24   if (order > kMaxFFTOrder || order < 0) {
     25     return NULL;
     26   }
     27 
     28   self = malloc(sizeof(struct RealFFT));
     29   if (self == NULL) {
     30     return NULL;
     31   }
     32   self->order = order;
     33 
     34   return self;
     35 }
     36 
     37 void WebRtcSpl_FreeRealFFTC(struct RealFFT* self) {
     38   if (self != NULL) {
     39     free(self);
     40   }
     41 }
     42 
     43 // The C version FFT functions (i.e. WebRtcSpl_RealForwardFFTC and
     44 // WebRtcSpl_RealInverseFFTC) are real-valued FFT wrappers for complex-valued
     45 // FFT implementation in SPL.
     46 
     47 int WebRtcSpl_RealForwardFFTC(struct RealFFT* self,
     48                               const int16_t* real_data_in,
     49                               int16_t* complex_data_out) {
     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;
     72 }
     73 
     74 int WebRtcSpl_RealInverseFFTC(struct RealFFT* self,
     75                               const int16_t* complex_data_in,
     76                               int16_t* real_data_out) {
     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;
    102 }
    103 
    104 #if defined(WEBRTC_DETECT_ARM_NEON) || defined(WEBRTC_ARCH_ARM_NEON)
    105 // TODO(kma): Replace the following function bodies into optimized functions
    106 // for ARM Neon.
    107 struct RealFFT* WebRtcSpl_CreateRealFFTNeon(int order) {
    108   return WebRtcSpl_CreateRealFFTC(order);
    109 }
    110 
    111 void WebRtcSpl_FreeRealFFTNeon(struct RealFFT* self) {
    112   WebRtcSpl_FreeRealFFTC(self);
    113 }
    114 
    115 int WebRtcSpl_RealForwardFFTNeon(struct RealFFT* self,
    116                                  const int16_t* real_data_in,
    117                                  int16_t* complex_data_out) {
    118   return WebRtcSpl_RealForwardFFTC(self, real_data_in, complex_data_out);
    119 }
    120 
    121 int WebRtcSpl_RealInverseFFTNeon(struct RealFFT* self,
    122                                  const int16_t* complex_data_in,
    123                                  int16_t* real_data_out) {
    124   return WebRtcSpl_RealInverseFFTC(self, complex_data_in, real_data_out);
    125 }
    126 #endif  // WEBRTC_DETECT_ARM_NEON || WEBRTC_ARCH_ARM_NEON
    127