1 /*
2 * Copyright (c) 2019 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 "modules/audio_processing/ns/ns_fft.h"
12
13 #include "common_audio/third_party/ooura/fft_size_256/fft4g.h"
14
15 namespace webrtc {
16
NrFft()17 NrFft::NrFft() : bit_reversal_state_(kFftSize / 2), tables_(kFftSize / 2) {
18 // Initialize WebRtc_rdt (setting (bit_reversal_state_[0] to 0 triggers
19 // initialization)
20 bit_reversal_state_[0] = 0.f;
21 std::array<float, kFftSize> tmp_buffer;
22 tmp_buffer.fill(0.f);
23 WebRtc_rdft(kFftSize, 1, tmp_buffer.data(), bit_reversal_state_.data(),
24 tables_.data());
25 }
26
Fft(rtc::ArrayView<float,kFftSize> time_data,rtc::ArrayView<float,kFftSize> real,rtc::ArrayView<float,kFftSize> imag)27 void NrFft::Fft(rtc::ArrayView<float, kFftSize> time_data,
28 rtc::ArrayView<float, kFftSize> real,
29 rtc::ArrayView<float, kFftSize> imag) {
30 WebRtc_rdft(kFftSize, 1, time_data.data(), bit_reversal_state_.data(),
31 tables_.data());
32
33 imag[0] = 0;
34 real[0] = time_data[0];
35
36 imag[kFftSizeBy2Plus1 - 1] = 0;
37 real[kFftSizeBy2Plus1 - 1] = time_data[1];
38
39 for (size_t i = 1; i < kFftSizeBy2Plus1 - 1; ++i) {
40 real[i] = time_data[2 * i];
41 imag[i] = time_data[2 * i + 1];
42 }
43 }
44
Ifft(rtc::ArrayView<const float> real,rtc::ArrayView<const float> imag,rtc::ArrayView<float> time_data)45 void NrFft::Ifft(rtc::ArrayView<const float> real,
46 rtc::ArrayView<const float> imag,
47 rtc::ArrayView<float> time_data) {
48 time_data[0] = real[0];
49 time_data[1] = real[kFftSizeBy2Plus1 - 1];
50 for (size_t i = 1; i < kFftSizeBy2Plus1 - 1; ++i) {
51 time_data[2 * i] = real[i];
52 time_data[2 * i + 1] = imag[i];
53 }
54 WebRtc_rdft(kFftSize, -1, time_data.data(), bit_reversal_state_.data(),
55 tables_.data());
56
57 // Scale the output
58 constexpr float kScaling = 2.f / kFftSize;
59 for (float& d : time_data) {
60 d *= kScaling;
61 }
62 }
63
64 } // namespace webrtc
65