blob: 6fe247011bf6e5b86666e188d6fa0b12beef727d [file] [log] [blame]
Patrik Höglundf715c532017-11-17 11:04:15 +01001/*
2 * Copyright (c) 2014 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/fir_filter_c.h"
12
13#include <string.h>
14
15#include <memory>
16
17#include "common_audio/fir_filter_neon.h"
18#include "common_audio/fir_filter_sse.h"
19#include "rtc_base/checks.h"
20
21namespace webrtc {
22
23FIRFilterC::~FIRFilterC() {
24}
25
26FIRFilterC::FIRFilterC(const float* coefficients, size_t coefficients_length)
27 : coefficients_length_(coefficients_length),
28 state_length_(coefficients_length - 1),
29 coefficients_(new float[coefficients_length_]),
30 state_(new float[state_length_]) {
31 for (size_t i = 0; i < coefficients_length_; ++i) {
32 coefficients_[i] = coefficients[coefficients_length_ - i - 1];
33 }
34 memset(state_.get(), 0, state_length_ * sizeof(state_[0]));
35}
36
37void FIRFilterC::Filter(const float* in, size_t length, float* out) {
38 RTC_DCHECK_GT(length, 0);
39
40 // Convolves the input signal |in| with the filter kernel |coefficients_|
41 // taking into account the previous state.
42 for (size_t i = 0; i < length; ++i) {
43 out[i] = 0.f;
44 size_t j;
45 for (j = 0; state_length_ > i && j < state_length_ - i; ++j) {
46 out[i] += state_[i + j] * coefficients_[j];
47 }
48 for (; j < coefficients_length_; ++j) {
49 out[i] += in[j + i - state_length_] * coefficients_[j];
50 }
51 }
52
53 // Update current state.
54 if (length >= state_length_) {
55 memcpy(
56 state_.get(), &in[length - state_length_], state_length_ * sizeof(*in));
57 } else {
58 memmove(state_.get(),
59 &state_[length],
60 (state_length_ - length) * sizeof(state_[0]));
61 memcpy(&state_[state_length_ - length], in, length * sizeof(*in));
62 }
63}
64
65} // namespace webrtc