aboutsummaryrefslogtreecommitdiff
path: root/webrtc/modules/audio_processing/three_band_filter_bank.h
blob: cb9cfbe7b1919a3f689679abb14721dddce500f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
 *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_THREE_BAND_FILTER_BANK_H_
#define WEBRTC_MODULES_AUDIO_PROCESSING_THREE_BAND_FILTER_BANK_H_

#include <cstring>
#include <vector>

#include "webrtc/common_audio/sparse_fir_filter.h"
#include "webrtc/system_wrappers/include/scoped_vector.h"

namespace webrtc {

// An implementation of a 3-band FIR filter-bank with DCT modulation, similar to
// the proposed in "Multirate Signal Processing for Communication Systems" by
// Fredric J Harris.
// The low-pass filter prototype has these characteristics:
// * Pass-band ripple = 0.3dB
// * Pass-band frequency = 0.147 (7kHz at 48kHz)
// * Stop-band attenuation = 40dB
// * Stop-band frequency = 0.192 (9.2kHz at 48kHz)
// * Delay = 24 samples (500us at 48kHz)
// * Linear phase
// This filter bank does not satisfy perfect reconstruction. The SNR after
// analysis and synthesis (with no processing in between) is approximately 9.5dB
// depending on the input signal after compensating for the delay.
class ThreeBandFilterBank final {
 public:
  explicit ThreeBandFilterBank(size_t length);

  // Splits |in| into 3 downsampled frequency bands in |out|.
  // |length| is the |in| length. Each of the 3 bands of |out| has to have a
  // length of |length| / 3.
  void Analysis(const float* in, size_t length, float* const* out);

  // Merges the 3 downsampled frequency bands in |in| into |out|.
  // |split_length| is the length of each band of |in|. |out| has to have at
  // least a length of 3 * |split_length|.
  void Synthesis(const float* const* in, size_t split_length, float* out);

 private:
  void DownModulate(const float* in,
                    size_t split_length,
                    size_t offset,
                    float* const* out);
  void UpModulate(const float* const* in,
                  size_t split_length,
                  size_t offset,
                  float* out);

  std::vector<float> in_buffer_;
  std::vector<float> out_buffer_;
  ScopedVector<SparseFIRFilter> analysis_filters_;
  ScopedVector<SparseFIRFilter> synthesis_filters_;
  std::vector<std::vector<float>> dct_modulation_;
};

}  // namespace webrtc

#endif  // WEBRTC_MODULES_AUDIO_PROCESSING_THREE_BAND_FILTER_BANK_H_