2022-08-16 19:22:35 +00:00
|
|
|
#pragma once
|
2022-08-11 12:47:44 +00:00
|
|
|
#include "lasp_filter.h"
|
2023-06-10 13:47:52 +00:00
|
|
|
#include "lasp_thread.h"
|
2022-08-11 12:47:44 +00:00
|
|
|
|
2022-09-22 08:18:38 +00:00
|
|
|
/**
|
2022-10-01 17:59:35 +00:00
|
|
|
* \addtogroup dsp
|
2022-09-22 08:18:38 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2022-08-11 12:47:44 +00:00
|
|
|
/**
|
|
|
|
* @brief A set of Biquad filters in series.
|
|
|
|
*/
|
|
|
|
class SeriesBiquad : public Filter {
|
|
|
|
|
|
|
|
/// The filter coefficients for each of the filters in the Filterbank
|
|
|
|
/// The *first* axis is the filter no, the second axis contains the
|
|
|
|
/// filter coefficients, in the order, b_0, b_1, b_2, a_0, a_1, a_2, which
|
|
|
|
/// corresponds to the transfer function
|
|
|
|
/// b_0 + b_1 z^-1 + b_2 z^-2
|
|
|
|
/// H[z] = -------------------------
|
|
|
|
/// a_0 + a_1 z^-1 + a_2 z^-2
|
|
|
|
///
|
|
|
|
dmat sos; /// sos[coef, filter_no]
|
|
|
|
///
|
|
|
|
/// Storage for the current state of the output, first axis correspond to
|
|
|
|
/// the state axis, the second axis to the series filter no. For each filter
|
|
|
|
/// in series, two state coefficients are remembered.
|
|
|
|
dmat state;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @brief Initalize a SeriesBiquad filter.
|
|
|
|
*
|
|
|
|
* @param filter_coefs Filter coefficients, should be given in the order as
|
|
|
|
* [b0, b1, b2, a0==1, a1, a2, b0,...]
|
|
|
|
*/
|
|
|
|
SeriesBiquad(const vd &filter_coefs);
|
|
|
|
|
2022-08-16 19:22:35 +00:00
|
|
|
virtual void filter(vd &inout) override final;
|
2022-08-11 12:47:44 +00:00
|
|
|
virtual ~SeriesBiquad() override {}
|
2022-08-16 19:22:35 +00:00
|
|
|
void reset() override final;
|
2022-10-04 07:27:27 +00:00
|
|
|
std::unique_ptr<Filter> clone() const override final;
|
2022-10-20 14:31:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Create a SeriesBiquad object for a first order high-pass filter
|
|
|
|
*
|
|
|
|
* @param fs Sampling frequency [Hz]
|
|
|
|
* @param cuton_Hz Cuton-frequency [Hz]
|
|
|
|
*
|
|
|
|
* @return SeriesBiquad object with a single biquad, corresponding to a first
|
|
|
|
* order high pass filter.
|
|
|
|
*/
|
|
|
|
static SeriesBiquad firstOrderHighPass(const d fs, const d cuton_Hz);
|
2022-08-11 12:47:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Multiple biquad filters in parallel, each multiplied with a gain
|
|
|
|
* value, and finally all added together. This class can be used to create
|
|
|
|
* a graphic equalizer.
|
|
|
|
*/
|
|
|
|
class BiquadBank : public Filter {
|
|
|
|
std::vector<SeriesBiquad> _filters;
|
|
|
|
vd _gains;
|
2023-06-12 07:11:08 +00:00
|
|
|
GlobalThreadPool _pool;
|
2022-10-12 19:32:24 +00:00
|
|
|
mutable std::mutex _mtx;
|
2022-10-04 07:27:27 +00:00
|
|
|
|
2022-08-11 12:47:44 +00:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @brief Initialize biquadbank.
|
|
|
|
*
|
2022-10-01 17:59:35 +00:00
|
|
|
* @param filters Filters for each filter in the bank. First axis isis the
|
|
|
|
* coefficient index, second axis is the filter index.
|
2022-08-11 12:47:44 +00:00
|
|
|
* @param gains Gain values. Given as pointer, if not given (nulltpr), gains
|
|
|
|
* are initialized with unity gain.
|
|
|
|
*/
|
|
|
|
BiquadBank(const dmat &filters, const vd *gains = nullptr);
|
|
|
|
|
2022-10-04 07:27:27 +00:00
|
|
|
/**
|
|
|
|
* @brief Construct biquad bank from already given set of series biquad filters and gain values
|
|
|
|
*
|
|
|
|
* @param filters The filters to set
|
|
|
|
* @param gains The gain values for each filter
|
|
|
|
*/
|
|
|
|
BiquadBank(std::vector<SeriesBiquad> filters,vd gains):
|
|
|
|
_filters(std::move(filters)),_gains(std::move(gains)) {}
|
|
|
|
|
2022-08-11 12:47:44 +00:00
|
|
|
/**
|
|
|
|
* @brief Set new gain values for each filter in the BiquadBank
|
|
|
|
*
|
|
|
|
* @param gains Vector of gain values. Should be of same length as the number
|
|
|
|
* of filters installed.
|
|
|
|
*/
|
|
|
|
void setGains(const vd& gains);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the number of Filters
|
|
|
|
*
|
|
|
|
* @return The number of filters
|
|
|
|
*/
|
|
|
|
us nfilters() const {return _filters.size();}
|
|
|
|
|
2022-08-16 19:22:35 +00:00
|
|
|
virtual void filter(vd &inout) override final;
|
|
|
|
|
|
|
|
void reset() override final;
|
2022-10-04 07:27:27 +00:00
|
|
|
std::unique_ptr<Filter> clone() const override final;
|
2022-08-11 12:47:44 +00:00
|
|
|
|
|
|
|
};
|
2022-09-22 08:18:38 +00:00
|
|
|
/** @} */
|