blob: bfd182b62aa407ef519e50d024c3a3f25d25090b [file] [log] [blame]
peah522d71b2017-02-23 05:16:26 -08001/*
2 * Copyright (c) 2017 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#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_AEC3_FFT_DATA_H_
12#define WEBRTC_MODULES_AUDIO_PROCESSING_AEC3_FFT_DATA_H_
13
14#include "webrtc/typedefs.h"
15#if defined(WEBRTC_ARCH_X86_FAMILY)
16#include <emmintrin.h>
17#endif
18#include <algorithm>
19#include <array>
20
kwiberg529662a2017-09-04 05:43:17 -070021#include "webrtc/api/array_view.h"
peah522d71b2017-02-23 05:16:26 -080022#include "webrtc/modules/audio_processing/aec3/aec3_common.h"
peah522d71b2017-02-23 05:16:26 -080023
24namespace webrtc {
25
26// Struct that holds imaginary data produced from 128 point real-valued FFTs.
27struct FftData {
28 // Copies the data in src.
29 void Assign(const FftData& src) {
30 std::copy(src.re.begin(), src.re.end(), re.begin());
31 std::copy(src.im.begin(), src.im.end(), im.begin());
32 im[0] = im[kFftLengthBy2] = 0;
33 }
34
35 // Clears all the imaginary.
36 void Clear() {
37 re.fill(0.f);
38 im.fill(0.f);
39 }
40
41 // Computes the power spectrum of the data.
42 void Spectrum(Aec3Optimization optimization,
43 std::array<float, kFftLengthBy2Plus1>* power_spectrum) const {
44 RTC_DCHECK(power_spectrum);
45 switch (optimization) {
46#if defined(WEBRTC_ARCH_X86_FAMILY)
47 case Aec3Optimization::kSse2: {
48 constexpr int kNumFourBinBands = kFftLengthBy2 / 4;
49 constexpr int kLimit = kNumFourBinBands * 4;
50 for (size_t k = 0; k < kLimit; k += 4) {
51 const __m128 r = _mm_loadu_ps(&re[k]);
52 const __m128 i = _mm_loadu_ps(&im[k]);
53 const __m128 ii = _mm_mul_ps(i, i);
54 const __m128 rr = _mm_mul_ps(r, r);
55 const __m128 rrii = _mm_add_ps(rr, ii);
56 _mm_storeu_ps(&(*power_spectrum)[k], rrii);
57 }
58 (*power_spectrum)[kFftLengthBy2] =
59 re[kFftLengthBy2] * re[kFftLengthBy2] +
60 im[kFftLengthBy2] * im[kFftLengthBy2];
61 } break;
62#endif
63 default:
64 std::transform(re.begin(), re.end(), im.begin(),
65 power_spectrum->begin(),
66 [](float a, float b) { return a * a + b * b; });
67 }
68 }
69
70 // Copy the data from an interleaved array.
71 void CopyFromPackedArray(const std::array<float, kFftLength>& v) {
72 re[0] = v[0];
73 re[kFftLengthBy2] = v[1];
74 im[0] = im[kFftLengthBy2] = 0;
75 for (size_t k = 1, j = 2; k < kFftLengthBy2; ++k) {
76 re[k] = v[j++];
77 im[k] = v[j++];
78 }
79 }
80
81 // Copies the data into an interleaved array.
82 void CopyToPackedArray(std::array<float, kFftLength>* v) const {
83 RTC_DCHECK(v);
84 (*v)[0] = re[0];
85 (*v)[1] = re[kFftLengthBy2];
86 for (size_t k = 1, j = 2; k < kFftLengthBy2; ++k) {
87 (*v)[j++] = re[k];
88 (*v)[j++] = im[k];
89 }
90 }
91
92 std::array<float, kFftLengthBy2Plus1> re;
93 std::array<float, kFftLengthBy2Plus1> im;
94};
95
96} // namespace webrtc
97
98#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AEC3_FFT_DATA_H_