tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

real_fourier_ooura.cc (3045B)


      1 /*
      2 *  Copyright (c) 2015 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 "common_audio/real_fourier_ooura.h"
     12 
     13 #include <algorithm>
     14 #include <cmath>
     15 #include <complex>
     16 #include <cstddef>
     17 
     18 #include "common_audio/third_party/ooura/fft_size_256/fft4g.h"
     19 #include "rtc_base/checks.h"
     20 
     21 namespace webrtc {
     22 
     23 using std::complex;
     24 
     25 namespace {
     26 
     27 void Conjugate(complex<float>* array, size_t complex_length) {
     28  std::for_each(array, array + complex_length,
     29                [=](complex<float>& v) { v = std::conj(v); });
     30 }
     31 
     32 size_t ComputeWorkIpSize(size_t fft_length) {
     33  return static_cast<size_t>(
     34      2 + std::ceil(std::sqrt(static_cast<float>(fft_length))));
     35 }
     36 
     37 }  // namespace
     38 
     39 RealFourierOoura::RealFourierOoura(int fft_order)
     40    : order_(fft_order),
     41      length_(FftLength(order_)),
     42      complex_length_(ComplexLength(order_)),
     43      // Zero-initializing work_ip_ will cause rdft to initialize these work
     44      // arrays on the first call.
     45      work_ip_(new size_t[ComputeWorkIpSize(length_)]()),
     46      work_w_(new float[complex_length_]()) {
     47  RTC_CHECK_GE(fft_order, 1);
     48 }
     49 
     50 RealFourierOoura::~RealFourierOoura() = default;
     51 
     52 void RealFourierOoura::Forward(const float* src, complex<float>* dest) const {
     53  {
     54    // This cast is well-defined since C++11. See "Non-static data members" at:
     55    // http://en.cppreference.com/w/cpp/numeric/complex
     56    auto* dest_float = reinterpret_cast<float*>(dest);
     57    std::copy(src, src + length_, dest_float);
     58    WebRtc_rdft(length_, 1, dest_float, work_ip_.get(), work_w_.get());
     59  }
     60 
     61  // Ooura places real[n/2] in imag[0].
     62  dest[complex_length_ - 1] = complex<float>(dest[0].imag(), 0.0f);
     63  dest[0] = complex<float>(dest[0].real(), 0.0f);
     64  // Ooura returns the conjugate of the usual Fourier definition.
     65  Conjugate(dest, complex_length_);
     66 }
     67 
     68 void RealFourierOoura::Inverse(const complex<float>* src, float* dest) const {
     69  {
     70    auto* dest_complex = reinterpret_cast<complex<float>*>(dest);
     71    // The real output array is shorter than the input complex array by one
     72    // complex element.
     73    const size_t dest_complex_length = complex_length_ - 1;
     74    std::copy(src, src + dest_complex_length, dest_complex);
     75    // Restore Ooura's conjugate definition.
     76    Conjugate(dest_complex, dest_complex_length);
     77    // Restore real[n/2] to imag[0].
     78    dest_complex[0] =
     79        complex<float>(dest_complex[0].real(), src[complex_length_ - 1].real());
     80  }
     81 
     82  WebRtc_rdft(length_, -1, dest, work_ip_.get(), work_w_.get());
     83 
     84  // Ooura returns a scaled version.
     85  const float scale = 2.0f / length_;
     86  std::for_each(dest, dest + length_, [scale](float& v) { v *= scale; });
     87 }
     88 
     89 int RealFourierOoura::order() const {
     90  return order_;
     91 }
     92 
     93 }  // namespace webrtc