3 #include "frequency_domain/convolver.h" 4 #include "frequency_domain/filter.h" 6 #include "utilities/aligned/vector.h" 26 template <
typename It>
27 util::aligned::vector<float>
filter(It begin, It end) {
28 return convolver_.convolve(
29 std::begin(kernel_), std::end(kernel_), begin, end);
32 void set_params(
double co,
double s);
35 static constexpr
size_t KERNEL_LENGTH{99};
37 std::array<float, KERNEL_LENGTH> kernel_;
46 template <
typename It>
47 util::aligned::vector<float>
filter(It begin, It end) {
48 return convolver_.convolve(
49 std::begin(kernel_), std::end(kernel_), begin, end);
52 void set_params(
double co,
double s);
55 static constexpr
size_t KERNEL_LENGTH{99};
57 std::array<float, KERNEL_LENGTH> kernel_;
66 template <
typename It>
67 util::aligned::vector<float>
filter(It begin, It end) {
68 return convolver_.convolve(
69 std::begin(kernel_), std::end(kernel_), begin, end);
72 void set_params(
double l,
double h,
double s);
75 static constexpr
size_t KERNEL_LENGTH{99};
77 std::array<float, KERNEL_LENGTH> kernel_;
95 double b0{0}, b1{0}, b2{0}, a1{0}, a2{0};
99 : coefficients_{coefficients} {}
103 const auto out = i * coefficients_.b0 + z1_;
104 z1_ = i * coefficients_.b1 - coefficients_.a1 * out + z2_;
105 z2_ = i * coefficients_.b2 - coefficients_.a2 * out;
110 constexpr
void clear() { z1_ = z2_ = 0; }
113 coefficients coefficients_;
114 double z1_{0}, z2_{0};
130 template <
typename Filter,
typename It>
131 void run_one_pass(Filter&
filter, It begin, It end) {
133 for (; begin != end; ++begin) {
134 *begin = filter.filter(*begin);
140 template <
typename Filter,
typename It>
141 void run_two_pass(Filter& filter, It begin, It end) {
142 run_one_pass(filter, begin, end);
144 std::make_reverse_iterator(end),
145 std::make_reverse_iterator(begin));
150 template <
typename It>
151 void linkwitz_riley_bandpass(
double lo,
double hi,
double s, It begin, It end) {
152 biquad lopass{compute_linkwitz_riley_lopass_coefficients(hi, s)};
153 run_two_pass(lopass, begin, end);
154 biquad hipass{compute_linkwitz_riley_hipass_coefficients(lo, s)};
155 run_two_pass(hipass, begin, end);
160 template <
size_t... i>
161 constexpr
auto make_biquads(
163 std::index_sequence<i...>) {
167 template <
size_t num>
168 constexpr
auto make_biquads(
169 const std::array<biquad::coefficients, num>&
coefficients) {
170 return make_biquads(coefficients, std::make_index_sequence<num>{});
174 template <
size_t num>
177 static constexpr
auto num_biquads = num;
178 static constexpr
auto order = num_biquads * 2;
181 const std::array<biquad::coefficients, num_biquads>& coefficients)
182 : biquads_{make_biquads(coefficients)} {}
184 constexpr
double filter(
double i) {
185 for (
auto&
biquad : biquads_) {
191 constexpr
void clear() {
192 for (
auto&
biquad : biquads_) {
198 std::array<biquad, num_biquads> biquads_;
201 template <
size_t num>
202 constexpr
auto make_series_biquads(
203 const std::array<biquad::coefficients, num>& coefficients) {
217 template <
typename Callback,
size_t... i>
218 auto compute_butterworth_segments(
double cf,
220 std::index_sequence<i...>) {
221 const auto order =
sizeof...(i) * 2;
223 {callback(cf, order, i)...}};
226 template <
size_t num,
typename Callback>
227 auto compute_butterworth_coefficients(
double cutoff,
230 return compute_butterworth_segments(std::tan(M_PI * cutoff / sr),
232 std::make_index_sequence<num>{});
235 template <
size_t num>
236 auto compute_hipass_butterworth_coefficients(
double cutoff,
double sr) {
237 return compute_butterworth_coefficients<num>(
238 cutoff, sr, compute_hipass_butterworth_segment);
241 template <
size_t num>
242 auto compute_lopass_butterworth_coefficients(
double cutoff,
double sr) {
243 return compute_butterworth_coefficients<num>(
244 cutoff, sr, compute_lopass_butterworth_segment);
247 template <
typename IIR>
248 util::aligned::vector<float> impulse_response(IIR& filt,
size_t steps) {
249 util::aligned::vector<float> ret(steps, 0.0f);
251 run_one_pass(filt, ret.begin(), ret.end());
constexpr void clear()
Resets delay lines, does not affect filter coefficients.
Definition: filters_common.h:110
Definition: convolver.h:11
util::aligned::vector< float > filter(It begin, It end)
Filter a vector of data.
Definition: filters_common.h:67
An interesting windowed-sinc bandpass filter.
Definition: filters_common.h:61
Definition: filters_common.h:94
util::aligned::vector< float > filter(It begin, It end)
Filter a vector of data.
Definition: filters_common.h:27
constexpr double filter(double i)
Run the filter for one step.
Definition: filters_common.h:102
Definition: filters_common.h:92
Adheres to the IIR filter concept.
Definition: filters_common.h:175
Definition: capsule_base.h:9
util::aligned::vector< float > filter(It begin, It end)
Filter a vector of data.
Definition: filters_common.h:47
Definition: filters_common.h:21
An interesting windowed-sinc hipass filter.
Definition: filters_common.h:41