2022-07-04 17:03:07 -04:00
|
|
|
// Copyright 2015-2021 modemm17 LLC.
|
2022-06-06 21:22:18 -04:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Filter.h"
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <cstddef>
|
|
|
|
|
2022-07-04 17:03:07 -04:00
|
|
|
namespace modemm17
|
2022-06-06 21:22:18 -04:00
|
|
|
{
|
|
|
|
|
2022-06-09 14:12:35 -04:00
|
|
|
template <size_t N>
|
|
|
|
struct BaseIirFilter : FilterBase<float>
|
2022-06-06 21:22:18 -04:00
|
|
|
{
|
2022-06-09 14:12:35 -04:00
|
|
|
const std::array<float, N>& numerator_;
|
|
|
|
const std::array<float, N> denominator_;
|
|
|
|
std::array<float, N> history_{0};
|
|
|
|
|
|
|
|
BaseIirFilter(const std::array<float, N>& b, const std::array<float, N>& a)
|
2022-06-06 21:22:18 -04:00
|
|
|
: numerator_(b), denominator_(a)
|
|
|
|
{
|
|
|
|
history_.fill(0.0);
|
|
|
|
}
|
2022-06-09 14:12:35 -04:00
|
|
|
|
|
|
|
float operator()(float input) {
|
2022-06-06 21:22:18 -04:00
|
|
|
|
|
|
|
for (size_t i = N - 1; i != 0; i--) history_[i] = history_[i - 1];
|
2022-06-09 14:12:35 -04:00
|
|
|
|
2022-06-06 21:22:18 -04:00
|
|
|
history_[0] = input;
|
|
|
|
|
|
|
|
for (size_t i = 1; i != N; i++) {
|
|
|
|
history_[0] -= denominator_[i] * history_[i];
|
|
|
|
}
|
2022-06-09 14:12:35 -04:00
|
|
|
|
|
|
|
float result = 0;
|
2022-06-06 21:22:18 -04:00
|
|
|
for (size_t i = 0; i != N; i++) {
|
|
|
|
result += numerator_[i] * history_[i];
|
|
|
|
}
|
2022-06-09 14:12:35 -04:00
|
|
|
|
2022-06-06 21:22:18 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-09 14:12:35 -04:00
|
|
|
template <size_t N>
|
|
|
|
BaseIirFilter<N> makeIirFilter(
|
|
|
|
const std::array<float, N>& b, const std::array<float, N>& a)
|
2022-06-06 21:22:18 -04:00
|
|
|
{
|
2022-06-09 14:12:35 -04:00
|
|
|
return std::move(BaseIirFilter<N>(b, a));
|
2022-06-06 21:22:18 -04:00
|
|
|
}
|
|
|
|
|
2022-07-04 17:03:07 -04:00
|
|
|
} // modemm17
|